uCOS-Ⅱ message queue

 1 /*****************************************************************************************************************************
  2 *
  3 * File name: main.c
  4 * File function: main function
  5 * File description: none
  6 *
  7 *****************************************************************************************************************************/
  8 /**********************************************************
  9 *
 10 * Header file declaration
 11 *  
 12 **********************************************************/
 13 
 14 #include "pbdata.h"
 15 
 16 
 17 
 18 
 19 
 20 
 21 /**********************************************************
 22 *
 23 * Call function declaration
 24 *  
 25 **********************************************************/
 26 void RCC_Configuration(void);//Clock configuration function
 27 
 28 void  start_task(void *pdata);//Start task function
 29 
 30 
 31 
 32 
 33 
 34 /**********************************************************
 35 *
 36 * Main function
 37 *  
 38 **********************************************************/
 39 int main(void)
 40 {
 41     Delay_Init();//Delay function initialization
 42     
 43     RCC_Configuration();//Clock configuration
 44     
 45     LED_GPIO();//LED GPIO pin configuration
 46     
 47 
 48     
 49 /*Operating system processing*/
 50     OSInit();//Initialize UCOS operating system
 51     led1_MsgQueue=OSQCreate(&MsgQueuetb[0],20);//Create a message queue
 52     OSTaskCreate(
 53               start_task,  //Pointer to task code
 54               (void *)0,    //When the task starts to execute, the pointer passed to the task parameter
 55               (OS_STK *) & START_TASK_STK[START_STK_SIZE-1],//The top pointer assigned to the task stack
 56               START_TASK_PRIO//Priority assigned to the task
 57              );
 58     OSStart();//Start the OS operating system
 59     
 60     
 61     
 62     
 63 }
 64 
 65 
 66 
 67 /**********************************************************
 68 *
 69 * Function name: RCC_Configuration(void)
 70 * Function: clock configuration
 71 * Input parameters: none
 72 * Output parameters: none
 73 * Function description: none
 74 *  
 75 **********************************************************/
 76 void  RCC_Configuration(void)
 77 {
 78     SystemInit();//system initialization
 79     
 80     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//LED1 corresponds to GPIO clock enable
 81     
 82     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//Button corresponding to GPIO clock enable
 83 }
 84 
 85 
 86 
 87 
 88 
 89 
 90 /**********************************************************
 91 *
 92 * Function name: start_task(void *pdata)
 93 * Function function: start task
 94 * Input parameters: *pdata
 95 * Output parameters: none
 96 * Function description: none
 97 *  
 98 **********************************************************/
 99 void  start_task(void *pdata)
