一、简介
1、FreeRTOS内存管理简介
2、FreeRTOS提供的内存管理算法

1、heap_1内存管理算法


2、heap_2内存管理算法


4、heap_4内存管理算法
5、heap_5内存管理算法
二、FreeRTOS内存管理相关API函数介绍

三、 FreeRTOS内存管理实验

1、代码
main.c
cs
#include "stm32f10x.h"
#include "FreeRTOS.h"
#include "task.h"
#include "freertos_demo.h"
#include "Delay.h"
#include "sys.h"
#include "usart.h"
#include "LED.h"
#include "Key.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组 4
uart_init(115200);
delay_init();
Key_Init();
LED_Init();
// 创建任务
FrrrRTOS_Demo();
}
freertos_dome.c
cs
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "LED.h"
#include "Key.h"
#include "usart.h"
#include "delay.h"
/******************************************************************任务配置****************************************************/
//任务优先级
#define START_TASK_PRIO 1
//任务堆栈大小
#define START_TASK_STACK_SIZE 128
//任务句柄
TaskHandle_t StartTask_Handler;
//任务函数
void start_task(void *pvParameters);
//任务优先级
#define TASK1_PRIO 2
//任务堆栈大小
#define TASK1_STACK_SIZE 128
//任务句柄
TaskHandle_t Task1_Handler;
//任务函数
void task1(void *pvParameters);
/******************************************************************任务函数****************************************************/
QueueHandle_t semaphore_handle; //二值信号量句柄
void FrrrRTOS_Demo(void)
{
semaphore_handle = xSemaphoreCreateBinary();
if(semaphore_handle != NULL)
{
printf("\r\n二值信号量创建成功\r\n");
}
//创建开始任务
xTaskCreate((TaskFunction_t )start_task, //任务函数
( char* )"start_task", //任务名称
(uint16_t )START_TASK_STACK_SIZE, //任务堆栈大小
(void* )NULL, //传递给任务函数的参数
(UBaseType_t )START_TASK_PRIO, //任务优先级
(TaskHandle_t* )&StartTask_Handler); //任务句柄
// 启动任务调度
vTaskStartScheduler();
}
void start_task(void *pvParameters)
{
taskENTER_CRITICAL(); //进入临界区
//创建1任务
xTaskCreate((TaskFunction_t )task1,
(const char* )"task1",
(uint16_t )TASK1_STACK_SIZE,
(void* )NULL,
(UBaseType_t )TASK1_PRIO,
(TaskHandle_t* )&Task1_Handler);
vTaskDelete(NULL); //删除开始任务
taskEXIT_CRITICAL(); //退出临界区
}
//1 申请和释放内存并显示剩余内存信息
void task1(void *pvParameters)
{
uint8_t key = 0;
uint8_t t = 0;
uint8_t * buffer = NULL;
while(1)
{
key = Key_GetNum();
if(key == 2)
{
buffer = pvPortMalloc(30); //申请内存
if(buffer != NULL)
{
printf("申请内存成功\r\n");
}else{printf("申请内存失败\r\n");};
}else if(key == 3){
if(buffer != NULL)
{
vPortFree(buffer);
printf("释放内存\r\n");
}
//释放内存
}
if(t++>50)
{
t = 0;
printf("剩余内存空间大小为:%d\r\n",xPortGetFreeHeapSize());
}
vTaskDelay(10);
}
}
key.c
cs
#include "stm32f10x.h" // Device header
#include "FreeRTOS.h"
#include "task.h"
#include "usart.h"
#include "Delay.h"
/**
* 函 数:按键初始化
* 参 数:无
* 返 回 值:无
* 按键:PB4/PB12/PB14
*/
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*开启时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //开启GPIOB的时钟
/*GPIO初始化*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_12 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**
* 函 数:按键获取键码
* 参 数:无
* 返 回 值:按下按键的键码值,范围:0~3,返回0代表没有按键按下
* 注意事项:此函数是阻塞式操作,当按键按住不放时,函数会卡住,直到按键松手
*/
uint8_t Key_GetNum(void)
{
uint8_t KeyNum = 0; //定义变量,默认键码值为0
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0) //读PB4输入寄存器的状态,如果为0,则代表按键1按下
{
KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4);
delay_xms(20); //延时消抖
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0); //等待按键松手
delay_xms(20); //延时消抖
KeyNum = 1; //置键码为1
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0)
{
KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12);
delay_xms(20);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0);
delay_xms(20);
KeyNum = 2;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)
{
KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
delay_xms(20);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0);
delay_xms(20);
KeyNum = 3;
}
return KeyNum; //返回键码值,如果没有按键按下,所有if都不成立,则键码为默认值0
}
2、实验解析
