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