广州网站设计专注乐云seo,哈尔滨网站建设方案维护,建公司网站的详细步骤,enjooy wordpressSTM32F4X UCOSIII任务消息队列 任务消息队列和内核消息队列对比内核消息队列内核消息队列 UCOSIII任务消息队列API任务消息队列发送函数任务消息队列接收函数 UCOSIII任务消息队列例程 之前的章节中讲解过消息队列这个机制#xff0c;UCOSIII除了有内核消息队列之外#xff0… STM32F4X UCOSIII任务消息队列 任务消息队列和内核消息队列对比内核消息队列内核消息队列 UCOSIII任务消息队列API任务消息队列发送函数任务消息队列接收函数 UCOSIII任务消息队列例程 之前的章节中讲解过消息队列这个机制UCOSIII除了有内核消息队列之外还有任务消息队列。在UCOSIII中每个任务内部都会有一个内嵌消息队列。在大多数情况下任务消息队列可以代替内核消息队列。 任务消息队列和内核消息队列对比
内容内核消息队列任务消息队列是否需要创建是否是否需要指定任务否是是否可以广播是否
内核消息队列
内核消息队列在使用前需要用户创建消息队列内核消息队列不需要指定接收消息队列的任务并且可以以广播的方式给所有等待消息队列的任务获取消息。
内核消息队列
任务消息队列不需要用户创建任务消息队列在创建任务的时候就已经内嵌到任务控制块里面。任务消息队列使用的时候需要指定接收任务消息队列的任务而且一次只能指定一个任务不能广播。
UCOSIII任务消息队列API
任务消息队列发送函数
/** p_tcb:指向需要发送信号量的任务NULL则代表自己* p_void:需要发送的消息指针* msg_size:需要发送的消息长度* opt:用户选项* p_err:错误代码
*/
void OSTaskQPost (OS_TCB *p_tcb,void *p_void,OS_MSG_SIZE msg_size,OS_OPT opt,OS_ERR *p_err)opt可以选择OS_OPT_POST_FIFO和OS_OPT_POST_LIFO
OS_OPT_POST_FIFO:消息以先进先出的模式存放OS_OPT_POST_LIFO:消息以后进先出的模式存放
任务消息队列接收函数
/** timeout:超时时间* opt:用户选项* p_msg_size:接收到的消息长度* p_ts:时间戳* p_err:错误代码
返回值:返回接收到消息指针如果为NULL则接收错误
*/
void *OSTaskQPend (OS_TICK timeout,OS_OPT opt,OS_MSG_SIZE *p_msg_size,CPU_TS *p_ts,OS_ERR *p_err)opt可以选择OS_OPT_PEND_BLOCKING和OS_OPT_PEND_NON_BLOCKING
OS_OPT_PEND_BLOCKING:阻塞等待任务消息队列除非有任务消息队列否则任务不会恢复OS_OPT_PEND_NON_BLOCKING:不阻塞等待任务消息队列如果任务等待时间超过设定的超时时间任务会恢复并返回一个错误代码
UCOSIII任务消息队列例程
例程中任务1会每隔1秒向任务2发送任务消息,任务2则阻塞等待消息
/*
*********************************************************************************************************
* EXAMPLE CODE
*
* (c) Copyright 2013; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may not be used to write a similar
* product. This file may only be used in accordance with a license
* and should not be redistributed in any way.
*********************************************************************************************************
*//*
*********************************************************************************************************
*
* EXAMPLE CODE
*
* IAR Development Kits
* on the
*
* STM32F429II-SK KICKSTART KIT
*
* Filename : app.c
* Version : V1.00
* Programmer(s) : YS
*********************************************************************************************************
*//*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/#include includes.h/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*//*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*//* ----------------- APPLICATION GLOBALS -------------- */
static OS_TCB AppTaskStartTCB;
static CPU_STK AppTaskStartStk[APP_CFG_TASK_START_STK_SIZE];#define APPTASK1NAME App Task1
#define APP_TASK1_PRIO 3
#define APP_TASK1_STK_SIZE 1024
static OS_TCB AppTask1TCB;
static void AppTask1 (void *p_arg);
static CPU_STK AppTask1Stk[APP_TASK1_STK_SIZE];#define APPTASK2NAME App Task2
#define APP_TASK2_PRIO 4
#define APP_TASK2_STK_SIZE 1024
static OS_TCB AppTask2TCB;
static void AppTask2 (void *p_arg);
static CPU_STK AppTask2Stk[APP_TASK2_STK_SIZE];
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/static void AppTaskStart (void *p_arg);
struct msg
{char msg_string[50];int value;
};/*
*********************************************************************************************************
* main()
*
* Description : This is the standard entry point for C code. It is assumed that your code will call
* main() once you have performed all necessary initialization.
*
* Arguments : none
*
* Returns : none
*********************************************************************************************************
*/int main(void)
{OS_ERR err;OSInit(err); /* Init uC/OS-III. */OSTaskCreate((OS_TCB *)AppTaskStartTCB, /* Create the start task */(CPU_CHAR *)App Task Start,(OS_TASK_PTR )AppTaskStart,(void *)0u,(OS_PRIO )APP_CFG_TASK_START_PRIO,(CPU_STK *)AppTaskStartStk[0u],(CPU_STK_SIZE )AppTaskStartStk[APP_CFG_TASK_START_STK_SIZE / 10u],(CPU_STK_SIZE )APP_CFG_TASK_START_STK_SIZE,(OS_MSG_QTY )0u,(OS_TICK )0u,(void *)0u,(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),(OS_ERR *)err);OSStart(err); /* Start multitasking (i.e. give control to uC/OS-III). */}/*
*********************************************************************************************************
* STARTUP TASK
*
* Description : This is an example of a startup task. As mentioned in the books text, you MUST
* initialize the ticker only once multitasking has started.
*
* Arguments : p_arg is the argument passed to AppTaskStart() by OSTaskCreate().
*
* Returns : none
*
* Notes : 1) The first line of code is used to prevent a compiler warning because p_arg is not
* used. The compiler should not generate any code for this statement.
*********************************************************************************************************
*/static void AppTaskStart (void *p_arg)
{CPU_INT32U cpu_clk_freq;CPU_INT32U cnts;OS_ERR err;(void)p_arg;BSP_Init(); CPU_Init(); /* Initialize the uC/CPU services */cpu_clk_freq BSP_CPU_ClkFreq(); /* Determine SysTick reference freq. */cnts cpu_clk_freq /* Determine nbr SysTick increments *// (CPU_INT32U)OSCfg_TickRate_Hz;OS_CPU_SysTickInit(cnts); /* Init uC/OS periodic time src (SysTick). */Mem_Init(); /* Initialize memory managment module */Math_Init(); /* Initialize mathematical module */#if OS_CFG_STAT_TASK_EN 0uOSStatTaskCPUUsageInit(err); /* Compute CPU capacity with no task running */
#endif#ifdef CPU_CFG_INT_DIS_MEAS_ENCPU_IntDisMeasMaxCurReset();
#endif#if (APP_CFG_SERIAL_EN DEF_ENABLED)App_SerialInit(); /* Initialize Serial communication for application ... */
#endifOSTaskCreate((OS_TCB *)AppTask1TCB, // 线程TCB (CPU_CHAR *)APPTASK1NAME, // 线程名字(OS_TASK_PTR ) AppTask1, // 线程入口函数(void *) TASK1, // 线程参数(OS_PRIO ) APP_TASK1_PRIO, // 线程优先级(CPU_STK *)AppTask1Stk[0], // 线程栈起始地址(CPU_STK_SIZE) APP_TASK1_STK_SIZE / 10, // 栈深度的限制位置(CPU_STK_SIZE) APP_TASK1_STK_SIZE, // 栈大小(OS_MSG_QTY ) 5u, // 最大的消息个数(OS_TICK ) 0u, // 时间片(void *) 0, // 向用户提供的内存位置的指针(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), // 线程特定选项(OS_ERR *)err); // 错误标志if(OS_ERR_NONE err)printf(%s Create Success\r\n,APPTASK1NAME);elseprintf(%s Create Error\r\n,APPTASK1NAME);OSTaskCreate((OS_TCB *)AppTask2TCB, // 线程TCB (CPU_CHAR *)APPTASK2NAME, // 线程名字(OS_TASK_PTR ) AppTask2, // 线程入口函数(void *) TASK2, // 线程参数(OS_PRIO ) APP_TASK2_PRIO, // 线程优先级(CPU_STK *)AppTask2Stk[0], // 线程栈起始地址(CPU_STK_SIZE) APP_TASK2_STK_SIZE / 10, // 栈深度的限制位置(CPU_STK_SIZE) APP_TASK2_STK_SIZE, // 栈大小(OS_MSG_QTY ) 5u, // 最大的消息个数(OS_TICK ) 0u, // 时间片(void *) 0, // 向用户提供的内存位置的指针(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), // 线程特定选项(OS_ERR *)err); // 错误标志if(OS_ERR_NONE err)printf(%s Create Success\r\n,APPTASK2NAME);elseprintf(%s Create Error\r\n,APPTASK2NAME);OSTaskDel ( AppTaskStartTCB, err ); }static void AppTask1 (void *p_arg)
{OS_ERR err;static struct msg msg_send {0};while(DEF_TRUE){OSTimeDly ( 1000, OS_OPT_TIME_DLY, err ); // 1s运行一次Str_Copy_N(msg_send.msg_string,hello this is a msg1,sizeof(msg_send.msg_string)); // 填充消息OSTaskQPost(AppTask2TCB,msg_send,sizeof(struct msg),OS_OPT_POST_FIFO,err); // 向TASK2发送任务消息msg_send.value;if(err OS_ERR_NONE)printf(Task1 Msg Post Success\r\n);elseprintf(Task1 Msg Post Error\r\n);}}
static void AppTask2 (void *p_arg)
{OS_ERR err;struct msg *pmsg;OS_MSG_SIZE msg_size;while(DEF_TRUE){pmsg OSTaskQPend(0,OS_OPT_PEND_BLOCKING,msg_size,0,err); // 阻塞等待任务消息if(err OS_ERR_NONE)printf(Task2 Get Msg Success \r\n);elseprintf(Task1 Get Msg Error\r\n);printf(msg srting %s\r\n,pmsg-msg_string);printf(msg value %d\r\n,pmsg-value);}}