一文理解ZigBee通信全过程(基于ZStack-CC2530-2.5.1a协议栈)

自己在word中整理的笔记,基本上详细的分析了Zigbee的原理,比较透彻,分享给大家交流参考!

目录

0概述... 2

1协议栈整体运行流程... 3

1.1. 操作系统初始化流程... 5

1.2操作系统启动... 9

2相关数据结构与函数调用... 13

3 ZigBee协议栈OSAL理解... 14

3.1 OSAL存在的目的:... 14

3.2 OSAL提供的主要功能:... 14

3.3 OSAL消息队列:... 15

3.4 OSAL添加任务:... 15

3.5 OSAL应用接口编程:... 15

4其它相关问题... 15

 

0概述

1协议栈整体运行流程

OSAL作为操作系统抽象层,是整个Z-Stack运行的基础,用户自己建立的任务和应用程序都必须在此基础上运行,那我们知道整个Z-Stack协议就是用C语言编写的,既然使用C语言编写的,那程序的入口点就是main()函数,而且整个Z-Stack都只有一个main()函数入口,那我们的入口点也是main()函数,由int 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 ishigh enough to run

  //电压检测,确保芯片能正常工作的电压

 zmain_vdd_check();

 

  // Initialize board I/O

 //初始化板载I/O

 InitBoard( OB_COLD );

 

  // Initialze HAL drivers

 //初始化硬件驱动

 HalDriverInit();

 

  // Initialize NV System

 //初始化NV系统

 osal_nv_init( NULL );

 

  // Initialize the MAC

 //初始化MAC

 ZMacInit();

 

 // Determine the extended address

 //确定扩展地址(64位IEEE/物理地址)

 zmain_ext_addr();

 

#if defined ZCL_KEY_ESTABLISH

  // Initialize the Certicom certificate information.

 zmain_cert_init();

#endif

 

  // Initialize basic NVitems

 //初始化基本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 informationabout 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.//不会运行到这

}

 

 

1.1. 操作系统初始化流程

 在这个函数中初始化了操作系统用到的重要信息,比如:内存,堆栈等,但我们重点关注的是初始化系统任务函数osalInitTasks();

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 );

}

 

 

任务初始化函数-------osalInitTasks();

void osalInitTasks( void )

{

 uint8 taskID = 0;

  //osal_mem_alloc()该函数是OSAL中的内存管理函数,是一个存储分配函数,返回指向一个缓存的指针,参数是被分配缓存的大小,其tasksCnt的定义如下const uint8tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );tasksEvents指向被分配的内存空间,这里注意tasksArr[]函数指针数组的联系是一一对应的。tasksEvents就是指向第一个被分配的任务的内存空间

 tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);

 

  //把申请的内存空间全部设置为0,tasksCnt任务数 * 单个任务占的内存空间(4byte)

 osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

 

  //下面就是Z-Stack协议栈中,从MAC层到ZDO层的初始化函数,其中的参数都是任务的ID,不过ID号是依次递增的

  macTaskInit(taskID++ ); //mac_ID = 0

 nwk_init( taskID++ ); //nwk_ID = 1

 Hal_Init( taskID++ ); //Hal_ID = 2

 

#if defined( MT_TASK )

 MT_TaskInit( taskID++ );//mt_ID = 3

#endif

 APS_Init( taskID++ ); //APS_ID =4

#if defined ( ZIGBEE_FRAGMENTATION )

  APSF_Init(taskID++ ); //ZDO_ID =5

#endif

 ZDApp_Init( taskID++ ); ;//ZDO_ID =6

#if defined ( ZIGBEE_FREQ_AGILITY ) ||defined ( ZIGBEE_PANID_CONFLICT )

 ZDNwkMgr_Init( taskID++ ); //ZDO_ID =7

#endif

 GenericApp_Init( taskID ); //ZDO_ID =8

}

 

注意:任务初始化,就是为系统的各个任务分配存储空间,当然,这个空间初始化时为全0(NULL),然后为各任务分配taskID;这里的顺序要注意.系统主循环函数里tasksEvents[ idx]和tasksArr[ idx]的idx与这里taskID是一一对应关系。我们可以在OSAL_GenericApp.c文件中看到有下面的一个数组的定义,数组中的每个成员都是一个函数,这里的函数和上面的初始化顺序是一样的,也是和tasksEvents所指向的任务的内存地址一保持一致。在这个数组中我们仍然重点观注的是最后一个GenericApp_ProcessEvent,这个是用户应用层任务处理函数。

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

 GenericApp_ProcessEvent

};

指针数组t

  • 39
    点赞
  • 249
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值