基于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 头文件中。

相关推荐
天空'之城32 分钟前
单片机基础核心知识点汇总(三十四)
单片机·嵌入式硬件
Horn Still Sounds1 小时前
51单片机入门:硬件基础、位运算、数码管动态显示完整梳理
嵌入式硬件·mongodb·51单片机
程序员阿鹏2 小时前
为什么MySQL InnoDB选择B+树?
数据结构·数据库·b树·sql·mysql·算法·缓存
三佛科技-187366133972 小时前
24W-48W隔离双绕组非标自供电芯片LP3718CD(电路原理图)
单片机·嵌入式硬件
瀚高PG实验室3 小时前
使用pg_stat_statements抓取数据库TOP SQL
数据库·sql·postgresql·瀚高数据库
yanwumuxi4 小时前
【数据库系列】opensearch数据备份和恢复
数据库
anxiao_m4 小时前
2026企业AI数字孪生选型攻略,不同场景对应不同解决方案
大数据·网络·数据库
SelectDB4 小时前
从 ClickHouse 迁移到 Doris:SQL 兼容、同步与验证清单(实战笔记)
大数据·数据库·数据分析
糖糖单片机设计4 小时前
基于 STM32 的小型鸟类宠物繁殖系统
stm32·嵌入式硬件·宠物
SelectDB5 小时前
Doris 还是 ClickHouse?一张表说清 6 个关键差异(实战笔记)
大数据·数据库·数据分析