文章目录
- 前言
- 实验步骤
- 1.复制二值信号量工程,添加task2和task3,修改任务服务函数名称
- 2.修改开始任务,任务1以及任务2、3的内容
- 3.查看串口现象![在这里插入图片描述](https://img-blog.csdnimg.cn/efa5ee2d92b54fe8be5a419adcf92ead.png)
- 测试代码
- 工程文件
- 总结
前言
无,仅作记录,不具有参考价值,所用开发板为STM32F411RET6nucleo开发板。
实验步骤
1.复制二值信号量工程,添加task2和task3,修改任务服务函数名称
2.修改开始任务,任务1以及任务2、3的内容
3.查看串口现象
测试代码
us-os3_demo.c:
/**
****************************************************************************************************
* @file uc-os3_demo.c
* @author ÕýµãÔ×ÓÍŶÓ(ALIENTEK)
* @version V1.0
* @date 2022-04-13
* @brief uC/OS-III ÒÆֲʵÑé
* @license Copyright (c) 2020-2032, ¹ãÖÝÊÐÐÇÒíµç×ӿƼ¼ÓÐÏÞ¹«Ë¾
****************************************************************************************************
* @attention
*
* ʵÑéƽ̨:ÕýµãÔ×Ó Ì½Ë÷Õß F407¿ª·¢°å
* ÔÚÏßÊÓƵ:www.yuanzige.com
* ¼¼ÊõÂÛ̳:www.openedv.com
* ¹«Ë¾ÍøÖ·:www.alientek.com
* ¹ºÂòµØÖ·:openedv.taobao.com
*
****************************************************************************************************
*/
#include "uc-os3_demo.h"
#include "./MALLOC/malloc.h"
/*uC/OS-III*********************************************************************************************/
#include "os.h"
#include "cpu.h"
#include "main.h"
#include "key.h"
#include "stdio.h"
#include "interrupt.h"
#include "./SYSTEM/delay/delay.h"
extern struct keys key[4];
/******************************************************************************************************/
/*uC/OS-IIIÅäÖÃ*/
/* START_TASK ÈÎÎñ ÅäÖÃ
* °üÀ¨: ÈÎÎñÓÅÏȼ¶ ÈÎÎñÕ»´óС ÈÎÎñ¿ØÖÆ¿é ÈÎÎñÕ» ÈÎÎñº¯Êý
*/
#define START_TASK_PRIO 5
#define START_TASK_STACK_SIZE 256
CPU_STK start_task_stack[START_TASK_STACK_SIZE];
OS_TCB start_task_tcb;
void start_task(void * p_arg);
/* TASK1 ÈÎÎñ ÅäÖÃ
* °üÀ¨: ÈÎÎñÓÅÏȼ¶ ÈÎÎñÕ»´óС ÈÎÎñ¿ØÖÆ¿é ÈÎÎñÕ» ÈÎÎñº¯Êý
*/
#define TASK1_PRIO 4
#define TASK1_STACK_SIZE 256
CPU_STK * task1_stack;
OS_TCB task1_tcb;
void low_task(void * p_arg);
/* TASK2 ÈÎÎñ ÅäÖÃ
* °üÀ¨: ÈÎÎñÓÅÏȼ¶ ÈÎÎñÕ»´óС ÈÎÎñ¿ØÖÆ¿é ÈÎÎñÕ» ÈÎÎñº¯Êý
*/
#define TASK2_PRIO 3
#define TASK2_STACK_SIZE 256
CPU_STK * task2_stack;
OS_TCB task2_tcb;
void middle_task(void * p_arg);
/* TASK3 ÈÎÎñ ÅäÖÃ
* °üÀ¨: ÈÎÎñÓÅÏȼ¶ ÈÎÎñÕ»´óС ÈÎÎñ¿ØÖÆ¿é ÈÎÎñÕ» ÈÎÎñº¯Êý
*/
#define TASK3_PRIO 2
#define TASK3_STACK_SIZE 256
CPU_STK * task3_stack;
OS_TCB task3_tcb;
void high_task(void * p_arg);
OS_SEM binary_sem;
/**
* @brief uC/OS-IIIÀý³ÌÈë¿Úº¯Êý
* @param ÎÞ
* @retval ÎÞ
*/
void uc_os3_demo(void)
{
OS_ERR err;
/* ³õʼ»¯uC/OS-III */
OSInit(&err);
OSTaskCreate ( (OS_TCB *) &start_task_tcb,
(CPU_CHAR *) "start_task",
(OS_TASK_PTR ) start_task,
(void *) 0,
(OS_PRIO ) START_TASK_PRIO,
(CPU_STK *) start_task_stack,
(CPU_STK_SIZE ) START_TASK_STACK_SIZE / 10,
(CPU_STK_SIZE ) START_TASK_STACK_SIZE,
(OS_MSG_QTY ) 0,
(OS_TICK ) 0,
(void *) 0,
(OS_OPT ) (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *) &err);
/* ¿ªÊ¼ÈÎÎñµ÷¶È */
OSStart(&err);
}
void start_task(void * p_arg)
{
OS_ERR err;
CPU_INT32U cnts = 0;
CPU_Init();
CPU_SR_ALLOC();
cnts = HAL_RCC_GetSysClockFreq() / OS_CFG_TICK_RATE_HZ;
OS_CPU_SysTickInit(cnts);
/* ´´½¨¶þÖµÐźÅÁ¿ */
OSSemCreate (&binary_sem, "binary_sem", 1, &err);
CPU_CRITICAL_ENTER(); /* ½øÈëÁÙ½çÇø */
task1_stack = mymalloc(SRAMIN, TASK1_STACK_SIZE * sizeof(CPU_STK));
task2_stack = mymalloc(SRAMIN, TASK2_STACK_SIZE * sizeof(CPU_STK));
task3_stack = mymalloc(SRAMIN, TASK3_STACK_SIZE * sizeof(CPU_STK));
/* ´´½¨µÍÓÅÏȼ¶ÈÎÎñ */
OSTaskCreate ((OS_TCB *) &task1_tcb,
(CPU_CHAR *) "low_task",
(OS_TASK_PTR ) low_task,
(void *) 0,
(OS_PRIO ) TASK1_PRIO,
(CPU_STK *) task1_stack,
(CPU_STK_SIZE ) TASK1_STACK_SIZE / 10,
(CPU_STK_SIZE ) TASK1_STACK_SIZE,
(OS_MSG_QTY ) 0,
(OS_TICK ) 0,
(void *) 0,
(OS_OPT ) (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *) &err);
/* ´´½¨ÖеÈÓÅÏȼ¶ÈÎÎñ */
OSTaskCreate ((OS_TCB *) &task2_tcb,
(CPU_CHAR *) "middle_task",
(OS_TASK_PTR ) middle_task,
(void *) 0,
(OS_PRIO ) TASK2_PRIO,
(CPU_STK *) task2_stack,
(CPU_STK_SIZE ) TASK2_STACK_SIZE / 10,
(CPU_STK_SIZE ) TASK2_STACK_SIZE,
(OS_MSG_QTY ) 0,
(OS_TICK ) 0,
(void *) 0,
(OS_OPT ) (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *) &err);
/* ´´½¨¸ßÓÅÏȼ¶ÈÎÎñ */
OSTaskCreate ((OS_TCB *) &task3_tcb,
(CPU_CHAR *) "high_task",
(OS_TASK_PTR ) high_task,
(void *) 0,
(OS_PRIO ) TASK3_PRIO,
(CPU_STK *) task3_stack,
(CPU_STK_SIZE ) TASK3_STACK_SIZE / 10,
(CPU_STK_SIZE ) TASK3_STACK_SIZE,
(OS_MSG_QTY ) 0,
(OS_TICK ) 0,
(void *) 0,
(OS_OPT ) (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *) &err);
CPU_CRITICAL_EXIT(); /* Í˳öÁÙ½çÇø */
printf("ÕýÔÚ´´½¨ÖÐ\r\n");
OSTaskDel((OS_TCB*)0, &err); //±ðÍüÁËɾ³ýÈÎÎñStart
}
/* µÍÓÅÏȼ¶ÈÎÎñ */
void low_task(void * p_arg)
{
OS_ERR err;
while(1)
{
printf("low_task»ñÈ¡¶þÖµÐźÅÁ¿!!\r\n");
OSSemPend(&binary_sem, 0, OS_OPT_PEND_BLOCKING, 0, &err);
printf("low_taskÕýÔÚÔËÐÐ!!\r\n");
delay_ms(3000);
printf("low_taskÊͷŶþÖµÐźÅÁ¿!!\r\n");
OSSemPost(&binary_sem, OS_OPT_PEND_ABORT_1, &err);
OSTimeDly(1000, OS_OPT_TIME_DLY, &err);
}
}
/* ÖеÈÓÅÏȼ¶ÈÎÎñ */
void middle_task(void * p_arg)
{
OS_ERR err;
while(1)
{
printf("middle_taskÕýÔÚÔËÐÐ!!!\r\n");
OSTimeDly(1000, OS_OPT_TIME_DLY, &err);
}
}
/* ¸ßÓÅÏȼ¶ÈÎÎñ */
void high_task(void * p_arg)
{
OS_ERR err;
while(1)
{
printf("high_task»ñÈ¡¶þÖµÐźÅÁ¿!!\r\n");
OSSemPend(&binary_sem, 0, OS_OPT_PEND_BLOCKING, 0, &err);
printf("high_taskÕýÔÚÔËÐÐ!!\r\n");
delay_ms(1000);
printf("high_taskÊͷŶþÖµÐźÅÁ¿!!\r\n");
OSSemPost(&binary_sem, OS_OPT_PEND_ABORT_1, &err);
OSTimeDly(1000, OS_OPT_TIME_DLY, &err);
}
}
工程文件
优先级翻转实验
总结
【正点原子】手把手教你学UCOS-III实时操作系统第27讲优先级翻转