hal_ble_uart0_HasTXDEnPin.c 10 KB

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