#ifndef _GW3323_HSUART_H
#define _GW3323_HSUART_H

#include "include.h"

//--------------- HSUART Flag Definition ---------------//
#define HSUART_FLAG_TMROV       ((uint32_t)0x00008000)
#define HSUART_FLAG_TX          ((uint32_t)0x00002000)
#define HSUART_FLAG_RX          ((uint32_t)0x00001000)
#define HSUART_FLAG_RXOV        ((uint32_t)0x00000800)
//---------------- HSUART IT Definition ----------------//
#define HSUART_IT_TX            ((uint32_t)0x00000008)
#define HSUART_IT_RX            ((uint32_t)0x00000004)

//TX two stop bit enumeration
typedef enum {
    HSUT_STOP_BIT_1BIT      = 0x0000,
    HSUT_STOP_BIT_2BIT      = 0x0200,
} HSUT_TX_STOP_BIT_TYPEDEF;

//TX word length enumeration
typedef enum {
    HSUT_TX_LENGTH_8b       = 0x0000,
    HSUT_TX_LENGTH_9b       = 0x0100,
} HSUT_TX_WORD_LENGTH;

//TX transmit mode select
typedef enum {
    HSUT_TX_BUF_MODE        = 0x00,
    HSUT_TX_DMA_MODE        = 0x80,
} HSUT_TX_MODE;

//RX word length enumeration
typedef enum {
    HSUT_RX_LENGTH_8b       = 0x00,
    HSUT_RX_LENGTH_9b       = 0x20,
} HSUT_RX_WORD_LENGTH;

//RX transmit mode select
typedef enum {
    HSUT_RX_BUF_MODE        = 0x00,
    HSUT_RX_DMA_MODE        = 0x10,
} HSUT_RX_MODE;

//Receive or Transmit select
typedef enum {
    HSUT_TRANSMIT           = 0x02,
    HSUT_RECEIVE            = 0x01,
} HSUT_REC_TRA_CMD;

//uart init structure
typedef struct {
    uint32_t                    baud;
    HSUT_TX_STOP_BIT_TYPEDEF    tx_stop_bit;
    HSUT_TX_WORD_LENGTH         tx_word_len;
    HSUT_RX_WORD_LENGTH         rx_word_len;
    HSUT_TX_MODE                tx_mode;
    HSUT_RX_MODE                rx_mode;
} hsuart_init_typedef;

//--------------- Function used to configure hsuart ----------------//
void hsuart_init(hsuart_init_typedef* hsuart_init_struct);
void hsuart_cmd(uint32_t rec_tra_sel, FUNCTIONAL_STATE state);
void hsuart_send_data(uint16_t data);
uint16_t hsuart_receive_data(void);
//---------- Function used to configure hsuart interrupt -----------//
void hsuart_pic_config(isr_t isr, int pr, uint16_t hsuart_ie_bit, FUNCTIONAL_STATE state);
FLAG_STATE hsuart_get_flag(uint16_t hsuart_flag);
void hsuart_clear_flag(uint16_t hsuart_flag);
//------------- Function used to configure hsuart DMA --------------//
void hsuart_dma_config(uint32_t rec_tra_sel, uint32_t addr);
void hsuart_rx_idle_config(uint32_t t);
void hsuart_dma_start(uint32_t rec_tra_sel, uint32_t addr, uint16_t len);
void hsuart_dma_stop(uint32_t rec_tra_sel);

#endif // _GW3323_HSUART_H
