257 lines
6.2 KiB
C
257 lines
6.2 KiB
C
|
/**
|
|||
|
******************************************************************************
|
|||
|
* @file hc32_uart_driver.h
|
|||
|
* @author (C)2020, Qindao ieslab Co., Ltd
|
|||
|
* @version V1.0
|
|||
|
* @date 2020-7-1
|
|||
|
* @brief the function of the entity of system processor
|
|||
|
******************************************************************************
|
|||
|
*/
|
|||
|
|
|||
|
/* Define to prevent recursive inclusion -------------------------------------*/
|
|||
|
|
|||
|
#ifndef __UWater_UART_DRIVER_H
|
|||
|
#define __UWater_UART_DRIVER_H
|
|||
|
|
|||
|
#ifdef __cplusplus
|
|||
|
extern "C"
|
|||
|
{
|
|||
|
#endif
|
|||
|
|
|||
|
#include "sys_config.h"
|
|||
|
#include "type.h"
|
|||
|
#include "ddl.h"
|
|||
|
|
|||
|
#define HDSC_HC32L196
|
|||
|
|
|||
|
/* MACRO Define---------------------------------------------------------------*/
|
|||
|
|
|||
|
//<2F><><EFBFBD>д<EFBFBD><D0B4>ڣ<EFBFBD><DAA3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>1
|
|||
|
#define COM0_OPEN 0
|
|||
|
#define COM1_OPEN 0
|
|||
|
#define COM2_OPEN 0
|
|||
|
#define COM3_OPEN 0
|
|||
|
#define LPCOM0_OPEN 1 //<2F><><EFBFBD><EFBFBD><E2B4AE>
|
|||
|
#define LPCOM1_OPEN 1 //NB<4E><42><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CDB4>ڶ<EFBFBD><DAB6><EFBFBD> <20><><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>
|
|||
|
typedef enum
|
|||
|
{
|
|||
|
COM0 = 0,
|
|||
|
COM1,
|
|||
|
COM2,
|
|||
|
COM3,
|
|||
|
LPCOM0, //<2F><EFBFBD><CDB9>Ĵ<EFBFBD><C4B4><EFBFBD>
|
|||
|
LPCOM1,
|
|||
|
COM_MAX_NUM
|
|||
|
}UART_ENUM;
|
|||
|
|
|||
|
#if MODULE_BLE_USED
|
|||
|
#define BLE_UART_COM LPCOM1
|
|||
|
#else
|
|||
|
#define NB_UART_COM LPCOM1 //NB<4E><42><EFBFBD><EFBFBD>
|
|||
|
#endif
|
|||
|
#define IR_UART_COM LPCOM0 //<2F><><EFBFBD><EFBFBD><E2B4AE>
|
|||
|
/*!< LPCOM USART Enable in SLEEP Mode */
|
|||
|
#define LPCOM1_ENABLESLEEPMODE 0
|
|||
|
|
|||
|
|
|||
|
#define IR_38K_OPEN 1
|
|||
|
#if (HARDWARE_VERSION==0x14)//<2F><><EFBFBD>DZ<EFBFBD>
|
|||
|
#define IR_UART_CTL_PORT (GpioPortE)
|
|||
|
#define IR_UART_CTL_PIN (GpioPin2)
|
|||
|
|
|||
|
#elif (HARDWARE_VERSION==0x18)//Բ<>DZ<EFBFBD>
|
|||
|
#define IR_UART_CTL_PORT (GpioPortF)
|
|||
|
#define IR_UART_CTL_PIN (GpioPin6)
|
|||
|
#endif
|
|||
|
|
|||
|
|
|||
|
#if IR_38K_OPEN
|
|||
|
//TIM2_CHB
|
|||
|
#define IR_38K_PORT (GpioPortA)
|
|||
|
#define IR_38K_PIN (GpioPin3)
|
|||
|
#define IR_38K_AF (GpioAf4)
|
|||
|
#endif
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>IO<49><4F><EFBFBD><EFBFBD>(IOһ<4F><D2BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ȷ)<29><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>ʱ<EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#if COM0_OPEN
|
|||
|
//UART0
|
|||
|
#define UART0_RX_PORT (GpioPortA)
|
|||
|
#define UART0_RX_PIN (GpioPin10)
|
|||
|
#define UART0_RX_AF (GpioAf1)
|
|||
|
|
|||
|
#define UART0_TX_PORT (GpioPortA)
|
|||
|
#define UART0_TX_PIN (GpioPin9)
|
|||
|
#define UART0_TX_AF (GpioAf1)
|
|||
|
#endif
|
|||
|
|
|||
|
#if COM1_OPEN
|
|||
|
//UART1
|
|||
|
#define UART1_RX_PORT (GpioPortA)
|
|||
|
#define UART1_RX_PIN (GpioPin3)
|
|||
|
#define UART1_RX_AF (GpioAf1)
|
|||
|
|
|||
|
#define UART1_TX_PORT (GpioPortA)
|
|||
|
#define UART1_TX_PIN (GpioPin2)
|
|||
|
#define UART1_TX_AF (GpioAf1)
|
|||
|
#endif
|
|||
|
|
|||
|
#if COM2_OPEN
|
|||
|
//UART2
|
|||
|
#define UART2_RX_PORT (GpioPortC)
|
|||
|
#define UART2_RX_PIN (GpioPin2)
|
|||
|
#define UART2_RX_AF (GpioAf4)
|
|||
|
|
|||
|
#define UART2_TX_PORT (GpioPortC)
|
|||
|
#define UART2_TX_PIN (GpioPin3)
|
|||
|
#define UART2_TX_AF (GpioAf5)
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
#if COM3_OPEN
|
|||
|
//UART3
|
|||
|
#define UART3_RX_PORT (GpioPortA)
|
|||
|
#define UART3_RX_PIN (GpioPin2)
|
|||
|
#define UART3_RX_AF (GpioAf1)
|
|||
|
|
|||
|
#define UART3_TX_PORT (GpioPortA)
|
|||
|
#define UART3_TX_PIN (GpioPin3)
|
|||
|
#define UART3_TX_AF (GpioAf1)
|
|||
|
#endif
|
|||
|
|
|||
|
#if LPCOM0_OPEN
|
|||
|
//LPUART0
|
|||
|
#define LPUART0_RX_PORT (GpioPortC)
|
|||
|
#define LPUART0_RX_PIN (GpioPin11)
|
|||
|
#define LPUART0_RX_AF (GpioAf2)
|
|||
|
|
|||
|
#define LPUART0_TX_PORT (GpioPortC)
|
|||
|
#define LPUART0_TX_PIN (GpioPin12)
|
|||
|
#define LPUART0_TX_AF (GpioAf1)
|
|||
|
#endif
|
|||
|
|
|||
|
#if LPCOM1_OPEN
|
|||
|
//LPUART1
|
|||
|
#define LPUART1_RX_PORT (GpioPortA)
|
|||
|
#define LPUART1_RX_PIN (GpioPin1)
|
|||
|
#define LPUART1_RX_AF (GpioAf2)
|
|||
|
|
|||
|
#define LPUART1_TX_PORT (GpioPortA)
|
|||
|
#define LPUART1_TX_PIN (GpioPin0)
|
|||
|
#define LPUART1_TX_AF (GpioAf2)
|
|||
|
#endif
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>ģ<DEB8>
|
|||
|
#define UP_BUF_SIZE 512 //<2F><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
|
|||
|
|
|||
|
#define UART0_BUF_SIZE UP_BUF_SIZE
|
|||
|
#define UART1_BUF_SIZE UP_BUF_SIZE
|
|||
|
#define UART2_BUF_SIZE UP_BUF_SIZE
|
|||
|
#define UART3_BUF_SIZE UP_BUF_SIZE
|
|||
|
#define LPUART0_BUF_SIZE UP_BUF_SIZE
|
|||
|
#define LPUART1_BUF_SIZE UP_BUF_SIZE
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#define BAUD1200 0
|
|||
|
#define BAUD2400 1
|
|||
|
#define BAUD4800 2
|
|||
|
#define BAUD9600 3
|
|||
|
#define BAUD19200 4
|
|||
|
#define BAUD38400 5
|
|||
|
#define BAUD57600 6
|
|||
|
#define BAUD115200 7
|
|||
|
|
|||
|
#define DATA_BIT_8 8
|
|||
|
//<2F><><EFBFBD><EFBFBD>λ
|
|||
|
#define UART_WORDLEN_8B ((u32)0x00000000) //8λ
|
|||
|
|
|||
|
#define STOPBIT_1 0 //1<><31>ֹͣλ
|
|||
|
#define STOPBIT_1_5 1 //1.5<EFBFBD><EFBFBD>ֹͣλ
|
|||
|
#define STOPBIT_2 2
|
|||
|
//ֹͣλ
|
|||
|
#define UART_STOPBIT_1 UartMsk1bit //1<><31>ֹͣλ
|
|||
|
#define UART_STOPBIT_1_5 UartMsk1_5bit //1.5<EFBFBD><EFBFBD>ֹͣλ
|
|||
|
#define UART_STOPBIT_2 UartMsk2bit //2<><32>ֹͣλ
|
|||
|
//У<><D0A3>
|
|||
|
#define UART_NONE_PARITY UartMskDataOrAddr ///<<3C><><EFBFBD><EFBFBD>ģʽʱ<CABD><CAB1>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>дSBUF[8]<5D><><EFBFBD><EFBFBD>֡Ϊ<D6A1><CEAA><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD>ַ֡ ((u32)0x00000000) //<2F><>У<EFBFBD><D0A3>
|
|||
|
#define UART_EVEN_PARITY UartMskEven ///<<3C>Ƕ<EFBFBD><C7B6><EFBFBD>ģʽżУ<C5BC><D0A3> ((u32)0x00000004) //żУ<C5BC><D0A3>
|
|||
|
#define UART_ODD_PARITY UartMskOdd ///<<3C>Ƕ<EFBFBD><C7B6><EFBFBD>ģʽ<C4A3><CABD>У<EFBFBD><D0A3> ((u32)0x00000008) //<2F><>У<EFBFBD><D0A3>
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>״̬
|
|||
|
#define UART_RCV_OVERTIME_S0 0
|
|||
|
#define UART_RCV_OVERTIME_S1 1
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>״̬
|
|||
|
#define UART_SEND_OVERTIME_S0 0
|
|||
|
#define UART_SEND_OVERTIME_S1 1
|
|||
|
//<2F><EFBFBD><CDB9><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
|||
|
#define UART_LPUART_Enable 0
|
|||
|
#define UART_LPUART_Disable 1
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>
|
|||
|
#define UART_BAUD_DIV_LPUART(__PCLK__, __BAUD__) (((u64)(__PCLK__)*256) / ((__BAUD__)))
|
|||
|
#define UART_BAUD_DIV_SAMPLING8(__PCLK__, __BAUD__) (((__PCLK__)*2) / ((__BAUD__)))
|
|||
|
#define UART_BAUD_DIV_SAMPLING16(__PCLK__, __BAUD__) (((__PCLK__)) / ((__BAUD__)))
|
|||
|
|
|||
|
//<2F><><EFBFBD>ڽ<EFBFBD><DABD>ճ<EFBFBD>ʱ
|
|||
|
#define FRAMEOVERTIME 20 //<2F><>ʱʱ<CAB1><CAB1>Ϊ100ms 20*5 = 100ms
|
|||
|
|
|||
|
//<2F><><EFBFBD>ڷ<EFBFBD><DAB7>ͳ<EFBFBD>ʱ
|
|||
|
#define UART_BUSY_OVERTIME 1800 //<2F><>ʱʱ<CAB1><CAB1>Ϊ180S 100ms*1800
|
|||
|
|
|||
|
/* variables Define---------------------------------------------------------------*/
|
|||
|
typedef struct
|
|||
|
{
|
|||
|
u32 BaudRate;
|
|||
|
u32 WordLength;
|
|||
|
u32 StopBits;
|
|||
|
u32 Parity;
|
|||
|
u32 Mode;
|
|||
|
u32 OverSampling;
|
|||
|
u8 LPModeFunction;
|
|||
|
u8 UARTMode;
|
|||
|
} UART_PARA_STRUCT;
|
|||
|
|
|||
|
typedef struct
|
|||
|
{
|
|||
|
M0P_UART_TypeDef *Instance;
|
|||
|
M0P_LPUART_TypeDef *LP_Instance;
|
|||
|
UART_PARA_STRUCT para;
|
|||
|
u8 uart_status;
|
|||
|
u8 *send_point;
|
|||
|
u16 send_len;
|
|||
|
u8 *send_status_point; //<2F><><EFBFBD><EFBFBD>״ָ̬<CCAC><D6B8>
|
|||
|
u8 rcv_state;
|
|||
|
u8 send_state;
|
|||
|
u8 start_send_flag;
|
|||
|
u8 finish_send_flag;
|
|||
|
u8 send_over_time; //<2F><><EFBFBD>ͳ<EFBFBD>ʱʱ<CAB1><CAB1>
|
|||
|
u8 rcv_flag;
|
|||
|
u16 rcv_len;
|
|||
|
u8 rcv_msg;
|
|||
|
} UART_STRUCT;
|
|||
|
|
|||
|
/* Function Declare------------------------------------------------------------*/
|
|||
|
u8 UartInit(u8 com, u8 baud, u8 databit, u8 stop, u8 check, u8 lpmode);
|
|||
|
u8 UartIfIdle(u8 com);
|
|||
|
void UartSend(u8 com, u16 len, u8 *data, u8 *out_msg);
|
|||
|
void UartRcv(u8 com, u16 *out_len, u8 **out_data);
|
|||
|
u8 UartRcvFinish(u8 com);
|
|||
|
void UartClearMsg(u8 com);
|
|||
|
u8 UartIfSleep(u8 com);
|
|||
|
void UartPreSleep(u8 com);
|
|||
|
void UartWakeSleep(u8 com, u8 baud, u8 databit, u8 stop, u8 check, u8 lpmode);
|
|||
|
void UartMachineDriver(void);
|
|||
|
M0P_UART_TypeDef *HAL_GetUart_Hander(u8 com);
|
|||
|
M0P_LPUART_TypeDef *HAL_GetLPUart_Hander(u8 com);
|
|||
|
|
|||
|
u8 UartRcvMachineIfIdle(u8 com);
|
|||
|
u8 UartSendMachineIfIdle(u8 com);
|
|||
|
#ifdef __cplusplus
|
|||
|
}
|
|||
|
#endif
|
|||
|
|
|||
|
#endif /* __UWater_UART_DRIVER_H */
|
|||
|
|