尝试自己建议一个简单的蓝牙代码模板.
但是现在连广播都没法开启.查了好多遍,也不知道问题出在哪里.我认为前面的代码应该都不会出问题.可能出在消息处理上.麻烦帮看看到底什么原因没有出现广播.代码基于ch582
main.c的主要代码
int?main(void) { #if(defined(DCDC_ENABLE))?&&?(DCDC_ENABLE?==?TRUE) ???PWR_DCDCCfg(ENABLE); #endif ???SetSysClock(CLK_SOURCE_PLL_60MHz); #if(defined(HAL_SLEEP))?&&?(HAL_SLEEP?==?TRUE) ????GPIOA_ModeCfg(GPIO_Pin_All,?GPIO_ModeIN_PU); ????GPIOB_ModeCfg(GPIO_Pin_All,?GPIO_ModeIN_PU); #endif #ifdef?DEBUG ????GPIOA_SetBits(bTXD1); ????GPIOA_ModeCfg(bTXD1,?GPIO_ModeOut_PP_5mA); ????UART1_DefInit(); #endif ????PRINT("%s\n",?VER_LIB); ????CH58X_BLEInit(); ????HAL_Init(); ????GAPRole_PeripheralInit(); ????Device_Init(); ????Main_Circulation(); }
初始化的代码
void Device_Init()
{
?? Device_TaskID = TMOS_ProcessEventRegister(Device_ProcessEvent);
?? uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
?? uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
?? // Setup the GAP Peripheral Role Profile
?? {
?????? uint8_t initial_advertising_enable = TRUE;
????? // Set the GAP Role Parameters
????? GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
????? GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
????? GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
????? GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval);
????? GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval);
??? }
?? // Set the GAP Characteristics
??? GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName);
?? {
?????? uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;
????? // Set advertising interval
?????? GAP_SetParamValue(TGAP_DISC_ADV_INT_MIN, advInt);
?????? GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, advInt);
????? // Enable scan req notify
?????? GAP_SetParamValue(TGAP_ADV_SCAN_REQ_NOTIFY, ENABLE);
?? }
??? // Setup the GAP Bond Manager
??? {
??????? uint32_t passkey = 0; // passkey "000000"
??????? uint8_t? pairMode = DEFAULT_PAIRING_PARAMETER;
??????? uint8_t? mitm = FALSE;
??????? uint8_t? ioCap = GAPBOND_IO_CAP_NO_INPUT_NO_OUTPUT;
??????? uint8_t ?bonding = REQ_BONDING;
??????? uint8_t ?autoSync = USING_WHITE_LIST;
?????? GAPBondMgr_SetParameter(GAPBOND_PERI_DEFAULT_PASSCODE, sizeof(uint32_t), &passkey);
?????? GAPBondMgr_SetParameter(GAPBOND_PERI_PAIRING_MODE, sizeof(uint8_t), &pairMode);
?????? GAPBondMgr_SetParameter(GAPBOND_PERI_MITM_PROTECTION, sizeof(uint8_t), &mitm);
?????? GAPBondMgr_SetParameter(GAPBOND_PERI_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
?????? GAPBondMgr_SetParameter(GAPBOND_PERI_BONDING_ENABLED, sizeof(uint8_t), &bonding);
?????? GAPBondMgr_SetParameter(GAPBOND_AUTO_SYNC_WL, sizeof(uint8_t), &autoSync);
??? }
??? // Initialize GATT attributes
??? GGS_AddService(GATT_ALL_SERVICES); ? ?? // GAP
??? GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes
??? DevInfo_AddService();
??? // Setup a delayed profile startup
??? tmos_set_event(Device_TaskID, START_DEVICE_EVT);
}
uint16_t Device_ProcessEvent(uint8_t task_id, uint16_t events)
{
??? if(events & SYS_EVENT_MSG)
??? {
???????? uint8_t *pMsg;
???????? if((pMsg = tmos_msg_receive(Device_TaskID)) != NULL)
???????? {
???????????? //这里是消息处理代码,往下传递的回调的函数代码在这里
??????????? // Release the TMOS message
?????????????? tmos_msg_deallocate(pMsg);
???????? }
???????? return (events ^ SYS_EVENT_MSG);
??? }
??? if(events & START_DEVICE_EVT)
???? {
???????? // Start the Device
???????? GAPRole_PeripheralStartDevice(Device_TaskID, &DeviceBondCB, &DevicePeripheralCB);
???????? return (events ^ START_DEVICE_EVT);
??? }
?? //下面是其他消息的处理
}
实际调试,回调函数DevicePeripheralCB里面的代码是被执行的,不过全部复制8个0也没啥意义.
static??void?DeviceGapStateCB(gapRole_States_t?newState,?gapRoleEvent_t?*pEvent) { ????if(newState?==?GAPROLE_STARTED) ?????{ ????????//?Set?the?system?ID?from?the?bd?addr ????????uint8_t?systemId[DEVINFO_SYSTEM_ID_LEN]; ????????GAPRole_GetParameter(GAPROLE_BD_ADDR,?systemId); ???????//?shift?three?bytes?up ????????systemId[7]?=?systemId[5]; ????????systemId[6]?=?systemId[4]; ????????systemId[5]?=?systemId[3]; ???????//?set?middle?bytes?to?zero ????????systemId[4]?=?0; ????????systemId[3]?=?0; ????????DevInfo_SetParameter(DEVINFO_SYSTEM_ID,?DEVINFO_SYSTEM_ID_LEN,?systemId); ???????} ???????gapProfileState?=?newState; }