Zigbee protocol stack application (1) - introduction and simple example of Zigbee protocol stack

1. Introduction to Zigbee protocol stack

  A protocol is a series of communication standards, and both parties need to transmit and receive data normally according to this standard. The protocol stack is the specific implementation form of the protocol. Generally speaking, the protocol stack is an interface between the protocol and the user. Developers use the protocol stack to use this protocol to realize wireless data transmission and reception.

  As shown in Figure 1: Zigbee protocol is divided into two parts, IEEE 802.15.4 defines PHY (physical layer) and MAC (medium access layer) technical specifications; Zigbee Alliance defines NWK (network layer), APS (application support layer) ), APL (application layer) technical specifications. The Zigbee protocol stack is a collection of protocols defined by each layer, implemented in the form of functions, and provides users with an API (application layer) that users can call directly.

            Figure 1 Architecture diagram of ZigBee wireless network protocol layer

 2. How to understand the Zigbee protocol stack

  The protocol stack is the implementation of the protocol, which can be understood as code and library functions, which are called by the upper-layer application . The protocol is independent of the lower layer and the application. The commercial protocol stack only provides you with an interface (in fact, it is very similar to the API model in the Internet industry). Just as you don't need to care how the underlying map is drawn based on position or coordinates when you call the map API, you also don't need to care about the underlying implementation of the protocol stack unless you want to do protocol research. Each manufacturer's protocol stack is different. For example, TI's BLE protocol stack is very different from nordic's BLE protocol stack (more on that, TI's BLE protocol stack is more like Android's BLE structure, so Android A bluetooth person might be able to read TI's code).

 3. How to use the Zigbee protocol stack

  Taking simple wireless data communication as an example, the general steps are:

① Networking : call the protocol stack networking function and join the network function to realize the establishment of the network and the addition of nodes

② Send : The sending node calls the sending function of the protocol stack to realize wireless data transmission

③ Receive : The receiving node calls the wireless receiving function of the protocol stack to realize wireless data reception

  Since the protocol stack has encapsulated these functions, it is more convenient for us to use them. The following is the wireless transmission function of the protocol stack:

  If you want to better apply the protocol stack, you need to have a detailed understanding of the specific functions and meanings of these functions and their parameters provided by the protocol stack, which will be introduced in detail in the next section.

 4. Install the Zigbee protocol stack

  Download 004 from all the materials in this series shared by me on Baidu, unzip and install:

               Figure 2 ZigBee protocol stack download path

 After installation, there will be the following file structure under the win7 start button:

                  Figure 3 The file structure after the ZigBee protocol is installed

  in:

 5. Explanation of LED project based on wireless transceiver control based on protocol stack (1)

  Download the ZStack-2.3.1a compressed file from the network disk:

                             Figure 4 The cloud disk directory where the project source code is located

    Use IAR to open the project file in .. \ZStack-2.5.1a\Projects\zstack\Samples\SampleApp\CC2530DB directory, be careful not to put 001Stack-2.5.1a in a relatively deep folder, otherwise IAR may open the project. I can't open the card all the time, and it is best not to have Chinese! After opening, the project and structure are as follows: (It's a bit dazzling, it doesn't matter, and the source code will be analyzed step by step later)

                  Figure 5 Overall architecture of the project

 6. Explanation of LED project based on wireless transceiver control based on protocol stack (2)

  Select XXXXEB in IAR, rebuild all the first time, after downloading the coordinator , switch to EndDeviceEB to compile and download to another board as a terminal device :

          Figure 6 Compile the project

 

After the two node programs are downloaded and powered on, you will see: D1 flashes after the networking is successful.

                Figure 7 Experimental phenomenon

 

7. Explanation of LED project based on wireless transceiver control based on protocol stack (3)

main code:

复制代码
 1 int main( void )
 2 {
 3     osal_int_disable( INTS_ALL );// Turn off interrupts         关中断
 4     HAL_BOARD_INIT();// Initialization for board related stuff such as LEDs
 5     zmain_vdd_check();// Make sure supply voltage is high enough to run   检查芯片是否上电正常
 6     InitBoard( OB_COLD );// Initialize board I/O  初始化I/O,LED,Timer等
 7     HalDriverInit();// Initialze HAL drivers 初始化硬件抽象层驱动模块
 8     osal_nv_init( NULL );// Initialize NV System 初始化flash存储器
 9     znpTestRF();// Initialize and check the ZNP RF Test Mode NV items. 
10     ZMacInit();// Initialize the MAC  初始化MAC层
11     zmain_ext_addr();// Determine the extended address  确定IEEE64位地址
12 
13 #if defined ZCL_KEY_ESTABLISH
14     zmain_cert_init();// Initialize the Certicom certificate information.
15 #endif
16 
17     zgInit();// Initialize basic NV items  初始化非易失变量
18 
19 #ifndef NONWK
20     afInit();// Since the AF isn't a task, call it's initialization routine
21 #endif
22 
23     osal_init_system();// Initialize the operating system     初始化OS(重点介绍1)
24     osal_int_enable( INTS_ALL );// Allow interrupts       使能中断
25     InitBoard( OB_READY );// Final board initialization      最终板载初始化
26     zmain_dev_info();// Display information about this device     显示设备信息(这里有LCD屏幕)
27 
28 #ifdef LCD_SUPPORTED/* Display the device info on the LCD 将信息显示在LCD上*/
29     zmain_lcd_init();      
30 #endif
31 
32 #ifdef WDT_IN_PM1
33     WatchDogEnable( WDTIMX );/* If WDT is used, this is a good place to enable it. */
34 #endif
35 
36     osal_start_znp(); // No Return from here    执行操作系统(重点介绍2)
37 
38     return 0;  // Shouldn't get here.
39 } // main()
复制代码

代码有点难懂,核心是执行初始化工作,包括硬件抽象层、网络层、任务等。然后执行osal_start_znp() ,进入一个死循环,不断对任务进行遍历执行。这里我们需要重点了解两个函数:

 

 ① 操作系统初始化函数

在操作系统初始化中需重点关注的是操作系统任务初始化osalInitTasks函数,操作系统初始化函数中主要来创建任务taskID任务ID,每增加一个任务ID++,同时ID越小表示该任务优先级越高!其中蓝框内的函数是要根据系统想完成的任务做修改的地方,其他都是官方提供的基本不用变的任务。

 ② 操作系统启动函数

 

执行OS的函数就是个大循环,不断取出当前优先级最高的待处理事件进行处理,处理的核心思想在osal_run_task函数内:通过调用一个函数指针来远程调用一个事件处理函数

 

8、小结

  至此,我们讲到任务如何建立、如何处理等,其中有一个环节没有讲——如何从events=(tasksArr[idx])(idx,event)关联到每个具体任务的事件处理函数的?这个其实我在CC2540/CC2541的前两篇中已有详细介绍:[接下来会针对具体通信梳理流程!]

1、CC2541蓝牙4.0芯片中级教程——基于OSAL操作系统的运行流程了解+定时器和串口例程了解

2、CC2541芯片中级教程-OSAL操作系统(进一步了解-OLED && 普通按键和5方向按键-中断!!!)这个系统驱动层和应用层不一样~

3、CC2541芯片中级教程-OSAL操作系统(ADC光敏电阻和修改串口波特率)

4、CC2541芯片中级教程-OSAL操作系统(简单AT指令实现+IIC软件和硬件实现驱动MPU6050)

5、CC2541芯片中级教程-OSAL操作系统(PWM+看门狗)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324892408&siteId=291194637