ZSTACK-CC2530-2.5.1A Main Code Analysis summary--understand Zstack-osal's principle and thought

Source: Internet
Author: User

First, download zstack-cc2530.2.5.1a

http://download.csdn.net/detail/thanksgining/8328925

After download:


Second, installation zstack-cc2530.2.5.1a

After installation:


Components : As the name implies, this is a library file, which puts some zdo,driver,hal,zcl and other library code

Documents: This needless to say that we all know is to put TI development documents, you can read these documents one by one, you are familiar with the protocol stack. A lot of it is a must-read API that tells the protocol stack

Projects: This file folder is the TI protocol stack example program, each example program is in the form of a project to us, learn these examples of the program one or two, basic you can do things

Tools : This file is a program that puts TI's example program on some host computer, as a tool to use

Components folder


As stated above, there are some library things, HAL is the hardware level of some driver and so on. Mac, Zmac is the MAC layer protocol interface, MT is the API we use almost all can be found here examples. Osal This is the TI's Zstack protocol stack operating system, is event-driven, stack is some zdo and zcl and so on.

Third, open Samleapp.eww project, path: projects\zstack\samples\sampleapp\cc2530db


1, enter the main function, file path: Zmain under the ZMAIN.C

int main (void) {/* * Turn off interrupts shut down all interrupts, in fact it is closed total interrupt * #define INTS_ALL 0xFF * Final Call EA = 0;  */osal_int_disable (Ints_all);  /* * Initialization for board related stuff such as LEDs * Initialize system clock, LEDs */Hal_board_init ();  /* * Make sure supply voltage are high enough to run * check the chip voltage is normal */Zmain_vdd_check ();  Initialize board I/O Initboard (ob_cold); /* * Initialze HAL Drivers * Initialize ADC, DMA, AES, LED, LCD, KEY * UART, SPI and so on have been initialized in this function, but not implemented * hardware related initialization */Haldriverinit ()  ;  /* * Initialize NV System * Initialize Flash, memory */Osal_nv_init (NULL);  /* * Initialize the Mac * initializes the MAC layer */Zmacinit (); /* * Determine the extended address * determines the IEEE 64-bit addresses */zmain_ext_addr (); #if defined zcl_key_establish//undefined//I  Nitialize the Certicom certificate information. Zmain_cert_init (); #endif/* * Initialize basic NV Items * Initialize non-volatile variable */zginit (); #ifndef nonwk//Since the AF is N ' t a task, call it ' s initialization routine afinit (); #endIf <span style= "color: #3333ff;"   ><strong>/* * Initialize the operating system * Initialize OS */Osal_init_system ();</strong></span>   /* * Allow interrupts all interrupts, in fact, the total interrupt is open * #define INTS_ALL 0xFF * Final Call EA = 1;  */osal_int_enable (Ints_all);  /* Final Board Initialization * Initialize button * #define OB_READY 2 */Initboard (Ob_ready);  /* Display information about this device * Print the device information on the LCD (* */Zmain_dev_info ()); /* Display the device info on the LCD */#ifdef lcd_supported//Not defined zmain_lcd_init (); #endif #ifdef WDT_IN_PM1//Not There is a definition/* If WDT is used, which is a good place to enable it. */watchdogenable (WDTIMX); #endif <strong><span style= "color: #3333ff;" >/* * No return from the here * operation of the operating system, will not return after entry */Osal_start_system ();  </span></strong> return 0; Shouldn ' t get here.} Main ()
For an on-chip system, there must be a power supply, crystal/clock, memory and other components, all of our protocol stack must initialize these. As can be seen from the main function, it does initialize the voltage, clock, memory, network, IEEE, System, non-volatile variables and other initialization, which are mainly based on the specific hardware platform. The Zstack protocol stack uses a multi-tasking mechanism and uses polling to perform these tasks. After calling Osal_start_system to start the system, the system begins to poll indefinitely to perform each task. Before we look at how to poll all the tasks after the system starts, let's take a look at the system initialization function Osal_init_system

uint8 Osal_init_system (void) {/* * Initialize the Memory Allocation system * Initialize RAM allocation  */Osal_mem_init ();   /* * Initialize the message queue * Initializes the messages queuing * typedef void * OSAL_MSG_Q_T;   * osal_msg_q_t Osal_qhead;  * Osal_qhead is a void pointer that can point to any type */Osal_qhead = NULL;   /* * Initialize the timers * function has only one statement: Osal_systemclock = 0;   * While the static uint32 Osal_systemclock;  * Initialize timing timing variable to 0 */osaltimerinit ();  /* * Initialize the Power Management System * to initialize the mains management */osal_pwrmgr_init (); <span style= "Background-color:rgb (255, 255, 255); ><span style= "color: #3333ff;"   ><strong>/* * Initialize the system tasks. * Initialize System Tasks */Osalinittasks ();</strong></span></span>/* * Setup efficient search for the first fre   E block of Heap.  * Set a valid search for the first heap of free blocks */Osal_mem_kick (); return (SUCCESS);} 
The initialization of the system is mainly from the operating system level to do the corresponding initialization, such as memory management, power management, Message Queuing and other initialization. The timer initialization is due to the fact that the Zstack-osal system uses timed capture task events. This is primarily the System task initialization function Osalinittask (), which initializes all tasks of the entire system.

void Osalinittasks (void) {uint8 TaskID = 0; 8-bit length variable, task id,0~255/* * Task Event table, global variable * uint16 *tasksevents;   From this definition you can see that taskevents points to a two-dimensional table with a number of 16 columns, a task can have multiple events * Const UINT8 taskscnt = sizeof (Tasksarr)/sizeof (tasksarr[0]); * While Tasksarr is an array of pointers to all task event handlers, that is, how many tasks are there, how many task event handlers, or taskcnt = (UInt16 *) osal_mem_alloc (sizeof (uint1  6) * taskscnt);  /* Initialize the task table all to 0*/osal_memset (tasksevents, 0, sizeof (uint16) * taskscnt));               /* Priority of the task is arranged from high to low, the value of high priority corresponds to TaskID is smaller than/mactaskinit (taskid++);                  MAC layer Task initialization, Mactaskinit (0), do not care, we can not enter this function nwk_init (taskid++);                  NWK Layer Task Initialization, Nwk_init (1), do not care, we can not enter this function hal_init (taskid++); Hal Layer Task initialization, Hal_init (2), #if defined (mt_task)//undefined mt_taskinit (taskid++); #endif Aps_init (taskid+                 + );                APS Layer Task initialization, Aps_init (3), do not care, we can not enter this function # if defined (zigbee_fragmentation)//have a definition, data subcontracting segmentation related apsf_init (taskid++); APSF task Initialization, Apsf_init (4), do not care, we enter notThis function #endif zdapp_init (taskid++); ZdO initialization, Zdapp_init (5), #if defined (zigbee_freq_agility) | |           Defined (ZIGBEE_PANID_CONFLICT)//have defined IP conflict correlation, frequency hopping related zdnwkmgr_init (taskid++);            NWK task Initialization, Zdnwkmgr_init (6) #endif sampleapp_init (TaskID); For this example, the task event initialization can be entered into the function to make the corresponding modification,}
The task initialization function allocates memory space for all tasks, initializes task events to 0, and then initializes each layer of the zstack-osal (Zstack-osal uses layered thinking), as to what each task initialization does, and temporarily does not enter the analysis. Because even based on the analysis of the predecessors, to analyze each task initialization function and in each line of code to add comments do not know, the task is initialized to do something, what the code really does not know. I'll just go in there. Analyze some task initialization code, and analyze it for a few days without knowing why they did it. I don't want you to waste your time analyzing some of the zstack-osal of our learning, and of course, as you learn, you will know what they do and why. Now that the task has been initialized, start the system and return to the main function's Osal_start_system start system function

/* * This is the main function of the task system polling. He will look for the event that occurred and then invoke the appropriate event execution function. * If no event registration is to occur, then go to sleep mode. This function is never returned. */void Osal_start_system (void) {#if!defined (zbit) &&!defined (ubit)  //are not defined, all enter for Dead loop for  (;;)  //Forever loop to query task events using the polling method #endif  {    /* As execution statement for the For Dead loop, execute system *    /Osal_run_system ();  }}
This for Dead loop, each loop executes the Oasl_run_system function at a time, to see how the Oasl_run_system function is the polling method

void Osal_run_system (void) {uint8 idx = 0;  /* Scan which task event is triggered, then put the corresponding flag 1*/osaltimeupdate ();  Hal_processpoll ();    /* The highest priority task sequentially determines if there is an event trigger for each task, if it jumps out of the loop */do {if (Tasksevents[idx])//task is highest. {break;//gets the highest priority task index number to be processed IDX}}    while (++idx < taskscnt);    /* If the IDX is less than the number of all tasks, it indicates that a task event occurred */if (IDX < taskscnt) {uint16 events;    halintstate_t intstate;    Hal_enter_critical_section (intstate);         events = Tasksevents[idx];  Save the triggered (active) task event Tasksevents[idx] = 0;    Clear the events for this task. Clears the event hal_exit_critical_section (intstate)              Activetaskid = idx;  Save the ID number of the active task event = (Tasksarr[idx]) (IDX, events); Executes the corresponding task event handler function, Tasksarr[idx] is a function pointer activetaskid = Task_no_task;    #define Task_no_task 0xFF hal_enter_critical_section (intstate);  TASKSEVENTS[IDX] |= events;  Add back unprocessed events to the current task. Add an event that is not processed later to Hal_exit_critical_section (Intstate) in the active event task; } #if Defined (power_saving)//Do not define else//complete pass through all task events with no activity?  {Osal_pwrmgr_powerconserve (); Put the Processor/system into sleep} #endif/* Yield in case cooperative scheduling is being used.  */#if defined (configuse_preemption) && (configuse_preemption = = 0)//no definition {Osal_task_yield (); } #endif}
The Zstack-osal system uses a timed task (osaltimerrec_t) list to string all tasks together, and timerhead points to the list header. Osal_run_system in Osaltimeupdate () is to determine whether the task timing is up to, and then set the task event for the corresponding task. When this task sets a task event, the correspondingTasksevents[idx] is not 0, which is used in the above do while () loop to exit and obtain the ID of the corresponding task, at which time the IDX is the ID of this task. Last Callevents = (Tasksarr[idx]) (IDX, events); The Task event handler function corresponding to the task, Tasksarr is an array of function pointers, each of which points to a task event handler function. From here we can see that we are using the task ID number to find the corresponding task event handler, so the order of all task event handlers is consistent with the order of all task initialization. That is , tasksarr the order of the members of this function pointer array, in accordance with the order of the task initialization in Oaslinittasks, given below Tasksarr

typedef unsigned short (*PTASKEVENTHANDLERFN) (unsigned char task_id, unsigned short event);

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

Summary :

Zstack-osal All task events are represented by a two-dimensional table, each row of the two-dimensional table represents a task, and the columns of the two-dimensional table represent the events of the corresponding task. With the tasksevents pointer pointing to this two-dimensional table,tasksevents is defined in OSAL_SAMPLEAPP.C as follows:

Const UINT8 taskscnt = sizeof (Tasksarr)/sizeof (tasksarr[0]); UInt16 *tasksevents;
From here, we can also see how many tasks the whole system has by calculating the number of task event handler functions. TASKSCNT represents the total number of system tasks.

The tasksevents pointer points to the two-dimensional table initialization, which is initialized in the task initialization function Osalinittasks, as follows:

Tasksevents = (UInt16 *) osal_mem_alloc (sizeof (uint16) * taskscnt);  /* Initialize the task table all to 0*/osal_memset (tasksevents, 0, sizeof (uint16) * taskscnt));

Once the task is timed to the time, the task is set to have an event (set the appropriate event), the task with the event Tasksevents[idx] is not 0, the task ID number to find the corresponding task event handler function to handle the tasks. (Of course, the number of tasks above is not necessarily 14, just to illustrate the problem, the previous 0~13 refers to the task ID number).















ZSTACK-CC2530-2.5.1A Main Code Analysis summary--understand Zstack-osal's principle and thought

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.