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 <stdio.h>
#include <string.h>
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
static TaskHandle_t AppTaskCreate_Handle = NULL;
static TaskHandle_t KeyTask_Handle = NULL;
static TaskHandle_t Led1Task_Handle = NULL;
/*Create a binary semaphore handle*/
static SemaphoreHandle_t Binary_Handle = NULL;
extern int f_printf(const char *format, ...);
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
/*Create a task function*/
static void AppTaskCreate(void);
static void KeyTaskCreate(void);
static void Led1TaskCreate(void);
/* USER CODE END PFP */
int main()
{
/*
*
*/
/* USER CODE BEGIN WHILE */
UBaseType_t xReturn = pdPASS;
xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
(const char *) "APPTASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 2,
(TaskHandle_t *)&AppTaskCreate_Handle);
if(xReturn ==pdPASS)
{
vTaskStartScheduler();
f_printf("start..\\r\\n");
}
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 = pdPASS;
xReturn = xTaskCreate((TaskFunction_t) KeyTaskCreate,
(const char *) "KEYTASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t ) 2,
(TaskHandle_t *) &KeyTask_Handle);
xReturn = xTaskCreate((TaskFunction_t) Led1TaskCreate,
(const char *) "LED1TASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 1,
(TaskHandle_t *)&Led1Task_Handle);
/*Create a binary semaphore*/
Binary_Handle = xSemaphoreCreateBinary();
if(Binary_Handle !=NULL)
f_printf("Create Semph_Binary PASS.\\r\\n");
if(xReturn == pdPASS)
vTaskDelete((TaskHandle_t)AppTaskCreate_Handle);
}
static void KeyTaskCreate(void)
{
while(1)
{
UBaseType_t xReturn = pdPASS;
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
f_printf("KEY TASK PASS.\\r\\n");
vTaskDelay(100);
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
xReturn = xSemaphoreGive(Binary_Handle);
if(xReturn == pdPASS)
f_printf("GIVE BINARY PASS.\\r\\n");
else if(xReturn == errQUEUE_FULL)
f_printf("QUEUE IS FULL.\\r\\n");
}
}
vTaskDelay(100);
}
}
static void Led1TaskCreate(void)
{
while(1)
{
UBaseType_t xReturn = pdPASS;
xReturn = xSemaphoreTake(Binary_Handle, portMAX_DELAY);
if(xReturn == pdPASS)
{
f_printf("TAKE BINARY PASS.\\r\\n");
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_2);
}
vTaskDelay(100);
}
}
/* USER CODE END 4 */
计数信号量的用法示例
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 <stdio.h>
#include <string.h>
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
static TaskHandle_t AppTaskCreate_Handle = NULL;
static TaskHandle_t KeyTask_Handle = NULL;
static TaskHandle_t Led1_Handle = NULL;
SemaphoreHandle_t SemaphCount_Handle = NULL;
extern int f_printf(const char *format, ...);
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
static void AppTaskCreate(void);
static void KeyTaskCreate(void);
static void LED1TaskCreate(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) 2,
(TaskHandle_t *)&AppTaskCreate_Handle);
if(xReturn ==pdPASS)
{
f_printf("TASK START.\\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 ;
xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,
(const char *) "KEYTASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 2,
(TaskHandle_t*)&KeyTask_Handle);
if(xReturn == pdPASS)
f_printf("KEY CREATE PASS.\\r\\n");
xReturn = xTaskCreate((TaskFunction_t)LED1TaskCreate,
(const char *) "LED1TASKCREATE",
(uint16_t) 128,
(void *) NULL,
(UBaseType_t) 1,
(TaskHandle_t*)&Led1_Handle);
SemaphCount_Handle = xSemaphoreCreateCounting(3,0);
if(SemaphCount_Handle != NULL)
f_printf("SEMAPHORE COUNT CREATE PASS.\\r\\n");
if(xReturn == pdPASS)
{
f_printf("LED CREATE PASS.\\r\\n");
vTaskDelete((TaskHandle_t)AppTaskCreate_Handle);
}
}
static void KeyTaskCreate(void)
{
while(1)
{
UBaseType_t xReturn = pdPASS;
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
vTaskDelay(100);
if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
{
f_printf("KEY PRESS.\\r\\n");
xReturn = xSemaphoreGive(SemaphCount_Handle);
if(xReturn == pdPASS)
f_printf("GIVE COUNTING PASS.\\r\\n");
else if(xReturn == errQUEUE_FULL)
f_printf("QUEUE IS FULL.\\r\\n");
}
}
vTaskDelay(10);
}
}
static void LED1TaskCreate(void)
{
while(1)
{
UBaseType_t xReturn = pdPASS;
xReturn = xSemaphoreTake(SemaphCount_Handle,portMAX_DELAY);
if(xReturn == pdTRUE)
{
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
f_printf("Counting Take PASS.\\r\\n");
}
else if(xReturn == errQUEUE_EMPTY)
f_printf("semaphore is empty.\\r\\n");
vTaskDelay(2000);
}
}
/* USER CODE END 4 */
以上是关于STM32G0学习手册——FreeRTOS信号量的主要内容,如果未能解决你的问题,请参考以下文章