基于 STM32CubeMX 添加 RT-Thread 操作系统组件(十四)- 内存管理

概述 

       本篇只要介绍这么使用STM32CubeMx工具添加RT-Thread操作系统组件,码代码的IDE是keil。介绍单线程SRAM静态内存使用。如果还不知道,这么使用STM32CubeMx工具添加RT-Thread操作系统组件,请移步到《基于 STM32CubeMX 添加 RT-Thread 操作系统组件(一)- 详细介绍操作步骤》文章阅读。好了,喝杯茶先^_^,继续前行。上一篇介绍关于《邮箱》
内存管理包含两种:1、静态内存管理;2、动态内存管理。
在实现这个功能之前,先去RT-Thread的源码仓库中获取,该文件在 rtthread--master\examples\kernel 路径下,名字叫cpuusage.c。

链接如下:
github
码云

 

一、STM32CubeMx配置

 

二、KEIL IDE

  1. 在keil 工程项目视图,Application/User文件夹,打开下载好源码,找到 mempool.c 文件,添加到工程中来,如下所示

  2. 在Application/User文件夹,新建app_rt_thread.c文件,并添加如下代码:
    #include "rtthread.h"
    #include "main.h"
    #include "stdio.h"
    
    
    
    #if 0
    //静态内存管理
    
    /* 定义线程控制块 */
    static rt_thread_t alloc_thread = RT_NULL;
    static rt_thread_t free_thread = RT_NULL;
    /* 定义内存池控制块 */
    static rt_mp_t test_mp = RT_NULL;   
    /* 定义申请内存的指针 */
    static rt_uint32_t *p_test = RT_NULL;
    
    
    /* 变量声明  */
    
    /* 相关宏定义 */
    #define BLOCK_COUNT 20 //内存块数量
    #define BLOCK_SIZE 3 //内存块大小
    
    
    /* 函数声明 */
    static void alloc_thread_entry(void* parameter);
    static void free_thread_entry(void* parameter);
    
    
    
    int MX_RT_Thread_Init(void)
    {
    	rt_kprintf("This is an RTT static memory management experiment!\n");
    	rt_kprintf("Creating a memory pool...........\n");
    	/* 创建一个静态内存池 */
    	test_mp = rt_mp_create("test_mp", 
    					BLOCK_COUNT,
    					BLOCK_SIZE);
    	if (test_mp != RT_NULL)
    		rt_kprintf("Static memory pool created successfully!\n\n");
    	/* 创建一个线程 */
    	alloc_thread = /* 线程控制块指针 */
    				rt_thread_create( "alloc", /* 线程名字 */
    				alloc_thread_entry, /* 线程入口函数 */
    				RT_NULL, /* 线程入口函数参数 */
    				512, /* 线程栈大小 */
    				1, /* 线程的优先级 */
    				20); /* 线程时间片 */
    	/* 启动线程,开启调度 */
    	if (alloc_thread != RT_NULL)
    		rt_thread_startup(alloc_thread);
    	else
    		return -1;
    	
    	free_thread = /* 线程控制块指针 */
    				rt_thread_create( "free", /* 线程名字 */
    				free_thread_entry, /* 线程入口函数 */
    				RT_NULL, /* 线程入口函数参数 */
    				512, /* 线程栈大小 */
    				2, /* 线程的优先级 */
    				20); /* 线程时间片 */
    	/* 启动线程,开启调度 */
    	if (free_thread != RT_NULL)
    		rt_thread_startup(free_thread);
    	else
    		return -1;
    }
    
    /*
    ************************************************************
    * 线程定义
    *********************************************************
    */
    static void alloc_thread_entry(void* parameter)
    {
    	rt_kprintf("Requesting memory from the memory pool...........\n");
    	p_test = rt_mp_alloc(test_mp,0);
    	if (RT_NULL == p_test) /* 没有申请成功 */
    		rt_kprintf("Static memory application failed!\n");
    	else
    		rt_kprintf("Static memory application successful, address is %d! \n\n",p_test);
    	rt_kprintf("Writing data to P test...........\n");
    	*p_test = 1234;
    	rt_kprintf("Data has been written to the P test address\n");
    	rt_kprintf("*p_test = %.4d ,The address is:%d \n\n", *p_test,p_test);
    	/* 线程都是一个无限循环,不能返回 */
    	while (1) {
    		HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
    		rt_thread_delay(1000); //每 1000ms 扫描一次
    	}
    }
    static void free_thread_entry(void* parameter)
    {
    	rt_err_t uwRet = RT_EOK;
    	rt_kprintf("Freeing memory...........\n");
    	rt_mp_free(p_test);
    	rt_kprintf("Free memory successfully!\n\n");
    	rt_kprintf("Deleting memory...........\n");
    	uwRet = rt_mp_delete(test_mp);
    	if (RT_EOK == uwRet)
    		rt_kprintf("Memory removed successfully!\n");
    	/* 线程都是一个无限循环,不能返回 */
    	while (1) {
    		HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
    		rt_thread_delay(500); //每 500ms 扫描一次
    	}
    }
    
    #else
    //动态内存管理
    
    /* 定义线程控制块 */
    static rt_thread_t alloc_thread = RT_NULL;
    static rt_thread_t free_thread = RT_NULL;
    
    /* 定义申请内存的指针 */
    static rt_uint32_t *p_test = RT_NULL;
    
    /* 变量声明  */
    
    /* 相关宏定义 */
    #define TEST_SIZE 100 //内存大小(字节)
    
    
    /* 函数声明 */
    static void alloc_thread_entry(void* parameter);
    static void free_thread_entry(void* parameter);
    
    
    
    int MX_RT_Thread_Init(void)
    {
    	rt_kprintf("This is an RTT dynamic memory management experiment!\n");
    	/* 创建一个线程 */
    	alloc_thread = /* 线程控制块指针 */
    	rt_thread_create( "alloc", /* 线程名字 */
    					alloc_thread_entry, /* 线程入口函数 */
    					RT_NULL, /* 线程入口函数参数 */
    					512, /* 线程栈大小 */
    					1, /* 线程的优先级 */
    					20); /* 线程时间片 */
    	/* 启动线程,开启调度 */
    	if (alloc_thread != RT_NULL)
    		rt_thread_startup(alloc_thread);
    	else
    		return -1;
    	
    	free_thread = /* 线程控制块指针 */
    	rt_thread_create( "free", /* 线程名字 */
    					free_thread_entry, /* 线程入口函数 */
    					RT_NULL, /* 线程入口函数参数 */
    					512, /* 线程栈大小 */
    					2, /* 线程的优先级 */
    					20); /* 线程时间片 */
    	/* 启动线程,开启调度 */
    	if (free_thread != RT_NULL)
    		rt_thread_startup(free_thread);
    	else
    		return -1;
    }
    
    /*
    ************************************************************
    * 线程定义
    *********************************************************
    */
    static void alloc_thread_entry(void* parameter)
    {
    	rt_kprintf("Requesting memory from the memory pool...........\n");
    	p_test = rt_malloc(TEST_SIZE); /* 申请动态内存 */
    	if (RT_NULL == p_test) /* 没有申请成功 */
    		rt_kprintf("Dynamic memory application failed!\n");
    	else
    		rt_kprintf("Dynamic memory application successful, address is %d! \n\n",p_test);
    	rt_kprintf("Writing data to P test...........\n");
    	*p_test = 1234;
    	rt_kprintf("Data has been written to the P test address\n");
    	rt_kprintf("*p_test = %.4d ,Address is:%d \n\n", *p_test,p_test);
    	/* 线程都是一个无限循环,不能返回 */
    	while (1) {
    		HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
    		rt_thread_delay(1000); //每 1000ms 扫描一次
    	}
    }
    static void free_thread_entry(void* parameter)
    {
    	rt_kprintf("Freeing memory...........\n");
    	rt_free(p_test);
    	rt_kprintf("Free memory successfully!\n\n");
    	/* 线程都是一个无限循环,不能返回 */
    	while (1) {
    		HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
    		rt_thread_delay(500); //每 500ms 扫描一次
    	}
    }
    
    #endif
    
  3. 在main.c文件添加如下代码:
    /* USER CODE END Header */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "usart.h"
    #include "gpio.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    extern int MX_RT_Thread_Init(void);
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    
    /* USER CODE BEGIN PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    /* USER CODE BEGIN PFP */
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    int main(void)
    {
      /* USER CODE BEGIN 1 */
    
      /* USER CODE END 1 */
    
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* USER CODE BEGIN Init */
    
      /* USER CODE END Init */
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* USER CODE BEGIN SysInit */
    
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_USART1_UART_Init();
      /* USER CODE BEGIN 2 */
      MX_RT_Thread_Init();
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
      }
      /* USER CODE END 3 */
    }

     

  4. 自定义rt_hw_console_output()函数,在kservice.c文件添加中(重映射串口控制台到 rt_kprintf 函数)代码:
    #include "usart.h"
    .
    .
    .
    
    RT_WEAK void rt_hw_console_output(const char *str)
    {
        /* empty console output */
    	/* 进入临界段 */
    	rt_enter_critical();
    	/* 直到字符串结束 */
    	while (*str!='\0') {
    	/* 换行 */
    	if (*str=='\n') {
    		HAL_UART_Transmit(&huart1,(uint8_t *)'\r',1,1000);
    	}
    		HAL_UART_Transmit(&huart1,(uint8_t *)(str++),1,1000);
    	}
    	/* 退出临界段 */
    	rt_exit_critical();
    }

  5. 在rtconfig.h文件中添加如下代码:
    // <h>Memory Management Configuration
    // <c1>Using Memory Pool Management
    //  <i>Using Memory Pool Management
    #define RT_USING_MEMPOOL

  6. 运行结果
          
        
                                静态内存管理                                                                                 动态内存管理

    源码:git​​​​​​​

猜你喜欢

转载自blog.csdn.net/qq_36075612/article/details/107629074