#include "hal_uart.h" #include "bsp_uart.h" #include "bsp_time.h" #include "main.h" //********************* 选择串口 接口 **********************// static unsigned char CheckLen(void) //查看收到多少数据 { return UART0_CheckLen(); } static unsigned char CheckByte(int dex) //查看数据 { return UART0_CheckByte(dex); } static void Discard(unsigned int dex) //丢弃RX数据几位 { UART0_Discard(dex); } static void SendBuf(unsigned char *p,int L) //发送 { UART0_SendBuff(p,L); } //********************* 接收 **********************// #define UART0_RX_CMD_LEN 256 static UART0_Rx_t mUART0_Rx[UART0_NUM_OF_R]; static unsigned char cmdDatBuf[UART0_RX_CMD_LEN]; //存放一条完整命令 int UART0_Rx_Regist(unsigned char cmd,UART0_Callback cb) //注册 { if(cmd>=UART0_NUM_OF_R) return -1; mUART0_Rx[cmd].cb = cb; mUART0_Rx[cmd].cmd = cmd; mUART0_Rx[cmd].pDat = cmdDatBuf; return 0; } //********************* 接收协议 **********************// //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) static void Protocol(unsigned char len) //协议处理 { #if DEBUG_UART0 SEGGER_RTT_printf(0,"Rx_UART0(%d):",CheckByte(1)); for(int i=0;i=UART0_NUM_OF_R) return; if(mUART0_Rx[cmd].cb){ for(int i=0;i=5)){ R++;}else { Discard(1);}} } break; // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验 case 1: if( CheckLen()=1){ if(mUART0_Tx[i].periodTimecnt>0){mUART0_Tx[i].periodTimecnt--; if(mUART0_Tx[i].nTimes==0&&mUART0_Tx[i].periodTimecnt==0&&mUART0_Tx[i].cb){ mUART0_Tx[i].cb((UART0_Tx_t*)&mUART0_Tx[i]); } } } if(mUART0_Tx[i].nTimes==1&&mUART0_Tx[i].periodTimecnt==0){ UART0_Send(i,mUART0_Tx[i].pDat,mUART0_Tx[i].datLen); mUART0_Tx[i].periodTimecnt = mUART0_Tx[i].waitTime; mUART0_Tx[i].nTimes--; }else if(mUART0_Tx[i].nTimes>1&&mUART0_Tx[i].periodTimecnt==0){ UART0_Send(i,mUART0_Tx[i].pDat,mUART0_Tx[i].datLen); mUART0_Tx[i].periodTimecnt = mUART0_Tx[i].periodTime; mUART0_Tx[i].nTimes--; } } } int UART0_Tx_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen,uint8_t nTimes,uint32_t periodTime,uint32_t afterTime,uint32_t waitTime) { if(cmd>=UART0_NUM_OF_T) return -1; mUART0_Tx[cmd].cmd = cmd; mUART0_Tx[cmd].pDat = pDat; mUART0_Tx[cmd].nTimes = nTimes; mUART0_Tx[cmd].datLen = datLen; mUART0_Tx[cmd].periodTime = periodTime; mUART0_Tx[cmd].periodTimecnt = afterTime; mUART0_Tx[cmd].waitTime = waitTime; return 0; } int UART0_Tx_Regist(unsigned char cmd,UART0_Callback timeout_cb) { if(cmd>=UART0_NUM_OF_T) return -1; mUART0_Tx[cmd].cb = timeout_cb; return 0; } void UART0_Tx_Clear(unsigned char cmd) { if(cmd>=UART0_NUM_OF_T) return; mUART0_Tx[cmd].nTimes = 0; mUART0_Tx[cmd].periodTimecnt = 0; } void UART0_Initialize(void) { UART0_Init(115200); Process_Regist(UART0_Rx_Process); Process_Regist(UART0_Tx_Process); }