main函数先执行初始化工作,包括硬件、网络层、任务等的初始化。
一 系统初始化
系统初始化函数主要完成内存分配、消息队列头、定时器、电源管理、任务系统及内存栈等的初始化,具体如下代码所示:
//osal.c
1 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 );
}
二 任务初始化
任务初始化,就是为系统的各个任务分配存储空间,初始化后,内存空间为全0(NULL),然后为各任务分配任务标识号taskID。即,这里重点是各任务的初始化,MAC层和NWK层的未开源看不到。
//OSAL_SampleApp.c
1 void osalInitTasks( void )
{
uint8 taskID = ; tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);//为当前OSAL中的各任务分配存储空间(实际上是一个任务数组),函数返回指向任务缓冲区的指针,因此tasksEvents指向该任务数组的首地址。
osal_memset( tasksEvents, , (sizeof( uint16 ) * tasksCnt));
//把开辟的内存全部设置为0;sizeof( uint16 )是4个字节,即一个任务的长度(同样是uint16定义),乘以任务数量tasksCnt,即全部内存空间
macTaskInit( taskID++ );//初始化各层任务 mac_taskID=0;
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 );//SampleApp_taskID=6;用户创建的任务
}
系统主循环函数里tasksEvents[ idx]和tasksArr[ idx]的idx与这里taskID是一一对应关系。数组tasksEvents[]里面元素是各任务事件,不是指向任务事件的指针,数组tasksArr[ ]是这个指针数组,里面元素是指向各任务事件处理函数的指针,这两个指针数组里面各元素的顺序要一一对应,因为后面需要相应任务调用相应事件处理函数。如下代码所示,其中的xx_loop与上面的yy_init是一一对应的关系。
//OSAL_Tasks.h
typedef unsigned short (*pTaskEventHandlerFn)( unsigned char task_id, unsigned short event );
//OSAL_SampleApp.c
const pTaskEventHandlerFn tasksArr[] = {
macEventLoop,
nwk_event_loop,
Hal_ProcessEvent,
#if defined( MT_TASK )
MT_ProcessEvent,
#endif
APS_event_loop,
#if defined ( ZIGBEE_FRAGMENTATION )
APSF_ProcessEvent,
#endif
ZDApp_event_loop,
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
ZDNwkMgr_event_loop,
#endif
SampleApp_ProcessEvent
}; const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[] );
三 应用初始化
void SampleApp_Init( uint8 task_id )
{
SampleApp_TaskID = task_id;//osal分配的任务ID,这里为6,随着用户添加任务的增多而改变
SampleApp_NwkState = DEV_INIT;//设备状态设定为ZDO层中定义的初始化状态(无连接)
SampleApp_TransID = ;//消息发送ID(多消息时有顺序之分)
// Device hardware initialization can be added here or in main() (Zmain.c).
// If the hardware is application specific - add it here.
// If the hardware is other parts of the device add it in main().
//以下是通过跳线判断是路由器还是协调器
#if defined ( BUILD_ALL_DEVICES )
// The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START
// We are looking at a jumper (defined in SampleAppHw.c) to be jumpered
// together - if they are - we will start up a coordinator. Otherwise,
// the device will start as a router.
if ( readCoordinatorJumper() )
zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;
else
zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
#endif // BUILD_ALL_DEVICES
//以下是决断是通过外部按键触发还是系统启动过程中触发启动芯片
#if defined ( HOLD_AUTO_START )
// HOLD_AUTO_START is a compile option that will surpress ZDApp
// from starting the device and wait for the application to
// start the device.
ZDOInitDevice();
#endif
//以下代码设置网络的通讯方式
// Setup for the periodic message's destination address
// Broadcast to everyone
SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;
SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; // Setup for the flash command's destination address - Group 1
SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup;
SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; // Fill out the endpoint description.
SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT;
SampleApp_epDesc.task_id = &SampleApp_TaskID;
SampleApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;
SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF
afRegister( &SampleApp_epDesc ); // Register for all key events - This app will handle all key events
RegisterForKeys( SampleApp_TaskID ); // By default, all devices start out in Group 1
SampleApp_Group.ID = 0x0001;
osal_memcpy( SampleApp_Group.name, "Group 1", );
aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); #if defined ( LCD_SUPPORTED )
HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 );
#endif
}
以上为OSAL初始化大体流程,OSAL以及各软硬部件初始化完成后,就进入了系统主循环函数osal_start_system(),下节将介绍此。
四 参考链接