main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "yc11xx_bt_interface.h"
  10. #include "att.h"
  11. #include "core_cm0.h"
  12. #define LED_GPIO 19
  13. #define adv_len 30
  14. SYS_TIMER_TYPE gAdv_Timer;
  15. void UartxInit(USART_TypeDef UARTx);
  16. #define IO_TXA 19
  17. #define IO_RXA 23
  18. #define IO_TXB 6
  19. #define IO_RXB 14
  20. #define UARTBUFSIZE 256
  21. void UartxInit(USART_TypeDef UARTx)
  22. {
  23. USART_InitTypeDef USART_InitStruct ;
  24. USART_InitStruct.USART_BaudRate = UARTE_BAUDRATE_BAUDRATE_Baud921600;
  25. USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  26. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  27. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  28. USART_InitStruct.USART_Mode =USART_Mode_duplex;
  29. USART_InitStruct.USART_Parity = USART_Parity_Even ;
  30. USART_InitStruct.USART_RXLen = UARTBUFSIZE;
  31. USART_InitStruct.USART_TXLen = UARTBUFSIZE;
  32. if(UARTA == UARTx){
  33. GPIO_SetGpioMultFunction(IO_TXA,GPCFG_UART_TXD);
  34. GPIO_SetGpioMultFunction(IO_RXA,GPCFG_UART_RXD);
  35. }else if (UARTB == UARTx){
  36. GPIO_SetGpioMultFunction(IO_TXB,GPCFG_UARTB_TXD);
  37. GPIO_SetGpioMultFunction(IO_RXB,GPCFG_UARTB_RXD);
  38. }
  39. USART_Init(UARTx,&USART_InitStruct);
  40. }
  41. void LED_Run(uint8_t GPIO)
  42. {
  43. static uint32_t times = 0;
  44. times++;
  45. if(times>0x5000)
  46. {
  47. times = 0;
  48. GPIO_CONFIG(GPIO) = (GPIO_CONFIG(GPIO)==GPCFG_OUTPUT_HIGH)? GPCFG_OUTPUT_LOW: GPCFG_OUTPUT_HIGH;
  49. }
  50. }
  51. void Bt_StartAdv(int time_10ms,uint16_t gap){
  52. Bt_SetLeAdvInterval(gap);
  53. Lpm_Set_Interval(gap);
  54. // SYS_SetTimer(&gAdv_Timer,time_10ms,TIMER_SINGLE|TIMER_TYPE_BIT,Bt_stop_adv);
  55. Bt_SndCmdLeStartAdv();
  56. }
  57. void Bt_EvtCallBack(uint8_t len,uint8_t *dataPtr)
  58. {
  59. BLE_Conn_param Ble_Conn_Param;
  60. switch(*dataPtr)
  61. {
  62. case IPC_EVT_LE_DISCONNECTED: //断连
  63. #ifdef DEBUG_DRV_BT
  64. MyPrintf("\r\n***************IPC_EVT_LE_DISCONNECTED***************\r\n");
  65. #endif
  66. Bt_StartAdv(60,0x80);
  67. break;
  68. case IPC_EVT_LE_CONNECTED: //连接
  69. // HWRITE(mem_local_mtu_size,527);
  70. // Bt_SndCmdLeUpdateAttMTU();
  71. // Ble_Conn_Param.min_interval=0x0006;
  72. // Ble_Conn_Param.max_interval=0x0012;
  73. // Ble_Conn_Param.latency=0x0010;
  74. // Ble_Conn_Param.timeout=0x258;
  75. // Bt_SndCmdLeUpdateConn(&Ble_Conn_Param);
  76. IPC_TxControlCmd(IPC_CMD_UPDATE_CONN);
  77. SYS_ReleaseTimer(&gAdv_Timer);
  78. #ifdef DEBUG_DRV_BT
  79. MyPrintf("\r\n***************IPC_EVT_LE_CONNECTED***************\r\n");
  80. #endif
  81. break;
  82. case IPC_EVT_BB_CONNECTED:
  83. #ifdef DEBUG_DRV_BT
  84. MyPrintf("\r\n****************IPC_EVT_BB_CONNECTED*****************\r\n");
  85. #endif
  86. break;
  87. case IPC_EVT_SETUP_COMPLETE:
  88. #ifdef DEBUG_DRV_BT
  89. MyPrintf("\r\n****************IPC_EVT_SETUP_COMPLETE*****************\r\n");
  90. #endif
  91. break;
  92. case IPC_EVT_BT_PAIRING_SUCCESS:
  93. #ifdef DEBUG_DRV_BT
  94. MyPrintf("\r\n****************IPC_EVT_BT_PAIRING_SUCCESS*****************\r\n");
  95. #endif
  96. break;
  97. case IPC_EVT_LINKKEY_GENERATE:
  98. #ifdef DEBUG_DRV_BT
  99. MyPrintf("\r\n****************IPC_EVT_LINKKEY_GENERATE*****************\r\n");
  100. #endif
  101. break;
  102. case IPC_EVT_RESET: //上电
  103. #ifdef DEBUG_DRV_BT
  104. MyPrintf("\r\n****************IPC_EVT_RESET*****************\r\n");
  105. #endif
  106. // Bt_Reset();
  107. break;
  108. case IPC_EVT_WAKEUP: //唤醒
  109. #ifdef DEBUG_DRV_BT
  110. MyPrintf("\r\n****************IPC_EVT_WAKEUP*****************\r\n");
  111. #endif
  112. // SysTick_Config(SYSTEM_CLOCK/100); //each systick interrupt is 10ms
  113. OS_EXIT_CRITICAL();
  114. break;
  115. case IPC_EVT_LE_TK_GENERATE:
  116. #ifdef DEBUG_DRV_BT
  117. MyPrintf("\r\n****************IPC_EVT_LE_TK_GENERATE*****************\r\n");
  118. #endif
  119. break;
  120. case IPC_EVT_LE_DISCONNECTED_ABNORMAL:
  121. #ifdef DEBUG_DRV_BT
  122. MyPrintf("\r\n******************IPC_EVT_LE_DISCONNECTED_ABNORMAL******************\r\n");
  123. #endif
  124. break;
  125. default:
  126. break;
  127. }
  128. return;
  129. }
  130. #define ATT_WRITE_REQUEST 0x12
  131. #define ATT_PREPARE_WRITE_REQUEST 0x16
  132. #define ATT_WRITE_COMMAND 0X52
  133. void Bt_SndWriteResponse()
  134. {
  135. IPC_TxControlCmd(IPC_CMD_SEND_WRITE_RESPONSE);
  136. }
  137. void Bt_BleCallBack(uint8_t len,uint8_t *dataPtr)
  138. {
  139. uint16_t handle;
  140. uint8_t opcode;
  141. opcode = dataPtr[0];
  142. handle = dataPtr[1] +(dataPtr[2]<<8);
  143. // MyPrintf("opcode=%x\r\n",opcode);
  144. // MyPrintf("handle=%x\r\n",handle);
  145. switch(opcode)
  146. {
  147. case ATT_WRITE_REQUEST:
  148. switch(handle)
  149. {
  150. case 0x0004:
  151. break;
  152. case 0x000e:
  153. break;
  154. case 0x0011:
  155. break;
  156. }
  157. Bt_SndWriteResponse();
  158. break;
  159. case ATT_PREPARE_WRITE_REQUEST:
  160. switch(handle)
  161. {
  162. case 0x001c:
  163. break;
  164. }
  165. break;
  166. case ATT_WRITE_COMMAND:
  167. switch(handle)
  168. {
  169. case 0x001c:
  170. USART_SendDataFromBuff(UARTB,dataPtr+3,len-3);
  171. // Bt_SndBleData(0x001e,dataPtr+2,len-2);
  172. break;
  173. }
  174. break;
  175. }
  176. }
  177. tIPCHandleCb gTIPCHandleCb[IPC_TYPE_NUM]=
  178. {
  179. 0,
  180. IpcDefaultCallBack,//cmd
  181. Bt_EvtCallBack,//evt //evt回调函数,函数在drv_bt.c实现
  182. IpcDefaultCallBack,//hid
  183. IpcDefaultCallBack,//spp
  184. Bt_BleCallBack,//ble //ble data回调函数,函数在drv_bt.c实现
  185. IpcDefaultCallBack,//24g
  186. IpcDefaultCallBack,//mesh
  187. IpcDefaultCallBack,
  188. IpcDefaultCallBack,//mesh
  189. IpcDefaultCallBack,//a2dp
  190. IpcDefaultCallBack,//hfp
  191. IpcDefaultCallBack
  192. };
  193. void Bt_Reset()
  194. {
  195. uint8_t advD[22] = {0x02,0x01,0x06,0x05,0x03,0x12,0x18,0x0f,0x18,0x03,0x19,0x80,0x01,0x08,0x09,0x31,0x31,0x58,0x58,0x42,0x4c,0x45};
  196. uint8_t addr[6] = {0xac,0x02,0x88,0x11,0x43,0Xac};
  197. uint8_t name[] = {"1121BLE_Test"};
  198. Bt_Renew_Le_AdvData(advD,22);
  199. Bt_SetLeMac(addr); //设置蓝牙地址
  200. Bt_Re_LeAdvName(name,12);
  201. Bt_Re_LeDeivcename(name,12);
  202. Lpm_Set_Conn_Interval(0x1a0);
  203. // HWRITEW(mem_local_mtu_size,0x20f);
  204. Bt_SetLocalMtuSize(200);
  205. Bt_StartAdv(100,80);
  206. }
  207. int main(void)
  208. {
  209. if(HREAD(mem_wake_flag)==POWERON_WAKE)
  210. {
  211. //初始化蓝牙回调函数
  212. IPC_init(&gTIPCHandleCb);
  213. //初始化定时器
  214. SYS_TimerInit();
  215. UartxInit(UARTB);
  216. printport_init();
  217. // BLE profile init.
  218. Att_profile_Config();
  219. //clos lpm
  220. Lpm_exit();
  221. //init
  222. Bt_Reset();
  223. GPIO_SetOut(GPIO_19,OUT_HIGH);
  224. }
  225. else
  226. {
  227. SysTick_Config(SYSTEM_CLOCK/100);
  228. }
  229. SYS_ClkTicks();
  230. while(1)
  231. {
  232. switch (HREAD(IPC_MCU_STATE))
  233. {
  234. case IPC_MCU_STATE_RUNNING:
  235. IPC_HandleRxPacket(); //m0和bt交互数据类型判断,是evt还是ble data?并执行对应回调函数
  236. SYS_timerPolling();
  237. LED_Run(LED_GPIO);
  238. Lpm_LockLpm(M0_LPM_FLAG);
  239. break;
  240. case IPC_MCU_STATE_LMP: //lpm
  241. if (IPC_IsTxBuffEmpty())
  242. {
  243. OS_ENTER_CRITICAL();
  244. Bt_ActionBeforeLpm();
  245. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP);
  246. }
  247. else{
  248. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_RUNNING);
  249. }
  250. break;
  251. case IPC_MCU_STATE_HIBERNATE: // HIBERNATE
  252. OS_ENTER_CRITICAL();
  253. Bt_ActionBeforeHibernate();
  254. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP);
  255. break;
  256. case IPC_MCU_STATE_STOP:
  257. break;
  258. }
  259. }
  260. }