main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "yc11xx_pwm.h"
  13. #include "user_driver.h"
  14. #include "Drv_adc.h"
  15. #include "yc11xx_gpio.h"
  16. #include "yc11xx_qspi.h"
  17. #include "LH_TaskManager.h"
  18. #include "u_main.h"
  19. #include "yc11xx_wdt.h"
  20. #include "user_driver.h"
  21. #include "u_main.h"
  22. #include "u_ble.h"
  23. #define LED_GPIO 28
  24. #define adv_len 30
  25. BleRecv_t BleRecDataMsg;
  26. static uint8_t UrataReDataIndex = 0x00;
  27. void Bt_NecCallBack(uint8_t len,uint8_t *dataPtr);
  28. #define IO_TXA 6 //暂时没有用到 定义一个没有硬件的io
  29. #define IO_RXA 20 //暂时没有用到 定义一个没有硬件的io
  30. #define IO_TXB 31
  31. #define IO_RXB 14
  32. #define UARTBUFSIZE 256
  33. volatile uint32_t UserCnt=0;
  34. void Bt_StartAdv(int time_10ms,uint16_t gap){
  35. Bt_SetLeAdvInterval(gap);//设置广播间隔
  36. Lpm_Set_Interval(gap);
  37. Bt_SndCmdLeStartAdv();
  38. }
  39. void Bt_EvtCallBack(uint8_t len,uint8_t *dataPtr)
  40. {
  41. BLE_Conn_param Ble_Conn_Param;
  42. switch(*dataPtr)
  43. {
  44. case IPC_EVT_LE_DISCONNECTED: //断连
  45. u_gapc_disconnect_cd(IPC_EVT_LE_DISCONNECTED);
  46. #ifdef DEBUG_DRV_BT
  47. MyPrintf("\r\n***************IPC_EVT_LE_DISCONNECTED***************\r\n");
  48. #endif
  49. Bt_StartAdv(60,0x80);
  50. break;
  51. case IPC_EVT_LE_CONNECTED: //连接
  52. IPC_TxControlCmd(IPC_CMD_UPDATE_CONN);
  53. //SYS_ReleaseTimer(&gAdv_Timer);
  54. Bt_SndCmdLeUpdateAttMTU();
  55. u_gapc_connection_cd();
  56. #ifdef DEBUG_DRV_BT
  57. MyPrintf("\r\n***************IPC_EVT_LE_CONNECTED***************\r\n");
  58. #endif
  59. break;
  60. case IPC_EVT_BB_CONNECTED:
  61. #ifdef DEBUG_DRV_BT
  62. MyPrintf("\r\n****************IPC_EVT_BB_CONNECTED*****************\r\n");
  63. #endif
  64. break;
  65. case IPC_EVT_SETUP_COMPLETE:
  66. #ifdef DEBUG_DRV_BT
  67. MyPrintf("\r\n****************IPC_EVT_SETUP_COMPLETE*****************\r\n");
  68. #endif
  69. break;
  70. case IPC_EVT_BT_PAIRING_SUCCESS:
  71. #ifdef DEBUG_DRV_BT
  72. MyPrintf("\r\n****************IPC_EVT_BT_PAIRING_SUCCESS*****************\r\n");
  73. #endif
  74. break;
  75. case IPC_EVT_LINKKEY_GENERATE:
  76. #ifdef DEBUG_DRV_BT
  77. MyPrintf("\r\n****************IPC_EVT_LINKKEY_GENERATE*****************\r\n");
  78. #endif
  79. break;
  80. case IPC_EVT_RESET: //上电
  81. #ifdef DEBUG_DRV_BT
  82. MyPrintf("\r\n****************IPC_EVT_RESET*****************\r\n");
  83. #endif
  84. break;
  85. case IPC_EVT_WAKEUP: //唤醒
  86. #ifdef DEBUG_DRV_BT
  87. MyPrintf("\r\n****************IPC_EVT_WAKEUP*****************\r\n");
  88. #endif
  89. OS_EXIT_CRITICAL();
  90. break;
  91. case IPC_EVT_LE_TK_GENERATE:
  92. #ifdef DEBUG_DRV_BT
  93. MyPrintf("\r\n****************IPC_EVT_LE_TK_GENERATE*****************\r\n");
  94. #endif
  95. break;
  96. case IPC_EVT_LE_DISCONNECTED_ABNORMAL:
  97. #ifdef DEBUG_DRV_BT
  98. MyPrintf("\r\n******************IPC_EVT_LE_DISCONNECTED_ABNORMAL******************\r\n");
  99. #endif
  100. break;
  101. default:
  102. break;
  103. }
  104. return;
  105. }
  106. #define ATT_WRITE_REQUEST 0x12
  107. #define ATT_PREPARE_WRITE_REQUEST 0x16
  108. #define ATT_WRITE_COMMAND 0X52
  109. void Bt_SndWriteResponse()
  110. {
  111. IPC_TxControlCmd(IPC_CMD_SEND_WRITE_RESPONSE);
  112. }
  113. void Bt_BleCallBack(uint8_t len,uint8_t *dataPtr)
  114. {
  115. uint16_t handle;
  116. uint8_t opcode;
  117. opcode = dataPtr[0];
  118. handle = dataPtr[1] +(dataPtr[2]<<8);
  119. switch(opcode)
  120. {
  121. case ATT_WRITE_REQUEST:
  122. switch(handle)
  123. {
  124. case 0x0004:
  125. break;
  126. case 0x000e:
  127. break;
  128. case 0x0011:
  129. break;
  130. }
  131. Bt_SndWriteResponse();
  132. break;
  133. case ATT_PREPARE_WRITE_REQUEST:
  134. switch(handle)
  135. {
  136. case 0x001c:
  137. break;
  138. }
  139. break;
  140. case ATT_WRITE_COMMAND:
  141. switch(handle)
  142. {
  143. case 0x001c:
  144. {
  145. /*app通过ble发的数据串口打印出来*/
  146. if((len-3)<=20)
  147. {
  148. memcpy(BleRecDataMsg.recv_data,dataPtr+3,len-3);
  149. BleRecDataMsg.recv_len=len-3;
  150. //Bt_SndBleData(BLE_SEND_HANDLE,BleReDataBuf,BleCntData);
  151. }else
  152. {
  153. memcpy(BleRecDataMsg.recv_data,dataPtr+3,20);
  154. BleRecDataMsg.recv_len=20;
  155. }
  156. // UserSet_OutPWM(0,PWM_CHANNEL_0,20000,BleRecDataMsg.recv_data[0]);// G
  157. //light_set_color(BleRecDataMsg.recv_data[0]);
  158. u_ble_data_recv(BleRecDataMsg.recv_data,BleRecDataMsg.recv_len);
  159. }
  160. break;
  161. }
  162. break;
  163. }
  164. }
  165. tIPCHandleCb gTIPCHandleCb[IPC_TYPE_NUM]=
  166. {
  167. 0,
  168. IpcDefaultCallBack,//cmd
  169. Bt_EvtCallBack,//evt //evt回调函数,函数在drv_bt.c实现
  170. IpcDefaultCallBack,//hid
  171. IpcDefaultCallBack,//spp
  172. Bt_BleCallBack,//ble //ble data回调函数,函数在drv_bt.c实现
  173. IpcDefaultCallBack,//24g
  174. IpcDefaultCallBack,//mesh
  175. IpcDefaultCallBack,
  176. IpcDefaultCallBack,//mesh
  177. IpcDefaultCallBack,//a2dp
  178. IpcDefaultCallBack,//hfp
  179. #if (NEC_ENABLE==1)
  180. Bt_NecCallBack
  181. #else
  182. IpcDefaultCallBack
  183. #endif
  184. };
  185. void Bt_Reset()
  186. {
  187. 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};
  188. uint8_t addr[6] = {0xAC,0x02,0x88,0x11,0x43,0Xac};
  189. uint8_t name[] = {"Light Life"};
  190. Bt_Renew_Le_AdvData(advD,22);
  191. Bt_SetLeMac(addr); //设置蓝牙地址
  192. Bt_Re_LeAdvName(name,sizeof(name));
  193. Bt_Re_LeDeivcename(name,sizeof(name));
  194. Lpm_Set_Conn_Interval(0x1a0);
  195. Bt_SetLocalMtuSize(200);
  196. Bt_StartAdv(100,80);
  197. }
  198. int Test_val=0xffffffff;
  199. int user_task1(void)
  200. {
  201. Test_val++;
  202. MyPrintf("Test_value:%d\r\n",Test_val);
  203. return 2000;
  204. }
  205. int user_task2(void)
  206. {
  207. return 50;
  208. }
  209. int main(void)
  210. {
  211. if(HREAD(mem_wake_flag)==POWERON_WAKE)
  212. {
  213. //初始化蓝牙回调函数
  214. IPC_init(&gTIPCHandleCb);
  215. //初始化定时器
  216. SYS_TimerInit();
  217. printport_init();//初始化UARTB
  218. // BLE profile init.
  219. Att_profile_Config();
  220. //clos lpm
  221. Lpm_exit();
  222. //init
  223. Bt_Reset();
  224. }
  225. else
  226. {
  227. SysTick_Config(SYSTEM_CLOCK/100);
  228. }
  229. //SYS_ClkTicks();
  230. HWRITE(mem_le_adv_led,1);//解除gpio0的系统占用 解决官方代码bug
  231. GPIO_SetOut(GPIO_0,OUT_LOW);//同时将io 输出低电平 上电熄灭灯
  232. GPIO_SetOut(GPIO_25,OUT_LOW);
  233. GPIO_SetOut(GPIO_24,OUT_LOW);
  234. MyPrintf("\r\n****************RESET_BOOT*****************\r\n");
  235. Audio_sampling_init();
  236. u_init();
  237. TaskManager_Init();
  238. UserTask_Create(1,user_task1,1);
  239. UserTask_Create(2,user_task2,1);
  240. //WDT_Enable();//开启看门狗
  241. //light_set_color(0xff00);
  242. while(1)
  243. {
  244. //WDT_Kick();//喂狗
  245. ag_timer_sched();//定时轮询
  246. TaskManager_Run();//初始化任务调度器
  247. //Audio_to_uart_start();
  248. switch (HREAD(IPC_MCU_STATE))
  249. {
  250. case IPC_MCU_STATE_RUNNING:
  251. {
  252. IPC_HandleRxPacket(); //m0和bt交互数据类型判断,是evt还是ble data?并执行对应回调函数
  253. // SYS_timerPolling();//定时器任务轮询
  254. Lpm_LockLpm(M0_LPM_FLAG);
  255. }
  256. break;
  257. case IPC_MCU_STATE_LMP: //lpm
  258. {
  259. if (IPC_IsTxBuffEmpty())
  260. {
  261. OS_ENTER_CRITICAL();
  262. Bt_ActionBeforeLpm();
  263. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP);
  264. }
  265. else{
  266. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_RUNNING);
  267. }
  268. }
  269. break;
  270. case IPC_MCU_STATE_HIBERNATE: // HIBERNATE
  271. {
  272. OS_ENTER_CRITICAL();
  273. Bt_ActionBeforeHibernate();
  274. HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP);
  275. }
  276. break;
  277. case IPC_MCU_STATE_STOP:
  278. break;
  279. }
  280. }
  281. }