Entry-level use of μC/OS-II event flag
Imagine the following: There are three events A, B, and C. When A and B meet A certain condition (or execute an action) then C can be run (continue or run once and then run again after the and B conditions are met ).
If you need to implement this function, you can use the Event Identification Group!
Take a look at the instance:
// Define an event flag
OS _FLAG_GRP * Sem_Flg = 0;
// LED0 task
Void led0_task (void * pdata)
{
INT8U err = 0;
Pdata = pdata;
// Create an event flag
Sem_Flg = OSFlagCreate (0, & err );
While (1)
{
Delay_ms (5000u );
// Sends the semaphore set
OSFlagPost (
Sem_Flg,
(OS _FLAGS) 1, // send a signal to 0th bits
OS _FLAG_SET, // set the semaphore to 1
& Err
);
}
}
// LED1 task
Void led1_task (void * pdata)
{
INT8U err = 0u;
While (1)
{
Delay_ms (1000u );
// Sends the semaphore set
OSFlagPost (
Sem_Flg,
(OS _FLAGS) 2, // send a signal to 1st bits
OS _FLAG_SET, // set the semaphore to 1
& Err
);
}
}
// UART task
Void uart_task (void * pdata)
{
INT8U err = 0;
Pdata = pdata;
While (1)
{
// Request semaphore set
OSFlagPend (
Sem_Flg,
(OS _FLAGS) 0x03, // request 0th-bit and 1st-bit signals
OS _FLAG_WAIT_SET_ALL // this parameter is valid when both the 0th-bit and 1-bit values are set to 1. Otherwise, the task is mounted here.
| OS _FLAG_CONSUME, // clear the specified event flag
0, // infinite wait until the signal is received
& Err
);
Printf ("run uart_task \ r \ n"); // print once every 5 seconds
}
}
If OS _FLAG_WAIT_SET_ALL is changed to OS _FLAG_WAIT_SET_ANY,
It indicates that the value is valid when the 0th-bit or 1st-bit position is 1. Otherwise, the task is mounted here.
If OS _FLAG_CONSUME is not added, the print will continue once the condition is true,
You do not need to wait until the condition is set up again to print it!