找到协议栈安装目录中的工程文件ZStack-CC2530-2.5.1a\Projects\zstack\Samples\SampleApp\CC2530DB\SampleApp,双击打开,左侧的工程列表有很多协议栈相关的文件夹,例如APP、HAL、MAC等,如图3-2-2所示,从这些文件夹的编排上就可以充分体现出 ZigBee的分层思想,同一层的实现函数文件都存放在同一个文件夹中。
任何程序都在 main 函数开始运行,Z-stack 也不例外。找到ZMain目录,打开ZMain.c文件,现在将对协议栈的main函数进行一个分析。
/*********************************************************************
* @fn main
* @brief First function called after startup.
* @return don't care
*/
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();
// Determine the extended address
zmain_ext_addr();
#if defined ZCL_KEY_ESTABLISH
// Initialize the Certicom certificate information.
zmain_cert_init();
#endif
// 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
return 0; // Shouldn't get here.
} // main()
在main函数中,对板载硬件资源以及协议栈系统进行了初始化,到函数最后,系统开始运行,不再返回。
现在我们着重关注系统初始化和系统运行函数。首先分析osal_init_system()函数,将光标放在函数上,右键然后点击go to definition of,进入到函数内部:
/*********************************************************************
* @fn osal_init_system
*
* @brief
*
* This function initializes the "task" system by creating the
* tasks defined in the task table (OSAL_Tasks.h).
*
* @param void
*
* @return SUCCESS
*/
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 );
}
发现函数中有6个语句,我们还是从重点开始着手,找到系统任务初始化函数osalInitTasks(),进入到函数内部:
/*********************************************************************
* FUNCTIONS
*********************************************************************/
/*********************************************************************
* @fn osalInitTasks
*
* @brief This function invokes the initialization function for each task.
*
* @param void
*
* @return none
*/
void osalInitTasks( void )
{
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++ );
#if defined ( ZIGBEE_FRAGMENTATION )
APSF_Init( taskID++ );
#endif
ZDApp_Init( taskID++ );
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
ZDNwkMgr_Init( taskID++ );
#endif
SampleApp_Init( taskID ); // 用户层任务函数
}
函数中,首先给指针tasksEvents分配内存,给它分配的内存用于存放事件表,所分配内存的大小可以看到由tasksCnt来决定,继续往下看,会发现有一个变量taskID贯穿整个函数,每一次把taskID传进一个函数里面,其值就递增1。如果跳进某个函数里面查看,就会发现在函数中,往往会在第一句程序就用一个全局变量保存了传进去的taskID的值。这个保存下来的值,将作为任务的ID来使用,值越小,说明任务初始化越靠前,任务的优先级越高。