Drv_bt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include <stdio.h>
  2. #include "Drv_bt.h"
  3. #include "ipc.h"
  4. #include "yc_timer.h"
  5. #include "yc_it.h"
  6. #include "drv_ota.h"
  7. #include "yc_drv_common.h"
  8. #include "yc_lpm.h"
  9. #include "system.h"
  10. #include "core_cm0.h"
  11. #define DEBUG_DRV_BT
  12. #undef INFO_HEADER
  13. #define INFO_HEADER "DRVBT"
  14. #ifndef ATT_LIST_LEN
  15. #define ATT_LIST_LEN mem_rc_att_list_end-mem_rc_att_list
  16. #endif
  17. SYS_TIMER_TYPE OTA_sleep_timer;
  18. SYS_TIMER_TYPE gAdv_Timer;
  19. BR_STATE gBRState;
  20. BLE_STATE gBLEState;
  21. #ifdef PACKET_LEN_100
  22. //SYS_TIMER_TYPE gMTU_Timer;
  23. #endif
  24. #define Flash_BLE_ADDR 0x7f000
  25. SYS_TIMER_TYPE gConnect_Timer;
  26. void Ble_SendBleData(uint16_t BLE_WRITE_HANDLE,uint8_t* data, uint16_t len)//eg handle = 0x0008
  27. {
  28. uint8_t buf[len+2];
  29. if(len<0 || len>20)
  30. return;
  31. uint8_t BleSendData[30]={0};
  32. buf[0]= BLE_WRITE_HANDLE&0xff;
  33. buf[1]= (BLE_WRITE_HANDLE>>8)&0xff;
  34. memcpy(buf+2,data,len);
  35. Bt_SndBleData(buf,len+2);
  36. }
  37. void Bt_SetBleAddr(unsigned char* pAddr)
  38. {
  39. int AddrLen = 6;
  40. for(int i=0; i<AddrLen; i++){
  41. HWRITE(mem_le_lap+i,pAddr[i]);
  42. }
  43. }
  44. void Bt_SetSppAddr(unsigned char* pAddr)
  45. {
  46. int AddrLen = 6;
  47. for(int i=0; i<AddrLen; i++){
  48. HWRITE(mem_lap+i,pAddr[i]);
  49. }
  50. }
  51. void Bt_SetSppName(unsigned char* Name,int Len)
  52. {
  53. if(Len>27)
  54. return;
  55. for(int i=0;i<Len;i++)
  56. {
  57. HWRITE(mem_local_name_length+1+i,*(Name+i));
  58. }
  59. }
  60. void Bt_Re_BleAdvName(uint8_t *name,uint8_t len)
  61. {
  62. HWRITE(mem_le_adv_data,len+1);
  63. HWRITE(mem_le_adv_data+1,Adv_Type_CompleteLocalName);
  64. for(uint8_t i=0;i<len; i++)
  65. {
  66. /* adv data add name */
  67. HWRITE(mem_le_adv_data+2+i,name[i]);
  68. }
  69. HWRITE(mem_le_adv_data_len,len+1);
  70. }
  71. void Bt_Re_BleDeivcename(uint8_t *name,uint8_t len)
  72. {
  73. int i;
  74. int cache_len,oldname_len;
  75. uint8_t att_list[ATT_LIST_LEN];
  76. memset(att_list,0,ATT_LIST_LEN);
  77. oldname_len=HREAD(mem_rc_att_list+20);
  78. cache_len = ATT_LIST_LEN-21-oldname_len;
  79. #ifdef DEBUG_DRV_BT
  80. MyPrintf("cache_len=%d\r\n",cache_len);
  81. #endif
  82. for(i=0;i<cache_len;i++)
  83. {
  84. att_list[i]=HREAD(mem_rc_att_list+21+oldname_len+i);
  85. }
  86. #ifdef DEBUG_DRV_BT
  87. for(i=0;i<20;i++)
  88. {
  89. MyPrintf("att_list[%d]=%02x \r\n",i,att_list[i]);
  90. }
  91. #endif
  92. HWRITE(mem_rc_att_list+20,len);
  93. for(i=0; i<len; i++){
  94. HWRITE(mem_rc_att_list+21+i,*(name+i));
  95. }
  96. for(i=0;i<cache_len;i++){
  97. HWRITE(mem_rc_att_list+21+len+i,att_list[i]);
  98. }
  99. }
  100. void Bt_Renew_Adv_Data(uint8_t *data,uint8_t len)
  101. {
  102. uint8_t length=0;
  103. if(len > LE_ADV_DATA_MAX_LENGTH)
  104. return;
  105. HWRITE(mem_le_adv_enable,ADV_DISABLE);
  106. while(len--)
  107. {
  108. HWRITE(mem_le_adv_data+length,*data);
  109. data++;
  110. length++;
  111. }
  112. HWRITE(mem_le_adv_data_len,length);
  113. }
  114. void Bt_Renew_Scan_Rsp(uint8_t *data,uint8_t len)
  115. {
  116. uint8_t length=0;
  117. if(len > LE_SCAN_RSP_MAX_LENGTH)
  118. return;
  119. HWRITE(mem_le_adv_enable,ADV_DISABLE);
  120. while(len--)
  121. {
  122. HWRITE(mem_le_scan_data+length,*data);
  123. data++;
  124. length++;
  125. }
  126. HWRITE(mem_le_scan_data_len,length);
  127. }
  128. void Bt_SetAdvType(advType Type)
  129. {
  130. HWRITE(mem_le_adv_type, Type);
  131. }
  132. void Bt_SetScanRspType(scanType Type)
  133. {
  134. HWRITE(mem_le_scan_type, Type);
  135. }
  136. void Update_connection_param(BLE_Conn_param* param)
  137. {
  138. HWRITEW(mem_le_connection_updata_param,param->min_interval);
  139. HWRITEW(mem_le_connection_updata_param+2,param->max_interval);
  140. HWRITEW(mem_le_connection_updata_param+4,param->latency);
  141. HWRITEW(mem_le_connection_updata_param+6,param->timeout);
  142. IPC_TxControlCmd(IPC_CMD_UPDATE_CONN);
  143. }
  144. void Bt_SetAdvInterval(uint16_t gap)
  145. {
  146. HWRITEW(mem_le_adv_interval_max,gap);
  147. }
  148. void Bt_init()
  149. {
  150. gBRState.topState = BR_IDLE;
  151. gBRState.secondState=BR_IDLE;
  152. gBRState.thirdlyState = BR_IDLE;
  153. gBLEState.topState = BLE_IDLE;
  154. gBLEState.secondState = BLE_IDLE;
  155. }
  156. void Bt_StartAdv(int time_10ms,uint16_t gap){
  157. Bt_setBLEState(BLE_ADV);
  158. Bt_SetAdvInterval(gap);
  159. Lpm_Set_Interval(gap);
  160. // SYS_SetTimer(&gAdv_Timer,time_10ms,TIMER_SINGLE|TIMER_TYPE_BIT,Bt_stop_adv);
  161. Bt_SndCmdBleStartAdv();
  162. }
  163. void Bt_StartDirectAdv(int time_10ms, uint16_t gap){
  164. Bt_SetAdvInterval(gap);
  165. Lpm_Set_Interval(gap);
  166. Bt_setBLEState(BLE_DIRECT_ADV);
  167. // SYS_SetTimer(&gAdv_Timer,time_10ms,TIMER_SINGLE|TIMER_TYPE_BIT,Bt_send_stop_direct_adv);
  168. Bt_SndCmdBleStartDirectAdv();
  169. }
  170. uint8_t Bt_checkBleIsConnected()
  171. {
  172. if(gBLEState.topState == BLE_CONNECTED)
  173. return 1;
  174. return 0;
  175. }
  176. uint8_t Bt_checkBRIsConnected()
  177. {
  178. if(gBRState.topState == BR_CONNECTED)
  179. return 1;
  180. return 0;
  181. }
  182. void BT_OTA_Sleep_func(int i){
  183. Lpm_unLockLpm(OTA_UPDATE_FLAG);
  184. }
  185. uint8_t Bt_checkBleIsAdvStart()
  186. {
  187. if(gBLEState.topState == BLE_ADV)
  188. return 1;
  189. return 0;
  190. }
  191. BLE_STATE_TYPE Bt_getBLEState(){
  192. return gBLEState.topState;
  193. }
  194. void Bt_setBLEState(uint8_t inState)
  195. {
  196. switch (inState){
  197. case BLE_IDLE:
  198. case BLE_ADV:
  199. case BLE_CONNECTED:
  200. case BLE_CONNECTING:
  201. case BLE_DIRECT_ADV:
  202. case BLE_PAIRED:
  203. gBLEState.topState = inState;
  204. default:
  205. break;
  206. }
  207. //HWRITE(mem_save_state,gBLEState.topState);
  208. }
  209. void Bt_force_disconn(){
  210. Bt_SndCmdBleDisconnect();
  211. Bt_SndCmdPwroff();
  212. }
  213. void Bt_Reset()
  214. {
  215. uint8_t advD[19] = {0x02,0x01,0x06,0x05,0x03,0x12,0x18,0x0f,0x18,0x03,0x19,0x80,0x01};
  216. uint8_t addr[6] = {0xac,0x02,0x78,0x11,0x43,0Xac};
  217. uint8_t name[11] = {"11XXBLE"};
  218. Bt_Re_BleAdvName(name,11);
  219. Bt_Renew_Scan_Rsp(advD,19); //设置广播参数
  220. Bt_SetBleAddr(addr); //设置蓝牙地址
  221. Bt_Re_BleDeivcename(name,11);
  222. Bt_init();
  223. // Lpm_enter(); //
  224. Lpm_Set_Conn_Interval(0x1a0);
  225. Bt_StartAdv(100,0x80);
  226. }
  227. void Bt_lpm_enable(int none){
  228. Lpm_unLockLpm(CONNECT_WAKE_FLAG);
  229. }
  230. void Bt_EvtCallBack(uint8_t len,uint8_t *dataPtr)
  231. {
  232. switch(*dataPtr)
  233. {
  234. case IPC_EVT_LE_DISCONNECTED: //断连
  235. gBLEState.topState = BLE_IDLE;
  236. Lpm_ClearLpmFlag();
  237. #ifdef DEBUG_DRV_BT
  238. MyPrintf("\r\n***************IPC_EVT_LE_DISCONNECTED***************\r\n");
  239. #endif
  240. Bt_StartAdv(60,0x80);
  241. break;
  242. case IPC_EVT_LE_CONNECTED: //连接
  243. Bt_SndCmdUpdateAttMTU( );
  244. Bt_SndCmdLeUpdateConn( );
  245. SYS_ReleaseTimer(&gAdv_Timer);
  246. Lpm_unLockLpm(ADV_FLAG);
  247. Lpm_LockLpm(CONNECT_WAKE_FLAG);
  248. Bt_setBLEState(BLE_CONNECTED);
  249. SYS_SetTimer(&gConnect_Timer,400,TIMER_SINGLE|TIMER_TYPE_BIT,Bt_lpm_enable);
  250. #ifdef DEBUG_DRV_BT
  251. MyPrintf("\r\n***************IPC_EVT_LE_CONNECTED***************\r\n");
  252. #endif
  253. break;
  254. case IPC_EVT_BB_CONNECTED:
  255. #ifdef DEBUG_DRV_BT
  256. MyPrintf("\r\n****************IPC_EVT_BB_CONNECTED*****************\r\n");
  257. #endif
  258. break;
  259. case IPC_EVT_SETUP_COMPLETE:
  260. #ifdef DEBUG_DRV_BT
  261. MyPrintf("\r\n****************IPC_EVT_SETUP_COMPLETE*****************\r\n");
  262. #endif
  263. break;
  264. case IPC_EVT_BT_PAIRING_SUCCESS:
  265. #ifdef DEBUG_DRV_BT
  266. MyPrintf("\r\n****************IPC_EVT_BT_PAIRING_SUCCESS*****************\r\n");
  267. #endif
  268. break;
  269. case IPC_EVT_LINKKEY_GENERATE:
  270. #ifdef DEBUG_DRV_BT
  271. MyPrintf("\r\n****************IPC_EVT_LINKKEY_GENERATE*****************\r\n");
  272. #endif
  273. break;
  274. case IPC_EVT_RESET: //上电
  275. #ifdef DEBUG_DRV_BT
  276. MyPrintf("\r\n****************IPC_EVT_RESET*****************\r\n");
  277. #endif
  278. // Bt_Reset();
  279. break;
  280. case IPC_EVT_WAKEUP: //唤醒
  281. #ifdef DEBUG_DRV_BT
  282. MyPrintf("\r\n****************IPC_EVT_WAKEUP*****************\r\n");
  283. #endif
  284. // SysTick_Config(SYSTEM_CLOCK/100); //each systick interrupt is 10ms
  285. OS_EXIT_CRITICAL();
  286. break;
  287. case IPC_EVT_LE_TK_GENERATE:
  288. #ifdef DEBUG_DRV_BT
  289. MyPrintf("\r\n****************IPC_EVT_LE_TK_GENERATE*****************\r\n");
  290. #endif
  291. break;
  292. case IPC_EVT_LE_DISCONNECTED_ABNORMAL:
  293. #ifdef DEBUG_DRV_BT
  294. MyPrintf("\r\n******************IPC_EVT_LE_DISCONNECTED_ABNORMAL******************\r\n");
  295. #endif
  296. break;
  297. default:
  298. break;
  299. }
  300. HWRITE(mem_save_state,gBLEState.topState);
  301. return;
  302. }
  303. void Bt_BleCallBack(uint8_t len,uint8_t *dataPtr)
  304. {
  305. uint16_t handle;
  306. handle = dataPtr[0] +(dataPtr[1]<<8);
  307. switch(handle){
  308. case OTA_BLE_WRITE_HANDLE: //ota write handle
  309. Lpm_LockLpm(OTA_UPDATE_FLAG); //停止进入休眠
  310. Bt_BleOTA(len, dataPtr); //ota 升级
  311. SYS_SetTimer(&OTA_sleep_timer,50,TIMER_SINGLE|TIMER_TYPE_BIT,BT_OTA_Sleep_func);
  312. SYS_ResetTimer(&OTA_sleep_timer);
  313. break;
  314. case 0x001a:
  315. for(int i=0;i<len;i++)
  316. {
  317. }
  318. break;
  319. case 0x0017:
  320. for(int i=0;i<len;i++)
  321. {
  322. }
  323. Ble_SendBleData(0x0016,dataPtr,len);
  324. break;
  325. default:
  326. break;
  327. }
  328. }