hal_ble_host.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "hal_ble_host.h"
  2. #include "system.h"
  3. //#include "bsp_time.h"
  4. #include "hal_flash.h"
  5. #include "hal_imu.h"
  6. /************************ 函数声明 ***********************************/
  7. extern int send_bytes_server(uint8_t * bytes, uint16_t len);
  8. void BLE_Host_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 BLE_Host_Push(unsigned char* p,int len)
  16. {
  17. volatile unsigned char *W=RxW; //这里要与上面指针相同
  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_Host_RX_CMD_LEN 256
  52. static BLE_Host_Rx_t mBLE_Host_Rx[BLE_NUM_OF];
  53. static unsigned char cmdDatBuf[BLE_Host_RX_CMD_LEN]; //存放一条完整命令
  54. static unsigned int rxNum = 0;
  55. int BLE_Host_Rx_Regist(BLE_CMD_n cmd,BLE_Host_Callback cb) //注册
  56. {
  57. if(rxNum>=BLE_NUM_OF) return -1;
  58. mBLE_Host_Rx[rxNum].cb = cb;
  59. mBLE_Host_Rx[rxNum].cmd = cmd;
  60. mBLE_Host_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_Host
  69. SEGGER_RTT_printf(0,"Rx_BLE_Host(%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(cmd<5){
  73. uint8_t buf[BLE_Host_RX_CMD_LEN];
  74. for(int i=0;i<len-5;i++) buf[i] = CheckByte(i+4);
  75. BLE_Host_Tx_Send(0,(BLE_CMD_n)cmd,buf,len-5);
  76. }
  77. if(len<5) return;
  78. for(int j=0;j<rxNum;j++){
  79. if(mBLE_Host_Rx[j].cmd==cmd&&mBLE_Host_Rx[j].cb){
  80. for(int i=0;i<len-5;i++) mBLE_Host_Rx[j].pDat[i] = CheckByte(i+4);
  81. mBLE_Host_Rx[j].datLen = len-5;
  82. mBLE_Host_Rx[j].cb((BLE_Host_Rx_t*)&mBLE_Host_Rx[j]);
  83. break;
  84. }
  85. }
  86. }
  87. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  88. void BLE_Host_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. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  112. void BLE_Host_Send(BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen) //协议发送
  113. {
  114. unsigned char buf[255];
  115. unsigned char ver=0;
  116. unsigned char i,L=0,Len=datLen+5;
  117. buf[L]=0xAA; ver+=buf[L++]; // 头
  118. buf[L]=Len; ver+=buf[L++]; //1位长度
  119. buf[L]=~Len; ver+=buf[L++]; // 1位长度反码
  120. buf[L]=cmd; ver+=buf[L++]; //1命令
  121. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  122. buf[L++]=ver; //效验
  123. #if DEBUG_BLE_Host
  124. SEGGER_RTT_printf(0,"Tx_BLE_Host:"); for(int i=0;i<L;i++){SEGGER_RTT_printf(0,"%02X ",buf[i]);} SEGGER_RTT_printf(0,"\r\n");
  125. #endif
  126. send_bytes_server(buf,L); //压入发送缓存
  127. }
  128. static BLE_Host_Tx_t* head_handle = 0;
  129. void BLE_Host_Tx_Send(BLE_Host_Tx_t* handle,BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen)
  130. {
  131. BLE_Host_Tx_t* target = head_handle;
  132. if(handle){
  133. handle->cmd = cmd;
  134. handle->pDat = pDat;
  135. handle->datLen = datLen;
  136. handle->tcnt = handle->t;
  137. if(handle->n>0) handle->ncnt = handle->n-1;
  138. else handle->ncnt = 0;
  139. handle->holdon = 1;
  140. while(target){ //检查是否已经存在
  141. if(target==handle){
  142. // SEGGER_RTT_printf(0,"handle(%d)\n",handle);
  143. return;
  144. }
  145. target = target->next ;
  146. }
  147. // SEGGER_RTT_printf(0,"add handle(%d)\n",handle);
  148. handle->next = head_handle;
  149. head_handle = handle;
  150. }
  151. BLE_Host_Send(cmd,pDat,datLen);
  152. }
  153. void BLE_Host_Tx_Clear(BLE_Host_Tx_t* handle)
  154. {
  155. BLE_Host_Tx_t** curr;
  156. for(curr=&head_handle;*curr;){
  157. BLE_Host_Tx_t* entry = *curr;
  158. if(entry==handle){
  159. handle->holdon = 0;
  160. *curr = entry->next;
  161. }else{
  162. curr = &entry->next;
  163. }
  164. }
  165. }
  166. void BLE_Host_Tx_Process(void)
  167. {
  168. BLE_Host_Tx_t* target;
  169. uint8_t holdon = 0;
  170. for(target=head_handle; target; target=target->next) {
  171. if(target->tcnt > 0){
  172. if(target->tcnt >= HeartTime_Interval)target->tcnt -= HeartTime_Interval;
  173. else target->tcnt =0;
  174. if(target->tcnt==0){
  175. if(target->ncnt>0){target->ncnt--;
  176. target->tcnt = target->t;
  177. BLE_Host_Send((BLE_CMD_n)target->cmd,target->pDat,target->datLen);
  178. }else{
  179. BLE_Host_Tx_Clear(target);
  180. if(target->cb){
  181. target->cb(target);
  182. }
  183. }
  184. }
  185. }
  186. holdon |= target->holdon;
  187. }
  188. Process_SetHoldOn(BLE_Host_Tx_Process,holdon);
  189. }
  190. void BLE_Host_Initialize(void)
  191. {
  192. Process_Start(0,"BLE_Host_Rx",BLE_Host_Rx_Process);
  193. Process_Start(1,"BLE_Host_Tx",BLE_Host_Tx_Process);
  194. }