【FreeRTOS】小白进阶之如何使用FreeRTOS事件标志组
事件标志组使用浅析。
1、头文件声明
#include "FreeRTOS.h"
#include "task.h"
#include "event_groups.h"
#include "timers.h"
#include "supporting_functions.h"// 中断号 0 to 2 被用于 FreeRTOS Windows port
#define mainINTERRUPT_NUMBER 3// 事件标志组三个位
#define mainFIRST_TASK_BIT ( 1UL << 0UL ) /* Event bit 0, which is set by a task. */
#define mainSECOND_TASK_BIT ( 1UL << 1UL ) /* Event bit 1, which is set by a task. */
#define mainISR_BIT ( 1UL << 2UL ) /* Event bit 2, which is set by an ISR. */// 任务声明
static void vIntegerGenerator( void *pvParameters );
static void vEventBitSettingTask( void *pvParameters );
static void vEventBitReadingTask( void *pvParameters );// daemon 任务,用于处理中断延时回调函数
void vPrintStringFromDaemonTask( void *pvParameter1, uint32_t ulParameter2 );// 启动模拟中断
static uint32_t ulEventBitSettingISR( void );// 事件标志组定义
EventGroupHandle_t xEventGroup;
2、启动任务
int main( void )
{// 创建事件标志组xEventGroup = xEventGroupCreate();// 设置事件任务xTaskCreate( vEventBitSettingTask, "BitSetter", 1000, NULL, 1, NULL );// 读事件标志任务xTaskCreate( vEventBitReadingTask, "BitReader", 1000, NULL, 2, NULL );// 启动模拟中断任务xTaskCreate( vIntegerGenerator, "IntGen", 1000, NULL, 3, NULL );// 注册中断函数vPortSetInterruptHandler( mainINTERRUPT_NUMBER, ulEventBitSettingISR );// 开始调度vTaskStartScheduler();for( ;; );return 0;
}
3、事件位设置任务
static void vEventBitSettingTask( void *pvParameters )
{const TickType_t xDelay200ms = pdMS_TO_TICKS( 200UL ), xDontBlock = 0;for( ;; ){vTaskDelay( xDelay200ms );// 设置事件位 0vPrintString( "Bit setting task -\t about to set bit 0.\r\n" );xEventGroupSetBits( xEventGroup, mainFIRST_TASK_BIT );vTaskDelay( xDelay200ms );// 设置事件位 1vPrintString( "Bit setting task -\t about to set bit 1.\r\n" );xEventGroupSetBits( xEventGroup, mainSECOND_TASK_BIT );}
}
4、中断处理函数
static uint32_t ulEventBitSettingISR( void )
{BaseType_t xHigherPriorityTaskWoken;////The string is not printed within the interrupt service, but is instead//sent to the RTOS daemon task for printing. It is therefore declared static to//ensure the compiler does not allocate the string on the stack of the ISR (as the//ISR's stack frame will not exist when the string is printed from the daemon//task. // static const char *pcString = "Bit setting ISR -\t about to set bit 2.\r\n";xHigherPriorityTaskWoken = pdFALSE;// 将打印信息传递给中断延时回调函数xTimerPendFunctionCallFromISR( vPrintStringFromDaemonTask, ( void * ) pcString, 0, &xHigherPriorityTaskWoken );// 设置事件位 2xEventGroupSetBitsFromISR( xEventGroup, mainISR_BIT, &xHigherPriorityTaskWoken );portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
5、读事件标志位
static void vEventBitReadingTask( void *pvParameters )
{const EventBits_t xBitsToWaitFor = ( mainFIRST_TASK_BIT | mainSECOND_TASK_BIT | mainISR_BIT );EventBits_t xEventGroupValue;for( ;; ){/* Block to wait for event bits to become set within the event group. */xEventGroupValue = xEventGroupWaitBits( xEventGroup, // 要读的事件标志组xBitsToWaitFor, // 需要测试的事件位pdTRUE,pdFALSE,portMAX_DELAY );if( ( xEventGroupValue & mainFIRST_TASK_BIT ) != 0 ){vPrintString( "Bit reading task -\t event bit 0 was set\r\n" );}if( ( xEventGroupValue & mainSECOND_TASK_BIT ) != 0 ){vPrintString( "Bit reading task -\t event bit 1 was set\r\n" );}if( ( xEventGroupValue & mainISR_BIT ) != 0 ){vPrintString( "Bit reading task -\t event bit 2 was set\r\n" );}vPrintString( "\r\n" );}
}
6、中断延时触发回调函数
void vPrintStringFromDaemonTask( void *pvParameter1, uint32_t ulParameter2 )
{vPrintString( ( const char * ) pvParameter1 );
}
7、模拟中断触发函数
static void vIntegerGenerator( void *pvParameters )
{
TickType_t xLastExecutionTime;
const TickType_t xDelay500ms = pdMS_TO_TICKS( 500UL );/* Initialize the variable used by the call to vTaskDelayUntil(). */xLastExecutionTime = xTaskGetTickCount();for( ;; ){/* This is a periodic task. Block until it is time to run again.The task will execute every 500ms. */vTaskDelayUntil( &xLastExecutionTime, xDelay500ms );/* Generate the interrupt that will set a bit in the event group. */vPortGenerateSimulatedInterrupt( mainINTERRUPT_NUMBER );}
}
【FreeRTOS】小白进阶之如何使用FreeRTOS事件标志组相关推荐
- FreeRTOS 事件标志组 ——提高篇
假设你已经看过FreeRTOS 事件标志组这篇随笔了. 之前的基础篇,真的就只是简单了解一下,相当于大学实验室的实验,但是,我们实际公司项目中,需要更多地思考,就算我们之前只是学习了基础概念以及基础语 ...
- FreeRTOS源码分析与应用开发07:事件标志组
目录 1. 概述 2. 事件标志组类型 3. 创建事件标志组 4. 删除事件标志组 5. 设置事件标志位 5.1 任务级设置 5.2 中断级设置 6. 清除事件标志位 6.1 任务级清除 6.2 中断 ...
- FreeRTOS 任务计数信号量,任务二值信号量,任务事件标志组,任务消息邮箱
以下基础内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 计数信号量的另一种实现方式----基于任务通知(Task Not ...
- FreeRTOS 事件标志组
FreeRTOS任务可以使用信号量来完成同步,但是使用信号量来同步的话任务只能与单个的事件或任务进行同步.有时候某个任务可能会需要与多个事件或任务进行同步,此时信号量就无能为力了. FreeRTO ...
- FreeRTOS记录(七、FreeRTOS信号量、事件标志组、邮箱和消息队列、任务通知的关系)
我们在前面单独介绍过FreeRTOS的任务通知和消息队列, 但是在FreeRTOS中任务间的通讯还有信号量,邮箱,事件组标志等可以使用 这篇文章就这些成员与消息队列和任务通知的关系进行说明分析 ..增 ...
- 【IoT】STM32 系统级开发之 ucosIII 或 freeRTOS 事件标志组详解
1.轻型操作系统同步的方案详解 1)信号量 假设有两个任务 Task1 和 Task2,第一个任务进行按键的扫描,第二个任务进行LED灯的点亮 需求: 扫描到按键按下后点亮 LED 灯,也就是说第二个 ...
- FreeRTOS操作系统——任务通知模拟消息邮箱及事件标志组(十八)
FreeRTOS操作系统学习 文章目录 FreeRTOS操作系统学习 一.消息邮箱API函数 二.消息邮箱实验 三.事件标志组实验 总结 一.消息邮箱API函数 任务通知也可用来向任务发送数据,但是相 ...
- FreeRTOS基于任务通知的信号量 事件标志组 消息邮箱
FreeRTOS创建的任务都有一个任务控制块. 任务控制块本质上是一个结构体变量,用于记录任务的相关的消息. 结构体变量中有一个专门用于任务通知的32位变量ulNotifiedValue. ulNot ...
- 用事件标志组实现多事件的单向同步
文章目录 1 用事件标志组实现多事件的单向同步 1 用事件标志组实现多事件的单向同步 问题需求: 某一任务等待事件中一个或多个事件同时发生. 解决方案: 事件发生时,设置指定事件标志位,任务4等待所有 ...
最新文章
- 关于 MySQL LEFT JOIN 你可能需要了解的三点
- 第二节 RabbitMQ配置
- python多列填充缺点_在Python中比较多列中的值并在另一列中添加新值
- LeetCode 406 Queue Reconstruction by Height
- hdu1166敌兵布阵 树状数组裸题
- 软件工程心理学之3---甲方项目负责人 PK 乙方
- poj2109 Power of Cryptography(数学题)
- 哈啰单车涨价:起步价1元/15分钟 仅限北京地区
- Docker 私有仓库搭建
- 适合上班族的两种兼职
- 生成可编辑的pdf(可java代码动态赋值)
- 模电——二级管的限幅与稳压
- 有道 - 扇贝 - 海词词典发音链接
- 小程序的申请开通需要什么条件
- 动手写一个探测网络质量(丢包率/RTT/队形等)的工具
- windows下服务或SYSTEM权限读取当前用户注册表HKEY_CURRENT_USER
- VirtualBox安装Ubuntu20.04 + 安装增强功能
- 如何提高代码质量,或者说高质量代码的特征是什么
- java模拟京东登陆_requests+beautifulsoup模拟登陆京东
- k3s证书过期的处理 以及 修改k3s证书有效期为10年(或自定义时间)
热门文章
- web项目PDF导出---freemarker 与 ITextRenderer--加页码
- 淘宝京东购物网站突然访问不了
- 初级程序员进大厂必刷的100道算法题
- 但是生活不止眼前的苟且
- 2021年安全员-C证最新解析及安全员-C证新版试题
- 百花村区块链山的选民们, 超有趣!
- 【大数据基础】Hadoop3.1.3安装教程
- python全栈开发第二天(CSS简介,CSS常用的属性)
- lol现在哪个服务器有无限火力,LOL公布最新消息,无限火力模式再次登场,这次是全新的体验...
- 现代密码学实验2 RC4加/解密算法