当前位置:文档之家› ZigBee协议栈初始化网络启动流程

ZigBee协议栈初始化网络启动流程

. .. . ..w.. ZigBee协议栈初始化网络启动流程

ZigBee的基本流程:由协调器的组网(创建PAN ID),终端设备和路由设备发现网络以及加入网络。

基本流程:main()->osal_init_system()->osalInitTasks()->ZDApp_Init(),进协议栈初始化函数ZDApp_Init()。

1.1 进入程序入口main()。 ZMain.c中 C++ Code int main( void ) { // Turn off interrupts osal_int_disable( INTS_ALL );

// Initialization for board related stuff such as LEDs HAL_BOARD_INIT();

// Make sure supply voltage is high enough to run zmain_vdd_check();

// Initialize board I/O InitBoard( OB_COLD );

// Initialze HAL drivers HalDriverInit();

// Initialize NV System osal_nv_init( NULL );

// Initialize the MAC ZMacInit(); . .. . ..w.. // Determine the extended address zmain_ext_addr();

// Initialize basic NV items zgInit();

#ifndef NONWK // Since the AF isn't a task, call it's initialization routine afInit(); #endif

// Initialize the operating system osal_init_system();

// Allow interrupts osal_int_enable( INTS_ALL );

// Final board initialization InitBoard( OB_READY );

// Display information about this device zmain_dev_info();

/* Display the device info on the LCD */ #ifdef LCD_SUPPORTED zmain_lcd_init(); #endif

#ifdef WDT_IN_PM1 /* If WDT is used, this is a good place to enable it. */ WatchDogEnable( WDTIMX ); #endif

osal_start_system(); // No Return from here . .. . ..w.. return 0; // Shouldn't get here. } // main()

1.2 给任务添加ID sapi.c中 C++ Code void osalInitTasks( void ) //为各自进程添加ID 用于任务的查找 { uint8 taskID = 0;

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

macTaskInit( taskID++ ); nwk_init( taskID++ ); Hal_Init( taskID++ );//硬件抽象层初始化 #if defined( MT_TASK ) MT_TaskInit( taskID++ ); #endif APS_Init( taskID++ ); ZDApp_Init( taskID++ );//判断如果协调器节点建立网络、如果终端节点加入网络 SAPI_Init( taskID ); }

1.3 初始化ZigBee协议栈网络 ZDApp.c C++ Code void ZDApp_Init( uint8 task_id ) { // Save the task ID . .. . ..w.. 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 "Hold Auto Start" //检测到有手工设置HAL_KEY_SW_1则会设置devState = DEV_HOLD,从而避开网络初始化 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 ) { ZDOInitDevice( 0 ); } else { // Blink LED to indicate HOLD_START HalLedBlink ( HAL_LED_4, 0, 50, 500 ); } . .. . ..w.. ZDApp_RegisterCBs(); } /* ZDApp_Init() */

如果设置devState为DEV_HOLD,则不会执行ZDOInitDevice;反之,系统会调用此函数是设备组网或者入网。看下这个函数完成的功能是什么样子的。ZDOInitDevice是设备在网络中启动。它会读取NV中的ZCD_NV_STARTUP_OPTION选项决定是否恢复网络状态。如果应用层强制进行新的join操作,它应该在调用这个函数之前设置ZCD_NV_STARTUP_OPTION中的ZCD_STARTOPT_DEFAULT_NETWORK_STATE位。可以调用zgWrieStartupOptions()函数完成这些设置。

1.4 初始化设备(启动网络和设置网络类型) ZDApp.c C++ Code uint8 ZDOInitDevice( uint16 startDelay ) { uint8 networkStateNV = ZDO_INITDEV_NEW_NETWORK_STATE; uint16 extendedDelay = 0;

if ( devState == DEV_HOLD ) { // Initialize the RAM items table, in case an NV item has been updated. zgInitItems( FALSE ); }

ZDConfig_InitDescriptors(); //devtag.071807.todo - fix this temporary solution _NIB.CapabilityInfo = ZDO_Config_Node_Descriptor.CapabilityFlags;

devState = DEV_INIT; // Remove the Hold state // 函数读取NV项目ZCD_NV_LEAVE_CTRL的值,ZDApp_LeaveCtrl指向这个值 // Initialize leave control logic

相关主题