hal_ble_client.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "hal_ble_client.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "nrf_gpio.h"
  5. /************************ 函数声明 ***********************************/
  6. extern unsigned int send_bytes_client(unsigned char *bytes, uint16_t len);
  7. void BLE_Client_Tx_Process(void);
  8. /************************ 变量 ***********************************/
  9. /********************** 环形缓存区 *************************/
  10. static const int RxLen = 1024;
  11. static volatile unsigned char RxBuf[RxLen];
  12. static volatile unsigned char* RxW=RxBuf;
  13. static volatile unsigned char* RxR=RxBuf;
  14. void BLE_Client_Push(unsigned char* p,int len)
  15. {
  16. volatile unsigned char *W=RxW; //这里要与上面指针相同
  17. if(len<=0) return;
  18. for(int i=0;i<len;i++){
  19. W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底)
  20. if(W!=RxR){*RxW=*(p+i); RxW=W;}
  21. else break;
  22. }
  23. }
  24. static unsigned int CheckLen(void) //检查RX接收了多少数据
  25. {
  26. unsigned int Len; //short
  27. volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
  28. if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果)
  29. return Len;
  30. }
  31. //static unsigned char ReadByte(void) //读RX中数锯,地指加一,和丢弃
  32. //{
  33. // unsigned char R=*RxR; //读数
  34. // if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
  35. // return R;
  36. //}
  37. static unsigned char CheckByte(unsigned short n) //看RX中数锯,地指不变,
  38. {
  39. volatile unsigned char *R=RxR+n;
  40. if(R>=(RxBuf+RxLen))R-=RxLen;
  41. return *R;
  42. }
  43. static void Discard(unsigned short n) //丢弃RX数据几位
  44. {
  45. while(n){ n--;
  46. if(RxR==RxW) return;
  47. if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标
  48. }
  49. }
  50. //********************* 接收 **********************//
  51. #define BLE_Client_RX_CMD_LEN 256
  52. static BLE_Client_Rx_t mBLE_Client_Rx[BLE_NUM_OF];
  53. static unsigned char cmdDatBuf[BLE_Client_RX_CMD_LEN]; //存放一条完整命令
  54. static unsigned int rxNum = 0;
  55. int BLE_Client_Rx_Regist(BLE_CMD_n cmd,BLE_Client_Callback cb) //注册
  56. {
  57. if(rxNum>=BLE_NUM_OF) return -1;
  58. mBLE_Client_Rx[rxNum].cb = cb;
  59. mBLE_Client_Rx[rxNum].cmd = cmd;
  60. mBLE_Client_Rx[rxNum].pDat = cmdDatBuf;
  61. rxNum++;
  62. return 0;
  63. }
  64. //********************* 接收协议 **********************//
  65. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  66. static void Protocol(unsigned char len) //协议处理
  67. {
  68. #if DEBUG_BLE_Client
  69. SEGGER_RTT_printf(0,"Rx_BLE_Client(%d):",CheckByte(1)); for(int i=0;i<len;i++){SEGGER_RTT_printf(0,"%02X ",CheckByte(i));} SEGGER_RTT_printf(0,"\r\n");
  70. #endif
  71. uint8_t cmd = CheckByte(3);
  72. if(len<5) return;
  73. for(int j=0;j<rxNum;j++){
  74. if(mBLE_Client_Rx[j].cmd==cmd&&mBLE_Client_Rx[j].cb){
  75. for(int i=0;i<len-5;i++) mBLE_Client_Rx[j].pDat[i] = CheckByte(i+4);
  76. mBLE_Client_Rx[j].datLen = len-5;
  77. mBLE_Client_Rx[j].cb((BLE_Client_Rx_t*)&mBLE_Client_Rx[j]);
  78. break;
  79. }
  80. }
  81. }
  82. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  83. void BLE_Client_Rx_Process(void)
  84. {
  85. static unsigned char R=0;
  86. static unsigned char L=0;
  87. //接收缓存有数据,就全部处理
  88. while(1){
  89. switch( R ){
  90. case 0: if( CheckLen()<3 )return; else{
  91. if(CheckByte(0)!=0xAA){ Discard(1);}
  92. else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
  93. } break;
  94. // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验
  95. 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");
  96. unsigned char i,ver=0;
  97. for(i=0;i<L-1;i++){ ver += CheckByte(i); }
  98. if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);//丢弃整条命令
  99. } else Discard(1);
  100. R=0;
  101. } break;
  102. default: R=0;break; }
  103. }
  104. }
  105. //********************* 发送协议 **********************//
  106. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  107. void BLE_Client_Send(BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen) //协议发送
  108. {
  109. unsigned char buf[255];
  110. unsigned char ver=0;
  111. unsigned char i,L=0,Len=datLen+5;
  112. buf[L]=0xAA; ver+=buf[L++]; // 头
  113. buf[L]=Len; ver+=buf[L++]; //1位长度
  114. buf[L]=~Len; ver+=buf[L++]; // 1位长度反码
  115. buf[L]=cmd; ver+=buf[L++]; //1命令
  116. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  117. buf[L++]=ver; //效验
  118. #if DEBUG_BLE_Client
  119. SEGGER_RTT_printf(0,"Tx_BLE_Client:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",buf[i]);} SEGGER_RTT_printf(0,"\r\n");
  120. #endif
  121. send_bytes_client(buf,L); //压入发送缓存
  122. }
  123. static BLE_Client_Tx_t* head_handle = 0;
  124. void BLE_Client_Tx_Send(BLE_Client_Tx_t* handle,BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen)
  125. {
  126. BLE_Client_Tx_t* target = head_handle;
  127. if(handle){
  128. handle->cmd = cmd;
  129. handle->pDat = pDat;
  130. handle->datLen = datLen;
  131. handle->tcnt = handle->t;
  132. if(handle->n>0) handle->ncnt = handle->n-1;
  133. else handle->ncnt = 0;
  134. handle->holdon = 1;
  135. while(target){ //检查是否已经存在
  136. if(target==handle){
  137. return;
  138. }
  139. target = target->next ;
  140. }
  141. handle->next = head_handle;
  142. head_handle = handle;
  143. }
  144. BLE_Client_Send(cmd,pDat,datLen);
  145. }
  146. void BLE_Client_Tx_Clear(BLE_Client_Tx_t* handle)
  147. {
  148. BLE_Client_Tx_t** curr;
  149. for(curr=&head_handle;*curr;){
  150. BLE_Client_Tx_t* entry = *curr;
  151. if(entry==handle){
  152. handle->holdon = 0;
  153. *curr = entry->next;
  154. }else{
  155. curr = &entry->next;
  156. }
  157. }
  158. }
  159. void BLE_Client_Tx_Process(void)
  160. {
  161. BLE_Client_Tx_t* target;
  162. uint8_t holdon = 0;
  163. for(target=head_handle; target; target=target->next) {
  164. if(target->tcnt > 0){
  165. if(target->tcnt >= HeartTime_Interval)target->tcnt -= HeartTime_Interval;
  166. else target->tcnt =0;
  167. if(target->tcnt==0){
  168. if(target->ncnt>0){target->ncnt--;
  169. target->tcnt = target->t;
  170. BLE_Client_Send(target->cmd,target->pDat,target->datLen);
  171. }else{
  172. BLE_Client_Tx_Clear(target);
  173. if(target->cb){
  174. target->cb(target);
  175. }
  176. }
  177. }
  178. }
  179. holdon |= target->holdon;
  180. }
  181. Process_SetHoldOn(BLE_Client_Tx_Process,holdon);
  182. }
  183. #include "ble_comm.h"
  184. void BLE_Client_Initialize(void)
  185. {
  186. Process_Start(0,"BLE_Client_Rx",BLE_Client_Rx_Process);
  187. Process_Start(10,"BLE_Client_Tx",BLE_Client_Tx_Process);
  188. Process_Start(0,"send_bytes_client_pcs",send_bytes_client_pcs);
  189. Process_SetHoldOn(send_bytes_client_pcs,1);
  190. }