#ifndef _GW3323_UART_H
#define _GW3323_UART_H

#include "include.h"

//---------- UART Flag Definition ----------//
#define UART_FLAG_RX            ((uint16_t)0x0200)
#define UART_FLAG_TX            ((uint16_t)0x0100)

//Rx enable enumeration
typedef enum {
    UART_SEND_MODE      = 0x00000000,
    UART_RW_MODE        = 0x00000080,
} UART_MODE_TYPEDEF;

//one-line mode enumeration
typedef enum {
    UART_SEPARATE       = 0x00000000,
    UART_ONE_LINE       = 0x00000040,
} UART_ONELINE_TYPEDEF;

//clock select enumeration
typedef enum {
    UART_SYSTEM_CLOCK   = 0x00000000,
    UART_INC_CLOCK      = 0x00000020,
} UART_CLOCK_TYPEDEF;

//two stop bit enumeration
typedef enum {
    UART_STOP_BIT_1BIT  = 0x00000000,
    UART_STOP_BIT_2BIT  = 0x00000010,
} UART_STOP_BIT_TYPEDEF;

//word length enumeration
typedef enum {
    UART_WORD_LENGTH_8b = 0x00000000,
    UART_WORD_LENGTH_9b = 0x00000002,
} UART_WORD_LENGTH;

typedef enum {
    UART_INC_DIV1       = 0x00000000,
    UART_INC_DIV2       = 0x00004000,
} UART_INC_DIVISION;

//uart init structure
typedef struct {
    uint32_t                baud;
    UART_MODE_TYPEDEF       mode;
    UART_ONELINE_TYPEDEF    one_line_enable;
    UART_CLOCK_TYPEDEF      clock_source;
    UART_STOP_BIT_TYPEDEF   stop_bits;
    UART_WORD_LENGTH        work_len;
    UART_INC_DIVISION       inc_div;
} uart_init_typedef;

//--------------- Function used to configure uart  ---------------//
void uart_init(uart_typedef *uartx, uart_init_typedef* uart_init_struct);
void uart_cmd(uart_typedef *uartx, FUNCTIONAL_STATE state);
void uart_send_data(uart_typedef *uartx, uint16_t data);
uint16_t uart_receive_data(uart_typedef *uartx);
void uart_pic_config(uart_typedef *uartx, isr_t isr, int pr, uint16_t uart_ie_bit, FUNCTIONAL_STATE state);
FLAG_STATE uart_get_flag(uart_typedef *uartx, uint16_t uart_flag);
void uart_clear_flag(uart_typedef *uartx, uint16_t uart_flag);

#endif // _GW3323_UART_H
