目录
一、项目创建
二、代码移植
1、uC/OS-III源码处理
2、KEIL文件配置
编辑3、文件修改
启动文件
编辑app_cfg.h
includes.h
bsp.c和bsp.h
main.c
lib_ cfg.h
app.c和app.h
三、总结
学习目标:
学习嵌入式实时操作系统(RTOS),以uc/OS为例,将其移植到STM32F103上。
一、项目创建
仍然是使用STM32CubeMX建立新的项目,如何选择STM32相关型号请见之前的博客,避免过多叙述影响内容,下面直接跳到项目配置。(之前的操作步骤不了解的请参考下例博客:https://blog.csdn.net/weixin_74345079/article/details/139183523?utm_source=miniapp_weixin)
首先选择PC13为GPIO_Output,再根据下面指示的操作进行配置RCC、SYS等。
基本配置完成后,点击右上角 " GENERATE CODE " ,进入以下界面,跟随操作并生成代码。
二、代码移植
1、uC/OS-III源码处理
移植之前我们要获取uC/OS-III源码,获取方法:进入官网链接(Micrium Software and Documentation - Silicon Labs)进行下载。
再建一个新的文件夹 " UCOSIII " ,将uC-CPU、uC-LIB、uCOS-III三个文件复制到该文件夹。
在Src文件夹下新建一个OS文件夹,然后将下载的源码中\EvalBoards\Micrium\uC-Eval-STM32F107\uCOS-III下的文件app.c 、 app_cfg.h 、 cpu_cfg.h 、 includes.h 、 lib_cfg.h 、 os_app_hooks.c 、os_app_hook.h、os_cfg.h、os_cfg_app.h复制到该文件夹中。
然后再新建三个空白文件: bsp.c、bsp.h、app.h
。
2、KEIL文件配置
在工程下添加如下文件;
将Src/OS中的bsp.c 和 bsp.h文件添加至 bsp 组中;
将 app.c 添加进 Application/User/Core 组中;
将UCOSIII/uC-CPU中的cpu_core.c、cpu_core.h、cpu_def.h添加进uCOSIII_CPU组中,以及UCOSIII/uC-CPU/ARM-Cortex-M3/RealView中的三个文件添加到该组;
将UCOSIII/uC-LIB中的9个文件添加到uCOSIII-LIB组中,同时将UCOSIII/uC-LIB/Ports/ARM-Cortex-M3/Realview中的lib_mem_a.asm添加到该组中;
将UCOSIII/UcosIII/Ports/ARM-Cortex-M3/Generic/RealView中的3个文件添加的uCOSIII_Ports组中;
将UCOSIII/UcosIII/Source中的20个文件添加到uCOSIII_Source组中;
将Core/Src/OS中的app_cfg.h、cpu_cfg.h、includes.h、lib_cfg.h、os_appa_hooks.c、os_app_hooks.h、os_cfg.h、os_cfg_app.h添加到OS_cfg组中。
下面是配置好过后的keil工程:
随后点击魔法棒,再配置头文件路径:
3、文件修改
启动文件
app_cfg.h
#define APP_CFG_SERIAL_EN DEF_ENABLED修改为#define APP_CFG_SERIAL_EN DEF_DISABLED;
#define APP_TRACE BSP_Ser_Printf修改为#define APP_TRACE (void)。
includes.h
添加三个头文件:#include "gpio.h"、#include "app_cfg.h"、#include "app.h"。
bsp.c和bsp.h
bsp.c:
// bsp.c
#include "includes.h"
#define DWT_CR *(CPU_REG32 *)0xE0001000
#define DWT_CYCCNT *(CPU_REG32 *)0xE0001004
#define DEM_CR *(CPU_REG32 *)0xE000EDFC
#define DBGMCU_CR *(CPU_REG32 *)0xE0042004
#define DEM_CR_TRCENA (1 << 24)
#define DWT_CR_CYCCNTENA (1 << 0)
CPU_INT32U BSP_CPU_ClkFreq (void)
{
return HAL_RCC_GetHCLKFreq();
}
void BSP_Tick_Init(void)
{
CPU_INT32U cpu_clk_freq;
CPU_INT32U cnts;
cpu_clk_freq = BSP_CPU_ClkFreq();
#if(OS_VERSION>=3000u)
cnts = cpu_clk_freq/(CPU_INT32U)OSCfg_TickRate_Hz;
#else
cnts = cpu_clk_freq/(CPU_INT32U)OS_TICKS_PER_SEC;
#endif
OS_CPU_SysTickInit(cnts);
}
void BSP_Init(void)
{
BSP_Tick_Init();
MX_GPIO_Init();
}
#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
void CPU_TS_TmrInit (void)
{
CPU_INT32U cpu_clk_freq_hz;
DEM_CR |= (CPU_INT32U)DEM_CR_TRCENA; /* Enable Cortex-M3's DWT CYCCNT reg. */
DWT_CYCCNT = (CPU_INT32U)0u;
DWT_CR |= (CPU_INT32U)DWT_CR_CYCCNTENA;
cpu_clk_freq_hz = BSP_CPU_ClkFreq();
CPU_TS_TmrFreqSet(cpu_clk_freq_hz);
}
#endif
#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
CPU_TS_TMR CPU_TS_TmrRd (void)
{
return ((CPU_TS_TMR)DWT_CYCCNT);
}
#endif
#if (CPU_CFG_TS_32_EN == DEF_ENABLED)
CPU_INT64U CPU_TS32_to_uSec (CPU_TS32 ts_cnts)
{
CPU_INT64U ts_us;
CPU_INT64U fclk_freq;
fclk_freq = BSP_CPU_ClkFreq();
ts_us = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);
return (ts_us);
}
#endif
#if (CPU_CFG_TS_64_EN == DEF_ENABLED)
CPU_INT64U CPU_TS64_to_uSec (CPU_TS64 ts_cnts)
{
CPU_INT64U ts_us;
CPU_INT64U fclk_freq;
fclk_freq = BSP_CPU_ClkFreq();
ts_us = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);
return (ts_us);
}
#endif
bsp.h:
// bsp.h
#ifndef __BSP_H__
#define __BSP_H__
#include "stm32f1xx_hal.h"
void BSP_Init(void);
#endif
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <includes.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* 任务优先级 */
#define APP_TASK_START_PRIO 2
#define MyTask_LED1_PRIO 3
#define MyTask_LED2_PRIO 3
/* 任务堆栈大小 */
#define APP_TASK_START_STK_SIZE 128
#define MyTask_LED1_STK_SIZE 512
#define MyTask_LED2_STK_SIZE 512
/* 任务控制块 */
static OS_TCB AppTaskStartTCB;
static OS_TCB MyTask_LED1TCB;
static OS_TCB MyTask_LED2TCB;
/* 任务栈 */
static CPU_STK AppTaskStartStk[APP_TASK_START_STK_SIZE];
static CPU_STK TASK_LED1STK[MyTask_LED1_STK_SIZE];
static CPU_STK TASK_LED2STK[MyTask_LED2_STK_SIZE];
/* 声明用户任务 */
static void AppTaskStart (void *p_arg);
static void MyTask_LED1 (void *p_arg);
static void MyTask_LED2 (void *p_arg);
/* 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 */
static void AppTaskStart (void *p_arg)
{
CPU_INT32U cpu_clk_freq;
CPU_INT32U cnts;
OS_ERR err;
(void)p_arg;
BSP_Init();
CPU_Init();
cpu_clk_freq=BSP_CPU_ClkFreq();
cnts=cpu_clk_freq/(CPU_INT32U)OSCfg_TickRate_Hz;
OS_CPU_SysTickInit(cnts);
Mem_Init();
#if OS_CFG_STAT_TASK_EN>0u
OSStatTaskCPUUsageInit(&err);
#endif
CPU_IntDisMeasMaxCurReset();
OSTaskCreate((OS_TCB *)&MyTask_LED1TCB, /* Create the start task */
(CPU_CHAR *)"LED1",
(OS_TASK_PTR ) MyTask_LED1,
(void *) 0,
(OS_PRIO ) MyTask_LED1_PRIO,
(CPU_STK *)&TASK_LED1STK[0],
(CPU_STK_SIZE) MyTask_LED1_STK_SIZE/10,
(CPU_STK_SIZE) MyTask_LED1_STK_SIZE,
(OS_MSG_QTY ) 0,
(OS_TICK ) 0,
(void *) 0,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSTaskCreate((OS_TCB *)&MyTask_LED2TCB, /* Create the start task */
(CPU_CHAR *)"LED2",
(OS_TASK_PTR ) MyTask_LED2,
(void *) 0,
(OS_PRIO ) MyTask_LED2_PRIO,
(CPU_STK *)&TASK_LED2STK[0],
(CPU_STK_SIZE) MyTask_LED2_STK_SIZE/10,
(CPU_STK_SIZE) MyTask_LED2_STK_SIZE,
(OS_MSG_QTY ) 0,
(OS_TICK ) 0,
(void *) 0,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
}
static void MyTask_LED1(void *p_arg)
{
OS_ERR err;
(void)p_arg;
while(1)
{
OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err);
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_8);
}
}
static void MyTask_LED2(void *p_arg)
{
OS_ERR err;
(void)p_arg;
while(1)
{
OSTimeDlyHMSM(0,0,1,500,OS_OPT_TIME_HMSM_STRICT,&err);
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_9);
}
}
/* 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();
/* USER CODE BEGIN 2 */
OS_ERR err;
OSInit(&err);
/* 创建任务 */
OSTaskCreate((OS_TCB *)&AppTaskStartTCB, /* Create the start task */
(CPU_CHAR *)"App Task Start",
(OS_TASK_PTR ) AppTaskStart,
(void *) 0,
(OS_PRIO ) APP_TASK_START_PRIO,
(CPU_STK *)&AppTaskStartStk[0],
(CPU_STK_SIZE) APP_TASK_START_STK_SIZE/10,
(CPU_STK_SIZE) APP_TASK_START_STK_SIZE,
(OS_MSG_QTY ) 5u,
(OS_TICK ) 0u,
(void *) 0,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
/* 启动多任务系统,控制权交给uC/OS-III */
OSStart(&err);
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
lib_ cfg.h
加入宏定义:#define LIB_MEM_CFG_HEAP_SIZE 10u * 1024u。
app.c和app.h
app.c:
#include "includes.h"
app.h
// app.h
#ifndef __APP_H__
#define __APP_H__
#include <includes.h>
#endif /* __APP_H__ */
全部配置、修改完成即成功移植,后面可以再根据开发板进行实物验证。
三、总结
在探索STM32微控制器与uc/OS-III多任务程序的学习过程中,我对嵌入式实时操作系统的运作原理有了更清晰的认识。通过将uc/OS-III移植到STM32F103微控制器上的实践,我深入理解了RTOS如何与特定硬件平台交互。这个过程中,我学会了如何编写多任务程序,并使用uc/OS-III提供的API进行任务管理和通信。除了提升了我的编程技能,这个学习经历还加深了我对实时系统和嵌入式开发的理解。同时对于KEIL、STM32CubeMX的使用也更加熟练,对于遇到的问题也能更好解决。
参考链接:
https://blog.csdn.net/YangMax1/article/details/121617909
https://blog.csdn.net/weixin_46628481/article/details/121561800?spm=1001.2014.3001.5501