ucos-ii study notes-the principle and use of message queues

Publisher:春林初盛Latest update time:2015-08-18 Source: eefocusKeywords:ucos-ii Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Created  on:  2012-10-7

      Author:  zhang  bin

 

Study Notes

for  ucos-ii   PC

redesigned  by  zhang  bin

2012-10-7

versions : V-0.1

All  Rights  Reserved
 

 

 

 

#include  "includes.h"

 

#define   TASK_STK_SIZE                  512       

#define   N_MESSAGES                     128       

 

OS_STK         StartTaskStk[TASK_STK_SIZE];

OS_STK         MyTaskStk[TASK_STK_SIZE];

OS_STK         YouTaskStk[TASK_STK_SIZE];

 

char  *s_flag;    //This string indicates which task is running

//char  *s_1;  

 

char           *ss;   //Store the received message pointer

 

char           *s100;  //Store the pointer to send message

char           *s;

char           *s500;

void           *MsgGrp[N_MESSAGES];     //Define message pointer array

//To create a message queue, you first need to define a pointer array (for storing message mailboxes), and then store the first address of each message data buffer into this array

//Finally call the function OSQCreate() to create a message queue

 

INT8U          err;

INT8U          y=0;

 

OS_EVENT       *Str_Q;                 //Define the event control block pointer  The event control block pointer of the queue  is used to store the pointer of the created message queue

 

void   MyTask(void  *data);                     

void   StartTask(void  *data);       

void   YouTask(void  *data); 

 

 

 

void   main  (void)

{

    OSInit();                                              

    PC_DOSSaveReturn();                                    

    PC_VectSet(uCOS,  OSCtxSw);                             

    

Str_Q=OSQCreate(&MsgGrp[0],N_MESSAGES);                 //Create a message queue

//The first parameter of the function &MsgGrp[0] is void  **start, which is the address of the message buffer pointer array. It is a pointer to the pointer array.

//It can be represented by the address of the first element of the pointer array

//N_MESSAGES is the size of the array

//The return value is the pointer to the message queue.    Str_Q is a pointer of type OS_EVENT, which is a pointer to the event control block.

 

    OSTaskCreate(StartTask,  (void  *)0,  &StartTaskStk[TASK_STK_SIZE  1],  0);

    OSStart();                                             

}

 

 

 

void   StartTask  (void  *pdata)

{

#if  OS_CRITICAL_METHOD  ==                                 

    OS_CPU_SR   cpu_sr;

#endif

 

    INT16S      key;

 

    pdata  pdata;     

 

    OS_ENTER_CRITICAL();

    PC_VectSet(0x08,  OSTickISR);                           

    PC_SetTickRate(OS_TICKS_PER_SEC);                      

    OS_EXIT_CRITICAL();

    OSStatInit();                                          

    

    OSTaskCreate(MyTask,  (void  *)0,  &MyTaskStk[TASK_STK_SIZE  1],  3);

    OSTaskCreate(YouTask,  (void  *)0,  &YouTaskStk[TASK_STK_SIZE  1],  4);

    

   //  s="How  many  strings  could  be  got?";

    //OSQPostFront(Str_Q,s);       //Send messages  in LIFO mode

    //The first parameter Str_Q is the pointer to the message queue, which is the return value of OSQCreate, and the second parameter s is the message pointer

    

    for  (;;) 

    {

     s_flag="The  StartTask  is  running!";

     PC_DispStr(50,++y,  s_flag,  DISP_FGND_RED  DISP_BGND_LIGHT_GRAY);    //Prompt which task is running

    

        if(OSTimeGet()>100&&OSTimeGet()<500)

        {

         s100="The  value  of  OSTIME  is  from  100  to  500  NOW!!";

         OSQPostFront(Str_Q,s100);    //Send messages  in LIFO mode

         //Send messages  in LIFO mode

           //The first parameter Str_Q is the pointer to the message queue, which is the return value of OSQCreate, and the second parameter s is the message pointer

         s="The  string  belongs  to  which  task."; 

         OSQPostFront(Str_Q,s);       //Send messages  in LIFO mode   , so if you want to apply for a message, you will get s first, then s100

        }

 

if(OSTimeGet()>1000&&OSTimeGet()<1500)

        {

         s500="The  value  of  OSTIME  is  from  1000  to  1500  NOW!!";

         OSQPostFront(Str_Q,s500);    //Send message

        }

        

        if  (PC_GetKey(&key)  ==  TRUE)   

                          

            if  (key  ==  0x1B) 

                     

                PC_DOSReturn();          

            }

        }

 

        OSTimeDlyHMSM(0,  0,  1,  0);        

    }

}

 

 

 

