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

相关推荐
AI的探索之旅8 分钟前
AI Agent辅助立创EDA设计——从原理图到PCB
人工智能·stm32·硬件工程
风和先行8 分钟前
Android 数据库相关学习总结
android·数据库·学习
宠友信息17 分钟前
Spring Boot与异步审核构建仿小红书源码内容发布全流程
java·数据库·spring boot·redis·mysql·oracle·uni-app
嵌入式-老费31 分钟前
esp32开发与应用(从esp32到esp32s3)
嵌入式硬件
阿拉斯攀登1 小时前
无人售货柜设备开机自动注册逻辑:唯一标识绑定、防重复、批量铺货适配
嵌入式硬件·安卓·零售·安卓驱动·无人售货柜·无人售货机
2401_873479401 小时前
如何识别代理IP和伪装流量?用IP情报工具三步穿透住宅代理伪装
网络·数据库·网络协议·tcp/ip·ip
诚信定制8391 小时前
如何启动 Redis 服务:详细步骤指南
数据库·redis·缓存
AllData公司负责人1 小时前
数据同步平台|AIIData数据中台实现MySQL、Hive、Kafka 一键接入Doris
大数据·数据库·hive·mysql·kafka·实时同步
Edward111111111 小时前
一AI名词
java·开发语言·数据库·学习
hey you~1 小时前
400电话选型技术测评:一个开发视角的线路质量评估方案
运维·网络·数据库·400电话·企业通信