当前位置:网站首页>[FreeRTOS] 10 event flag group
[FreeRTOS] 10 event flag group
2022-07-24 20:03:00 【xiaobaibai_ two thousand and twenty-one】
Let's talk about freeRTOS Event flag group . It is also a mechanism for communication and synchronization between tasks , And semaphores have some similar functions , But it has its own characteristics .
1) What is an event flag group
An event flag group has multiple event bits , Each event bit represents the flag of an event .
For example, we use the event flag group bit0 Indicates an event A、bit1 Indicates an event B、bit2 Indicates an event C, Then this event flag group can at least represent 3 Whether events occurred .
We talked about semaphores before , It is used for signal synchronization , It can only indicate the presence or absence of a resource ; And the event flag group , It can represent the presence or absence of multiple resources at the same time .
The above example , This event flag group is equivalent to 3 Functions of two binary semaphores , Tasks can wait on an event flag group at the same time A、B、C Three events .
The task is waiting for the event of the event flag group , You can choose to wait for one or several event bits in the Group . For example, in the above example , Although the event flag group indicates A、B、C Three events , But the user can set it to only wait A and B.
Besides , Another difference between event flag group and semaphore , It can choose whether to clear the event bit when waiting for an event bit . And with semaphores , When a semaphore is obtained , The semaphore count will automatically decrease 1.
2)FreeRTOS Event flag group
freeRTOS Event flag group provided , By data type EventGroupHandle_t Definition . It needs to be confirmed before use FreeRTOSConfig.h In file configUSE_16_BIT_TICKS The definition of this macro , If defined as 1, An event flag group can store up to 8 Event bits , If you define bits 0, You can store 24 Event bits .
So let's talk about that freeRTOS Some functions commonly used in the operation event flag group .
Create event flag group :
EventGroupHandle_t xEventGroupCreate( void );// Create an event flag group , What is returned is the event standard group handle that was successfully created
This function is actually a dynamically created event flag group , If you want to use static creation , Need to use xEventGroupCreateStatic() function .
Set the event bit :
The most important thing is xEventGroupSetBits()、xEventGroupClearBits() Two functions , The function is to place the event 1 Or Qing 0. The function prototype is :
EventBits_t xEventGroupClearBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear); // clear 0 Event flag set ,xEventGroup Standard group handle for the event to operate ,uxBitsToClear Is the event bit to be cleared , You can clear multiple bits at the same time
EventBits_t xEventGroupSetBits(EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ); // Set the event flag group to 1,xEventGroup Standard group handle for the event to operate ,uxBitsToClear To set 1 The event bit
The above two functions can only be used in tasks , If you want to set the event flag group in the interrupt , To use the suffix ISR A set of functions :xEventGroupSetBitsFromISR() and xEventGroupClearBitsFromISR().
Wait event bit :
EventBits_t xEventGroupWaitBits(
EventGroupHandle_t xEventGroup, /* Event flag group to wait */
const EventBits_t uxBitsToWaitFor, /* Event bits to wait */
const BaseType_t xClearOnExit, /* Whether the waiting event bit is cleared ,pdTRUE For clearing ,pdFALSE unchanged */
const BaseType_t xWaitForAllBits, /* Whether to wait for all bits ,pdTRUE For waiting uxBitsToWaitFor All bits specified ,pfFALSE as long as uxBitsToWaitFor One of the designated locations 1 Just go back to */
const TickType_t xTicksToWait ); /* Waiting timeout */
Get the value of the event flag group :
EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );// Get the value of the event standard group ( It doesn't change its value )
You need to use xEventGroupGetBitsFromISR This function .
3) Programming test
Let's use an example to demonstrate the use of event flag groups .
As in previous issues , We still use cubemx Establish three tasks , Priorities vary .
Generate keil engineering :

Write the code as follows , The first is the creation of event flag group :

Let's write the task with the lowest priority first DefaultTask Send events in , The first 1s Send event bits bit0, The first 2s Occurrence bit bit1, So circular :

In medium priority tasks Task02 in , Wait event bit bit0 and bit1, Wait until later , Zero clearing , And send event bits bit3:

On high priority tasks Task03 in , Wait event bit bit3, Wait until after not clear :

The running result of this program is shown in the figure below :

At the beginning , The two tasks with higher priority are waiting for events , Only the lowest priority tasks DefaultTask Can run ;
The first 1s when DefaultTask Sent the event bit0, The first 2s when DefaultTask Sent the event bit1;
because Task02 It is set to wait at the same time bit0 and bit1, therefore , To DefaultTask End of hair bit1 when ,Task02 Until it needs all the event bits , Wait until the event is clear 0;
then ,Task02 Event bits sent bit3;
Task03 Waiting to arrive bit3 after , Because the setting is not clear , The bit3 Event bit it is always valid ; therefore Task03 Then the loop runs to the wait event bit bit3 Time will not be blocked , Instead, return to execution immediately . Will keep printing out Task03 has waited event bit3.
Okay , That's all for this section .
If you find it useful, you can pay attention to the author's wechat “ Xiaobai learns Electronics ”, You can find the code and data download address in the official account :

边栏推荐
- Mass modify attribute values in objects in JS
- Substr and substring function usage in SQL
- MySQL stored procedure
- Leetcode652 finding duplicate subtrees
- English translation Chinese common dirty words
- Data transmission of different fragments in the same activity
- Thymeleaf application notes
- [face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.
- Unity3d eventsystem (event)
- 6.0 holes stepped by fragment request permission in the system
猜你喜欢

Browser local storage webstroage
![Microservice architecture | service monitoring and isolation - [sentinel] TBC](/img/28/8ca90e9dbd492688e50446f55959ff.png)
Microservice architecture | service monitoring and isolation - [sentinel] TBC

Look at the interface control devaxpress WinForms - how to customize auxiliary function properties (Part 2)

Valdo2021 - vascular space segmentation in vascular disease detection challenge (2)

day 1

Richview table table alignment

C# 窗体应用TreeView控件使用

Alibaba cloud technology expert Yang Zeqiang: building observable capabilities on elastic computing cloud

原反补及大小端

Day 9 (this keyword and experiment)
随机推荐
MySQL advanced 2
Sword finger offer 45. arrange the array into the smallest number
Introduction to fastdfs high availability
C language implementation of raii
[face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.
About the largeheap attribute
Solve the problem that gd32f207 serial port can receive but send 00
Usage and introduction of MySQL binlog
870. Approximate number
Use of paging assistant PageHelper
Lunch break train & problem thinking: on multidimensional array statistics of the number of elements
Please ask a question. Follow the quick start method. After creating the table, the Flink SQL queries and displays the table structure, but there is an error when it exceeds the limit. What should we
day 1
拿捏C指针
Functional test of redisgraph multi active design scheme
Student achievement management system based on PHP
英文翻译中文常见脏话
Solve the problem of error l6218e undefined symbol XXX
Istio二之流量劫持过程
Lunch break train & problem thinking: thinking about the problem of converting the string formed by hour: minute: second to second