STM32G0学习手册——FreeRTOS事件组
Posted SCCELE
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32G0学习手册——FreeRTOS事件组相关的知识,希望对你有一定的参考价值。
1.创建好带有printf-stdarg.c的FreeRTOS的工程
2.修改main.c文件
/* USER CODE BEGIN Includes */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
static TaskHandle_t AppTask_Handle = NULL;
static TaskHandle_t KEY1Task_Handle = NULL;
static TaskHandle_t KEY2Task_Handle = NULL;
static TaskHandle_t LEDTask_Handle = NULL;
static EventGroupHandle_t KeyEventGroup_Handle = NULL;
static EventBits_t KeyEvent_Counting = 0;
extern int f_printf(const char *format, ...);
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
static void AppTaskCreate(void);
static void Key1TaaskCreate(void);
static void Key2TaskCreate(void);
static void LedTaskCreate(void);
/* USER CODE END PFP */
int main()
{
/*
*
*/
/* USER CODE BEGIN WHILE */
UBaseType_t xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
(const char *) "APPTASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 1,
(TaskHandle_t*)&AppTask_Handle);
if(xReturn != NULL)
{
f_printf("APPTASK CREATE PASS.\\r\\n");
vTaskStartScheduler();
}
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
static void AppTaskCreate(void)
{
UBaseType_t xReturn = NULL;
while(1)
{
/* The event group must be created first, otherwise the task cannot be executed.*/
KeyEventGroup_Handle = xEventGroupCreate();
xReturn = xTaskCreate((TaskFunction_t)Key1TaaskCreate,
(const char *) "KEY1TASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 3,
(TaskHandle_t *)&KEY1Task_Handle);
if(xReturn != NULL)
f_printf("KEY1TASK CREATE PASS.\\r\\n");
xReturn = xTaskCreate((TaskFunction_t)Key2TaskCreate,
(const char *) "KEY2TASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 4,
(TaskHandle_t *)&KEY2Task_Handle);
if(xReturn != NULL)
f_printf("KEY2TASK CREATE PASS.\\r\\n");
xReturn = xTaskCreate((TaskFunction_t)LedTaskCreate,
(const char *) "LEDTASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 1,
(TaskHandle_t *)&LEDTask_Handle);
if(KeyEventGroup_Handle != NULL)
{
f_printf("KEY EVENTGROUP CREATE PASS.\\r\\n");
vTaskDelete((TaskHandle_t)AppTask_Handle);
}
}
}
static void Key1TaaskCreate(void)
{
while(1)
{
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
vTaskDelay(40);
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
xEventGroupSetBits(KeyEventGroup_Handle,0x01);
}
}
vTaskDelay(100);
}
}
static void Key2TaskCreate(void)
{
while(1)
{
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12)==0)
{
vTaskDelay(40);
if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12)==0)
{
xEventGroupSetBits(KeyEventGroup_Handle,0x2);
}
}
vTaskDelay(100);
}
}
static void LedTaskCreate(void)
{
while(1)
{
KeyEvent_Counting = xEventGroupWaitBits ( KeyEventGroup_Handle, 0x1, pdTRUE, pdTRUE, 10);
if((KeyEvent_Counting & 0x01) == 0x01)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
vTaskDelay(200);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
}
KeyEvent_Counting = xEventGroupWaitBits ( KeyEventGroup_Handle, 0x2, pdTRUE, pdTRUE, 10);
if((KeyEvent_Counting & 0x02)==0x02)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_RESET);
vTaskDelay(200);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,GPIO_PIN_SET);
}
KeyEvent_Counting = xEventGroupWaitBits ( KeyEventGroup_Handle, 0x3, pdTRUE, pdTRUE, 10);
if((KeyEvent_Counting &0x03)==0x03)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2|GPIO_PIN_1,GPIO_PIN_RESET);
vTaskDelay(200);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2|GPIO_PIN_1,GPIO_PIN_SET);
xEventGroupClearBits(KeyEventGroup_Handle,0x03);
}
vTaskDelay(50);
}
}
/* USER CODE END 4 */
以上是关于STM32G0学习手册——FreeRTOS事件组的主要内容,如果未能解决你的问题,请参考以下文章