一、在STM32CubeMX中建立工程
- 配置RCC
- 配置SYS
- 配置PC13为GPIO_Output
- 配置USART1
- 生成代码
二、获取uC/OS-III源码
官网下载地址:Micrium Software and Documentation - Silicon Labs
网盘下载:百度网盘 请输入提取码
提取码:lzjl
三、复制uC/OS-III到工程文件夹
- 在生成的keil工程文件夹中新建一个UCOSIII的文件夹,将我们下载的源代码中四个文件夹:uC-CPU、uC-LIB、ucoS CONFIG、uCOS-II复制到我们新建的文件夹中:
- 在工程文件夹中的Src文件夹下新建一个OS文件夹。
- 将刚才下载源码打开,将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复制到上一步建立的OS文件夹中,同时新建三个空白文件: bsp.c、bsp.h、app.h
四、添加工程组件和头文件路径
(一)添加工程分组
打开STM32CubeMX创建的工程, 按照如图所示添加六个新的组:bsp、 uCOSIII CPU、
uCOSIII_ LIB、uCOSIII_ Ports、uCOSIII_ Source、OS_cfg。
(二)添加文件到分组
1、文件目录是: Src/OS,将其中 bsp.c 和 bsp.h文件添加至 bsp 组中,将 app.c 添加进 Application/User 组中
2、uCOSIII_CPU组件, 点击Add Files…按钮,将文件目录跳转至:UCOSIII/uC-CPU,选择 ALL files文件类型,将其中的三个文件点击 Add 添加, 然后再打开: ARM-Cortex-M3\RealView, 同样选择 ALL files 文件类型,将三个文件添加进uCOSIII_CPU组。
3、同理来添加 uCOSIII_LIB 组件文件:选择 uCOSIII_LIB 组,点击 Add Files…按钮, 将文件目录跳转至: UCOSIII/uCLIB,选择 ALL files 文件类型,将其中的九个文件添加进 uCOSIII_LIB 组;然后继续打开: Ports/ARM-Cortex-M3/Realview, 添加 lib_mem_a.asm 文件
4、添加选择 uCOSIII_Ports 组,点击 Add Files…按钮, 将文件目录调整至: UCOSIII/UcosIII/Ports/RAM-Cortex-M4/Generic/RealView。选择 ALL files 文件类型, 将其中三个文件添加进 uCOSIII_Ports 组。
5、选择uCOSIII_Sourc组,点击Add Files…按钮, 将文件目录调整至: UCOSIII/UcosIII/Source。选择 ALL files 文件类型, 将其中二十个文件添加进 uCOSIII_Sourc 组。
6、选择 OS_cfg 组,点击 Add Files…按钮, 将文件目录调整至: Src/OS。选择 ALLfiles 文件类型, 将图中的八个文件添加进 uCOSIII_Sourc 组
至此所有工程组件都添加完毕。
(三)添加头文件路径
将下图所框的地址加入其中:
至此头文件路径也添加完毕。
五、修改文件的内容
(一)启动文件
(二)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 <bsp.h>
修改后:
#include <bsp.h>
#include "gpio.h"
#include "app_cfg.h"
#include "app.h"
- 第二处修改:添加HAL库
修改前:
#include <stm32f10x_lib.h>
修改后:
#include "stm32f1xx_hal.h"
(四)bsp.c和bsp.h
bsp的.c和.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;#elsecnts = cpu_clk_freq/(CPU_INT32U)OS_TICKS_PER_SEC;#endifOS_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
(五)app.c和app.h
app.c我们实际是用main.c代替,所以app.c中没有什么实际的代码,这里将其保留,也可选择删除,注意头文件包含即可。
#include <includes.h>
// app.h
#ifndef __APP_H__
#define __APP_H__#include <includes.h>#endif /* __APP_H__ */
(六)main.c
/* 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 *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */static OS_TCB AppTaskStartTCB;static CPU_STK AppTaskStartStk[APP_TASK_START_STK_SIZE];static void AppTaskCreate(void);
static void AppObjCreate(void);
static void AppTaskStart(void *p_arg);
static void send_msg (void *p_arg);
/* 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 */
/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/**Initializes the CPU, AHB and APB busses clocks */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 busses 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 END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 */OS_ERR err;/* 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 */OSInit(&err); /* USER CODE END SysInit *//* Initialize all configured peripherals */
// MX_GPIO_Init();/* USER CODE BEGIN 2 *//* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */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 ) 0,(OS_TICK ) 0,(void *) 0,(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),(OS_ERR *)&err);OSStart(&err); /* Start multitasking (i.e. give control to uC/OS-III). */}static void AppTaskStart (void *p_arg)
{OS_ERR err;(void)p_arg;BSP_Init(); /* Initialize BSP functions */CPU_Init();Mem_Init(); /* Initialize Memory Management Module */#if OS_CFG_STAT_TASK_EN > 0uOSStatTaskCPUUsageInit(&err); /* Compute CPU capacity with no task running */
#endifCPU_IntDisMeasMaxCurReset();AppTaskCreate(); /* Create Application Tasks */AppObjCreate(); /* Create Application Objects */while (DEF_TRUE){HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin, GPIO_PIN_SET);OSTimeDlyHMSM(0, 0, 0, 500,OS_OPT_TIME_HMSM_STRICT,&err);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}static void send_msg (void *p_arg)
{OS_ERR err;(void)p_arg;BSP_Init(); /* Initialize BSP functions */CPU_Init();Mem_Init(); /* Initialize Memory Management Module */#if OS_CFG_STAT_TASK_EN > 0uOSStatTaskCPUUsageInit(&err); /* Compute CPU capacity with no task running */
#endifCPU_IntDisMeasMaxCurReset();AppTaskCreate(); /* Create Application Tasks */AppObjCreate(); /* Create Application Objects */while (DEF_TRUE){printf("hello world \r\n");OSTimeDlyHMSM(0, 0, 0, 500,OS_OPT_TIME_HMSM_STRICT,&err);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}static void AppTaskCreate (void)
{}static void AppObjCreate (void)
{}
/* 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 *//* 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,tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT *//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
(七)lib_ cfg.h
这个头文件中有一个宏定义:
#define LIB_MEM_CFG_HEAP_SIZE 5u * 1024u /* Configure heap memory size [see Note #2a]. */
六、编译运行
运行结果如下所示:
参考文献:STM32F103C8T6移植uC/OS-III基于HAL库超完整详细过程_ostimedlyhmsm(0,0,0,800);-CSDN博客