你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

RTOS超低功耗平台应用---信号量应用

[复制链接]
BinWin 发布时间:2018-4-14 23:11
接上篇:RTOS超低功耗平台应用---FreeRTOS启动&任务创建



     首先了解一下信号量,一般信号量主要实现两个功能:
     1,两个任务之间或者中断函数跟任务之间的同步功能
     2,多个共享资源的管理
    FreeRTOS针对这两种功能分别提供了二值信号量和计数信号量,其中二值信号量可以理解成计数,即初始化为仅有一个资源可以使用。
    实验是创建一个线程,该线程通过ISR发出的信号量打印按钮按下计数,包含一个LED闪烁任务。
  1. /**
  2. * @brief  Start a Task.
  3. * @param  argument: Not used
  4. * @retval None
  5. */
  6. static void vAppStartTask( void *pvParameters )
  7. {
  8.         (void) pvParameters;
  9.         
  10.         for(;;)
  11.         {
  12.                 BSP_LED_Toggle(LED2);
  13.                
  14.                 vTaskDelay(200);
  15.         }
  16. }

  17. /**
  18. * @brief  Button Task.
  19. * @param  argument: Not used
  20. * @retval None
  21. */
  22. static void prvAppButtonTask( void *pvParameters )
  23. {
  24.         configASSERT( xTestSemaphore ); /* The assert function determines whether the semaphore is valid. */
  25.         
  26.         /* This is the task used as an example of how to synchronise a task with
  27.         an interrupt.  Each time the button interrupt gives the semaphore, this task
  28.         will unblock, increment its execution counter, then return to block
  29.         again. */
  30.         
  31.         /* Take the semaphore before started to ensure it is in the correct
  32.         state. */
  33.         xSemaphoreTake( xTestSemaphore, mainDONT_BLOCK );
  34.         
  35.         for( ;; )
  36.         {
  37.                 xSemaphoreTake( xTestSemaphore, portMAX_DELAY );
  38.                 ulButtonPressCounts++;
  39.                 printf("Button Press Counts:%d\r\n",ulButtonPressCounts);
  40.         }
  41. }

  42. /**
  43. * @brief EXTI line detection callback.
  44. * @param GPIO_Pin: Specifies the pins connected EXTI line
  45. * @retval None
  46. */
  47. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  48. {
  49.         if(GPIO_Pin == KEY_BUTTON_PIN)
  50.         {
  51.                 if (uwIncrementState == 0)
  52.                 {
  53.                         /* Change the Push button state */
  54.                         uwIncrementState = 1;
  55.                 }
  56.                 else
  57.                 {
  58.                         /* Change the Push button state */
  59.                         uwIncrementState = 0;
  60.                 }
  61.         }  
  62. }
复制代码
    主函数中使用xSemaphoreCreateBinary() 创建信号量,
  1. xTestSemaphore = xSemaphoreCreateBinary();
复制代码
    按键检测任务中使用xSemaphoreTake()函数来获取信号量。按键检测使用外部中断,信号量使用完毕后的释放也是在终端服务函数中,如下
  1. xSemaphoreTake( xTestSemaphore, mainDONT_BLOCK );  //获取信号,传入创建的信号量句柄xTestSemaphore
复制代码
  1. /**
  2. * @brief  EXTI15_10 IRQ
  3. * @param  None
  4. * @retval None
  5. */
  6. void EXTI15_10_IRQHandler(void)
  7. {
  8.         
  9.         static BaseType_t xHigherPriorityTaskWoken;                     
  10.         
  11.         /* Inerrupt message handling */
  12.         HAL_GPIO_EXTI_IRQHandler(KEY_BUTTON_PIN);               
  13.         
  14.         /* The handler releases a semaphore.
  15.         xHigherPriorityTaskWoken has been initialised to zero. */
  16.         
  17.         if (utime_tick >= 200)
  18.         {
  19.                 /* The second parameter of the send sync signal is used to save
  20.                 whether a high-priority task is ready.*/
  21.                 xSemaphoreGiveFromISR( xTestSemaphore, &xHigherPriorityTaskWoken );        
  22.                 utime_tick = 0;
  23.         }
复制代码


       程序运行后会执行LED闪烁任务按键计数任务,当获取到信号量后计数值加1,同时释放信号量共下次资源可用。运行效果如下       C(HBH$]QP{3[)V]GZJ6EWHF.png

收藏 评论0 发布时间:2018-4-14 23:11

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版