#include "hal_ble_uart0.h" #include "system.h" #include "bsp_time.h" #include "app_uart.h" #include "nrf_gpio.h" /************************ 函数声明 ***********************************/ //extern void UART0_send_buff(uint8_t *p,uint16_t len); void UART0_Tx_Process(void); /************************ 变量 ***********************************/ /********************** 环形缓存区 *************************/ static const int RxLen = 1024; static volatile unsigned char RxBuf[RxLen]; static volatile unsigned char* RxW=RxBuf; static volatile unsigned char* RxR=RxBuf; void UART0_Push(unsigned char* p,int len) { volatile unsigned char *W=RxW; //这里要与上面指针相同 if(len<=0) return; for(int i=0;i=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底) if(W!=RxR){*RxW=*(p+i); RxW=W;} else break; } } static unsigned int CheckLen(void) //检查RX接收了多少数据 { unsigned int Len; //short volatile unsigned char *W=RxW; volatile unsigned char *R=RxR; if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果) return Len; } //static unsigned char ReadByte(void) //读RX中数锯,地指加一,和丢弃 //{ // unsigned char R=*RxR; //读数 // if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标 // return R; //} static unsigned char CheckByte(unsigned short n) //看RX中数锯,地指不变, { volatile unsigned char *R=RxR+n; if(R>=(RxBuf+RxLen))R-=RxLen; return *R; } static void Discard(unsigned short n) //丢弃RX数据几位 { while(n){ n--; if(RxR==RxW) return; if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标 } } //********************* 接收 **********************// #define UART0_RX_CMD_LEN 256 static UART0_Rx_t mUART0_Rx[UART0_NUM_OF_R]; static unsigned char cmdDatBuf[UART0_RX_CMD_LEN]; //存放一条完整命令 static unsigned int rxNum = 0; int UART0_Rx_Regist(unsigned char cmd,UART0_Callback cb) //注册 { if(rxNum>=UART0_NUM_OF_R) return -1; mUART0_Rx[rxNum].cb = cb; mUART0_Rx[rxNum].cmd = cmd; mUART0_Rx[rxNum].pDat = cmdDatBuf; rxNum++; return 0; } //********************* 接收协议 **********************// //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) static void Protocol(unsigned char len) //协议处理 { #if DEBUG_UART0 DEBUG_LOG("Rx_UART0(%d):",CheckByte(1)); for(int i=0;i=5)){ R++;}else { Discard(1);}} } break; // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验 case 1: if( CheckLen()TXD = (unsigned int)ch; while(NRF_UART0->EVENTS_TXDRDY == 0x0UL); NRF_UART0->EVENTS_TXDRDY = 0x0UL; } void UART0_SendBuff(unsigned char *p,int L)//发送缓存 { unsigned int len = L; while (len>0){len--; UART0_SendChar(*p++); } } void UART0_SendStr(char *p)//发送字符串 { while (*p){ UART0_SendChar(*p++); } } //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验) void UART0_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen) //协议发送 { unsigned char buf[255]; unsigned char ver=0; unsigned char i,L=0,Len=datLen+5; buf[L]=0xAA; ver+=buf[L++]; // 头 buf[L]=Len; ver+=buf[L++]; //1位长度 buf[L]=~Len; ver+=buf[L++]; // 1位长度反码 buf[L]=cmd; ver+=buf[L++]; //1命令 for(i=0;icmd = cmd; handle->pDat = pDat; handle->datLen = datLen; handle->tcnt = handle->t; if(handle->n>0) handle->ncnt = handle->n-1; else handle->ncnt = 0; handle->holdon = 1; while(target){ //检查是否已经存在 if(target==handle){ return; } target = target->next ; } handle->next = head_handle; head_handle = handle; } UART0_Send(cmd,pDat,datLen); } void UART0_Tx_Clear(UART0_Tx_t* handle) { UART0_Tx_t** curr; for(curr=&head_handle;*curr;){ UART0_Tx_t* entry = *curr; if(entry==handle){ handle->holdon = 0; *curr = entry->next; }else{ curr = &entry->next; } } } void UART0_Tx_Process(void) { UART0_Tx_t* target; uint8_t holdon = 0; for(target=head_handle; target; target=target->next) { if(target->tcnt>0){target->tcnt--; if(target->tcnt==0){ if(target->ncnt>0){target->ncnt--; target->tcnt = target->t; UART0_Send(target->cmd,target->pDat,target->datLen); }else{ UART0_Tx_Clear(target); if(target->cb){ target->cb(target); } } } } holdon |= target->holdon; } Process_SetHoldOn(UART0_Tx_Process,holdon); } //void uart_event_handle(app_uart_evt_t * p_event) //{ // unsigned char byte; // switch (p_event->evt_type) // { // /**@snippet [Handling data from UART] */ // case APP_UART_DATA_READY: // app_uart_get(&byte); // UART0_Push(&byte,1); // break; // /**@snippet [Handling data from UART] */ // case APP_UART_COMMUNICATION_ERROR: // //APP_ERROR_HANDLER(p_event->data.error_communication); // break; // case APP_UART_FIFO_ERROR: // //APP_ERROR_HANDLER(p_event->data.error_code); // break; // default: // break; // } //} //*****************************************************************// void UARTE0_UART0_IRQHandler(void) { volatile unsigned char *W=RxW; //这里要与上面指针相同 if(NRF_UART0->EVENTS_RXDRDY!=0){NRF_UART0->EVENTS_RXDRDY=0; uint8_t ch = NRF_UART0->RXD; // DEBUG_LOG(" %02X\n",ch); W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底) if(W!=RxR){*RxW=ch; RxW=W;} } if(NRF_UART0->EVENTS_RXTO!=0){NRF_UART0->EVENTS_RXTO=0; //DEBUG_LOG("EVENTS_RXTO\n"); } if(NRF_UART0->EVENTS_CTS!=0){NRF_UART0->EVENTS_CTS=0; //DEBUG_LOG("EVENTS_CTS\n"); } if(NRF_UART0->EVENTS_NCTS!=0){NRF_UART0->EVENTS_NCTS=0; //DEBUG_LOG("EVENTS_NCTS\n"); } if(NRF_UART0->EVENTS_TXDRDY!=0){//NRF_UART0->EVENTS_TXDRDY=0; //DEBUG_LOG("EVENTS_TXDRDY\n"); } if(NRF_UART0->EVENTS_ERROR!=0){NRF_UART0->EVENTS_ERROR=0; //DEBUG_LOG("EVENTS_ERROR=%d\n",NRF_UART0->EVENTS_ERROR); } } uint32_t get_baud(uint32_t baud) { switch(baud){ case 1200: return UART_BAUDRATE_BAUDRATE_Baud1200; case 2400: return UART_BAUDRATE_BAUDRATE_Baud2400; case 4800: return UART_BAUDRATE_BAUDRATE_Baud4800; case 9600: return UART_BAUDRATE_BAUDRATE_Baud9600; case 14400: return UART_BAUDRATE_BAUDRATE_Baud14400; case 19200: return UART_BAUDRATE_BAUDRATE_Baud19200; case 28800: return UART_BAUDRATE_BAUDRATE_Baud28800; case 31250: return UART_BAUDRATE_BAUDRATE_Baud31250; case 38400: return UART_BAUDRATE_BAUDRATE_Baud38400; case 56000: return UART_BAUDRATE_BAUDRATE_Baud56000; case 57600: return UART_BAUDRATE_BAUDRATE_Baud57600; case 76800: return UART_BAUDRATE_BAUDRATE_Baud76800; case 115200: return UART_BAUDRATE_BAUDRATE_Baud115200; case 230400: return UART_BAUDRATE_BAUDRATE_Baud230400; case 250000: return UART_BAUDRATE_BAUDRATE_Baud250000; case 460800: return UART_BAUDRATE_BAUDRATE_Baud460800; case 921600: return UART_BAUDRATE_BAUDRATE_Baud921600; case 1000000: return UART_BAUDRATE_BAUDRATE_Baud1M; default: return UART_BAUDRATE_BAUDRATE_Baud115200; } } void UART0_Init(uint32_t baud) { NRF_UART0->INTENCLR = UART_INTENCLR_CTS_Msk | UART_INTENCLR_RXTO_Msk | UART_INTENCLR_NCTS_Msk | UART_INTENCLR_ERROR_Msk; NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos; NRF_UART0->PSELTXD = PIN_TXD_BLE; NRF_UART0->PSELRXD = PIN_RXD_BLE; NRF_UART0->BAUDRATE = get_baud(baud); NRF_UART0->ENABLE |= UART_ENABLE_ENABLE_Enabled; NRF_UART0->TASKS_STARTTX = 0XFFFFFFFF; NRF_UART0->TASKS_STARTRX = 0XFFFFFFFF; NVIC_SetPriority(UARTE0_UART0_IRQn, 7); NVIC_EnableIRQ(UARTE0_UART0_IRQn); NRF_UART0->EVENTS_TXDRDY = 0x0UL; } void UART0_unInit(void) { NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled; NRF_UART0->INTENCLR = UART_INTENCLR_RXDRDY_Msk | UART_INTENCLR_TXDRDY_Msk | UART_INTENCLR_RXTO_Msk | UART_INTENCLR_ERROR_Msk; NVIC_DisableIRQ(UARTE0_UART0_IRQn); NVIC_ClearPendingIRQ(UARTE0_UART0_IRQn); } static void cb_uarteWakeup(uint32_t t) { UART0_Init(38400); } static void cb_uarteSleep(uint32_t t) { UART0_unInit(); } void UART0_Initialize(void) { UART0_Init(38400); Process_Start(0,UART0_Rx_Process); Process_Start(10,UART0_Tx_Process); Wakeup_Regist(cb_uarteWakeup); Sleep_Regist(cb_uarteSleep); }