main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include <stdarg.h>
  2. #include "yc11xx.h"
  3. #include "yc11xx_uart.h"
  4. #include "yc11xx_gpio.h"
  5. #include "yc_timer.h"
  6. #include "ipc.h"
  7. #include "yc_lpm.h"
  8. #include "system.h"
  9. #include "att.h"
  10. #include "yc11xx_bt_interface.h"
  11. #include "core_cm0.h"
  12. #define adv_len 30
  13. /*
  14. dataPtr: adv receive data buf;
  15. len: receive data len
  16. */
  17. typedef struct
  18. {
  19. uint8_t adv_type;
  20. uint8_t adv_rssi;
  21. uint8_t mac[6];
  22. uint8_t adv_data_len;
  23. uint8_t ad_tye;
  24. uint16_t ad_id;
  25. uint8_t adv_data[100];
  26. }ADV_ParseDef;
  27. #define ADV_IND 0
  28. #define ADV_DIRECT_IND 1
  29. #define ADV_NONCONN_IND 2
  30. #define SCAN_REQ 3
  31. #define SCAN_RSP 4
  32. #define CONNECT_REQ 5
  33. #define ADV_SCAN_IND 6
  34. //#define adv_parse
  35. void adv_receive_back(uint8_t len,uint8_t *dataPtr)
  36. {
  37. #ifdef adv_parse
  38. int adv_type= *(dataPtr);
  39. int rssi = *(dataPtr+1);
  40. uint8_t mac[6];
  41. printf("receive len=%d\r\n",len);
  42. switch(adv_type)
  43. {
  44. case 0:
  45. printf("\r\nadv type: ADV_IND\r\n");
  46. break;
  47. case 1:
  48. printf("\r\nadv type: ADV_DIRECT_IND\r\n");
  49. break;
  50. case 2:
  51. printf("\r\nadv type: ADV_NONCONN_IND\r\n");
  52. break;
  53. case 3:
  54. printf("\r\nadv type: SCAN_REQ\r\n");
  55. return;
  56. // break;
  57. case 4:
  58. printf("\r\nadv type: SCAN_RSP\r\n");
  59. return;
  60. // break;
  61. case 5:
  62. printf("\r\nadv type: CONNECT_REQ\r\n");
  63. return;
  64. // break;
  65. case 6:
  66. printf("\r\nadv type: ADV_SCAN_IND\r\n");
  67. break;
  68. }
  69. printf("\r\n RSSI: -%x\r\n",rssi);
  70. memcpy(mac,dataPtr+2,6);
  71. printf("\r\n MAC: %x %x %x %x %x %x \r\n",mac[5],mac[4],mac[3],mac[2],mac[1],mac[0]);
  72. printf("receive data=\r\n");
  73. for(int i=0;i<len-2;i++)
  74. printf("%02x ",*(dataPtr+i+2));
  75. printf("\r\n");
  76. #endif
  77. ADV_ParseDef ADV_Parse;
  78. memcpy(&ADV_Parse,dataPtr,len);
  79. switch(ADV_Parse.adv_type)
  80. {
  81. case ADV_IND:
  82. MyPrintf("\r\nadv type: ADV_IND\r\n");
  83. break;
  84. case ADV_DIRECT_IND:
  85. MyPrintf("\r\nadv type: ADV_DIRECT_IND\r\n");
  86. break;
  87. case ADV_NONCONN_IND:
  88. MyPrintf("\r\nadv type: ADV_NONCONN_IND\r\n");
  89. break;
  90. case SCAN_REQ:
  91. MyPrintf("\r\nadv type: SCAN_REQ\r\n");
  92. return;
  93. case SCAN_RSP:
  94. MyPrintf("\r\nadv type: SCAN_RSP\r\n");
  95. return;
  96. case CONNECT_REQ:
  97. MyPrintf("\r\nadv type: CONNECT_REQ\r\n");
  98. return;
  99. case ADV_SCAN_IND:
  100. MyPrintf("\r\nadv type: ADV_SCAN_IND\r\n");
  101. break;
  102. }
  103. MyPrintf("\r\n RSSI: -%x\r\n",ADV_Parse.adv_rssi);
  104. // printf("adv_rssi:-%02D ",ADV_Parse.ad_tye);
  105. MyPrintf("\r\nMAC:");
  106. for(int i=0;i<6;i++)
  107. MyPrintf("%02x ",ADV_Parse.mac[i]);
  108. MyPrintf("\r\n");
  109. MyPrintf("adv_data_len=%d\r\n",ADV_Parse.adv_data_len);
  110. MyPrintf("receive adv data=\r\n");
  111. for(int i=0;i<ADV_Parse.adv_data_len;i++)
  112. MyPrintf("%02x ",*(dataPtr+i+9));
  113. MyPrintf("\r\n");
  114. }
  115. void Bt_EvtCallBack(uint8_t len,uint8_t *dataPtr)
  116. {
  117. switch(*dataPtr)
  118. {
  119. case IPC_EVT_LE_DISCONNECTED: //断连
  120. Lpm_ClearLpmFlag();
  121. #ifdef DEBUG_DRV_BT
  122. MyPrintf("\r\n***************IPC_EVT_LE_DISCONNECTED***************\r\n");
  123. #endif
  124. break;
  125. case IPC_EVT_LE_CONNECTED: //连接
  126. Bt_SndCmdLeUpdateAttMTU();
  127. IPC_TxControlCmd(IPC_CMD_UPDATE_CONN);
  128. #ifdef DEBUG_DRV_BT
  129. MyPrintf("\r\n***************IPC_EVT_LE_CONNECTED***************\r\n");
  130. #endif
  131. break;
  132. case IPC_EVT_BB_CONNECTED:
  133. #ifdef DEBUG_DRV_BT
  134. MyPrintf("\r\n****************IPC_EVT_BB_CONNECTED*****************\r\n");
  135. #endif
  136. break;
  137. case IPC_EVT_SETUP_COMPLETE:
  138. #ifdef DEBUG_DRV_BT
  139. MyPrintf("\r\n****************IPC_EVT_SETUP_COMPLETE*****************\r\n");
  140. #endif
  141. break;
  142. case IPC_EVT_BT_PAIRING_SUCCESS:
  143. #ifdef DEBUG_DRV_BT
  144. MyPrintf("\r\n****************IPC_EVT_BT_PAIRING_SUCCESS*****************\r\n");
  145. #endif
  146. break;
  147. case IPC_EVT_LINKKEY_GENERATE:
  148. #ifdef DEBUG_DRV_BT
  149. MyPrintf("\r\n****************IPC_EVT_LINKKEY_GENERATE*****************\r\n");
  150. #endif
  151. break;
  152. case IPC_EVT_RESET: //上电
  153. #ifdef DEBUG_DRV_BT
  154. MyPrintf("\r\n****************IPC_EVT_RESET*****************\r\n");
  155. #endif
  156. // Bt_Reset();
  157. break;
  158. case IPC_EVT_WAKEUP: //唤醒
  159. #ifdef DEBUG_DRV_BT
  160. MyPrintf("\r\n****************IPC_EVT_WAKEUP*****************\r\n");
  161. #endif
  162. // SysTick_Config(SYSTEM_CLOCK/100); //each systick interrupt is 10ms
  163. OS_EXIT_CRITICAL();
  164. break;
  165. case IPC_EVT_LE_TK_GENERATE:
  166. #ifdef DEBUG_DRV_BT
  167. MyPrintf("\r\n****************IPC_EVT_LE_TK_GENERATE*****************\r\n");
  168. #endif
  169. break;
  170. case IPC_EVT_LE_DISCONNECTED_ABNORMAL:
  171. #ifdef DEBUG_DRV_BT
  172. MyPrintf("\r\n******************IPC_EVT_LE_DISCONNECTED_ABNORMAL******************\r\n");
  173. #endif
  174. break;
  175. default:
  176. break;
  177. }
  178. return;
  179. }
  180. void Bt_BleCallBack(uint8_t len,uint8_t *dataPtr)
  181. {
  182. uint16_t handle;
  183. handle = dataPtr[0] +(dataPtr[1]<<8);
  184. switch(handle){
  185. }
  186. }
  187. tIPCHandleCb gTIPCHandleCb[IPC_TYPE_NUM]=
  188. {
  189. 0,
  190. IpcDefaultCallBack,//cmd
  191. Bt_EvtCallBack,//evt //evt回调函数,函数在drv_bt.c实现
  192. IpcDefaultCallBack,//hid
  193. IpcDefaultCallBack,//spp
  194. Bt_BleCallBack,//ble //ble data回调函数,函数在drv_bt.c实现
  195. IpcDefaultCallBack,//24g
  196. IpcDefaultCallBack,//mesh
  197. adv_receive_back,
  198. IpcDefaultCallBack,//mesh
  199. IpcDefaultCallBack,//a2dp
  200. IpcDefaultCallBack,//hfp
  201. IpcDefaultCallBack
  202. };
  203. int main(void)
  204. {
  205. uint8_t name[11] = {'1','1','4','7','-','B','L','E'};
  206. uint8_t lap[6]={0x11,0x22,0x33,0x44,0x55,0x66};
  207. if(HREAD(mem_wake_flag)==POWERON_WAKE)
  208. {
  209. IPC_init(&gTIPCHandleCb); //蓝牙回调函数初始化
  210. SYS_TimerInit();
  211. printport_init();
  212. // att_list_init();
  213. Att_profile_Config();
  214. MyPrintf("********** BlueTooth scan Demo*********************!\r\n");
  215. Lpm_exit();
  216. Bt_SndCmdStartScan();
  217. }
  218. else
  219. {
  220. printport_init();
  221. SysTick_Config(SYSTEM_CLOCK/100);
  222. }
  223. SYS_ClkTicks();
  224. while(1)
  225. {
  226. switch (HREAD(IPC_MCU_STATE))
  227. {
  228. case IPC_MCU_STATE_RUNNING:
  229. IPC_HandleRxPacket(); //m0和bt交互数据类型判断,是evt还是ble data?并执行对应回调函数
  230. SYS_timerPolling();
  231. Lpm_unLockLpm(M0_LPM_FLAG);
  232. break;
  233. case IPC_MCU_STATE_LMP: //lpm
  234. if (IPC_IsTxBuffEmpty())
  235. {
  236. OS_ENTER_CRITICAL();
  237. Bt_ActionBeforeLpm();
  238. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP);
  239. }
  240. else{
  241. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_RUNNING);
  242. }
  243. break;
  244. case IPC_MCU_STATE_HIBERNATE: // HIBERNATE
  245. OS_ENTER_CRITICAL();
  246. Bt_ActionBeforeHibernate();
  247. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP);
  248. break;
  249. case IPC_MCU_STATE_STOP:
  250. break;
  251. }
  252. }
  253. }