zigbee组网程序解读 下载本文

内容发布更新时间 : 2024/5/19 13:01:16星期一 下面是文章的全部内容请认真阅读。

Zigbee组网过程解读

第一个功能:协调器的组网,终端设备和路由设备发现网络以及加入网络

//第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统 int main( void ) {

.......

// Initialize the operating system

osal_init_system(); //第二步,操作系统初始化 ......

osal_start_system(); //初始化完系统任务事件后,正式开始执行操作系统 ...... }

//第二步,进入 osal_init_system()函数,执行操作系统初始化

uint8 osal_init_system( void ) //初始化操作系统,其中最重要的是,初始化操作系统的任务 {

// Initialize the Memory Allocation System osal_mem_init();

// Initialize the message queue osal_qHead = NULL; // Initialize the timers osalTimerInit();

// Initialize the Power Management System osal_pwrmgr_init();

// Initialize the system tasks.

osalInitTasks(); //第三步,执行操作系统任务初始化函数 // Setup efficient search for the first free block of heap. osal_mem_kick(); return ( SUCCESS ); }

//第三步,进入osalInitTasks()函数,执行操作系统任务初始化 void osalInitTasks( void ) //第三步,初始化操作系统任务 {

uint8 taskID = 0;

tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt); osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

//任务优先级由高向低依次排列,高优先级对应 taskID 的值反而小 macTaskInit( taskID++ ); //不需要用户考虑

nwk_init( taskID++ ); //不需要用户考虑

Hal_Init( taskID++ ); //硬件抽象层初始化,需要我们考虑 #if defined( MT_TASK ) MT_TaskInit( taskID++ ); #endif

APS_Init( taskID++ ); //不需要用户考虑 #if defined ( ZIGBEE_FRAGMENTATION ) APSF_Init( taskID++ ); #endif

ZDApp_Init( taskID++ ); //第四步,ZDApp层,初始化,执行ZDApp_init函数后,如果是协调器将建立网络,如果是终端设备将加入网络。

#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT ) ZDNwkMgr_Init( taskID++ ); #endif

SerialApp_Init( taskID ); //应用层SerialApp层初始化,需要用户考虑 在此处设置了一个按键触发事件,

//当有按键按下的时候,产生一个系统消息 }

//第四步,进入ZDApp_init()函数,执行ZDApp层初始化 //The first step

void ZDApp_Init( uint8 task_id ) //The first step,ZDApp层初始化。 {

// Save the task ID

ZDAppTaskID = task_id;

// Initialize the ZDO global device short address storage ZDAppNwkAddr.addrMode = Addr16Bit;

ZDAppNwkAddr.addr.shortAddr = INVALID_NODE_ADDR; (void)NLME_GetExtAddr(); // Load the saveExtAddr pointer. // Check for manual \ ZDAppCheckForHoldKey();

// Initialize ZDO items and setup the device - type of device to create. ZDO_Init();

// Register the endpoint description with the AF

// This task doesn't have a Simple description, but we still need // to register the endpoint.

afRegister( (endPointDesc_t *)&ZDApp_epDesc ); #if defined( ZDO_USERDESC_RESPONSE ) ZDApp_InitUserDesc();

#endif // ZDO_USERDESC_RESPONSE // Start the device?

if ( devState != DEV_HOLD ) //devState 初值为DEV_INIT ,所以在初始化ZDA层时,就执行该条件语句 {

ZDOInitDevice( 0 ); //The second step, 接着转到ZDOInitDevice()函数,执行The third step;

} else {

// Blink LED to indicate HOLD_START HalLedBlink ( HAL_LED_4, 0, 50, 500 ); }

ZDApp_RegisterCBs(); }

//The third step,执行ZDOInitDevice()函数,执行设备初始化

uint8 ZDOInitDevice( uint16 startDelay ) //The third step, ZDO层初始化设备, {

.......

// Trigger the network start

ZDApp_NetworkInit( extendedDelay ); //网络初始化,跳到相应的函数里头,执行The fourth step ....... }

//The fouth step,执行 ZDApp_NetWorkInit()函数

void ZDApp_NetworkInit( uint16 delay ) //The fourth step,网络初始化 {

if ( delay ) {

// Wait awhile before starting the device

osal_start_timerEx( ZDAppTaskID, ZDO_NETWORK_INIT, delay ); //发送ZDO_NETWORK_INIT(网络初始化)消息到 ZDApp层,转到 //ZDApp层,执行The fifth step , ZDApp_event_loop() 函数 } else {

osal_set_event( ZDAppTaskID, ZDO_NETWORK_INIT ); } }

//The fifth step,转到ZDApp_event_loop()函数

UINT16 ZDApp_event_loop( uint8 task_id, UINT16 events ) {

if ( events & ZDO_NETWORK_INIT ) //The fivth step,网络初始化事件处理 {

// Initialize apps and start the network devState = DEV_INIT;

//设备逻辑类型,启动模式,信标时间,超帧长度,接着转到The sixth step,去启动设备,接着执行The sixth step,转到ZDO_StartDevice()

ZDO_StartDevice( (uint8)ZDO_Config_Node_Descriptor.LogicalType, devStartMode, DEFAULT_BEACON_ORDER, DEFAULT_SUPERFRAME_ORDER );