其中,初始化操作系统和执行操作系统这两步是最为关键的两步。这两步都是在位于 ZMain 文件夹下的 ZMain.c 文件里的 main 函数里进行的。 main 函数如下面代码所示:
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 ); //初始化I/O ,LED 、Timer 等
// Initialze HAL drivers
HalDriverInit(); //初始化芯片各硬件模块
// Initialize NV System
osal_nv_init( NULL ); //初始化Flash 存储器
// Initialize the MAC
ZMacInit(); //初始化MAC 层
// Determine the extended address
zmain_ext_addr(); //确定IEEE 64位地址
// 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(); //初始化LCD
#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.
}
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
}
}
* void SampleApp_MessageMSGCB( afIncomingMSGPacket_t pkt )
这个函数管理所有接收到的数据,至于数据来自哪个设备,它是根据簇 ID 来分辨的。
函数里面就是一个 switch 语句,关键是 case 及其后面的服务函数。用户可以根据不同的功能,定义不同的簇 ID(在 SampleApp.h 里进行),然后在这个 switch 语句里添加一个以簇 ID 来命名的 case,并在 case 里面编写自己的应用程序,如“基于协议栈的串口透传”实验里会有这个知识的讲解。
typedef struct
{
osal_event_hdr_thdr; /* OSAL Message header */
uint16 groupId; /* Message's group ID - 0 if not set */
uint16 clusterId; /* Message's cluster ID */
afAddrType_tsrcAddr; /* Source Address, if endpoint is STUBAPS_INTER_PAN_EP,it's an InterPAN message */
uint16 macDestAddr; /* MAC header destination short address */
uint8 endPoint; /* destination endpoint */
uint8 wasBroadcast; /* TRUE if network destination was a broadcast address */
uint8 LinkQuality; /* The link quality of the received data frame */
uint8 correlation; /* The raw correlation value of the received data frame */
int8 rssi; /* The received RF power in units dBm */
uint8 SecurityUse; /* deprecated */
uint32 timestamp; /* receipt timestamp from MAC */
afMSGCommandFormat_t cmd; /* Application Data */
}afIncomingMSGPacket_t;