Entry-level use of μC/OS-II event flag

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.