本文主要是介绍STM32F103C8移植uCOSIII并以不同周期点亮两个LED灯(HAL库方式)【uCOS】【STM32开发板】【STM32CubeMX】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
STM32F103C8移植uC/OSIII并以不同周期点亮两个LED灯(HAL库方式)【uC/OS】【STM32开发板】【STM32CubeMX】
实验说明
将嵌入式操作系统uC/OSIII移植到STM32F103C8上,构建两个任务,两个任务分别以1s和3s周期对LED进行点亮—熄灭的控制。
获取uC/OSIII源码
链接:https://pan.baidu.com/s/1Axjz1ptMjIPcH6_0tjKKDw
提取码:ucos
建立STM32CubeMX项目
选择STM32F103C8T6芯片,设置PB0和PB1为GPIO_Output,用于和LED相连。
如下图设置时钟为72MHz。
配置其它参数后,生成Keil项目。
复制uC/OS-III文件到项目文件夹
在生成的项目文件夹下新建一个uCOSIII文件夹,将下载的源文件下的uC-CPU、uC-LIB、uCOS-III复制到该文件夹,如下图:
在Core\Src文件夹下新建OS文件夹,将下载的源码中uCOS-CONFIG中的文件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文件,如下图:
添加项目组件和头文件路径
添加项目分组
在生成的Keil项目中添加六个新组:bsp, uCOSIII-CPU, uCOSIII-LIB, uCOSIII-Port, uCOSIII-Source, OS-cfg, 如下图:
添加文件到分组
将Core\Src\OS路径下的bsp.c和bsp.h文件添加至bsp组中,如下图:
将uCOSIII\uC-CPU路径下的所有文件及uCOSIII\uC-CPU\ARM-Cortex-M4\RealView路径下的所有文件添加至uCOSIII-CPU组中,如下图:
将uCOSIII\uC-LIB路径下的所有文件及uCOSIII\uC-LIB\Ports\ARM-Cortex-M4\RealView路径下的所有文件添加至uCOSIII-LIB组中,如下图:
将uCOSIII\uCOS-III\Ports\ARM-Cortex-M4\Generic\RealView路径下的所有文件添加至uCOSIII-Port组中,如下图:
将uCOSIII\uCOS-III\Source路径下的所有文件添加至uCOSIII-Source组中,如下图:
将Core\Src\OS路径下除bsp.c, bsp.h外的所有文件添加至OS-cfg组,如下图:
添加头文件路径
点击Options for Target,点击C/C++,在Include Path栏中添加以下路径:
修改文件内容及编写程序
启动文件 (startup_stm32f103xb.s)
将启动文件中的PendSV_Handler
和Systick_Handler
改为OS_CPU_PendSVHandler
和OS_CPU_SysTickHandler
,如下图:
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 "gpio.h"
, #include "app_cfg.h"
;#include <stm32f10x_lib.h>
改为#include "stm32f1xx_hal.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;#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
lib_cfg.h
该文件中有一个宏定义:
#define LIB_MEM_CFG_HEAP_SIZE 27u * 1024u
STM32F103C8T6的RAM仅有20KB,因此需要将堆空间改小,这里改成10KB:
#define LIB_MEM_CFG_HEAP_SIZE 10u * 1024u
main.c
我在main.c中创建了两个任务:LED0_Task控制B0端口的LED灯以1s为周期亮—灭;LED1_Task控制B1端口的LED灯以3s为周期亮—灭。
/* 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 LED0_TASK_PRIO 1
#define LED1_TASK_PRIO 2
//#define SEND_MSG_TASK_PRIO 3/* 任务堆栈大小 */
#define LED0_STK_SIZE 128
#define LED1_STK_SIZE 128
//#define SEND_MSG_STK_SIZE 128/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
//任务控制块
static OS_TCB LED0TaskTCB;
static OS_TCB LED1TaskTCB;//任务堆栈
static CPU_STK LED0_TASK_STK[LED0_STK_SIZE];
static CPU_STK LED1_TASK_STK[LED1_STK_SIZE];/* 私有函数原形 --------------------------------------------------------------*/
static void AppTaskCreate(void);
static void AppObjCreate(void);
static void LED0_Task(void *p_arg);
static void LED1_Task(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 *//* 创建LED0任务 */OSTaskCreate((OS_TCB *)&LED0TaskTCB, /* Create the start task */(CPU_CHAR *)"LED0_Task",(OS_TASK_PTR ) LED0_Task,(void *) 0,(OS_PRIO ) LED0_TASK_PRIO,(CPU_STK *)&LED0_TASK_STK[0],(CPU_STK_SIZE) LED0_STK_SIZE / 10,(CPU_STK_SIZE) LED0_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);/* 创建LED1任务 */OSTaskCreate((OS_TCB *)&LED1TaskTCB, /* Create the start task */(CPU_CHAR *)"LED1_Task",(OS_TASK_PTR ) LED1_Task,(void *) 0,(OS_PRIO ) LED1_TASK_PRIO,(CPU_STK *)&LED1_TASK_STK[0],(CPU_STK_SIZE) LED1_STK_SIZE / 10,(CPU_STK_SIZE) 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);/* 启动多任务系统,控制权交给uC/OS-III */OSStart(&err); /* Start multitasking (i.e. give control to uC/OS-III). */}/*** 函数功能: 启动任务函数体。* 输入参数: p_arg 是在创建该任务时传递的形参* 返 回 值: 无* 说 明:无*/
static void LED0_Task (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){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, &err);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, &err);}/* USER CODE END 3 */
}static void LED1_Task (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){/* USER CODE END WHILE *//* USER CODE BEGIN 3 */HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);OSTimeDlyHMSM(0, 0, 1, 500, OS_OPT_TIME_HMSM_STRICT, &err);HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);OSTimeDlyHMSM(0, 0, 1, 500, OS_OPT_TIME_HMSM_STRICT, &err);}/* USER CODE END 3 */
}/* USER CODE BEGIN 4 */
/*** 函数功能: 创建应用任务* 输入参数: p_arg 是在创建该任务时传递的形参* 返 回 值: 无* 说 明:无*/
static void AppTaskCreate (void)
{}/*** 函数功能: uCOSIII内核对象创建* 输入参数: 无* 返 回 值: 无* 说 明:无*/
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****/
OSTimeDlyHMSM
函数是延时函数,括号中的四个数字表示延迟时长,从左至右分别为时、分、秒、毫秒。
实验结果
(绿色接B0,红色接B1)
参考文章
STM32F103基于HAL库移植uC/OS-III_ucos iii + hal移植到正点原子-CSDN博客
STM32F103C8移植uCOSIII(HAL库)_stm32f103移植ucosiii-CSDN博客
这篇关于STM32F103C8移植uCOSIII并以不同周期点亮两个LED灯(HAL库方式)【uCOS】【STM32开发板】【STM32CubeMX】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!