hal_ble_client.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. DEBUG_LOG("Rx_BLE_Client(%d):",CheckByte(1)); for(int i=0;i<len;i++){DEBUG_LOG("%02X ",CheckByte(i));} DEBUG_LOG("\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{ //DEBUG_LOG("Rx:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",CheckByte(i));} DEBUG_LOG("\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. int 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. if(datLen > 250)return -1;
  113. buf[L]=0xAA; ver+=buf[L++]; // 头
  114. buf[L]=Len; ver+=buf[L++]; //1位长度
  115. buf[L]=~Len; ver+=buf[L++]; // 1位长度反码
  116. buf[L]=cmd; ver+=buf[L++]; //1命令
  117. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  118. buf[L++]=ver; //效验
  119. #if DEBUG_BLE_Client
  120. DEBUG_LOG("Tx_BLE_Client:"); for(int i=0;i<L;i++){DEBUG_LOG("%02X ",buf[i]);} DEBUG_LOG("\r\n");
  121. #endif
  122. return send_bytes_client(buf,L); //压入发送缓存
  123. }
  124. static BLE_Client_Tx_t* head_handle = 0;
  125. void BLE_Client_Tx_Send(BLE_Client_Tx_t* handle,BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen)
  126. {
  127. BLE_Client_Tx_t* target = head_handle;
  128. if(handle){
  129. handle->cmd = cmd;
  130. handle->pDat = pDat;
  131. handle->datLen = datLen;
  132. handle->tcnt = handle->t;
  133. if(handle->n>0) handle->ncnt = handle->n-1;
  134. else handle->ncnt = 0;
  135. handle->holdon = 1;
  136. while(target){ //检查是否已经存在
  137. if(target==handle){
  138. return;
  139. }
  140. target = target->next ;
  141. }
  142. handle->next = head_handle;
  143. head_handle = handle;
  144. }
  145. BLE_Client_Send(cmd,pDat,datLen);
  146. }
  147. void BLE_Client_Tx_Clear(BLE_Client_Tx_t* handle)
  148. {
  149. BLE_Client_Tx_t** curr;
  150. for(curr=&head_handle;*curr;){
  151. BLE_Client_Tx_t* entry = *curr;
  152. if(entry==handle){
  153. handle->holdon = 0;
  154. *curr = entry->next;
  155. }else{
  156. curr = &entry->next;
  157. }
  158. }
  159. }
  160. void BLE_Client_Tx_Process(void)
  161. {
  162. BLE_Client_Tx_t* target;
  163. uint8_t holdon = 0;
  164. static uint32_t tim = 0;
  165. uint32_t tim_dif =0;
  166. for(target=head_handle; target; target=target->next) {
  167. if(target->tcnt > 0){
  168. tim_dif = (TIME_GetTicks()- tim);
  169. tim = TIME_GetTicks();
  170. if(tim_dif < HeartTime_Interval)tim_dif = HeartTime_Interval;
  171. if(target->tcnt >= tim_dif)target->tcnt -= tim_dif;
  172. else target->tcnt =0;
  173. if(target->tcnt==0){
  174. if(target->ncnt>0){target->ncnt--;
  175. target->tcnt = target->t;
  176. BLE_Client_Send(target->cmd,target->pDat,target->datLen);
  177. }else{
  178. BLE_Client_Tx_Clear(target);
  179. if(target->cb){
  180. target->cb(target);
  181. }
  182. }
  183. }
  184. }
  185. holdon |= target->holdon;
  186. }
  187. Process_SetHoldOn(BLE_Client_Tx_Process,holdon);
  188. }
  189. #include "ble_comm.h"
  190. void BLE_Client_Initialize(void)
  191. {
  192. Process_Start(0,"BLE_Client_Rx",BLE_Client_Rx_Process);
  193. Process_Start(HeartTime_Interval,"BLE_Client_Tx",BLE_Client_Tx_Process);
  194. #if USEFIFO
  195. Process_Start(0,"send_bytes_client_pcs",send_bytes_client_pcs);
  196. #endif
  197. }