#include #include "yc11xx.h" #include "yc11xx_uart.h" #include "yc11xx_gpio.h" #include "yc_timer.h" #include "ipc.h" #include "yc_lpm.h" #include "system.h" #include "yc11xx_bt_interface.h" #include "att.h" #include "core_cm0.h" #include "yc11xx_pwm.h" #include "user_driver.h" #include "Drv_adc.h" #include "yc11xx_gpio.h" #include "yc11xx_qspi.h" #include "LH_TaskManager.h" #include "u_main.h" #include "yc11xx_wdt.h" #include "user_driver.h" #include "u_main.h" #include "u_ble.h" #define LED_GPIO 28 #define adv_len 30 BleRecv_t BleRecDataMsg; static uint8_t UrataReDataIndex = 0x00; void Bt_NecCallBack(uint8_t len,uint8_t *dataPtr); #define IO_TXA 6 //暂时没有用到 定义一个没有硬件的io #define IO_RXA 20 //暂时没有用到 定义一个没有硬件的io #define IO_TXB 31 #define IO_RXB 14 #define UARTBUFSIZE 256 volatile uint32_t UserCnt=0; void Bt_StartAdv(int time_10ms,uint16_t gap){ Bt_SetLeAdvInterval(gap);//设置广播间隔 Lpm_Set_Interval(gap); Bt_SndCmdLeStartAdv(); } void Bt_EvtCallBack(uint8_t len,uint8_t *dataPtr) { BLE_Conn_param Ble_Conn_Param; switch(*dataPtr) { case IPC_EVT_LE_DISCONNECTED: //断连 u_gapc_disconnect_cd(IPC_EVT_LE_DISCONNECTED); #ifdef DEBUG_DRV_BT MyPrintf("\r\n***************IPC_EVT_LE_DISCONNECTED***************\r\n"); #endif Bt_StartAdv(60,0x80); break; case IPC_EVT_LE_CONNECTED: //连接 IPC_TxControlCmd(IPC_CMD_UPDATE_CONN); //SYS_ReleaseTimer(&gAdv_Timer); Bt_SndCmdLeUpdateAttMTU(); u_gapc_connection_cd(); #ifdef DEBUG_DRV_BT MyPrintf("\r\n***************IPC_EVT_LE_CONNECTED***************\r\n"); #endif break; case IPC_EVT_BB_CONNECTED: #ifdef DEBUG_DRV_BT MyPrintf("\r\n****************IPC_EVT_BB_CONNECTED*****************\r\n"); #endif break; case IPC_EVT_SETUP_COMPLETE: #ifdef DEBUG_DRV_BT MyPrintf("\r\n****************IPC_EVT_SETUP_COMPLETE*****************\r\n"); #endif break; case IPC_EVT_BT_PAIRING_SUCCESS: #ifdef DEBUG_DRV_BT MyPrintf("\r\n****************IPC_EVT_BT_PAIRING_SUCCESS*****************\r\n"); #endif break; case IPC_EVT_LINKKEY_GENERATE: #ifdef DEBUG_DRV_BT MyPrintf("\r\n****************IPC_EVT_LINKKEY_GENERATE*****************\r\n"); #endif break; case IPC_EVT_RESET: //上电 #ifdef DEBUG_DRV_BT MyPrintf("\r\n****************IPC_EVT_RESET*****************\r\n"); #endif break; case IPC_EVT_WAKEUP: //唤醒 #ifdef DEBUG_DRV_BT MyPrintf("\r\n****************IPC_EVT_WAKEUP*****************\r\n"); #endif OS_EXIT_CRITICAL(); break; case IPC_EVT_LE_TK_GENERATE: #ifdef DEBUG_DRV_BT MyPrintf("\r\n****************IPC_EVT_LE_TK_GENERATE*****************\r\n"); #endif break; case IPC_EVT_LE_DISCONNECTED_ABNORMAL: #ifdef DEBUG_DRV_BT MyPrintf("\r\n******************IPC_EVT_LE_DISCONNECTED_ABNORMAL******************\r\n"); #endif break; default: break; } return; } #define ATT_WRITE_REQUEST 0x12 #define ATT_PREPARE_WRITE_REQUEST 0x16 #define ATT_WRITE_COMMAND 0X52 void Bt_SndWriteResponse() { IPC_TxControlCmd(IPC_CMD_SEND_WRITE_RESPONSE); } void Bt_BleCallBack(uint8_t len,uint8_t *dataPtr) { uint16_t handle; uint8_t opcode; opcode = dataPtr[0]; handle = dataPtr[1] +(dataPtr[2]<<8); switch(opcode) { case ATT_WRITE_REQUEST: switch(handle) { case 0x0004: break; case 0x000e: break; case 0x0011: break; } Bt_SndWriteResponse(); break; case ATT_PREPARE_WRITE_REQUEST: switch(handle) { case 0x001c: break; } break; case ATT_WRITE_COMMAND: switch(handle) { case 0x001c: { /*app通过ble发的数据串口打印出来*/ if((len-3)<=20) { memcpy(BleRecDataMsg.recv_data,dataPtr+3,len-3); BleRecDataMsg.recv_len=len-3; //Bt_SndBleData(BLE_SEND_HANDLE,BleReDataBuf,BleCntData); }else { memcpy(BleRecDataMsg.recv_data,dataPtr+3,20); BleRecDataMsg.recv_len=20; } // UserSet_OutPWM(0,PWM_CHANNEL_0,20000,BleRecDataMsg.recv_data[0]);// G //light_set_color(BleRecDataMsg.recv_data[0]); u_ble_data_recv(BleRecDataMsg.recv_data,BleRecDataMsg.recv_len); } break; } break; } } tIPCHandleCb gTIPCHandleCb[IPC_TYPE_NUM]= { 0, IpcDefaultCallBack,//cmd Bt_EvtCallBack,//evt //evt回调函数,函数在drv_bt.c实现 IpcDefaultCallBack,//hid IpcDefaultCallBack,//spp Bt_BleCallBack,//ble //ble data回调函数,函数在drv_bt.c实现 IpcDefaultCallBack,//24g IpcDefaultCallBack,//mesh IpcDefaultCallBack, IpcDefaultCallBack,//mesh IpcDefaultCallBack,//a2dp IpcDefaultCallBack,//hfp #if (NEC_ENABLE==1) Bt_NecCallBack #else IpcDefaultCallBack #endif }; void Bt_Reset() { 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}; uint8_t addr[6] = {0xAC,0x02,0x88,0x11,0x43,0Xac}; uint8_t name[] = {"Light Life"}; Bt_Renew_Le_AdvData(advD,22); Bt_SetLeMac(addr); //设置蓝牙地址 Bt_Re_LeAdvName(name,sizeof(name)); Bt_Re_LeDeivcename(name,sizeof(name)); Lpm_Set_Conn_Interval(0x1a0); Bt_SetLocalMtuSize(200); Bt_StartAdv(100,80); } int Test_val=0xffffffff; int user_task1(void) { Test_val++; MyPrintf("Test_value:%d\r\n",Test_val); return 2000; } int user_task2(void) { return 50; } int main(void) { if(HREAD(mem_wake_flag)==POWERON_WAKE) { //初始化蓝牙回调函数 IPC_init(&gTIPCHandleCb); //初始化定时器 SYS_TimerInit(); printport_init();//初始化UARTB // BLE profile init. Att_profile_Config(); //clos lpm Lpm_exit(); //init Bt_Reset(); } else { SysTick_Config(SYSTEM_CLOCK/100); } //SYS_ClkTicks(); HWRITE(mem_le_adv_led,1);//解除gpio0的系统占用 解决官方代码bug GPIO_SetOut(GPIO_0,OUT_LOW);//同时将io 输出低电平 上电熄灭灯 GPIO_SetOut(GPIO_25,OUT_LOW); GPIO_SetOut(GPIO_24,OUT_LOW); MyPrintf("\r\n****************RESET_BOOT*****************\r\n"); Audio_sampling_init(); u_init(); TaskManager_Init(); UserTask_Create(1,user_task1,1); UserTask_Create(2,user_task2,1); //WDT_Enable();//开启看门狗 //light_set_color(0xff00); while(1) { //WDT_Kick();//喂狗 ag_timer_sched();//定时轮询 TaskManager_Run();//初始化任务调度器 //Audio_to_uart_start(); switch (HREAD(IPC_MCU_STATE)) { case IPC_MCU_STATE_RUNNING: { IPC_HandleRxPacket(); //m0和bt交互数据类型判断,是evt还是ble data?并执行对应回调函数 // SYS_timerPolling();//定时器任务轮询 Lpm_LockLpm(M0_LPM_FLAG); } break; case IPC_MCU_STATE_LMP: //lpm { if (IPC_IsTxBuffEmpty()) { OS_ENTER_CRITICAL(); Bt_ActionBeforeLpm(); HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP); } else{ HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_RUNNING); } } break; case IPC_MCU_STATE_HIBERNATE: // HIBERNATE { OS_ENTER_CRITICAL(); Bt_ActionBeforeHibernate(); HWRITE(IPC_MCU_STATE,IPC_MCU_STATE_STOP); } break; case IPC_MCU_STATE_STOP: break; } } }