ZigBee protocol stack Kernel analysis-key analysis

Source: Internet
Author: User

ZigBee protocol stack Kernel Analysis-key analysis (polling)

Jesse

Protocol stack Key Code analysis-Mind map (enlarge to see)

First, the summary

Starting with the main () function of the protocol stack, ROM lists the functions called in the main () function. Next we will have an analysis of the function, there is a function about key I will be in a yellow box.

II. Protocol stack Code analysis (according to stepwise analysis)

1,osal_int_disable (Ints_all); Shutdown Total Interrupt


2. Haldriverinit ();

Call Halkeyinit (); Key initialization function

Halkeyinit () {  ...  /*initialize callback function */phalkeyprocessfunction  = NULL;        Initialize the key callback function to null   /*start with key was not configured */halkeyconfigured = FALSE;                }

3,Osal_init_system (); System initialization function

Osal_init_system (); Call Osalinittasks (); Initialize the task;

Osalinittasks () {  ...  Sampleapp_init (TaskID);            The user initializes the task at the application-level customization. }sampleapp_init (TaskID) {  ...  Registerforkeys (sampleapp_taskid);  ......}

Registerforkeys (Sampleapp_taskid) is called in the custom task initialization; , inside the Registeredkeystaskid = task_id; , which is key to registering a user-defined Sampleapp_task task as a keystroke task.

4,osal_int_enable (Ints_all); To enable a total interruption


5. Initboard (Ob_ready);

Called the Halkeyconfig (Hal_key_interrupt_disable,onboard_keycallback); To configure the keys.

Halkeyconfig (hal_key_interrupt_disable,onboard_keycallback) {   ...   (void) osal_set_event (Hal_taskid, hal_key_event);    With tasksevents[task_id] |= Event_flag; To inform the Hal_task task of the message, so that the Hal_task task into readiness State ...   Phalkeyprocessfunction = Cback;        Assign the Onboard_keycallback function to the key callback function pointer}


6, the last is to enter our Osal_start_system (); Operating system, infinite loop.

