基于STM32F03ZET6移植FreeRTOS

1 下载FreeRTOS源码

(官网的下载是从github上下载的,网页打不开)

下载地址: https://sourceforge.net/projects/freertos/files/FreeRTOS/

2 解压源码包

移植核心文件:

3 将 FreeRTOS 源码添加到工程目录

基于 HAL工程模板 添加 FreeRTOS 源码。 在SDK目录下创建 FreeRTOS 目录。

进入 FreeRTOS 目录,创建 {src, inc, portable} 三个目录。

将如下.c 文件,

拷贝 portable 相关文件,

拷贝头文件,

拷贝一个 FreeRTOS 的配置头文件,

4 将 FreeRTOS 相关源码添加到 Keil 工程分组

增加 FreeRTOS 的分组,并加载源文件。

如下:

添加头文件路径。

5 修改相关文件

5.1 FreeRTOS 添加三个宏定义配置

c 复制代码
#define xPortPendSVHandler		PendSV_Handler
#define vPortSVCHandler			SVC_Handler
#define INCLUDE_xTaskGetSchedulerState	1

注释 00_STM32_Demo\Core\stm32f1xx_it.c 里面的 SVC_Handler 函数和 PendSV_Handler 函数。

5.2 修改 stm32f1xx_it.c 文件

引用 FreeRTOS 的头文件

文件开头添加声明: extern void xPortSysTickHandler( void );

修改 SysTick_Handler 函数。

5.3 修改 Time Base Source

由于 FreeRTOS 默认使用 SysTick 作为定时器,那么 Time Base Source 就要使用别的定时器作为源,避免冲突。

Core 目录下新建 stm32f1xx_hal_timebase.c 文件。

内容如下:

c 复制代码
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_tim.h"

TIM_HandleTypeDef  htim1;

/**
  * @brief  This function configures the TIM1 as a time base source.
  *         The time source is configured  to have 1ms time base with a dedicated
  *         Tick interrupt priority.
  * @note   This function is called  automatically at the beginning of program after
  *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  * @param  TickPriority: Tick interrupt priority.
  * @retval HAL status
  */
 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
 {
   RCC_ClkInitTypeDef    clkconfig;
   uint32_t              uwTimclock = 0U;
 
   uint32_t              uwPrescalerValue = 0U;
   uint32_t              pFLatency;
   HAL_StatusTypeDef     status = HAL_OK;
 
   /* Enable TIM1 clock */
   __HAL_RCC_TIM1_CLK_ENABLE();
   /* Get clock configuration */
   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
   /* Compute TIM1 clock */
       uwTimclock = HAL_RCC_GetPCLK2Freq();
 
   /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */
   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
 
   /* Initialize TIM1 */
   htim1.Instance = TIM1;
 
   /* Initialize TIMx peripheral as follow:
 
   + Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base.
   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
   + ClockDivision = 0
   + Counter direction = Up
   */
   htim1.Init.Period = (1000000U / 1000U) - 1U;
   htim1.Init.Prescaler = uwPrescalerValue;
   htim1.Init.ClockDivision = 0;
   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
   htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 
   status = HAL_TIM_Base_Init(&htim1);
   if (status == HAL_OK)
   {
     /* Start the TIM time Base generation in interrupt mode */
     status = HAL_TIM_Base_Start_IT(&htim1);
     if (status == HAL_OK)
     {
     /* Enable the TIM1 global Interrupt */
         HAL_NVIC_EnableIRQ(TIM1_UP_IRQn);
       /* Configure the SysTick IRQ priority */
       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
       {
         /* Configure the TIM IRQ priority */
         HAL_NVIC_SetPriority(TIM1_UP_IRQn, TickPriority, 0U);
         uwTickPrio = TickPriority;
       }
       else
       {
         status = HAL_ERROR;
       }
     }
   }
 
  /* Return function status */
   return status;
 }

修改 00_STM32_Demo\Core\stm32f1xx_it.c 文件,增减如下函数:

c 复制代码
extern TIM_HandleTypeDef htim1;

/**
  * @brief This function handles TIM1 update interrupt.
  */
 void TIM1_UP_IRQHandler(void)
 {
   /* USER CODE BEGIN TIM1_UP_IRQn 0 */
 
   /* USER CODE END TIM1_UP_IRQn 0 */
   HAL_TIM_IRQHandler(&htim1);
   /* USER CODE BEGIN TIM1_UP_IRQn 1 */
 
   /* USER CODE END TIM1_UP_IRQn 1 */
 }

将 TIM1_UP_IRQHandler 函数声明到 00_STM32_Demo\Core\stm32f1xx_it.h 头文件中。

相关推荐
倔强的石头_1 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab2 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence2 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神2 天前
三、用户与权限管理
数据库·mysql
✎ ﹏梦醒͜ღ҉繁华落℘3 天前
单片机基础知识---stm32单片机的优先级
stm32·单片机·mongodb
麦聪聊数据3 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
u152109648493 天前
S.S.Audio PRO A2音频隔离器
嵌入式硬件·音视频·实时音视频·视频编解码·视频
DARLING Zero two♡3 天前
【MySQL数据库】数据类型与表约束
数据库·mysql
zd8451015003 天前
RS485 总线详解
单片机·嵌入式硬件