13_信号量

信号量

信号量的简介

image-20240327143807800

image-20240327143819204

队列与信号量的对比

image-20240327143831464

二值信号量

image-20240327143845368

二值信号量相关API函数

image-20240327143859259

创建二值信号量函数

image-20240327144046949

释放二值信号量函数

image-20240327144143185

获取二值信号量函数

image-20240327144159738

二值信号量实验

image-20240327144213047

代码

freertos_demo.c

/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:正点原子 探索者F407开发板
 * 在线视频:www.yuanzige.com
 * 技术论坛:www.openedv.com
 * 公司网址:www.alientek.com
 * 购买地址:openedv.taobao.com
 *
 ****************************************************************************************************
 */

#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
#include "./SYSTEM/delay/delay.h"
#include "./MALLOC/malloc.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

/******************************************************************************************************/
/*FreeRTOS配置*/

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_STACK_SIZE 	128
#define START_TASK_PRIO			1
TaskHandle_t start_task_handler;

#define TASK1_STACK_SIZE 		128
#define TASK1_PRIO				2
TaskHandle_t task1_handler;

#define TASK2_STACK_SIZE 		128
#define TASK2_PRIO				3
TaskHandle_t task2_handler;

QueueHandle_t semphore_handle;

void start_task( void * pvParameters );

void task1( void * pvParameters )
{
	uint8_t key = 0;
	BaseType_t err;
	while(1)
	{
		key = key_scan(0);
		if(key == KEY0_PRES)
		{
			if(semphore_handle != NULL)
			{
				err = xSemaphoreGive(semphore_handle);
				if(err == pdPASS)
				{
					printf("二值信号量释放成功!!!\r\n");
				}else
				{
					printf("二值信号量释放失败!!!\r\n");
				}
			}
		}
		vTaskDelay(10);
	}
}

void task2( void * pvParameters )
{
	BaseType_t err;
	while(1)
	{
		err = xSemaphoreTake(semphore_handle, portMAX_DELAY); //死等
		if(err == pdTRUE)
		{
			printf("二值信号量获取成功!!!\r\n");
		}else
		{
			printf("二值信号量获取失败!!!\r\n");
		}
	}
}

/******************************************************************************************************/

/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void freertos_demo(void)
{
	semphore_handle = xSemaphoreCreateBinary(); //创建二值信号量
	if(semphore_handle != NULL)
	{
		printf("二值信号量创建成功!!!\r\n");
	}
    xTaskCreate(start_task, "start_task", START_TASK_STACK_SIZE, NULL, START_TASK_PRIO, &start_task_handler);
	vTaskStartScheduler();
}

void start_task( void * pvParameters )
{
	taskENTER_CRITICAL(); //进入临界区
	xTaskCreate(task1, "task1", TASK1_STACK_SIZE, NULL, TASK1_PRIO, &task1_handler);
	xTaskCreate(task2, "task2", TASK2_STACK_SIZE, NULL, TASK2_PRIO, &task2_handler);
    vTaskDelete(NULL);
	taskEXIT_CRITICAL(); //退出临界区
}

计数型信号量

image-20240327144232318

计数型信号量相关API函数

image-20240327144250965

计数型信号量创建API函数

image-20240327144309707

image-20240327144322059

计数型信号量实验

image-20240327144338023

代码

freertos_demo.c

/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:正点原子 探索者F407开发板
 * 在线视频:www.yuanzige.com
 * 技术论坛:www.openedv.com
 * 公司网址:www.alientek.com
 * 购买地址:openedv.taobao.com
 *
 ****************************************************************************************************
 */

#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
#include "./SYSTEM/delay/delay.h"
#include "./MALLOC/malloc.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

/******************************************************************************************************/
/*FreeRTOS配置*/

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_STACK_SIZE 	128
#define START_TASK_PRIO			1
TaskHandle_t start_task_handler;

#define TASK1_STACK_SIZE 		128
#define TASK1_PRIO				2
TaskHandle_t task1_handler;

#define TASK2_STACK_SIZE 		128
#define TASK2_PRIO				3
TaskHandle_t task2_handler;

QueueHandle_t count_semphore_handle;

void start_task( void * pvParameters );

void task1( void * pvParameters )
{
	uint8_t key = 0;
	BaseType_t err;
	while(1)
	{
		key = key_scan(0);
		if(key == KEY0_PRES)
		{
			if(count_semphore_handle != NULL)
			{
				err = xSemaphoreGive(count_semphore_handle);
				if(err == pdPASS)
				{
					printf("计数型信号量释放成功!!!\r\n");
				}else
				{
					printf("计数型信号量释放失败!!!\r\n");
				}
			}
		}
		vTaskDelay(10);
	}
}