The first thing to do is first:

  do{   if (Tasksevents[idx])      //Task is highest priority, that is, ready.    {Break     ;    }  } while (++idx < taskscnt);

To inquire whether a task has entered a readiness state. We go back to the 5th step, at the 5th step when there are calls to Osal_set_event (hal_taskid,hal_key_event); Set a message to the Hal_task task so that the Hal_task task enters the readiness state.

So the next protocol stack is going to execute:

if (idx < taskscnt)       //If the task is registered with  {   uint16 events;   halintstate_t intstate;   Hal_enter_critical_section (intstate);   events = Tasksevents[idx];   TASKSEVENTS[IDX] = 0;  Clearthe Events for this task.   Hal_exit_critical_section (intstate);   Activetaskid = idx;   Events = (Tasksarr[idx]) (IDX, events);       This sentence calls the processing function of the key   activetaskid = Task_no_task;   Hal_enter_critical_section (intstate);   TASKSEVENTS[IDX] |= events;  Add back unprocessed events to the current task.   Hal_exit_critical_section (intstate);  }

Events = (Tasksarr[idx]) (IDX, events); This sentence invokes the handler function of the Hal_task. Let's go into tasksarr[idx] and see, with the Hal_task task corresponding to the Hal_processevent hardware abstraction Layer event handler, let's go inside and see.

UInt16 hal_processevent (uint8 task_id,uint16 events) {   ...   #if (defined Hal_key) && (Hal_key = = TRUE)/   * Check for Keys *   /Halkeypoll ();   /* If interrupt disabled, do next polling *   /if (! hal_keyintenable)    {     Osal_start_timerex (Hal_taskid, hal_key_event, +);    }   #endif//Hal_key ...   }

Let's take a look at halkeypoll (); function:

Halkeypoll () {  ...  if (Newkeys && phalkeyprocessfunction)  {   (phalkeyprocessfunction) (Newkeys, Hal_key_state_normal) ;  Called the key handling callback function  }  ...}

The keystroke polling function is called (phalkeyprocessfunction) (newkeys,hal_key_state_normal); Press the key to process the callback function to handle the key event, so where does the function pointer point? Let's go back and look at the two places where the callback function pointer is assigned.

(1) at the 2nd step, phalkeyprocessfunction = NULL; Initialize the key callback function pointer to null;

(2) in the 5th step, call the Halkeyconfig (hal_key_interrupt_disable, onboard_keycallback); The Onboard_keycallback function assigns a value to the key callback function pointer.

So now let's go find Onboard_keycallback () and see how it handles key events.

Onboard_keycallback () {   ...   if (Onboard_sendkeys (keys, shift)! = zsuccess)   ...}

The Onboard_sendkeys () function sends a message to the Registeredkeystask task.

Onboard_sendkeys () {  ...  if (registeredkeystaskid! = no_task_id)  {   //Send the address to the   TASK msgptr = (keychange_t *) Osal_msg_al Locate (sizeof (keychange_t));   if (msgptr)    {     msgptr->hdr.event = Key_change;     Package the message     msgptr->state = State;     Msgptr->keys = keys;     Send message to key task     Osal_msg_send (Registeredkeystaskid, (uint8 *) msgptr);    }   return (zsuccess);  }  ......}

Looking at the code above, the stack first packages the message and then sends the message to the Registeredkeystask task (that is, the user-defined Sampleapp_task task) through the Osal_msg_send () function.

Let's go inside the Osal_msg_send () function to see how it sends the message.

Osal_msg_send () {  ...  Queue message  osal_msg_enqueue (&osal_qhead, msg_ptr);  Signal the task, a message is waiting Osal_set_event (Destination_task, sys_event_msg);  ......}

Looking at the code above, I listed the two most important sentences:

The first is Osal_msg_enqueue (&osal_qhead, msg_ptr); To put the message in the message queue;

Finally, sure enough to use our osal_set_event (Destination_task, sys_event_msg); function to notify the task that a message has been sent! This time is to inform that task, let us go back to see Destination_task refers to what task. It is the 6th step of the key callback function to look inside to see osal_msg_send (Registeredkeystaskid, (uint8 *) msgptr), so the notification is Registeredkeystask tasks, which are user-defined Sampleapp_task

Task.

Then Osal_run_system () went into

  do{   if (Tasksevents[idx])  //Task ishighest priority.    {Break     ;    }  } while (++idx < taskscnt);

At this time, osal_msg_send (Registeredkeystaskid, (uint8 *) msgptr); Blessing, our Sampleapp_task finally entered the state of readiness.

Then the next events = (Tasksarr[idx]) (IDX, events), we enter our user-defined sampleapp_processevent processing function.

Sampleapp_processevent () {   if (events & Sys_event_msg)  {    msgpkt = (afincomingmsgpacket_t*) osal_msg_ Receive (Sampleapp_taskid);    while (MSGPKT)    {      switch (msgpkt->hdr.event)      {        //Received If a key is pressed case        Key_c Hange:          Sampleapp_handlekeys (((keychange_t*) msgpkt)->state, (((keychange_t *) msgpkt)->keys);          break;        ... ...}

Then we enter the Sampleapp_handlekeys key processing function!

In this way, the button starts from the protocol stack system, and the process of performing a user-defined key handling function is all over!
Then is not the button to end this way, then how to call polling it?!!!

Let's take a look back to the 6th step of the hal_processevent processing function, which in addition to call the key polling function, but also called the Osal_start_timerex (Hal_taskid, hal_key_event, 100); , this function is 100ms again let Hal_taskid task into the ready state, at this time will be infinite loop repeat the 6th step, so as to achieve the function of polling!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZigBee protocol stack Kernel analysis-key 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.