本帖最后由 伊海 于 2014-7-24 08:41 编辑
在阅读了Zigbee及IEEE802.15.4协议的理论知识后,接下来看一下TI公司开发的基于Zigbee的协议实现ZStack。 我们仍然从TI提供的温度监测程序开始,首先查看一下,程序的主函数在ZMain.c文件中,从程序的说明看出,是ZStack的startup和shutdown代码. 以下是其中的main()函数,其实英文注释已经很清楚说明每一步,这里翻译一下: - int main( void )
- {
- // Turn off interrupts(关中断)
- osal_int_disable( INTS_ALL );
- // Initialization for board related stuff such as LEDs(初始化板上组件,如LED)
- HAL_BOARD_INIT();
- // Make sure supply voltage is high enough to run(电压检查)
- zmain_vdd_check();
- // Initialize board I/O(初始化I/O接口)
- InitBoard( OB_COLD );
- // Initialze HAL drivers(初始化HAL设备,在hal_drivers.c中实现)
- HalDriverInit();
- // Initialize NV System(初始化NV系统,即非易失设备,如Flash)
- osal_nv_init( NULL );
- // Initialize the MAC(初始化MAC)
- ZMacInit();
- // Determine the extended address(确定设备的长地址)
- zmain_ext_addr();
- // Initialize basic NV items(初始化ZStack的全局变量,如果在NV内存中不存在,则写入缺省值)
- 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(显示设备信息,比如CollectorEB板的IEEE地址就是在这个函数里面显示出来的。)
- zmain_dev_info();
- /* Display the device info on the LCD */(显示LCD设备信息)
- #ifdef LCD_SUPPORTED
- zmain_lcd_init();
- #endif
- #ifdef WDT_IN_PM1
- /* If WDT is used, this is a good place to enable it. */(开WatchDog)
- WatchDogEnable( WDTIMX );
- #endif
- osal_start_system(); // No Return from here(启动操作系统,实际上进入一个死循环)
- return 0; // Shouldn't get here.
- } // main()
复制代码
说明:以上初始化有的跟硬件相关,有的跟操作系统(以osal开头)相关,最后系统调用osal_start_system进入循环,然后在循环中处理消息,下面是这个函数的主体(见osal.c): - void osal_start_system( void )
- {
- #if !defined ( ZBIT ) && !defined ( UBIT )
- for(;;) // Forever Loop (死循环)
- #endif
- {
- uint8 idx = 0;
- osalTimeUpdate(); //更新时间
- Hal_ProcessPoll(); // This replaces MT_SerialPoll() and osal_check_timer().(轮询UART和Timer等设备消息)
-
- do {
- if (tasksEvents[idx]) // Task is highest priority that is ready.
- {
- break;
- }
- } while (++idx < tasksCnt);
- if (idx < tasksCnt)
- {
- uint16 events;
- halIntState_t intState;
- HAL_ENTER_CRITICAL_SECTION(intState); //加锁防上访问事件队列充突
- events = tasksEvents[idx];
- tasksEvents[idx] = 0; // Clear the Events for this task.
- HAL_EXIT_CRITICAL_SECTION(intState);
- events = (tasksArr[idx])( idx, events ); //处理当前要处理的事件,并返回事件ID
- HAL_ENTER_CRITICAL_SECTION(intState);
- tasksEvents[idx] |= events; // Add back unprocessed events to the current task.(更新事件状态)
- HAL_EXIT_CRITICAL_SECTION(intState);
- }
- #if defined( POWER_SAVING )
- else // Complete pass through all task events with no activity?
- {
- osal_pwrmgr_powerconserve(); // Put the processor/system into sleep(没有事件处理,进入休眠状态)
- }
- #endif
- }
- }
复制代码需要特别解析的是上面红色标注的行,这个地方看起来象是一个函数指针的形式,查看taskArr定义(见SAPI.c)如下: - #if OSAL_SAPI
- // The order in this table must be identical to the task initialization calls below inosalInitTask.
- const pTaskEventHandlerFn tasksArr[] = {
- macEventLoop,
- nwk_event_loop,
- Hal_ProcessEvent,
- #if defined( MT_TASK )
- MT_ProcessEvent,
- #endif
- APS_event_loop,
- ZDApp_event_loop,
- SAPI_ProcessEvent
- };
复制代码
|