查看: 3195|回复: 0

[分享] freeRTOS信号量学习

[复制链接]

该用户从未签到

7

主题

15

帖子

0

新手上路

Rank: 1

积分
43
最后登录
2018-4-10
发表于 2016-8-12 17:30:50 | 显示全部楼层 |阅读模式
freeRTOS信号量学习.jpg

信号量同样是RTOS学习中很重要的一节,信号量可以用在共享资源或者同步任务中,对执行权的控制,谁拥有信号量谁拥有执行权,在freeRTOS中信号量和互斥量有点不同,关于信号量的更多描述可以参考官网相关网页描述。每一个信号量都需要少量的内存来保持信号量的状态,那么这内存是如何分配的呢,这根据使用的API函数会有所不同,创建信号量主要有xSemaphoreCreateBinary()xSemaphoreCreateBinaryStatic() ,使用前者创建信号量,则所需的内存将会自动从freeRTOS的堆上分配,如果是使用后者创建的信号量,则所需内存由应用程序分配,且后者API需要另外的参数,在编译的时候静态分配给信号量,前者则是动态分配,关于静态分配和动态分配可以参阅freeRTOS官网详细信息。

我们看一下两种API创建信号量使用的例子

Example usage:

SemaphoreHandle_txSemaphore;
  1. void vATask( void *pvParameters )

  2. {

  3.     /* Attempt to create a semaphore. */

  4.     xSemaphore = xSemaphoreCreateBinary();



  5.     if( xSemaphore == NULL )

  6.     {

  7.         /* There was insufficient FreeRTOS heap available for thesemaphore to

  8.         be created successfully. */

  9.     }

  10.     else

  11.     {

  12.         /* The semaphore can now be used. Its handle is stored inthe

  13.         xSemahore variable.  Calling xSemaphoreTake() on the semaphorehere

  14.         will fail until the semaphore has firstbeen given. */

  15.     }

  16. }

  17. Example usage:

  18. SemaphoreHandle_t xSemaphore = NULL;

  19. StaticSemaphore_t xSemaphoreBuffer;

  20. void vATask( void * pvParameters )

  21. {

  22.     /* Create a binary semaphore without using any dynamic memory

  23.     allocation.  The semaphore's data structures will be saved into

  24.     the xSemaphoreBuffer variable. */

  25.     xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer );



  26.    /* The pxSemaphoreBuffer was not NULL, so it is expected that the

  27.     handle will not be NULL. */

  28.     configASSERT( xSemaphore );

  29.     /* Rest of the task code goes here. */

  30. }
复制代码

在公众号前面的文章中我们在kv46上移植的demo有官方提供的信号量的例程,推荐大家下载最新版的v9.0.0源码学习,新更新的特性和内容在源码包里都有提及,研究例程是最好的学习方法。这里只是给大家简单介绍使用方法,更加详细的内容还需自己仔细阅读源码和官方参考资料。

嵌入式程序猿

回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|恩智浦技术社区

GMT+8, 2025-7-18 05:49 , Processed in 0.077650 second(s), 19 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表