void   MyTask(void  *pdata)

{

#if  OS_CRITICAL_METHOD  ==                                 

    OS_CPU_SR   cpu_sr;

#endif

    pdata=pdata;

    for  (;;)

                

     s_flag="The  MyTask  is  running!";

     PC_DispStr(50,++y,  s_flag,  DISP_FGND_RED  DISP_BGND_LIGHT_GRAY);    //Prompt which task is running

    

     ss=OSQPend(Str_Q,0,&err);     //Request message queue, the parameters are: Str_Q is the pointer of the requested message queue,   the second parameter is the waiting time

     //0 means infinite waiting, &err is the error message, and the return value is the message pointed to by the OSQOut member of the queue control block OS_Q (if there is a message available in the queue).

     //No message is available, so the task that calls OSQPend is suspended, put into a waiting state, and triggers a task scheduling

     //Because the LIFO method was used when sending messages before, the first message received here is the last message sent above

    

     PC_DispStr(3,y,  ss,  DISP_FGND_BLACK  DISP_BGND_LIGHT_GRAY);   //Display the received message

     //s_1="M";

     PC_DispStr(0,y,"My",DISP_FGND_RED  DISP_BGND_LIGHT_GRAY);   //Show which task is displayed

    

     OSTimeDlyHMSM(0,  0,  1,  0);  

    }

}

 

void   YouTask(void  *pdata)

{

#if  OS_CRITICAL_METHOD  ==                                 

    OS_CPU_SR   cpu_sr;

#endif

    pdata=pdata;

    for  (;;)

                

     s_flag="The  YouTask  is  running!";

     PC_DispStr(50,++y,  s_flag,  DISP_FGND_RED  DISP_BGND_LIGHT_GRAY);    //Prompt which task is running

    

     ss=OSQPend(Str_Q,0,&err);     //Request message queue

    

     PC_DispStr(3,y,  ss,  DISP_FGND_BLACK  DISP_BGND_LIGHT_GRAY);   //Display the received message

     //s_1="Y";

     PC_DispStr(0,y,"You",DISP_FGND_RED  DISP_BGND_LIGHT_GRAY);   //Show which task is displayed

    

     OSTimeDlyHMSM(0,  0,  1,  0);  

    }

}

 

//The running phenomenon shows that the above analysis is correct, because when the clock beat number is greater than 100 and less than 500, the two strings s100 and s100 in the first if statement will be sent.

//The task that runs below receives and displays it. When the clock beat number is greater than 1000 and less than 1500, the string in the second if statement is sent. The task that runs below

//Receive and display. When the clock beat number is greater than 1500, no more messages are sent. The following tasks will wait indefinitely if they cannot get any messages, so they will not be displayed.

//It is not difficult to see from the running phenomenon that sometimes MyTask or YouTask runs, but no message is received and it is in a waiting state

//Using the above method, we can clearly see the relationship between task scheduling and running.  MyTask and YouTask  are run alternately because the delay time is equal.

Keywords:ucos-ii Reference address:ucos-ii study notes-the principle and use of message queues

Previous article:ucos-ii study notes - the principle and use of semaphore set (event flag group)
Next article:ucos-ii study notes-the principle and use of message mailbox

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号