#ifndef FM_UART_DEV_H_
#define FM_UART_DEV_H_

#include "ch32v30x.h"
#include "HD_UART.h"
#include "comm_types.h"
#include "ring_queue.h"

/* 是否使用中断发送，后期可能改为DMA发送 */
#define  UARTINTERRUPTSEND

/* 是否使用空闲中断，后期可以改为其他方式判断总线空闲中断 */
#define  RS485BUSIDLE1

#define  ASCII_CHAR_BACKSPACE                           0x08    /* '\b' */
#define  ASCII_CHAR_CHARACTER_TABULATION                0x09    /* '\t' */
#define  ASCII_CHAR_LINE_FEED                           0x0A    /* '\n' */
#define  ASCII_CHAR_LINE_TABULATION                     0x0B    /* '\v' */
#define  ASCII_CHAR_FORM_FEED                           0x0C    /* '\f' */
#define  ASCII_CHAR_CARRIAGE_RETURN                     0x0D    /* '\r' */

typedef enum{
    J1_485_INDEX        = 6,
    J2_485_INDEX        = 7,
    J3_485_INDEX        = 2,
    J4_485_INDEX        = 8,
    J5_0_485_INDEX      = 3,
    Upward_UART_INDEX  = 5,
}uartIndex_e;

typedef struct _uartSendInfo {
    uint8_t *data;    //指向数据
#ifdef UARTINTERRUPTSEND
    uint16_t count;   //已经发送数据字节个数
    uint16_t len;     //数据长度
#endif
}uartSendInfo;

/* UART 驱动数据结构，对应一个uart设备 */
typedef struct _uart_device_info{
    uint8_t       init;
    uartIndex_e   uart_index;
    uint32_t      uart_baudrate;
    RingQueue     uart_ring_queue;
    uartSendInfo  uart_send_data;
}uart_device_info;

typedef uint32_t device_handle;
extern device_handle g_J1_uart6_handle;
extern device_handle g_J2_uart7_handle;
extern device_handle g_J3_usart2_handle;
extern device_handle g_J4_uart8_handle;
extern device_handle g_J5_0_usart3_handle;
extern device_handle g_Upward_uart5_handle;

uint8_t uart_dev_in_char(device_handle device);
int uart_dev_char_present(device_handle device);
void Init_J1_485(uint32_t baud);
void Init_J2_485(uint32_t baud);
void Init_J3_485(uint32_t baud);
void Init_J4_485(uint32_t baud);
void Init_J5_0_485(uint32_t baud);
void Init_Upward_uart(uint32_t baud);

uint8_t getUartSendState(device_handle device);

#ifdef UARTINTERRUPTSEND
extern void J1_485_IN_TXE(void);
extern void J2_485_IN_TXE(void);
extern void J3_485_IN_TXE(void);
extern void J4_485_IN_TXE(void);
extern void J5_0_485_IN_TXE(void);
extern void Upward_USART_IN_TXE(void);
uint8_t uartInterruptSend(device_handle device, uint8_t *data, uint16_t len);
#endif

#ifdef RS485BUSIDLE1
extern void J1_485_IN_IDLE(void);
extern void J2_485_IN_IDLE(void);
extern void J3_485_IN_IDLE(void);
extern void J4_485_IN_IDLE(void);
extern void J5_0_485_IN_IDLE(void);

extern void J1_485_IN_RXNE(void);
extern void J2_485_IN_RXNE(void);
extern void J3_485_IN_RXNE(void);
extern void J4_485_IN_RXNE(void);
extern void J5_0_485_IN_RXNE(void);

uint8_t getBUSIDLEFlag(device_handle device);
// uint8_t getSoftwareIDLEFlag(device_handle device);
void setBUSIDLEFlag(device_handle device, uint8_t state);
// void setSoftwareIDLEFlag(device_handle device, uint8_t state);
#endif

/* J5_0连接到哪个端口 */
typedef enum _connectPortEnum {
    connectJ0 = 1,
    connectJ5,
    connectJ6,
    connectJ7,
    connectJ8,
    connectJ9,
} connectPortEnum;
void setConnectPort(uint8_t port);
uint8_t getConnectPort(void);


#endif
