hal_ble_uart0_HasTXDEnPin.c 10 KB

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