Ztack学习笔记(2)-系统初始化分析
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(),下节将介绍此。
四 参考链接
Ztack学习笔记(2)-系统初始化分析的更多相关文章
- Ztack学习笔记(4)-系统网络分析
协调器的组网,终端设备和路由设备发现网络以及加入网络 //第一步:Z-Stack 由 main()函数开始执行,main()函数共做了 2 件事:一是系统初始化,另外一件是开始执行轮转查询式操作系统 ...
- Linux学习笔记-Linux系统简介
Linux学习笔记-Linux系统简介 UNIX与Linux发展史 UNIX是父亲,Linux是儿子. UNIX发行版本 操作系统 公司 硬件平台 AIX IBM PowerPC HP-UX HP P ...
- Symfony2 学习笔记之系统路由
mfony2 学习笔记之系统路由 漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/ ...
- SPSS学习笔记之——Kaplan-Meier生存分析
SPSS学习笔记之--Kaplan-Meier生存分析 一.概述 关于生存分析的相关概念,Kaplan-Meier用于估计生存函数,允许有一个分组变量进行生存率的组间比较,还容许一个分层变量.若不考虑 ...
- Nginx学习笔记4 源码分析
Nginx学习笔记(四) 源码分析 源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_ ...
- Ztack学习笔记(3)-系统启动分析
一 系统启动 //OSAL.cvoid osal_start_system( void ) { #if !defined ( ZBIT ) && !defined ( UBIT ) f ...
- Hadoop学习笔记—20.网站日志分析项目案例(三)统计分析
网站日志分析项目案例(一)项目介绍:http://www.cnblogs.com/edisonchou/p/4449082.html 网站日志分析项目案例(二)数据清洗:http://www.cnbl ...
- Hadoop学习笔记—20.网站日志分析项目案例
1.1 项目来源 本次要实践的数据日志来源于国内某技术学习论坛,该论坛由某培训机构主办,汇聚了众多技术学习者,每天都有人发帖.回帖,如图1所示. 图1 项目来源网站-技术学习论坛 本次实践的目的就在于 ...
- Ztack学习笔记(1)-初识Ztack
一.Zigbee协议 Zigbee是IEEE 802.15.4协议的代名词,是一种短距离.低功耗的无线通信技术.这一名称来源于蜜蜂的八字舞,因为蜜蜂(bee)是靠飞翔和“嗡嗡”(zig)地抖动翅膀的“ ...
随机推荐
- iOS - UI - UISegmentedControl
1.UISegmentedControl NSArray * array = @[@"red",@"green",@"yellow",@&q ...
- iOS - UI - UIStepper
7.UIStepper //计数器控件 固定宽高 UIStepper * stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 10 ...
- 从Setting.settings到Resource.resx
之前由于经验不足,将常用的App提示信息串(string)放置在了配置文件中(*.Settings).目前需要将App国际化,对这些信息的翻译有两个途径: 直接翻译,将参数中的提示信息串用英文或者其他 ...
- Java中创建操作文件和文件夹的工具类
Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- python备忘录
本文主要是记录一下python,比较详尽的python学习资料: Python学习笔记_王纯业 http://pan.baidu.com/s/1eQrDEYA 部分有详细的博文链接 1.字符串: 切片 ...
- Javascript中对象类型的参数传递
function setName(obj){ obj.name = 'Niccholas'; console.log(obj.name); //Niccholas obj = new Object() ...
- Part 33 Difference between abstract classes and interfaces
- C# DateTime 日期加1天 减一天 加一月 减一月 等方法(转)
//今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一 DateTime.Now.AddDays(-).ToShortDateString ...
- 几款jQuery右键菜单插件
1.jQuery Very Simple ContextMenu Plugin 2.ContextJS Project Page:http://lab.jakiestfu.com/contextjs/ ...
- 20150222—LINQ to SQL 插入、更新和删除
注意,使用LINQ to SQL时, 表中必须有一个主键才可以起效,否则系统将无法对数据作出修改 插入新数据,根据上一片的文章实例在其中添加新的控件: 编号TextBox(Name):sno 名字Te ...