[TOC]
main分析
main函数源码
int main(void)
{
/* Device Initialization */
InitDevice();
PWM_Init();
/* Initialization of PWM value to 0 */
Appli_PWM_init();
LPS25HB_Init(& InitStructure);
Get_CrashHandlerInfo();
/* Check for valid Board Address */
if (!Appli_CheckBdMacAddr())
{
#if !defined(DISABLE_TRACES)
printf("Bad BD_MAC ADDR!\r\n");
#endif
/* LED Blinks if BDAddr is not appropriate */
while (1)
{
Appli_LedBlink();
}
}
/* Set BLE configuration function callbacks */
BluenrgMesh_BleHardwareInitCallBack(&user_ble_cb);
/* Initializes BlueNRG-Mesh Library */
if (MOBLE_FAILED (BluenrgMesh_Init(&BLEMeshlib_Init_params) ))
{
#if !defined(DISABLE_TRACES)
printf("Could not initialize BlueNRG-Mesh library!\r\n");
#endif
/* LED continuously blinks if library fails to initialize */
while (1)
{
Appli_LedBlink();
}
}
/* Checks if the node is already provisioned or not */
if (BluenrgMesh_IsUnprovisioned() == MOBLE_TRUE)
{
BluenrgMesh_UnprovisionedNodeInfo(&UnprovNodeInfoParams);
BluenrgMesh_InitUnprovisionedNode(); /* Initalizes Unprovisioned node */
}
else
{
BluenrgMesh_InitProvisionedNode(); /* Initalizes Provisioned node */
#if !defined(DISABLE_TRACES)
printf("Node is already provisioned \r\n");
#endif
}
/* Models intialization */
BluenrgMesh_ModelsInit();
/* Set attention timer callback */
BluenrgMesh_SetAttentionTimerCallback(Appli_BleAttentionTimerCb);
/* Prints the MAC Address of the board */
#if !defined(DISABLE_TRACES)
printf("BlueNRG-Mesh Lighting Demo v%s\n\r", BLUENRG_MESH_APPLICATION_VERSION);
printf("BlueNRG-Mesh Library v%s\n\r", BluenrgMesh_GetLibraryVersion());
printf("BD_MAC Address = [%02x]:[%02x]:[%02x]:[%02x]:[%02x]:[%02x] \n\r",
bdaddr[5],bdaddr[4],bdaddr[3],bdaddr[2],bdaddr[1],bdaddr[0] );
#endif
/* Check to manually unprovision the board */
Appli_CheckForUnprovision();
/* Set Yellow LED to full intensity */
#if LOW_POWER_FEATURE
Modify_PWM(PWM2, PWM_TIME_PERIOD);
#endif
/* Main infinite loop */
while(1)
{
BluenrgMesh_Process();
BluenrgMesh_ModelsProcess(); /* Models Processing */
Appli_Process();
}
}
设备初始化 InitDevice()
- 配置中断优先级、使能中断 、配置时钟,关闭外设时钟,开启中断响应
SystemInit() -->DeviceConfiguration(TRUE, TRUE);//配置时钟,此处需要根据自己硬件设计来选择晶振 -->CKGEN_SOC->CLOCK_EN = 0xE0066;//关闭外围模块时钟 -->__enable_irq();开启中断响应
- 设备识别
SdkEvalIdentification()
- 配置开启debug串口(IO配置,串口初始化)
SdkEvalComIOConfig(SdkEvalComIOProcessInputData)
- 初始化滴答定时器
Clock_Init(); //此处与STM32类似有一个独立的滴答定时器,可以作为系统任务切换等使用非常便利
- 初始化外设
SdkEvalLedInit(LED1);//初始化LED InitButton();//初始化按键 SystemSleepCmd(ENABLE);//系统休眠开启
pwm初始化
- 类似电源开关频率,通过pwm控制远远通断来改变light的明暗变化
PWM_Init() -->GPIO Configuration -->Configure GPIOs pin used to output PWM signal -->MFT Configuration //多功能定时器初始化配置开启
Appli_PWM_init(); 初始化pwm状态
传感器初始化
LPS25HB_Init(& InitStructure);
检查设备地址是否合法
/* Check for valid Board Address */
if (!Appli_CheckBdMacAddr())
{
#if !defined(DISABLE_TRACES)
printf("Bad BD_MAC ADDR!\r\n");
#endif
/* LED Blinks if BDAddr is not appropriate */
while (1)
{
Appli_LedBlink();
}
}
蓝牙协议栈初始化
BluenrgMesh_BleHardwareInitCallBack(&user_ble_cb);
/***&user_ble_cb***/
--> Appli_BleStackInitCb, //蓝牙协议栈初始化
--> Appli_BleSetTxPowerCb, //蓝牙发射功率
--> Appli_BleGattConnectionCompleteCb, //非初始化,仅判断状态开启信号指示
--> Appli_BleGattDisconnectionCompleteCb, //非初始化,仅判断状态开启信号指示
--> Appli_BleUnprovisionedIdentifyCb,
--> Appli_BleSetUUIDCb, //设置UUID,其中部分字节可以由用户定义,实现不同的功能
--> Appli_BleSetProductInfoCB, //产品ID,公司ID,等自定义参数设置
--> Appli_BleSetNumberOfElementsCb, //设置节点元素个数,default:3
--> Appli_BleDisableFilterCb //使能,开启蓝牙监听捕获所有数据功能
蓝牙mesh参数初始化
BluenrgMesh_Init()
bdaddr, //地址
&TrParams, //定义传输数据量10-65535
&FnParams, //与周围节点建立通讯的数量1-10
&LpnParams, //低功耗节点的参数设置
LPN_RSSI_FACTOR_LEVEL, //用于判定提供最佳链路,
LPN_RECIVE_WINDOW_FACTOR_LEVEL, //用于确保由良好的接收路径
LPN_MINIMUM_QUEUE_SIZE_LOG, //最小数据包队列大小
LPN_RECEIVE_DELAY, //接收延时,默认150U
LPN_POLL_TIMEOUT, //发送超时,之前确认的良好通讯路径将失效
LPN_FRIEND_REQUEST_FREQUENCY, //请求间隔
LPN_FRIEND_POLL_FREQUENCY, //轮询周期,需要小于超时
LPN_RECEIVE_WINDOW_SIZE, //最大接收数据量
LPN_SUBSCR_LIST_SIZE, //最小成员列表1-5
LPN_MINIMUM_RSSI, //低功耗节点信号强度最小值-100 应尽量保证在-60
LPN_NO_OF_RETRIES, 重新尝试建立链接的次数 default:10
MESH_FEATURES,
&DynBufferParam 数据传冲空间
初始化参数设置成功的节点
BluenrgMesh_InitUnprovisionedNode();
mesh mode初始化
-->BluenrgMesh_ModelsInit()
-->BluenrgMesh_SetVendorCbMap(&vendor_cb);
-->BluenrgMesh_SetModelGenericCbMap(&Model_Generic_cb); //设置消息,请求,命令服务
-->BluenrgMesh_AddGenericModels(); //添加generic服务
-->BluenrgMesh_SetModelLightCbMap(&Model_Light_cb);//灯的命令,消息,请求回调服务设置
-->BluenrgMesh_AddLightingModels();//添加灯的服务
-->BluenrgMesh_SetModelSensorCbMap(&Model_Sensor_cb); //传感器的命令请求服务设置
-->BluenrgMesh_AddSensorModels(); //添加传感器的服务
while循环
BluenrgMesh_Process(); //mesh组网进程
BluenrgMesh_ModelsProcess();
-->Generic_Process(); //通用状态信息传输
-->Lighting_Process(); //lighting状态信息传输
-->Vendor_Process(); //周期性发送数据
-->Sensor_Process();
Appli_Process();