一、簡介
1、FreeRTOS內存管理簡介
2、FreeRTOS提供的內存管理算法?
1、heap_1內存管理算法?
2、heap_2內存管理算法?
?4、heap_4內存管理算法?
?5、heap_5內存管理算法??
二、FreeRTOS內存管理相關API函數介紹?
三、?FreeRTOS內存管理實驗
1、代碼
main.c?
#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
#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
#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; //定義變量,默認鍵碼值為0if (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
}