100 {
101      u8  led1[2]={0x00,0x64};//100ms
102    u8  led2[2]={0x01,0xF4};//500ms
103     
104    OS_CPU_SR  cpu_sr=0;
105     
106    pdata=pdata;//Prevent the compiler from reporting errors
107     
108    OSStatInit();//Initialize statistics task
109     
110    OS_ENTER_CRITICAL();//Enter the critical section, interruption cannot be interrupted
111     
112 /*Create task*/ 
113    /*LED1 task*/    
114    OSTaskCreate(
115               led1_task,  //Pointer to task code
116               (void *)0,    //When the task starts to execute, the pointer passed to the task parameter
117               (OS_STK *) & LED1_TASK_STK[LED1_STK_SIZE-1],//The top pointer assigned to the task stack
118               LED1_TASK_PRIO//Priority assigned to the task
119                );
120 
121    //OSTaskSuspend(START_TASK_PRIO);//Suspend start task
122     
123    OS_EXIT_CRITICAL();//Exit the critical section, can be interrupted by interrupt
124     
125      while(1)
126    {
127          
128 
129             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
130             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
131             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
132             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
133             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
134             OSQPost(led1_MsgQueue, (void *)&led2[0]);//send
135 
136 
137             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
138             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
139             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
140             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
141             OSQPost(led1_MsgQueue, (void *)&led1[0]);//send
142             OSQPost(led1_MsgQueue, (void *)&led2[0]);//send
143 
144 
145             delay_ms(1000);
146             delay_ms(1000);
147             delay_ms(1000);
148             delay_ms(1000);
149             delay_ms(1000);
150 
151      
152    }
153      
154 }
1 /*****************************************************************************************************************************
  2 *
  3 * File name: pbdata.c
  4 * File function: custom function
  5 * File description: none
  6 *
  7 *****************************************************************************************************************************/
  8 
  9 
 10 
 11 #include "pbdata.h"
 12 
 13 u8 dt=0;
 14 static u8  fac_us=0;//us delay multiplier
 15 static u16 fac_ms=0;//ms delay multiplier
 16 
 17 OS_EVENT* led1_MsgQueue;//Define a message queue pointer declaration for a mailbox
 18 void* MsgQueuetb[20];//Define a message queue array declaration for a mailbox
 19 
 20 /**********************************************************
 21 *
 22 * Function name: Delay_Init(void)    
 23 * Function: Initialize the delay function
 24 * Input parameters: none
 25 * Output parameters: none
 26 * Function description: When using ucos, this function will initialize the clock beat of ucos
 27 * The clock of SYSTICK is fixed at 1/8 of the HCLK clock
 28 * SYSCLK: system clock
 29 *  
 30 **********************************************************/
 31 void Delay_Init(void)     
 32 {
 33     u32 reload;
 34     SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);    //Select external clock, HCLK/8
 35     fac_us=SystemCoreClock/8000000;    //1/8 of the system clock
 36      
 37     reload=SystemCoreClock/8000000;    //Number of counts per second, in K   
 38     reload*=1000000/OS_TICKS_PER_SEC;//Set overflow event according to OS_TICKS_PER_SEC, reload is a 24-bit register, maximum value: 16777216, at 72M, it is about 1.86s
 39                                 
 40     fac_ms=1000/OS_TICKS_PER_SEC;//  Represents the smallest unit that ucos can delay
 41     SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;       //Enable SYSTICK interrupt
 42     SysTick->LOAD=reload;     //Interrupt every 1/OS_TICKS_PER_SEC second
 43     SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;       //Turn on SYSTICK    
 44 }                                    
 45 
 46 
 47 
 48 
 49 
 50 /**********************************************************
 51 *
 52 * Function name: delay_us(u32 nus)
 53 * Function: Delay microsecond subroutine
 54 * Input parameters: nus, delay in microseconds
 55 * Output parameters: none
 56 * Function description: none
 57 *            
 58 **********************************************************/
 59 void delay_us(u32 nus)
 60 {        
 61     u32 ticks;
 62     u32 told,tnow,tcnt=0;
 63     u32 reload=SysTick->LOAD;    //LOAD value      
 64     ticks=nus*fac_us;             //Number of beats required         
 65     tcnt=0;
 66     told=SysTick->VAL;            //Counter value when first entered
 67     while(1)
 68     {
 69         tnow=SysTick->VAL;    
 70         if(tnow!=told)
 71         {        
 72             if(tnow<told)tcnt+=told-tnow;//Note here that SYSTICK is a decrementing counter.
 73             else tcnt+=reload-tnow+told;        
 74             told=tnow;
 75             if(tcnt>=ticks)break;//If the time exceeds/equal to the event to be delayed, then exit
 76         }  
 77     };                                         
 78 }
 79 
 80 
 81 
 82 
 83 
 84 
 85 /**********************************************************
 86 *
 87 * Function name: delay_ms(u16 nms)
 88 * Function: Delay millisecond subroutine
 89 * Input parameters: nms, delay in microseconds
 90 * Output parameters: none
 91 * Function description: none
 92 *            
 93 **********************************************************/
 94 void delay_ms(u16 nms)
 95 {    
 96     if(OSRunning==TRUE)//If os is already running   
 97     {          
 98         if(nms>=fac_ms)//Delayed events are greater than the minimum time period of ucos
 99         {
100                OSTimeDly(nms/fac_ms);//ucos delay
101         }
102         nms%=fac_ms;                //UCOS has been unable to provide such a small delay, use the normal way to delay    
103     }
104     delay_us((u32)(nms*1000));    //Delay in normal mode, ucos cannot start scheduling at this time
105 }
106 
107 
108 
109 
110 /**********************************************************
111 *
112 * Function name: delay(u32 nCount)
113 * Function function: ordinary inaccurate delay
114 * Input parameters: nCount, delay time
115 * Output parameters: none
116 * Function description: none
117 *            
118 **********************************************************/
119 void delay(u32 nCount)
120 {
121     for(;nCount!=0;nCount--);
122 }
1 /*****************************************************************************************************************************
 2 *
 3 * File name: pbdata.h
 4 * File function: header file declaration of custom function
 5 * File description: none
 6 *
 7 *****************************************************************************************************************************/
 8 
 9 
10 #ifndef _pbdata_H
11 #define _pbdata_H
12 
13 
14 
15 
16 
17 
18 /**********************************************************
19 *
20 * Header files that need to be imported
21 *  
22 **********************************************************/
23 #include "stm32f10x.h"
24 #include "includes.h"
25 #include "misc.h"
26 
27 #include "led.h"//LED related header files
28 #include "key.h"//key related header files
29 
30 
31 /**********************************************************
32 *
33 * Task related settings
34 *  
35 **********************************************************/
36 /*Start task*/
37     #define  START_TASK_PRIO   9  //Set task priority
38     #define  START_STK_SIZE    64  //Space size=64*4 (bytes)
39     static OS_STK  START_TASK_STK[START_STK_SIZE];//Create task stack space
40 
41 
42 /*LED1 task*/
43     #define  LED1_TASK_PRIO   6  //Set task priority
44     #define  LED1_STK_SIZE    64  //Space size=64*4 (bytes)
45     static OS_STK   LED1_TASK_STK[LED1_STK_SIZE];//Create task stack space
46 
47 
48 
49 
50 
51 
52 /**********************************************************
53 *
54 * External variable declaration
55 *  
56 **********************************************************/
57 
58 extern OS_EVENT* led1_MsgQueue;//Define a message queue for a mailbox
59 extern  void* MsgQueuetb[20];//Define a message queue array for mailbox
60 
61 
62 /**********************************************************
63 *
64 * Sub-function program declaration
65 *  
66 **********************************************************/
67 void delay(u32 nCount);//Ordinary imprecise delay
68 void Delay_Init(void);//Delay function initialization
69 void delay_us(u32 nus);//Microsecond delay
70 void delay_ms(u16 nms);//Millisecond delay
71 
72 
73 
74 
75 #endif

