CubeMX FreeRTOS(cmsis os)信号量的使用,Signal Events

相关函数官方解释:
资料来源,参照
https://blog.csdn.net/weixin_44578655/article/details/104622287

int32_t osSignalSet (osThreadId thread_id, int32_t signals)
//Set the specified Signal Flags of an active thread.
给一个线程创建信号量
int32_t osSignalClear (osThreadId thread_id, int32_t signals)
//Clear the specified Signal Flags of an active thread.
删除一个线程的信号量
osEvent osSignalWait (int32_t signals, uint32_t millisec)
//Wait for one or more Signal Flags to become signaled for the current RUNNING thread.
该线程进入阻塞态等待有一个或多个信号量来临

信号量的作用,官方给出如下解释:
Description
Signals are used to trigger execution states between threads. The signal management functions in CMSIS-RTOS allow you to control or wait for signal flags. Each thread has up to 31 assigned signal flags. The maximum number of signal flags is defined in the cmsis_os.h file (#define osFeature_Signals).
信号量用于不同任务间状态的切换,信号量提供了一种任务等待信号到来的机制,每个任务最多有31个信号量,最大数量定义在cmsis_os.h中,#define osFeature_Signals。
对于一个任务来说
A thread

  • can wait for signals to be set (using osSignalWait). Using this
    function, it enters the WAITING state. The osSignalWait parameter
    signals defines the signals that are required to put the thread back
    into READY state.
    一个任务可以使用osSignalWait来等待信号量的到来,使用这个函数,它会进入阻塞态,其中参数signals 定义了此任务要等待哪个信号量来以进入就绪态。
  • may set one or more flags in any other given thread (using
    osSignalSet).
    一个任务可以使用osSignalSet创建一个或多个信号量
  • may clear its own signals or the signals of other threads (using
    osSignalClear).
    一个任务可以使用osSignalClear清除它自己或者他人的信号量
    When a thread wakes up and resumes execution, its signal flags are automatically cleared.
    当一个任务被唤醒或恢复运行,他的信号量会被自动清除。
    下面演示任务间通过信号量通讯的过程
    官方示意图左边是一个用来产生信号量的任务,每1s产生一个信号量0x0001,右侧的任务等待信号量0x0001,一旦等到,则自动将该信号量清零,进行“do something”。
    能很清楚的看到,通过信号量实现了“事件”的功能,左侧相当于一个触发器,右侧相当于一个事件回调函数。
    上面图示的实例为:
	//左侧任务
    osSignalSet (tid_thread1, 0x0001);    // set the signal 0x0001 for thread tid_thread1
    osDelay (1000);                       // wait for 1 second
    //右侧任务
    osSignalWait (0x0001, osWaitForever); // wait forever for the signal 0x0001
    /****do something****/

osSignalSet 是支持在中断函数中调用的
信号量机制其实很像我们平时裸跑时轮询标志位的操作,但比那种方式方便,且实时性更好。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值