目录
二值信号量的用法示例
二值信号量的用法示例
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 */
|