ucos-ii example 7: memory management test

Publisher:MysticDreamerLatest update time:2016-12-18 Source: eefocusKeywords:ucos-ii Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

environment:

Host: WIN8

Development environment: MDK4.72

ucgui version: 3.90

ucos version: ucos-ii

mcu: stm32f103VE


illustrate:

In this example, task2 requests a memory partition, writes data to the partition, and then sends the data to task1 via email.


Effect picture:


Source code:


  1. #define TASK_STK_SIZE       512  

  2.                   

  3. /********************************************************************* 

  4. * Static functions 

  5. **********************************************************************/  

  6.   

  7. static void task_start(void *pdata);  

  8. static void task1(void *pdata);  

  9. static void task2(void *pdata);  

  10.   

  11. /********************************************************************* 

  12. * Static variables 

  13. **********************************************************************/  

  14.   

  15. /********************************************************************* 

  16. * Task stack space 

  17. **********************************************************************/  

  18.   

  19. static OS_STK Task_Start_Stk[TASK_STK_SIZE];  

  20. static OS_STK Task1_Stk[TASK_STK_SIZE];  

  21. static OS_STK Task2_Stk[TASK_STK_SIZE];  

  22.   

  23. /********************************************************************* 

  24. * signal 

  25. **********************************************************************/  

  26.   

  27. OS_EVENT *Semaphore;  

  28.   

  29. /********************************************************************* 

  30. * Message queue array 

  31. **********************************************************************/  

  32.   

  33. void *Msg_Group[10];  

  34.   

  35. char Msg_Arr[10][30];  

  36. uint8_t Index_Msg_Arr = 0;  

  37.   

  38. /********************************************************************* 

  39. * Memory partition 

  40. **********************************************************************/  

  41.   

  42. OS_MEM *Buffer_Ptr;  

  43. uint8_t Buffer[10][30];  

  44.   

  45. /********************************************************************* 

  46. * Functions 

  47. **********************************************************************/  

  48.   

  49. int main(void)  

  50. {     

  51.     // Initialize the kernel  

  52.     OSInit();  

  53.     //Create a task  

  54.     OSTaskCreate(task_start,(void *)0,Task_Start_Stk + TASK_STK_SIZE - 1,10);  

  55.       

  56.     //Start the task  

  57.     OSStart();  

  58. }  

  59.   

  60. /********************************************************************* 

  61. * Task function 

  62. **********************************************************************/  

  63.   

  64. static void task_start(void *pdata)  

  65. {     

  66.     uint8_t err = 0;  

  67.       

  68.     //Load the world  

  69.     world_load();  

  70.       

  71.     //Set the background color  

  72.     GUI_SetBkColor(0xb07c17);  

  73.     GUI_Clear();  

  74.     GUI_SetColor(GUI_BLACK);  

  75.       

  76.     //Create a new mailbox  

  77.     Semaphore = OSMboxCreate((void *)0);  

  78.       

  79.     //Create memory partition  

  80.     Buffer_Ptr = OSMemCreate(Buffer,10,30,&err);  

  81.       

  82.     //Create a new task  

  83.     OSTaskCreate(task1,(void *)0,Task1_Stk + TASK_STK_SIZE - 1,11);  

  84.     OSTaskCreate(task2,(void *)0,Task2_Stk + TASK_STK_SIZE - 1,12);  

  85.       

  86.     while(1)  

  87.     {  

  88.         OSTimeDlyHMSM(0,0,3,0);  

  89.     }  

  90. }  

  91.   

  92. static void task1(void *pdata)  

  93. {  

  94.     char str[30] = {0};  

  95.     uint8_t err = 0;  

  96.     uint8_t *mail;  

  97.   

  98.     while(1)  

  99.     {  

  100.         //Request semaphore  

  101.         mail = OSMboxPend(Semaphore,0,&err);  

  102.         //show  

  103.         sprintf(str,"task1 receive:%d,%d,%d",mail[0],mail[1],mail[2]);  

  104.         GUI_DispStringHCenterAt(str, 120, 40);  

  105.           

  106.         //show  

  107.         sprintf(str,"task1 time:%ds",OSTimeGet());  

  108.         GUI_DispStringHCenterAt(str, 120, 5);  

  109.         WM_Exec();  

  110.         OSTimeDlyHMSM(0,0,1,0);  

  111.     }  

  112. }  

  113.   

  114. static void task2(void *pdata)  

  115. {  

  116.     char str[30] = {0};  

  117.     uint8_t err = 0;  

  118.     uint8_t *ptr;  

  119.     static uint8_t a = 0;  

  120.   

  121.     while(1)  

  122.     {  

  123.         // Request memory partition  

  124.         ptr = OSMemGet(Buffer_Ptr,&err);  

  125.           

  126.         //Assignment  

  127.         ptr[0] = a++;  

  128.         ptr[1] = a++;  

  129.         ptr[2] = a++;  

  130.         //Send via email  

  131.         OSMboxPost(Semaphore,ptr);  

  132.           

  133.         //Release the memory partition  

  134.         OSMemPut(Buffer_Ptr,ptr);  

  135.           

  136.         //show  

  137.         sprintf(str,"task2 time:%ds",OSTimeGet());  

  138.         GUI_DispStringHCenterAt(str, 120, 20);  

  139.         WM_Exec();  

  140.         OSTimeDlyHMSM(0,0,3,0);  

  141.     }  

  142. }  



Keywords:ucos-ii Reference address:ucos-ii example 7: memory management test

Previous article:Porting emwin on STM32F407
Next article:ucos-ii example 6: message volume set test

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号