void task2( void * pvParameters )
{
	BaseType_t err;
	UBaseType_t count = 0;
	while(1)
	{
		err = xSemaphoreTake(count_semphore_handle, portMAX_DELAY); //死等
		if(err == pdTRUE)
		{
			printf("计数型信号量获取成功!!!\r\n");
			count = uxSemaphoreGetCount(count_semphore_handle);
			printf("信号量计数值为:%ld\r\n", count);
		}else
		{
			printf("计数型信号量获取失败!!!\r\n");
		}
		vTaskDelay(1000);
	}
}

/******************************************************************************************************/

/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void freertos_demo(void)
{
	count_semphore_handle = xSemaphoreCreateCounting(100, 0); //创建计数型信号量
	if(count_semphore_handle != NULL)
	{
		printf("计数型信号量创建成功!!!\r\n");
	}
    xTaskCreate(start_task, "start_task", START_TASK_STACK_SIZE, NULL, START_TASK_PRIO, &start_task_handler);
	vTaskStartScheduler();
}

void start_task( void * pvParameters )
{
	taskENTER_CRITICAL(); //进入临界区
	xTaskCreate(task1, "task1", TASK1_STACK_SIZE, NULL, TASK1_PRIO, &task1_handler);
	xTaskCreate(task2, "task2", TASK2_STACK_SIZE, NULL, TASK2_PRIO, &task2_handler);
    vTaskDelete(NULL);
	taskEXIT_CRITICAL(); //退出临界区
}

优先级翻转简介

image-20240327144352270

image-20240327144402619

优先级翻转实验

image-20240327144426709

代码

freertos_demo.c

/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:正点原子 F407电机开发板
 * 在线视频:www.yuanzige.com
 * 技术论坛:www.openedv.com
 * 公司网址:www.alientek.com
 * 购买地址:openedv.taobao.com
 *
 ****************************************************************************************************
 */

#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
#include "./SYSTEM/delay/delay.h"
#include "./MALLOC/malloc.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/******************************************************************************************************/
/*FreeRTOS配置*/

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_PRIO         1
#define START_TASK_STACK_SIZE   128
TaskHandle_t    start_task_handler;
void start_task( void * pvParameters );

/* TASK1 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define TASK1_PRIO         2
#define TASK1_STACK_SIZE   128
TaskHandle_t    low_task_handler;
void low_task( void * pvParameters );


/* TASK2 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define TASK2_PRIO         3
#define TASK2_STACK_SIZE   128
TaskHandle_t    middle_task_handler;
void middle_task( void * pvParameters );


/* TASK3 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define TASK3_PRIO         4
#define TASK3_STACK_SIZE   128
TaskHandle_t    high_task_handler;
void high_task( void * pvParameters );

/******************************************************************************************************/
QueueHandle_t semphore_handle;
/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void freertos_demo(void)
{    
    semphore_handle = xSemaphoreCreateBinary();
    if(semphore_handle != NULL)
    {
        printf("二值信号量创建成功!!!\r\n");
    }
    xSemaphoreGive(semphore_handle);        /* 释放一次信号量 */
    
    xTaskCreate((TaskFunction_t         )   start_task,
                (char *                 )   "start_task",
                (configSTACK_DEPTH_TYPE )   START_TASK_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   START_TASK_PRIO,
                (TaskHandle_t *         )   &start_task_handler );
    vTaskStartScheduler();
}


void start_task( void * pvParameters )
{
    taskENTER_CRITICAL();               /* 进入临界区 */
    xTaskCreate((TaskFunction_t         )   low_task,
                (char *                 )   "low_task",
                (configSTACK_DEPTH_TYPE )   TASK1_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   TASK1_PRIO,
                (TaskHandle_t *         )   &low_task_handler );
                
    xTaskCreate((TaskFunction_t         )   middle_task,
                (char *                 )   "middle_task",
                (configSTACK_DEPTH_TYPE )   TASK2_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   TASK2_PRIO,
                (TaskHandle_t *         )   &middle_task_handler );
                
    xTaskCreate((TaskFunction_t         )   high_task,
                (char *                 )   "high_task",
                (configSTACK_DEPTH_TYPE )   TASK3_STACK_SIZE,
                (void *                 )   NULL,
                (UBaseType_t            )   TASK3_PRIO,
                (TaskHandle_t *         )   &high_task_handler );
                             
    vTaskDelete(NULL);
    taskEXIT_CRITICAL();                /* 退出临界区 */
}

/* 任务一,低优先级任务 */
void low_task( void * pvParameters )
{
    while(1) 
    {
        printf("low_task获取信号量\r\n");
        xSemaphoreTake(semphore_handle,portMAX_DELAY);
        printf("low_task正在运行!!!\r\n");
        delay_ms(3000);
        printf("low_task释放信号量\r\n");
        xSemaphoreGive(semphore_handle); 
        vTaskDelay(1000);
    }
}

