UCOS operating system - task embedded semaphore (11)

UCOS operating system

foreword

We need to create a semaphore before when we use semaphore, but in UCOSIII each task has its own
embedded semaphore, this function can not only simplify the code, but also more effective than using independent semaphore. The task semaphore is
directly embedded in UCOSIII, and the related code of the task semaphore is in os_task.c. Embedded semaphores are unique to UCOS, not in FreeRTOS

First, the task embedded semaphore

insert image description here

2. How to use

1. Wait for task embedded semaphore

The function OSTaskSemPend() is used to wait for the embedded semaphore of a task. OStaskSemPend() allows a task to wait for
a signal directly sent by other tasks or ISR
.

OS_SEM_CTR OSTaskSemPend ( OS_TICK timeout,
 OS_OPT opt,
 CPU_TS *p_ts,
 OS_ERR *p_err)

timeout: If the semaphore is not received within the specified number of beats, the task will resume running due to waiting for a timeout.
If the timeout is 0, the task will wait until the semaphore is received.

opt: Used to choose whether to use blocking mode.
OS_OPT_PEND_BLOCKING Specifies that when the mutex semaphore is occupied, the task suspends waiting for the mutex semaphore.
OS_OPT_PEND_NON_BLOCKING specifies that when the mutex semaphore is occupied, return to the task directly.
Notice! When set to OS_OPT_PEND_NON_BLOCKING, the timeout parameter is meaningless and should be set to 0.
p_ts: Points to a timestamp that records the moment when the mutex semaphore is sent, terminated or deleted.
P_err: The error code returned after calling this function.

2. Release task semaphore

OSTaskSemPost() can send a semaphore to a task through a task's built-in semaphore. The function prototype is as follows:

OS_SEM_CTR OSTaskSemPost (OS_TCB *p_tcb,
 OS_OPT opt,
 OS_ERR *p_err)

p_tcb: Points to the TCB of the task to be signaled. When set to NULL, it can send a signal to itself.
opt: used to specify whether to perform task scheduling.
OS_OPT_POST_NONE does not specify a specific option.
OS_OPT_POST_NO_SCHED prohibits task scheduling within this function.
p_err: The error code returned after calling this function.

3. Complete code

Use the semaphore embedded in the task so there is no need to create a semaphore

//任务 1 的任务函数
void task1_task(void *p_arg)
{
    
    
u8 key;
u8 num;
OS_ERR err;
while(1)
{
    
    
key = KEY_Scan(0); //扫描按键
if(key==WKUP_PRES)
{
    
    
OSTaskSemPost(&Task2_TaskTCB, (1)
OS_OPT_POST_NONE,&err); //使用系统内建信号量向任务 task2 发送信号量
LCD_ShowxNum(150,111,Task2_TaskTCB.SemCtr,3,16,0); //显示信号量值
}
num++;
if(num==50)
{
    
    
num=0;
LED0=~LED0;
}
OSTimeDlyHMSM(0,0,0,10,OS_OPT_TIME_PERIODIC,&err); //延时 10ms
} 
}
//任务 2 的任务函数
void task2_task(void *p_arg)
{
    
    
u8 num;
OS_ERR err;
while(1)
{
    
     
OSTaskSemPend(0,OS_OPT_PEND_BLOCKING,\ (2)
0,&err); //请求任务内建的信号量
num++;
LCD_ShowxNum(150,111,Task2_TaskTCB.SemCtr,3,16,0); //显示任务内建信号量值
LCD_Fill(6,131,233,313,lcd_discolor[num%14]); //刷屏
LED1 = ~LED1;
OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err); //延时 1s
} }

(1) Call the function OSTaskSemPost() to send a task semaphore to the task task2_task.
(2) The task task2_task has been requesting the task semaphore.
After booting, the task task2_task will be blocked due to the lack of semaphore requests.
insert image description here
When we press the KEY_UP key, the semaphore will be sent, and the value of the embedded semaphore in the task2_task task will change (increase), we press the KEY_UP key several times
insert image description here
at this time . The value of the embedded semaphore of the task2_task task is 12, indicating that the task2_task task can request the semaphore embedded in the task 12 times. The task task2_task will request an embedded semaphore every 1s until the semaphore value embedded in the task is 0

Summarize

The task embedded semaphore in UCOS is different from other semaphores. It can be seen from the code that the semaphore does not need to be released. In the previous study, the semaphore needs to be released.

Guess you like

Origin blog.csdn.net/qq_51963216/article/details/123926254