当前位置:网站首页>【实战】STM32 FreeRTOS移植系列教程2:FreeRTOS 互斥信号量
【实战】STM32 FreeRTOS移植系列教程2:FreeRTOS 互斥信号量
2022-06-21 09:09:00 【华清远见IT开放实验室】
1.写在前面:
本文章为《STM32MP157开发教程之FreeRTOS操作系统篇》系列中的一篇,笔者使用的开发平台为华清远见FS-MP1A开发板(STM32MP157开发板)。stm32mp157是ARM双核,2个A7核,1个M4核,A7核上可以跑Linux操作系统,M4核上可以跑FreeRTOS、RT-Thread等实时操作系统,STM32MP157开发板所以既可以学嵌入式linux,也可以学stm32单片机。
针对FS-MP1A开发板,除了FreeRTOS操作系统篇外,还包括其他多系列教程,包括Cortex-A7开发篇、Cortex-M4开发篇、扩展板驱动移植篇、Linux应用开发篇、Linux系统移植篇、Linux驱动开发篇、硬件设计篇、人工智能机器视觉篇、Qt应用编程篇、Qt综合项目实战篇等。欢迎关注,更多stm32mp157开发教程及视频,可加技术交流Q群459754978,感谢关注。
FS-MP1A开发板详情介绍:https://item.taobao.com/item.htm?id=622457259672
2.FreeRTOS互斥信号量
2.1 互斥信号量简介
互斥信号量可以作为一种特殊的二值信号量,优先级继承机制,保护临界资源。
互斥信号量适合用于那些需要互斥访问的应用中。在互斥访问中互斥信号量相当于一个钥匙,当任务想要使用资源的时候就必须先获得这个钥匙,当使用完资源以后就必须归还这个钥匙,这样其他的任务就可以拿着这个钥匙去使用资源。
互斥信号量使用和二值信号量相同的 API 操作函数,所以互斥信号量也可以设置阻塞时间,不同于二值信号量的是互斥信号量具有优先级继承的特性。当一个互斥信号量正在被一个低优先级的任务使用,而此时有个高优先级的任务也尝试获取这个互斥信号量的话就会被阻塞。不过这个高优先级的任务会将低优先级任务的优先级提升到与自己相同的优先级,这个过程就是优先级继承。优先级继承尽可能的降低了高优先级任务处于阻塞态的时间,并且将已经出现的“优先级翻转”的影响降到最低。
2.2 互斥信号量函数
2.2.1 创建信号量
在新版FreeRTOS中,一般使用xSemaphoreCreateMutex()函数动态创建互斥信号量,其所需要的内存通过动态内存管理方法分配。此函数是个宏,具体创建工程是由函数xQueueCreateMutex()完成。此函数原型如下:
SemaphoreHandle_t xSemaphoreCreateMutex(void)
参数: 无
返回值:
NULL:互斥信号量创建失败
其他值:创建成功的互斥信号量的句柄
2.2.2 释放信号量
释放信号量的函数与二值信号量相同,具体请参考FreeRTOS二值信号量章节。
2.2.3获取信号量
获取信号量的函数也有两个,分别如下:
任务级信号量获取函数xSemaphoreTake(),此函数也是个宏,可以获取二值信号量、计数型信号量和互斥信号量,其函数原型如下:
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore,
TickType_t xBlockTime)
参数:
xSemaphore:要获取的信号量句柄
xBlockTime:阻塞时间
返回值:
pdTRUE:获取信号量成功
pdFALSE:超时,获取信号量失败
中断级信号量获取函数 xSemaphoreTakeFromISR(),此函数也是个宏,只能获取二值信号量和计数型信号量,其函数原型如下:
BaseType_t xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore,
BaseType_t* pxHigherPriorityTaskWoken)
参数:
xSemaphore:要获取的信号量句柄
pxHigherPriorityTaskWoken:标记退出此函数以后是否进行任务切换
返回值:
PdPASS:获取信号量成功
pdFALSE:获取信号量失败
2.3操作实验
2.3.1实验设计
互斥信号量其实就是一个拥有优先级继承的二值信号量,互斥信号量适合用于那些需要互斥访问的应用中。在本节实验中,分别控制LED1和LED2灯的亮灭来表示。
任务及其功能如下:
StartTask02(): 控制LED2熄灭,LED1点亮,延时2秒钟。
StartDefaultTask(): 控制LED1熄灭,LED2点亮,延时1秒钟。
2.3.2实验过程与分析
首先,根据之前几章内容配置好CubeMX,按照上一节配置“FREERTOS”,完成后生成代码。 在主函数中调用xSemaphoreCreateMutex()创建互斥信号量,在StartDefaultTask() 与StartTask02()中添加代码如下。
可参考12.3.2章节进行导入已有工程,工程存放路径【华清远见-FS-MP1A开发资料\02-程序源码\ARM体系结构与接口技术\FreeRTOS\3_MP1A-FreeRTOS-Mutex】

