hal_ble_host.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "hal_ble_host.h"
  2. #include "system.h"
  3. #include "bsp_time.h"
  4. #include "hal_flash.h"
  5. #include "hal_ser_imu_mode_manage.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. static BLE_Host_Callback_cb clientcb;
  16. int BLE_Host_Rx_Regist_Game(BLE_Host_Callback_cb cb) //注册
  17. {
  18. clientcb = cb;
  19. return 0;
  20. }
  21. void BLE_Host_Push(unsigned char* p,int len)
  22. {
  23. volatile unsigned char *W=RxW; //这里要与上面指针相同
  24. if(len<5) return;
  25. unsigned char L = p[1];
  26. unsigned char LF = ~p[2];
  27. if(p[0]==0xAA&&L==LF&&L==len&&p[3]<5){
  28. clientcb(p,len);
  29. }else{
  30. for(int i=0;i<len;i++){
  31. W=RxW+1; if(W>=RxBuf+RxLen) W=RxBuf; //取下一位置(到顶转到底)
  32. if(W!=RxR){*RxW=*(p+i); RxW=W;}
  33. else break;
  34. }
  35. }
  36. }
  37. static unsigned int CheckLen(void) //检查RX接收了多少数据
  38. {
  39. unsigned int Len; //short
  40. volatile unsigned char *W=RxW; volatile unsigned char *R=RxR;
  41. if(W>=R)Len=W-R;else Len=(W+RxLen)-R; //这样正确(中途中断改变也变不了结果)
  42. return Len;
  43. }
  44. //static unsigned char ReadByte(void) //读RX中数锯,地指加一,和丢弃
  45. //{
  46. // unsigned char R=*RxR; //读数
  47. // if(RxR!=RxW){ if(RxR+1>=(RxBuf+RxLen))RxR =RxBuf; else RxR++;}//下标
  48. // return R;
  49. //}
  50. static unsigned char CheckByte(unsigned short n) //看RX中数锯,地指不变,
  51. {
  52. volatile unsigned char *R=RxR+n;
  53. if(R>=(RxBuf+RxLen))R-=RxLen;
  54. return *R;
  55. }
  56. static void Discard(unsigned short n) //丢弃RX数据几位
  57. {
  58. while(n){ n--;
  59. if(RxR==RxW) return;
  60. if(RxR+1>=RxBuf+RxLen){RxR=RxBuf;} else RxR++; //下标
  61. }
  62. }
  63. //********************* 接收 **********************//
  64. #define BLE_Host_RX_CMD_LEN 256
  65. static BLE_Host_Rx_t mBLE_Host_Rx[BLE_NUM_OF];
  66. static unsigned char cmdDatBuf[BLE_Host_RX_CMD_LEN]; //存放一条完整命令
  67. static unsigned int rxNum = 0;
  68. int BLE_Host_Rx_Regist(BLE_CMD_n cmd,BLE_Host_Callback cb) //注册
  69. {
  70. if(rxNum>=BLE_NUM_OF) return -1;
  71. mBLE_Host_Rx[rxNum].cb = cb;
  72. mBLE_Host_Rx[rxNum].cmd = cmd;
  73. mBLE_Host_Rx[rxNum].pDat = cmdDatBuf;
  74. rxNum++;
  75. return 0;
  76. }
  77. //********************* 接收协议 **********************//
  78. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  79. static void Protocol(unsigned char len) //协议处理
  80. {
  81. #if DEBUG_BLE_Host
  82. 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");
  83. #endif
  84. uint8_t cmd = CheckByte(3);
  85. if(cmd<5){
  86. uint8_t buf[BLE_Host_RX_CMD_LEN];
  87. for(int i=0;i<len-5;i++) buf[i] = CheckByte(i+4);
  88. BLE_Host_Tx_Send(0,(BLE_CMD_n)cmd,buf,len-5);
  89. }
  90. if(len<5) return;
  91. for(int j=0;j<rxNum;j++){
  92. if(mBLE_Host_Rx[j].cmd==cmd&&mBLE_Host_Rx[j].cb){
  93. for(int i=0;i<len-5;i++) mBLE_Host_Rx[j].pDat[i] = CheckByte(i+4);
  94. mBLE_Host_Rx[j].datLen = len-5;
  95. mBLE_Host_Rx[j].cb((BLE_Host_Rx_t*)&mBLE_Host_Rx[j]);
  96. break;
  97. }
  98. }
  99. }
  100. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  101. void BLE_Host_Rx_Process(void)
  102. {
  103. static unsigned char R=0;
  104. static unsigned char L=0;
  105. //接收缓存有数据,就全部处理
  106. while(1){
  107. switch( R ){
  108. case 0: if( CheckLen()<3 )return; else{
  109. if(CheckByte(0)!=0xAA){ Discard(1);}
  110. else{unsigned char LF=CheckByte(2);LF=~LF; L=CheckByte(1); if((LF==L)&&(L>=5)){ R++;}else { Discard(1);}}
  111. } break;
  112. // 多收数据 7 = 3位头+ 1位长度负数+ 1位长度+ 2效验
  113. 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");
  114. unsigned char i,ver=0;
  115. for(i=0;i<L-1;i++){ ver += CheckByte(i); }
  116. if(CheckByte(L-1)==ver){ Protocol(L);Discard(L);//丢弃整条命令
  117. } else Discard(1);
  118. R=0;
  119. } break;
  120. default: R=0;break; }
  121. }
  122. }
  123. //********************* 发送协议 **********************//
  124. //协议(1位头+ 1位长度+ 1位长度反码+ 1命令+ N数据+ 1效验)
  125. void BLE_Host_Send(BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen) //协议发送
  126. {
  127. unsigned char buf[255];
  128. unsigned char ver=0;
  129. unsigned char i,L=0,Len=datLen+5;
  130. if(datLen > 250)return;
  131. buf[L]=0xAA; ver+=buf[L++]; // 头
  132. buf[L]=Len; ver+=buf[L++]; //1位长度
  133. buf[L]=~Len; ver+=buf[L++]; //1位长度反码
  134. buf[L]=cmd; ver+=buf[L++]; //1命令
  135. for(i=0;i<datLen; i++){ buf[L]=pDat[i];ver+=buf[L++];} //数据
  136. buf[L++]=ver; //效验
  137. #if DEBUG_BLE_Host
  138. 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");
  139. #endif
  140. send_bytes_server(buf,L); //压入发送缓存
  141. }
  142. static BLE_Host_Tx_t* head_handle = 0;
  143. void BLE_Host_Tx_Send(BLE_Host_Tx_t* handle,BLE_CMD_n cmd,unsigned char *pDat,unsigned char datLen)
  144. {
  145. BLE_Host_Tx_t* target = head_handle;
  146. if(handle){
  147. handle->cmd = cmd;
  148. handle->pDat = pDat;
  149. handle->datLen = datLen;
  150. handle->tcnt = handle->t;
  151. if(handle->n>0) handle->ncnt = handle->n-1;
  152. else handle->ncnt = 0;
  153. handle->holdon = 1;
  154. while(target){ //检查是否已经存在
  155. if(target==handle){
  156. // SEGGER_RTT_printf(0,"handle(%d)\n",handle);
  157. return;
  158. }
  159. target = target->next ;
  160. }
  161. // SEGGER_RTT_printf(0,"add handle(%d)\n",handle);
  162. handle->next = head_handle;
  163. head_handle = handle;
  164. }
  165. BLE_Host_Send(cmd,pDat,datLen);
  166. }
  167. void BLE_Host_Tx_Clear(BLE_Host_Tx_t* handle)
  168. {
  169. BLE_Host_Tx_t** curr;
  170. for(curr=&head_handle;*curr;){
  171. BLE_Host_Tx_t* entry = *curr;
  172. if(entry==handle){
  173. handle->holdon = 0;
  174. *curr = entry->next;
  175. }else{
  176. curr = &entry->next;
  177. }
  178. }
  179. }
  180. void BLE_Host_Tx_Process(void)
  181. {
  182. BLE_Host_Tx_t* target;
  183. uint8_t holdon = 0;
  184. static uint32_t tim = 0;
  185. uint32_t tim_dif =0;
  186. for(target=head_handle; target; target=target->next) {
  187. if(target->tcnt > 0){
  188. tim_dif = (TIME_GetTicks()- tim);
  189. tim = TIME_GetTicks();
  190. if(tim_dif < HeartTime_Interval)tim_dif = HeartTime_Interval;
  191. if(target->tcnt >= tim_dif)target->tcnt -= tim_dif;
  192. else target->tcnt =0;
  193. if(target->tcnt==0){
  194. if(target->ncnt>0){target->ncnt--;
  195. target->tcnt = target->t;
  196. BLE_Host_Send((BLE_CMD_n)target->cmd,target->pDat,target->datLen);
  197. }else{
  198. BLE_Host_Tx_Clear(target);
  199. if(target->cb){
  200. target->cb(target);
  201. }
  202. }
  203. }
  204. }
  205. holdon |= target->holdon;
  206. }
  207. Process_SetHoldOn(BLE_Host_Tx_Process,holdon);
  208. }
  209. void BLE_Host_Initialize(void)
  210. {
  211. Process_Start(0,"BLE_Host_Rx",BLE_Host_Rx_Process);
  212. Process_Start(HeartTime_Interval,"BLE_Host_Tx",BLE_Host_Tx_Process);
  213. }