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

【经验分享】STM32FreeRTOS队列的简介及操作

[复制链接]
STMCU小助手 发布时间:2022-5-21 18:00
前言
本文主要带大家了解队列的基本知识和队列的基本操作。

一、队列基本知识介绍
队列是为了任务与任务、任务与中断之间的通信而准备的,可以在任务与任务、任务与中断之间传递消息,队列中可以存储有限的、大小固定的数据项目。任务与任务、任务与中断之间要交流的数据保存在队列中,叫做队列项目。队列所能保存的最大数据项目数量叫做队列的长度,创建队列的时候会指定数据项目的大小和队列的长度。由于队列用来传递消息的,所以也称为消息队列。(本段话取自正点原子freeRTOS开发手册这里只做简单介绍想要详细了解可以去看看原子的手册,这里主要教大家如何去配置和使用队列)

二、队列的基本操作
cubeMX配置


G4_}{HU1SYU]73MXJT093EL.png

1.Queue name:队列名字
2.Queue size:队列大小
3.Lite size:队列里面每一个数据的类型

代码讲解
1.cubeMX自动生成的创建队列代码

  1. /* Create the queue(s) */
  2.   /* definition and creation of myQueue01 */
  3.   osMessageQDef(myQueue01, 16, uint16_t);
  4.   myQueue01Handle = osMessageCreate(osMessageQ(myQueue01), NULL);

  5.   /* USER CODE BEGIN RTOS_QUEUES */
  6.   /* add queues, ... */
  7.   /* USER CODE END RTOS_QUEUES */
复制代码

2.把一个消息写入队列函数

  1. /**
  2. * @brief Put a Message to a Queue.
  3. * @param  queue_id  message queue ID obtained with \ref osMessageCreate.
  4. * @param  info      message information.
  5. * @param  millisec  timeout value or 0 in case of no time-out.
  6. * @retval status code that indicates the execution status of the function.
  7. * @note   MUST REMAIN UNCHANGED: \b osMessagePut shall be consistent in every CMSIS-RTOS.
  8. */
  9. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec)
  10. {
  11.   portBASE_TYPE taskWoken = pdFALSE;
  12.   TickType_t ticks;

  13.   ticks = millisec / portTICK_PERIOD_MS;
  14.   if (ticks == 0) {
  15.     ticks = 1;
  16.   }

  17.   if (inHandlerMode()) {
  18.     if (xQueueSendFromISR(queue_id, &info, &taskWoken) != pdTRUE) {
  19.       return osErrorOS;
  20.     }
  21.     portEND_SWITCHING_ISR(taskWoken);
  22.   }
  23.   else {
  24.     if (xQueueSend(queue_id, &info, ticks) != pdTRUE) {
  25.       return osErrorOS;
  26.     }
  27.   }

  28.   return osOK;
  29. }
复制代码

3.从队列中读出消息函数

  1. /**
  2. * @brief Get a Message or Wait for a Message from a Queue.
  3. * @param  queue_id  message queue ID obtained with \ref osMessageCreate.
  4. * @param  millisec  timeout value or 0 in case of no time-out.
  5. * @retval event information that includes status code.
  6. * @note   MUST REMAIN UNCHANGED: \b osMessageGet shall be consistent in every CMSIS-RTOS.
  7. */
  8. osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec)
  9. {
  10.   portBASE_TYPE taskWoken;
  11.   TickType_t ticks;
  12.   osEvent event;

  13.   event.def.message_id = queue_id;
  14.   event.value.v = 0;

  15.   if (queue_id == NULL) {
  16.     event.status = osErrorParameter;
  17.     return event;
  18.   }

  19.   taskWoken = pdFALSE;

  20.   ticks = 0;
  21.   if (millisec == osWaitForever) {
  22.     ticks = portMAX_DELAY;
  23.   }
  24.   else if (millisec != 0) {
  25.     ticks = millisec / portTICK_PERIOD_MS;
  26.     if (ticks == 0) {
  27.       ticks = 1;
  28.     }
  29.   }

  30.   if (inHandlerMode()) {
  31.     if (xQueueReceiveFromISR(queue_id, &event.value.v, &taskWoken) == pdTRUE) {
  32.       /* We have mail */
  33.       event.status = osEventMessage;
  34.     }
  35.     else {
  36.       event.status = osOK;
  37.     }
  38.     portEND_SWITCHING_ISR(taskWoken);
  39.   }
  40.   else {
  41.     if (xQueueReceive(queue_id, &event.value.v, ticks) == pdTRUE) {
  42.       /* We have mail */
  43.       event.status = osEventMessage;
  44.     }
  45.     else {
  46.       event.status = (ticks == 0) ? osOK : osEventTimeout;
  47.     }
  48.   }

  49.   return event;
  50. }
复制代码

总结
队列操作大家可以试一试还是很简单的。


收藏 评论0 发布时间:2022-5-21 18:00

举报

0个回答

所属标签

相似技术帖

官网相关资源

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