此时会发现,LED1点亮、LED2熄灭状态下保持2秒,LED2点亮、LED1熄灭状态下保持1秒,进行交替闪烁。
华清远见教育科技有限公司
开发板、核心板、资源扩展板及配套海量资料
淘宝直销店:https://shop273057902.taobao.com/
淘宝企业店:https://shop533757171.taobao.com/
微信公众号:华清远见IT开放实验室
bilibili:https://space.bilibili.com/670826565
华清远见官网:http://www.fsdev.com.cn/
技术交流QQ群:459754978(免费提供下载资料)
边栏推荐
- Unity's network request_ Short connection
- R language list data object, create list data object, index list data with [[], list data practice
- The R language uses the fix function to modify the name of the data variable through the editor. For example, use the fix function to modify the name of the dataframe data column
- [JUC series] completionservice of executor framework
- Figure out how MySQL works
- The R language uses the sink function to export the string to the txt file in the specified directory. If no directory is specified, it will be output to the current working dir
- R language uses as The date function converts a single character variable to date data and specifies the format of the data format conversion
- [Mgt] model Mgt for code interpretation
- How to generate QR code
- Storage of floating point numbers in C language in memory
猜你喜欢

Understanding and use of advanced pointer
![[Mgt] model Mgt for code interpretation](/img/4f/694e7b93626436007021b718ad69b5.png)
[Mgt] model Mgt for code interpretation

The spring recruitment is also terrible. Ali asked at the beginning of the interview: how to design a high concurrency system? I just split

Unity 5 自带的Mono也可以支持C# 6

It is said that this year gold three silver four has become gold one silver two.

Alibaba P6 employees came to a small company for an interview and asked for an annual salary increase of 500000 yuan. How dare you speak

Qsort sort string

Abstractqueuedsynchronizer (AQS) source code detailed analysis - condition queue process analysis

leetcode:19. 删除链表的倒数第 N 个结点

tidb4.0.0遇见的问题、报错总结(tiup部署)
随机推荐
R language uses the < - operator to create new variables, uses the attach function to bind data, and directly uses the two data column names to calculate mean value to make new feelings in D
Give two strings S and T, and judge whether T is the word formed after rearrangement of S
Thread pool source code analysis_ 01 futuretask source code analysis
Unity's network request_ Short connection
【活动早知道】LiveVideoStack近期活动一览
Base de données de sauvegarde DumpLiNg
A command starts the monitoring journey!
GQL+Nodejs+MySQL数据库
适配华为机型中出现的那些坑
The way of filling holes in APK Decompilation
Tidb and MySQL modify system variables / common statements (kill the process in process)
Introduction to list operation in C #
Audio immersive experience
Job hopping is better than promotion
【C】 [time operation] time operation in C language
Dumpling备份数据库
Tidb3.0- 4.0 memory control / modification log saving days / maximum index length
What should I do if a white page appears during MySQL installation
Post-Process初级使用笔记(重要的几项)
[vs], [usage problem], [solution] when VS2010 is opened, it stays in the startup interface