TencentOS-tiny 任务间通信(六)- 完成量

一、任务间通信

  1. 完成量

概述

完成量是一种简单的任务间通信机制,用以在任务间同步某一事件是否已“完成”的信息。

API讲解

编程实例

1、在tos_config.h中,配置完成量组件开关TOS_CFG_COMPLETION_EN:

#define TOS_CFG_COMPLETION_EN 1u

TencentOS-tiny 任务间通信(六)- 完成量_第1张图片

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 */

/*
 这个例子里创建了两个任务,一个任务task_wait等待完成量完成,另一个任务负责触发完成量(让完成量完成)
 */
#define STK_SIZE_TASK_WAIT      512
#define STK_SIZE_TASK_TRIGGER   512 
 
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
k_stack_t stack_task_wait[STK_SIZE_TASK_WAIT];
k_stack_t stack_task_trigger[STK_SIZE_TASK_TRIGGER];

k_task_t task_wait;
k_task_t task_trigger;

k_completion_t completion; 

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
void entry_task_wait(void *arg)
{
    printf("wait: I won't go further until someone do the trigger(make it 'complete')\n");
    tos_completion_pend(&completion);
    printf("wait: someone has made it complete, so I'm here\n");
}

void entry_task_trigger(void *arg)
{
    printf("trigger: I'm the one who make complete, anyone waitting for the complete won't go further until I do the trigger\n");
    tos_completion_post(&completion);
    printf("trigger: I have done the completion\n");
}
/* 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_completion_create(&completion);
	(void)tos_task_create(&task_wait, "wait", entry_task_wait, NULL,
													3, stack_task_wait, STK_SIZE_TASK_WAIT, 0);
	(void)tos_task_create(&task_trigger, "trigger", entry_task_trigger, NULL,
													4, stack_task_trigger, STK_SIZE_TASK_TRIGGER, 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、运行效果:

TencentOS-tiny 任务间通信(六)- 完成量_第2张图片

源码链接:Git​​​​​​​

 

你可能感兴趣的:(RTOS)