STM32G0学习手册——FreeRTOS消息队列

Posted SCCELE

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32G0学习手册——FreeRTOS消息队列相关的知识,希望对你有一定的参考价值。

1.创建好带有printf-stdarg.c的FreeRTOS的工程

2.修改main.c文件

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include <stdio.h>
#include <string.h>

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define QUEUE_LEN   4
#define QUEUE_SIZE  4

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
static TaskHandle_t CreateAppTask_Handle = NULL;
static TaskHandle_t Led1Task_Handle =  NULL;
static TaskHandle_t Led2Task_Handle =  NULL;

static QueueHandle_t Message1 = NULL;

extern int f_printf(const char *format, ...);

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
static void CreateAppTask(void);
static void Led1Task(void);
static void Led2Task(void);

/* USER CODE END PV */

int main()
{
  /**/

  /* USER CODE BEGIN 2 */

	UBaseType_t xReturn = pdPASS;
	
	xReturn = xTaskCreate((TaskFunction_t) CreateAppTask,
	                      (const char *)   "CAP",
	                      (uint16_t)       512,
						  (void *)         NULL,
						  (UBaseType_t)    1,
						  (TaskHandle_t *) &CreateAppTask_Handle);
	
  if(xReturn == pdPASS)
	{
		f_printf("start.\\r\\n");
		vTaskStartScheduler();
	}

  /* USER CODE END 2 */
  while(1)
  {
  
  }
}

/* USER CODE BEGIN 4 */
static void CreateAppTask(void)
{
	UBaseType_t xReturn = pdPASS;
	
    taskENTER_CRITICAL();
	
	/*消息队列任务一定要在调用的任务前面创建,不然代码运行不了*/
	Message1 = xQueueCreate((UBaseType_t)QUEUE_LEN , (UBaseType_t)QUEUE_SIZE);
	
	taskEXIT_CRITICAL();
	
	xReturn = xTaskCreate((TaskFunction_t) Led1Task,
												(const char *) "LED1TASK",
												(uint16_t)      512,
												(void *)        NULL,
												(UBaseType_t)  1,
												(TaskHandle_t *) &Led1Task_Handle);
	
    xReturn = xTaskCreate((TaskFunction_t) Led2Task,
												(const char*) "LED2TASK",												
												(uint16_t)     512,
												(void *)       NULL,
												(UBaseType_t)  2,
												(TaskHandle_t *) &Led2Task_Handle);
	if(xReturn ==pdPASS)
		f_printf("TASKCREATE PASS.\\r\\n");
	

	
	if(NULL != Message1)
	{
	  f_printf("QUEUE PASS.\\r\\n");
	  vTaskDelete((TaskHandle_t)CreateAppTask_Handle);
	}
}
static void Led1Task(void)
{
	while(1)
	{
		UBaseType_t xReturn ;
		uint16_t a=1;
		uint16_t temp=0;
		uint16_t temp2=0;

		if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
		{
			for(temp = 0 ; temp < 10 ; temp++)
			{
				if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
				{
					temp2 |= 1;
					temp2 <<= 1;
					vTaskDelay(30);
				}
				else
				{
					temp2 &= 0xFFFE;
					temp2 <<= 1;
					vTaskDelay(30);
				}
			}
			if(temp2 > 0x1F)
			{
			  xReturn = xQueueSend(Message1,&a,0);
			  if(xReturn == pdPASS)
			  f_printf("SEND PASS \\r\\n");
			}
		}

	}
}
static void Led2Task(void)
{
	while(1)
	{
		BaseType_t xReturn = pdTRUE;
		uint16_t receive_buff ;
		xReturn = xQueueReceive (Message1, &receive_buff,portMAX_DELAY);
		
		if(xReturn == pdTRUE)
		{
			HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
			f_printf("RECEIVE PASS\\r\\n");
		}

	}
}

/* USER CODE END 4 */

 

以上是关于STM32G0学习手册——FreeRTOS消息队列的主要内容,如果未能解决你的问题,请参考以下文章

STM32G0学习手册——FreeRTOS消息队列

STM32G0学习手册——FreeRTOS任务通知

STM32G0学习手册——FreeRTOS任务通知

STM32G0学习手册——FreeRTOS任务通知

STM32G0学习手册——FreeRTOS事件组

STM32G0学习手册——FreeRTOS事件组