/* 任务二,中优先级任务 */
void middle_task( void * pvParameters )
{
    while(1)
    {
        printf("middle_task正在运行!!!\r\n");
        vTaskDelay(1000);
    }
}

/* 任务三,高优先级任务 */
void high_task( void * pvParameters )
{
    while(1)
    {
        printf("high_task获取信号量\r\n");
        xSemaphoreTake(semphore_handle,portMAX_DELAY);
        printf("high_task正在运行!!!\r\n");
        delay_ms(1000);
        printf("high_task释放信号量\r\n");
        xSemaphoreGive(semphore_handle); 
        vTaskDelay(1000);
    }
}

互斥信号量

image-20240327144439059

优先级继承示例

image-20240327144451756

image-20240327144509257

互斥信号量相关API函数

image-20240327144523070

互斥信号量创建API函数

image-20240327144542581

互斥信号量实验

image-20240327144553453

代码

freertos_demo.c

/**
 ****************************************************************************************************
 * @file        freertos.c
 * @author      正点原子团队(ALIENTEK)
 * @version     V1.4
 * @date        2022-01-04
 * @brief       FreeRTOS 移植实验
 * @license     Copyright (c) 2020-2032, 广州市星翼电子科技有限公司
 ****************************************************************************************************
 * @attention
 *
 * 实验平台:正点原子 探索者F407开发板
 * 在线视频:www.yuanzige.com
 * 技术论坛:www.openedv.com
 * 公司网址:www.alientek.com
 * 购买地址:openedv.taobao.com
 *
 ****************************************************************************************************
 */

#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
#include "./SYSTEM/delay/delay.h"
#include "./MALLOC/malloc.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

/******************************************************************************************************/
/*FreeRTOS配置*/

/* START_TASK 任务 配置
 * 包括: 任务句柄 任务优先级 堆栈大小 创建任务
 */
#define START_TASK_STACK_SIZE 	128
#define START_TASK_PRIO			1
TaskHandle_t start_task_handler;

#define TASK1_STACK_SIZE 		128
#define TASK1_PRIO				2
TaskHandle_t low_task_handler;

#define TASK2_STACK_SIZE 		128
#define TASK2_PRIO				3
TaskHandle_t middle_task_handler;

#define TASK3_STACK_SIZE 		128
#define TASK3_PRIO				4
TaskHandle_t high_task_handler;

QueueHandle_t mutex_semphore_handle;

void start_task( void * pvParameters );

void low_task( void * pvParameters )
{
	while(1)
	{
		printf("low_task获取信号量\r\n");
        xSemaphoreTake(mutex_semphore_handle,portMAX_DELAY);
        printf("low_task正在运行!!!\r\n");
        delay_ms(3000);
        printf("low_task释放信号量\r\n");
        xSemaphoreGive(mutex_semphore_handle); 
        vTaskDelay(1000);
	}
}

void middle_task( void * pvParameters )
{
	while(1)
	{
		printf("middle_task正在运行!!!\r\n");
		vTaskDelay(1000);
	}
}

void high_task( void * pvParameters )
{
	while(1)
	{
		printf("high_task获取信号量\r\n");
        xSemaphoreTake(mutex_semphore_handle,portMAX_DELAY);
        printf("high_task正在运行!!!\r\n");
        delay_ms(1000);
        printf("high_task释放信号量\r\n");
        xSemaphoreGive(mutex_semphore_handle); 
        vTaskDelay(1000);
	}
}

/******************************************************************************************************/

/**
 * @brief       FreeRTOS例程入口函数
 * @param       无
 * @retval      无
 */
void freertos_demo(void)
{
	mutex_semphore_handle = xSemaphoreCreateMutex(); //创建互斥信号量,会自动释放一次信号量
	if(mutex_semphore_handle != NULL)
	{
		printf("互斥信号量创建成功!!!\r\n");
	}
	
    xTaskCreate(start_task, "start_task", START_TASK_STACK_SIZE, NULL, START_TASK_PRIO, &start_task_handler);
	vTaskStartScheduler();
}

void start_task( void * pvParameters )
{
	taskENTER_CRITICAL(); //进入临界区
	xTaskCreate(low_task, "low_task", TASK1_STACK_SIZE, NULL, TASK1_PRIO, &low_task_handler);
	xTaskCreate(middle_task, "middle_task", TASK2_STACK_SIZE, NULL, TASK2_PRIO, &middle_task_handler);
	xTaskCreate(high_task, "high_task", TASK3_STACK_SIZE, NULL, TASK3_PRIO, &high_task_handler);
    vTaskDelete(NULL);
	taskEXIT_CRITICAL(); //退出临界区
}

posted @ 2024-03-28 20:26  爱吃冰激凌的黄某某  阅读(5)  评论(0编辑  收藏  举报