#ifndef _GW3323_TMR_H
#define _GW3323_TMR_H

#include "include.h"

//--------------- TMR[0..2] Flag Definition ---------------//
#define TMR_FLAG_UPDATE1        ((uint32_t)0x00000200)

//--------------- TMR[3..5] Flag Definition ---------------//
#define TMR_FLAG_CAP            ((uint32_t)0x00020000)
#define TMR_FLAG_UPDATE2        ((uint32_t)0x00010000)

//--------------- TMR[0..2] IT Definition ---------------//
#define TMR_IT_UPDATE1          ((uint32_t)0x00000080)

//--------------- TMR[3..5] IT Definition ---------------//
#define TMR_IT_CAP              ((uint32_t)0x00000100)
#define TMR_IT_UPDATE2          ((uint32_t)0x00000080)

//clock source selection
typedef enum {
    TMR_INC_SOURCE      = 0x00000000,
    TMR_PIN_SOURCE      = 0x00000040,
} TMR_CLOCK_TYPEDEF;

//capture edge selection
typedef enum {
    TMR_CAP_NULL        = 0x00000000,
    TMR_CAP_RISING      = 0x00000010,
    TMR_CAP_FALLING     = 0x00000020,
    TMR_CAP_EDGE        = 0x00000030,
} TMR_CAP_EDGE_TYPEDEF;

//clock triggle selection
typedef enum {
    TMR_SYSTEM_CLOCK    = 0x00000000,
    TMR_COUNTER_RISING  = 0x00000004,
    TMR_COUNTER_FALLING = 0x00000008,
    TMR_COUNTER_EDGE    = 0x0000000c,
} TMR_TRIGGLE_TYPEDEF;

//pwm mode selection
typedef enum {
    TMR_PWM_MODE_2      = 0x00000800,
    TMR_PWM_MODE_1      = 0x00000400,
    TMR_PWM_MODE_0      = 0x00000200,
    TMR_PWM_MODE_NULL   = 0x00000000,
} TMR_PWM_MODE_TYPEDDEF;

//timer init structure
typedef struct {
    uint32_t                period;
    uint32_t                pwm_duty;
    TMR_CLOCK_TYPEDEF       clock_source;
    TMR_TRIGGLE_TYPEDEF     increase_clock;
    TMR_PWM_MODE_TYPEDDEF   pwm_mode;
} tmr_base_init_typedef;

//--------------- Function used to configure timer base  ---------------//
void tmr_base_init(tmr_typedef *tmrx, tmr_base_init_typedef *tmr_base_init_struct);
void tmr_set_counter(tmr_typedef *tmrx, uint32_t cnt);
void tmr_set_period(tmr_typedef *tmrx, uint32_t period);
uint32_t tmr_get_counter(tmr_typedef *tmrx);
uint32_t tmr_get_period(tmr_typedef *tmrx);
void tmr_cmd(tmr_typedef *tmrx, FUNCTIONAL_STATE state);
//--------------------- Function used to set pwm  ---------------------//
void tmr_set_pwm0_duty(tmr_typedef *tmrx, uint32_t duty);
void tmr_set_pwm1_duty(tmr_typedef *tmrx, uint32_t duty);
void tmr_set_pwm2_duty(tmr_typedef *tmrx, uint32_t duty);
//------------------- Function used to set capture  -------------------//
void tmr_capture_config(tmr_typedef *tmrx, TMR_CAP_EDGE_TYPEDEF edge_select, FUNCTIONAL_STATE state);
uint32_t tmr_get_capture(tmr_typedef *tmrx);
//--------------- Function used to configure interrupt  ---------------//
void tmr_pic_config(tmr_typedef *tmrx, isr_t isr, int pr, uint16_t tmr_ie_bit, FUNCTIONAL_STATE state);//he current SDK does not support priority other than 0
FLAG_STATE tmr_get_flag(tmr_typedef *tmrx, uint32_t tmr_flag);
void tmr_clear_flag(tmr_typedef *tmrx, uint32_t tmr_flag);

#endif // _GW3323_TMR_H
