hal_ble_uart0.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #include "hal_ble_uart0.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "app_uart.h"
  5. #include "nrf_gpio.h"
  6. /************************ 函数声明 ***********************************/
  7. //extern void UART0_send_buff(uint8_t *p,uint16_t len);
  8. void UART0_Tx_Process(void);
  9. /************************ 变量 ***********************************/
  10. /********************** 环形缓存区 *************************/
  11. static const int RxLen = 1024;
  12. static volatile unsigned char RxBuf[RxLen];
  13. static volatile unsigned char* RxW=RxBuf;
  14. static volatile unsigned char* RxR=RxBuf;
  15. void UART0_Push(unsigned char* p,int len)
  16. {
  17. volatile unsigned char *W=RxW; //这里要与上面指针相同
  18. if(len<=0) return;
  19. for(int i=0;i<len;i++){
  20. W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底)
  21. if(W!=RxR){*RxW=*(p+i); RxW=W;}
  22. else break;
  23. }
  24. }
  25. static unsigned int CheckLen(void) //检查RX接收了多少数据
  26. {
  27. unsigned int Len; //short
  28. volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
  29. if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果)
  30. return Len;
  31. }
  32. //static unsigned char ReadByte(void) //读RX中数锯,地指加一,和丢弃
  33. //{
  34. // unsigned char R=*RxR; //读数
  35. // if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
  36. // return R;
  37. //}
  38. static unsigned char CheckByte(unsigned short n) //看RX中数锯,地指不变,
  39. {
  40. volatile unsigned char *R=RxR+n;
  41. if(R>=(RxBuf+RxLen))R-=RxLen;
  42. return *R;
  43. }
  44. static void Discard(unsigned short n) //丢弃RX数据几位
  45. {
  46. while(n){ n--;
  47. if(RxR==RxW) return;
  48. if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标
  49. }
  50. }
  51. //********************* 接收 **********************//
  52. #define UART0_RX_CMD_LEN 256
  53. static UART0_Rx_t mUART0_Rx[UART0_NUM_OF_R];
  54. static unsigned char cmdDatBuf[UART0_RX_CMD_LEN]; //存放一条完整命令
  55. static unsigned int rxNum = 0;
  56. int UART0_Rx_Regist(unsigned char cmd,UART0_Callback cb) //注册
  57. {
  58. if(rxNum>=UART0_NUM_OF_R) return -1;
  59. mUART0_Rx[rxNum].cb = cb;
  60. mUART0_Rx[rxNum].cmd = cmd;
  61. mUART0_Rx[rxNum].pDat = cmdDatBuf;
  62. rxNum++;
  63. return 0;
  64. }
  65. //********************* 接收协议 **********************//
  66. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  67. static void Protocol(unsigned char len) //协议处理
  68. {
  69. #if DEBUG_UART0
  70. SEGGER_RTT_printf(0,"Rx_UART0(%d):",CheckByte(1)); for(int i=0;i<len;i++){SEGGER_RTT_printf(0,"%02X ",CheckByte(i));} SEGGER_RTT_printf(0,"\r\n");
  71. #endif
  72. uint8_t cmd = CheckByte(3);
  73. if(len<5) return;
  74. for(int j=0;j<rxNum;j++){
  75. if(mUART0_Rx[j].cmd==cmd&&mUART0_Rx[j].cb){
  76. for(int i=0;i<len-5;i++) mUART0_Rx[j].pDat[i] = CheckByte(i+4);
  77. mUART0_Rx[j].datLen = len-5;
  78. mUART0_Rx[j].cb((UART0_Rx_t*)&mUART0_Rx[j]);
  79. break;
  80. }
  81. }
  82. }
  83. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  84. void UART0_Rx_Process(void)
  85. {
  86. static unsigned char R=0;
  87. static unsigned char L=0;
  88. //接收缓存有数据,就全部处理
  89. while(1){
  90. switch( R ){
  91. case 0: if( CheckLen()<3 )return; else{
  92. if(CheckByte(0)!=0xAA){ Discard(1);}
  93. else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
  94. } break;
  95. // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验
  96. case 1: if( CheckLen()<L) { return; }else{ //SEGGER_RTT_printf(0,"Rx:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",CheckByte(i));} SEGGER_RTT_printf(0,"\r\n");
  97. unsigned char i,ver=0;
  98. for(i=0;i<L-1;i++){ ver += CheckByte(i); }
  99. if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);//丢弃整条命令
  100. } else Discard(1);
  101. R=0;
  102. } break;
  103. default: R=0;break; }
  104. }
  105. }
  106. //********************* 发送协议 **********************//
  107. void UART0_SendChar(unsigned char ch)//发送一位数锯
  108. {
  109. NRF_UART0->TXD = (unsigned int)ch;
  110. while(NRF_UART0->EVENTS_TXDRDY == 0x0UL);
  111. NRF_UART0->EVENTS_TXDRDY = 0x0UL;
  112. }
  113. void UART0_SendBuff(unsigned char *p,int L)//发送缓存
  114. {
  115. unsigned int len = L;
  116. while (len>0){len--;
  117. UART0_SendChar(*p++);
  118. }
  119. }
  120. void UART0_SendStr(char *p)//发送字符串
  121. {
  122. while (*p){
  123. UART0_SendChar(*p++);
  124. }
  125. }
  126. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  127. void UART0_Send(unsigned char cmd,unsigned char *pDat,unsigned char datLen) //协议发送
  128. {
  129. unsigned char buf[255];
  130. unsigned char ver=0;
  131. unsigned char i,L=0,Len=datLen+5;
  132. buf[L]=0xAA; ver+=buf[L++]; // 头
  133. buf[L]=Len; ver+=buf[L++]; //1位长度
  134. buf[L]=~Len; ver+=buf[L++]; // 1位长度反码
  135. buf[L]=cmd; ver+=buf[L++]; //1命令
  136. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  137. buf[L++]=ver; //效验
  138. #if DEBUG_UART0
  139. SEGGER_RTT_printf(0,"Tx_UART0:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",buf[i]);} SEGGER_RTT_printf(0,"\r\n");
  140. #endif
  141. // UART0_send_buff(buf,L); //压入发送缓存
  142. for(i=0;i<L;i++) UART0_SendChar(buf[i]);
  143. }
  144. static UART0_Tx_t* head_handle = 0;
  145. void UART0_Tx_Send(UART0_Tx_t* handle,unsigned char cmd,unsigned char *pDat,unsigned char datLen)
  146. {
  147. UART0_Tx_t* target = head_handle;
  148. if(handle){
  149. handle->cmd = cmd;
  150. handle->pDat = pDat;
  151. handle->datLen = datLen;
  152. handle->tcnt = handle->t;
  153. if(handle->n>0) handle->ncnt = handle->n-1;
  154. else handle->ncnt = 0;
  155. handle->holdon = 1;
  156. while(target){ //检查是否已经存在
  157. if(target==handle){
  158. return;
  159. }
  160. target = target->next ;
  161. }
  162. handle->next = head_handle;
  163. head_handle = handle;
  164. }
  165. UART0_Send(cmd,pDat,datLen);
  166. }
  167. void UART0_Tx_Clear(UART0_Tx_t* handle)
  168. {
  169. UART0_Tx_t** curr;
  170. for(curr=&head_handle;*curr;){
  171. UART0_Tx_t* entry = *curr;
  172. if(entry==handle){
  173. handle->holdon = 0;
  174. *curr = entry->next;
  175. }else{
  176. curr = &entry->next;
  177. }
  178. }
  179. }
  180. void UART0_Tx_Process(void)
  181. {
  182. UART0_Tx_t* target;
  183. uint8_t holdon = 0;
  184. for(target=head_handle; target; target=target->next) {
  185. if(target->tcnt>0){target->tcnt--;
  186. if(target->tcnt==0){
  187. if(target->ncnt>0){target->ncnt--;
  188. target->tcnt = target->t;
  189. UART0_Send(target->cmd,target->pDat,target->datLen);
  190. }else{
  191. UART0_Tx_Clear(target);
  192. if(target->cb){
  193. target->cb(target);
  194. }
  195. }
  196. }
  197. }
  198. holdon |= target->holdon;
  199. }
  200. Process_SetHoldOn(UART0_Tx_Process,holdon);
  201. }
  202. //void uart_event_handle(app_uart_evt_t * p_event)
  203. //{
  204. // unsigned char byte;
  205. // switch (p_event->evt_type)
  206. // {
  207. // /**@snippet [Handling data from UART] */
  208. // case APP_UART_DATA_READY:
  209. // app_uart_get(&byte);
  210. // UART0_Push(&byte,1);
  211. // break;
  212. // /**@snippet [Handling data from UART] */
  213. // case APP_UART_COMMUNICATION_ERROR:
  214. // //APP_ERROR_HANDLER(p_event->data.error_communication);
  215. // break;
  216. // case APP_UART_FIFO_ERROR:
  217. // //APP_ERROR_HANDLER(p_event->data.error_code);
  218. // break;
  219. // default:
  220. // break;
  221. // }
  222. //}
  223. //*****************************************************************//
  224. void UARTE0_UART0_IRQHandler(void)
  225. {
  226. volatile unsigned char *W=RxW; //这里要与上面指针相同
  227. if(NRF_UART0->EVENTS_RXDRDY!=0){NRF_UART0->EVENTS_RXDRDY=0;
  228. uint8_t ch = NRF_UART0->RXD;
  229. // SEGGER_RTT_printf(0," %02X\n",ch);
  230. W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底)
  231. if(W!=RxR){*RxW=ch; RxW=W;}
  232. }
  233. if(NRF_UART0->EVENTS_RXTO!=0){NRF_UART0->EVENTS_RXTO=0;
  234. //SEGGER_RTT_printf(0,"EVENTS_RXTO\n");
  235. }
  236. if(NRF_UART0->EVENTS_CTS!=0){NRF_UART0->EVENTS_CTS=0;
  237. //SEGGER_RTT_printf(0,"EVENTS_CTS\n");
  238. }
  239. if(NRF_UART0->EVENTS_NCTS!=0){NRF_UART0->EVENTS_NCTS=0;
  240. //SEGGER_RTT_printf(0,"EVENTS_NCTS\n");
  241. }
  242. if(NRF_UART0->EVENTS_TXDRDY!=0){//NRF_UART0->EVENTS_TXDRDY=0;
  243. //SEGGER_RTT_printf(0,"EVENTS_TXDRDY\n");
  244. }
  245. if(NRF_UART0->EVENTS_ERROR!=0){NRF_UART0->EVENTS_ERROR=0;
  246. //SEGGER_RTT_printf(0,"EVENTS_ERROR=%d\n",NRF_UART0->EVENTS_ERROR);
  247. }
  248. }
  249. uint32_t get_baud(uint32_t baud)
  250. {
  251. switch(baud){
  252. case 1200: return UART_BAUDRATE_BAUDRATE_Baud1200;
  253. case 2400: return UART_BAUDRATE_BAUDRATE_Baud2400;
  254. case 4800: return UART_BAUDRATE_BAUDRATE_Baud4800;
  255. case 9600: return UART_BAUDRATE_BAUDRATE_Baud9600;
  256. case 14400: return UART_BAUDRATE_BAUDRATE_Baud14400;
  257. case 19200: return UART_BAUDRATE_BAUDRATE_Baud19200;
  258. case 28800: return UART_BAUDRATE_BAUDRATE_Baud28800;
  259. case 31250: return UART_BAUDRATE_BAUDRATE_Baud31250;
  260. case 38400: return UART_BAUDRATE_BAUDRATE_Baud38400;
  261. case 56000: return UART_BAUDRATE_BAUDRATE_Baud56000;
  262. case 57600: return UART_BAUDRATE_BAUDRATE_Baud57600;
  263. case 76800: return UART_BAUDRATE_BAUDRATE_Baud76800;
  264. case 115200: return UART_BAUDRATE_BAUDRATE_Baud115200;
  265. case 230400: return UART_BAUDRATE_BAUDRATE_Baud230400;
  266. case 250000: return UART_BAUDRATE_BAUDRATE_Baud250000;
  267. case 460800: return UART_BAUDRATE_BAUDRATE_Baud460800;
  268. case 921600: return UART_BAUDRATE_BAUDRATE_Baud921600;
  269. case 1000000: return UART_BAUDRATE_BAUDRATE_Baud1M;
  270. default: return UART_BAUDRATE_BAUDRATE_Baud115200;
  271. }
  272. }
  273. void UART0_Init(uint32_t baud)
  274. {
  275. NRF_UART0->INTENCLR = UART_INTENCLR_CTS_Msk | UART_INTENCLR_RXTO_Msk | UART_INTENCLR_NCTS_Msk | UART_INTENCLR_ERROR_Msk;
  276. NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos;
  277. NRF_UART0->PSELTXD = PIN_TXD_BLE;
  278. NRF_UART0->PSELRXD = PIN_RXD_BLE;
  279. NRF_UART0->BAUDRATE = get_baud(baud);
  280. NRF_UART0->ENABLE |= UART_ENABLE_ENABLE_Enabled;
  281. NRF_UART0->TASKS_STARTTX = 0XFFFFFFFF;
  282. NRF_UART0->TASKS_STARTRX = 0XFFFFFFFF;
  283. NVIC_SetPriority(UARTE0_UART0_IRQn, 7);
  284. NVIC_EnableIRQ(UARTE0_UART0_IRQn);
  285. NRF_UART0->EVENTS_TXDRDY = 0x0UL;
  286. }
  287. void UART0_unInit(void)
  288. {
  289. NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled;
  290. NRF_UART0->INTENCLR = UART_INTENCLR_RXDRDY_Msk | UART_INTENCLR_TXDRDY_Msk | UART_INTENCLR_RXTO_Msk | UART_INTENCLR_ERROR_Msk;
  291. NVIC_DisableIRQ(UARTE0_UART0_IRQn);
  292. NVIC_ClearPendingIRQ(UARTE0_UART0_IRQn);
  293. }
  294. static void cb_uarteWakeup(uint32_t t)
  295. {
  296. UART0_Init(38400);
  297. }
  298. static void cb_uarteSleep(uint32_t t)
  299. {
  300. UART0_unInit();
  301. }
  302. void UART0_Initialize(void)
  303. {
  304. UART0_Init(38400);
  305. Process_Start(0,UART0_Rx_Process);
  306. Process_Start(10,UART0_Tx_Process);
  307. Wakeup_Regist(cb_uarteWakeup);
  308. Sleep_Regist(cb_uarteSleep);
  309. }