概述
本篇只要介绍这么使用STM32CubeMx工具添加RT-Thread操作系统组件,码代码的IDE是keil。介绍单线程SRAM静态内存使用。如果还不知道,这么使用STM32CubeMx工具添加RT-Thread操作系统组件,请移步到《基于 STM32CubeMX 添加 RT-Thread 操作系统组件(一)- 详细介绍操作步骤》文章阅读。好了,喝杯茶先^_^,继续前行。上一篇介绍关于《串口重映射到 rt_kprintf 函数》
一、STM32CubeMx配置
二、KEIL IDE
- 在Application/User文件夹,新建app_rt_thread.c文件,并添加如下代码:
#include "rtthread.h" #include "main.h" #include "stdio.h" ///* 定义线程控制块指针 */ static rt_thread_t thread1 = RT_NULL; static rt_thread_t thread2 = RT_NULL; static rt_thread_t thread3 = RT_NULL; static void thread1_entry(void* parameter); static void thread2_entry(void* parameter); static void thread3_entry(void* parameter); #define THREAD1_STACK_SIZE 512 #define THREAD1_PRIORITY 3 #define THREAD1_TIMESLICE 20 #define THREAD2_STACK_SIZE 512 #define THREAD2_PRIORITY 4 #define THREAD2_TIMESLICE 20 #define THREAD3_STACK_SIZE 512 #define THREAD3_PRIORITY 5 #define THREAD3_TIMESLICE 20 //初始化线程函数 int MX_RT_Thread_Init(void) { thread1 = /* 线程控制块指针 */ rt_thread_create("thread1", /* 线程名字,字符串形式 */ thread1_entry, /* 线程入口函数 */ RT_NULL, /* 线程入口函数参数 */ THREAD1_STACK_SIZE, /* 线程栈大小,单位为字节 */ THREAD1_PRIORITY, /* 线程优先级,数值越大,优先级越小 */ THREAD1_TIMESLICE); /* 线程时间片 */ if (thread1 != RT_NULL) rt_thread_startup(thread1); else return -1; thread2 = /* 线程控制块指针 */ rt_thread_create("thread2", /* 线程名字,字符串形式 */ thread2_entry, /* 线程入口函数 */ RT_NULL, /* 线程入口函数参数 */ THREAD2_STACK_SIZE, /* 线程栈大小,单位为字节 */ THREAD2_PRIORITY, /* 线程优先级,数值越大,优先级越小 */ THREAD2_TIMESLICE); /* 线程时间片 */ if (thread2 != RT_NULL) rt_thread_startup(thread2); else return -1; thread3 = /* 线程控制块指针 */ rt_thread_create("thread3", /* 线程名字,字符串形式 */ thread3_entry, /* 线程入口函数 */ RT_NULL, /* 线程入口函数参数 */ THREAD3_STACK_SIZE, /* 线程栈大小,单位为字节 */ THREAD3_PRIORITY, /* 线程优先级,数值越大,优先级越小 */ THREAD3_TIMESLICE); /* 线程时间片 */ if (thread3 != RT_NULL) rt_thread_startup(thread3); else return -1; } /* ************************************************************************* * 线程定义 ************************************************************************* */ static void thread1_entry(void* parameter) { while (1) { rt_kprintf("thread1 running\r\n"); rt_thread_delay(500); /* 延时 500 个 tick */ } } static void thread2_entry(void* parameter) { while (1) { rt_kprintf("thread2 running\r\n"); rt_thread_delay(1000); /* 延时 1000 个 tick */ } } static void thread3_entry(void* parameter) { while (1) { rt_kprintf("thread3 running\r\n"); rt_thread_delay(1500); /* 延时 1500 个 tick */ } }
- 在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 */ }
- 自定义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(); }
- 运行结果
源码:git