Z-stack Software Architecture Analysis

Source: Internet
Author: User

Z-stackof theMainfunction inzmain.c, in general, it does a total of two work, one is the system initialization, that is, has the startup code to initialize the hardware system and the software architecture needs each module, another function is to open the temple to execute the operating system entity. 1. Initialization of the systemThe system boot code needs to complete the initialization of hardware platform and software architecture required by the modules, for the operating system to prepare for the operation, mainly divided into the initialization of the system clock, detection chip operating voltage, initialization stack, initialization of each hardware module, initialize FLASH storage, form the chip mac address, initialize non-volatile amount, initialize MAC layer protocol, initialize the application Frame layer protocol, initialize the operating system, etc. 10 more than 2. implementation of the operating systemAfter the startup code prepares the operating system for execution, it starts executing the operating system entry and transfers control to the operating system completely. in fact, the operating system entity has only one line of code:
1 // No Return from here
There is a comment at the end of this sentence, meaning that this function does not return, that is, it is a dead loop and can never be executed. This function is the main part of the osal system rotation query operation, it does the job is to constantly query each task whether there are events, if there is an event, call the corresponding event handler function, if no event occurred on the query. We can see the following is the implementation of this function, in fact, this function is in an infinite loop.
1 voidOsal_start_system (void )2 {3 #if!defined (Zbit)4    for(;;)//Forever Loop5 6 #endif7   {8Uint8 idx =0;9 TenHal_processpoll ();//This replaces Mt_serialpoll () and Osal_check_timer (). One  A  -      Do { -       if(Tasksevents[idx])//highest priority task is found the  -       { -          Break; -       } +} while(++idx < taskscnt);//where taskscnt is the number of elements in the Tasksarr array -  +     //Gets the index number of the task with the highest priority to be processed IDX A  at     if(IDX <taskscnt) -     { - uint16 events; - halintstate_t intstate; -       //Enter/exit the critical section to extract the events that need to be handled, in fact, this and μc/OS operating system into the critical section is very similar to the use of μc/Os os_enter_critical (); os_exit_critical (); -  in hal_enter_critical_section (intstate); -Events =Tasksevents[idx]; toTASKSEVENTS[IDX] =0;//Clear The Events for this task. +  -Hal_exit_critical_section (intstate);//perform the corresponding task handler function with a pointer call the  *  $Events =(Tasksarr[idx]) (IDX, events);Panax Notoginseng     //Enter/exit a critical section to save an event that has not been processed -  the hal_enter_critical_section (intstate); +TASKSEVENTS[IDX] |= events;//Add back unprocessed events to the current task. A  the hal_exit_critical_section (intstate); +}//the end of this event processing, -  $ #ifDefined (power_saving) $     Else //after all the task events have been queried, no events are activated -  -     { theOsal_pwrmgr_powerconserve ();//The system goes into hibernation state.  - Wuyi     } the #endif -     } Wu}

The operating system specifically allocates a tasksevents[] array for all task events, each of which corresponds to all events that hold each task, in which the first iteration through a do-while loop tasksevents[], Find a task with the highest priority for the event to be processed, the task with the lower order priority high, and then jump out of the loop, at this point, the highest priority task of the ordinal idx, and then through the EVENTS=TASKSEVENTS[IDX] statement, the current highest priority task of the event out , followed by the call (Tasksarr[idx]) (inx,events) function to execute the specific handler function, taskarr[] is a function pointer array, according to the different IDX can execute different functions.

TI gives a few examples to demonstrate the Z-stack protocol stack, in fact, most of the code in these examples is the same, only the user's application layer, the addition of different tasks and event handler functions. This is illustrated by the example of Generalapp.

First, identify several tasks to be performed in the system. In the Generalapp example, several task functions make up the above array of Tasksarr function pointers, defined in osal_generalapp.c, and the Osal_start_system () function is passed through the function pointer (Tasksarr[idx]) ( inx,events) call.

The Tasksarr array is as follows:

1 ConstPTASKEVENTHANDLERFN tasksarr[] = {2Maceventloop,//Mac Layer Task handler function3 4Nwk_event_loop,//Network Layer Task handler function5 6Hal_processevent,//Hardware Abstraction Layer Task handler function7 8 #ifDefined (mt_task)9 TenMt_processevent,//The Debug Task handler function is optional One  A #endif -  -Aps_event_loop,//Application Layer Task handler function, user does not have to modify the  -Zdapp_event_loop,//The device Application Layer task handler function, the user can modify as needed -  -Genericapp_processevent//User Application Layer task handler function, user generated +  -};

If the debugging task is not counted, the operating system has to deal with 6 tasks, namely, MAC layer, network layer. The hardware abstraction layer, the application layer, the ZigBee device application layer, and the application layer which is completely handled by the user, its priority level is high to low. The MAC layer task has the highest priority, and the user layer has the lowest priority level. Z-stack has written an event handler for the five-tier task from the MAC layer to the ZigBee device application layer, typically without modifying these functions, just write the application layer's task and event handler function as required.

In the other case files, the only difference is that the last function is the same. In general, users only need to add three additional files to complete a project. One is the main file, storing specific task event handler functions such as genericapp_processevent in the above case, one is the header file of the main file, the other is the operating system interface file starting with Osal, is dedicated to the task processing function array tasksarr[] The file. This enables the Z-stack code to be common, the user only need to add these files, write their own task handler function. Now is not the feeling is very simple, do not want to imagine the complexity, this operating system abstraction layer and the real-time operating system μc/Os have similarities, alas μc/Os can be assigned to 64 tasks. Understand this operating system, understand osal should not be very difficult, but, Z-stack just based on this osal run, but the focus is not here, but the realization of the communication between ZigBee devices, as well as networking, composed of different network structure, These are the core of the entire ZigBee protocol, and of course it should be far more complex than the number of files we add.

Z-stack the directory structure in the project

There are approximately 14 directory files in the Z-stack project, and there are many subdirectories and files underneath the directory file. Here's a look at these 14 root directories, specifically what it does:

(1) App: Application layer directory, files in this directory are the files you want to add when you create a new project.

(2) HAL: Hardware layer directory, files in the common directory are public files, basically hardware independent, where hal_assert.c is an assertion file, used for calling, Hal_ DRIVERS.C is a driver file that abstracts out hardware-independent driver functions, including hardware-related configuration and driver and operation functions. The Include directory contains the header files for each hardware module, and the files in the target directory are related to the hardware platform, and you may see two platforms, namely the cc2430db platform and a CC2430EB platform. The back of the DB and EB represents the TI company Development Board model, in fact, there is a type of BB, Bb:battery Board
Db:development Board eb:evaluation Board
Corresponding to the TI company developed three kinds of plate type, its function in order to become stronger. You can get a more intuitive understanding of the image of "Z-stack User's Guide for CC2430".

(3) Mac:mac layer directory, high and low level two directories that the MAC layer is divided into the upper and lower layers, the Include directory contains the MAC layer parameter configuration file and the base Mac Lib Library function interface file, where the MAC layer of the Protocol is not open source , given in the form of a library

(4) MT: Producer Debug Layer directory, the file in this directory is mainly used for debugging purposes, that is, through the serial port debugging each layer, directly interacting with each layer.

(5) NWK: Network layer directory, including network layer configuration parameter file and Network Layer library function interface file, and APS Layer library function interface

(6) Osal: Operating system Abstraction Layer directory for the protocol stack

(7) PROFILE:AF layer directory, Application Farmework application framework, including the AF layer processing function interface file.

(8) Security: Secure layer directory with Security layer processing function interface file

(9) Services:zigbee and 802.15.4 device address processing function directory, including the definition of address patterns and address processing functions

Tools: Work configuration directory, including spatial partitioning and z-stack-related configuration information

(one) ZDO: refers to the ZigBee device object, can be considered a common feature set, the file user calls the APS sub-layer service and the NWK layer service with the custom object

Zmac: Where ZMAC.C is the Z-STACKMAC export layer interface file, ZMAC_CB.C is the network layer function that the ZMAC needs to call

ZMAIN:ZMAIN.C mainly contains the entry function of the entire project main (), the interface function that is controlled by the ONBOARD.C containing hardware start platform class peripherals

Output: Export file directory, this is the EW8051 IDE automatically generated

Z-stack Software Architecture Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.