赞
踩
概述
优先级消息队列相对消息队列来说,给消息附加了一个优先级的概念,较高优先级的消息会比较低优先级的消息更快地被其他任务收到(本质上,消息队列的底层数据容器是环形队列,优先级消息队列的底层数据容器是优先级队列)。
API讲解
编程实例
1、在tos_config.h中,配置优先级消息队列组件开关TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN:
#define TOS_CFG_PRIORITY_MESSAGE_QUEUE_EN 1u
2、编写main.c示例代码:
- /* USER CODE END Header */
-
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "usart.h"
- #include "gpio.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "cmsis_os.h"
- #include "stdio.h"
-
- #include "tos_k.h"
- /* USER CODE END Includes */
-
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- #define STK_SIZE_TASK_RECEIVER 512
- #define STK_SIZE_TASK_SENDER 512
-
- #define MESSAGE_MAX 10
-
- /* USER CODE END PTD */
-
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- k_stack_t stack_task_receiver[STK_SIZE_TASK_RECEIVER];
- k_stack_t stack_task_sender[STK_SIZE_TASK_SENDER];
-
- uint8_t msg_pool[MESSAGE_MAX * sizeof(void *)];
-
- k_task_t task_receiver;
- k_task_t task_sender;
-
- k_prio_msg_q_t prio_msg_q;
-
- extern void entry_task_receiver(void *arg);
- extern void entry_task_sender(void *arg);
-
- /* USER CODE END PD */
-
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- void entry_task_receiver(void *arg)
- {
- k_err_t err;
- void *msg_received;
-
- while (K_TRUE) {
- err = tos_prio_msg_q_pend(&prio_msg_q, &msg_received, TOS_TIME_FOREVER);
- if (err == K_ERR_NONE) {
- printf("receiver: msg incoming[%s]\n", (char *)msg_received);
- }
- }
- }
-
- void entry_task_sender(void *arg)
- {
- char *msg_prio_0 = "msg with priority 0";
- char *msg_prio_1 = "msg with priority 1";
- char *msg_prio_2 = "msg with priority 2";
-
- printf("sender: post a message with priority 2\n");
- tos_prio_msg_q_post(&prio_msg_q, msg_prio_2, 2);
-
- printf("sender: post a message with priority 1\n");
- tos_prio_msg_q_post(&prio_msg_q, msg_prio_1, 1);
-
- printf("sender: post a message with priority 0\n");
- tos_prio_msg_q_post(&prio_msg_q, msg_prio_0, 0);
- }
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
-
- /* USER CODE BEGIN PV */
-
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
-
- /* USER CODE END PFP */
-
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
-
- /* USER CODE END 0 */
-
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
-
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
-
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
-
- /* USER CODE BEGIN Init */
-
- /* USER CODE END Init */
-
- /* Configure the system clock */
- SystemClock_Config();
-
- /* USER CODE BEGIN SysInit */
-
- /* USER CODE END SysInit */
-
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- /* USER CODE BEGIN 2 */
-
- tos_knl_init();
- tos_prio_msg_q_create(&prio_msg_q, msg_pool, MESSAGE_MAX);
- (void)tos_task_create(&task_receiver, "receiver", entry_task_receiver, NULL,
- 5, stack_task_receiver, STK_SIZE_TASK_RECEIVER, 0);
- (void)tos_task_create(&task_sender, "sender", entry_task_sender, NULL,
- 4, stack_task_sender, STK_SIZE_TASK_SENDER, 0);
- tos_knl_start();
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
-
- }
- /* USER CODE END 3 */
- }

3、运行效果:
源码链接:Git
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。