1 /*****************************************************************************************************************************
 2 *
 3 * File name: led.c
 4 * File function: led related functions
 5 * File description: none
 6 *
 7 *****************************************************************************************************************************/
 8 
 9 
10 
11 
12 #include "pbdata.h"
13 
14 
15 
16 /**********************************************************
17 *
18 * Function name: LED_GPIO(void)
19 * Function: GPIO configuration of LED
20 * Input parameters: none
21 * Output parameters: none
22 * Function description: none
23 *  
24 **********************************************************/
25 
26 void LED_GPIO(void)//GPIO initialization function
27 {
28     GPIO_InitTypeDef GPIO_InitStructure;//Define a structure variable for GPIO settings 
29     
30 /*LED1 configuration*/
31     /*Structure variable assignment*/
32     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;Pin configuration
33     GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//Configure frequency
34     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//Push-pull output
35 /*Corresponding GPIO initialization*/
36   GPIO_Init(GPIOB,&GPIO_InitStructure);
37 }
38 
39 
40 
41 
42 
43 
44 
45 /**********************************************************
46 *
47 * Function name: led1_task(void *pdata)
48 * Function: led1 task processing function
49 * Input parameters: *pdata
50 * Output parameters: none
51 * Function description: none
52 *  
53 **********************************************************/
54 
55 void led1_task(void *pdata)
56 {
57     INT8U err;
58     u16 led1_timer=500;
59     u8 *msg;
60     pdata=pdata;
61 
62 
63     while(1)
64     {
65         msg=(u8 *)OSQPend(led1_MsgQueue,0,&err);//Wait for the message queue to get the delay time
66 
67         if(err==OS_ERR_NONE)
68         {
69             led1_timer=(msg[0]<<8)+msg[1];
70 
71             if(led1_timer>1000) led1_timer=1000;
72             if(led1_timer<100) led1_timer=100;
73         }
74 
75         GPIO_SetBits(GPIOB,GPIO_Pin_5);
76         delay_ms(led1_timer);//
77         GPIO_ResetBits(GPIOB,GPIO_Pin_5);
78         delay_ms(led1_timer);//
79     }
80 }

1 /*****************************************************************************************************************************
 2 *
 3 * File name: led.h
 4 * File function: header file of led related functions
 5 * File description: none
 6 *
 7 *****************************************************************************************************************************/
 8 
 9 
10 #ifndef _LED_H
11 #define _LED_H
12 
13 #include "pbdata.h"
14 
15 
16 void LED_GPIO(void);//GIPO pin configuration of LED
17 
18 void  led1_task(void *pdata);//LED1 task
19 
20 
21 
22 
23 #endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UCOS-Ⅱ是嵌入式实时操作系统,它适用于各种嵌入式系统的移植和应用。在MC9S12XS128上,UCOS-Ⅱ的移植需要做以下几个步骤。 首先,需要根据MC9S12XS128的处理器架构和硬件资源进行配置。UCOS-Ⅱ提供了针对不同处理器架构的移植文件,可以根据MC9S12XS128的特点进行选择。配置包括时钟源、中断处理等设置,确保UCOS-Ⅱ能够正确运行。 其次,需要将UCOS-Ⅱ的源代码添加到工程中进行编译。UCOS-Ⅱ的源代码可以从官方网站上获得。在编译过程中,需要根据实际情况选择适合MC9S12XS128的编译选项和链接脚本。确保编译生成与MC9S12XS128兼容的可执行文件。 接下来,需要根据具体应用的需求进行UCOS-Ⅱ的配置。UCOS-Ⅱ提供了丰富的配置选项,可以根据应用的实际需求进行选择。配置包括任务数量、任务优先级、任务堆栈大小等参数的设置。通过这些配置,可以实现对系统行为的精确控制。 最后,需要在应用程序中调用UCOS-Ⅱ的相关函数,实现任务的创建、释放、切换等操作。UCOS-Ⅱ提供了一系列API函数,可以方便地进行任务管理、时间管理、内存管理等操作。通过调用这些函数,可以实现对系统资源的充分利用和合理调度。 在MC9S12XS128上的应用中,UCOS-Ⅱ可以发挥其实时性和可靠性的优势。通过合理的任务划分和优先级设置,可以实现对多个任务的并发执行。同时,UCOS-Ⅱ的中断处理机制可以保证在中断发生时能够及时响应,并快速切换任务。这样可以提高系统的响应速度和实时性,适用于需要高实时性的应用场景,如工业控制、汽车电子等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值