FreeRTOS移植-基于STM32F407

首先新建或找一个基于Keil的STM32基础工程,这里我已经创建好了一个STM32F407VET6的工程模板,工程结构如下图的第1步的矩形框内所示。

下面需要移植FreeRTOS了,将FreeRTOS的源码文件复制到工程文件夹中,一些用不到的文件可删除(哪些文件需要用到可参考上一篇的源码结构分析部分),然后在Keil中也创建一个FreeRTOS目录,将c文件添加进工程,注意port.c来自于RDVS的ARM_CM4F,对应于移植到的SMT32F407硬件。

FreeRTOS移植-基于STM32F407

添加完c文件后,还要添加对应的h文件的搜寻路径,具体如下:

FreeRTOS移植-基于STM32F407

然后就可以编译了,先进行第1次编译:


......(省略显示若干行)
FreeRTOS\portable\RVDS\ARM_CM4F\port.c: 0 warnings, 1 error
compiling heap_4.c...
.\FreeRTOS\include\FreeRTOS.h(98): error:  #5: cannot open source input file "FreeRTOSConfig.h": No such file or directory
  #include "FreeRTOSConfig.h"
FreeRTOS\portable\MemMang\heap_4.c: 0 warnings, 1 error
".\Objects\Template_FreeRTOS.axf" - 8 Error(s), 0 Warning(s).
Target not created.
Build Time Elapsed:  00:00:23

有一个错误,找不到"FreeRTOSConfig.h",这个文件在FreeRTOS源码的Demo文件中,

将Demo中的"FreeRTOSConfig.h"文件放到FreeRTOS文件夹下的include文件夹下,
进行第2次编译:


......(省略显示若干行)
compiling tasks.c...
compiling timers.c...
compiling port.c...
FreeRTOS\portable\RVDS\ARM_CM4F\port.c(713): error:  #20: identifier "SystemCoreClock" is undefined

ortNVIC_SYSTICK_LOAD_REG = ( 
onfigSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
FreeRTOS\portable\RVDS\ARM_CM4F\port.c: 0 warnings, 1 error

又提示"SystemCoreClock"未定义,因为在"FreeRTOSConfig.h" : 中使用了SysyemCoreClock来标记MCU的频率,

在"FreeRTOSConfig.h" :的87~95行:


#ifdef __ICCARM__
    #include <stdint.h>
    extern uint32_t SystemCoreClock;
#endif

#define configUSE_PREEMPTION            1
#define configUSE_IDLE_HOOK             1
#define configUSE_TICK_HOOK             1
#define configCPU_CLOCK_HZ              ( SystemCoreClock )

将条件编译


#ifdef __ICCARM__

修改为

#if defined(__ICCARM__)||defined(__CC_ARM)||defined(__GNU__)

再次进行第3次编译:


......(省略显示若干行)
compiling port.c...
compiling heap_4.c...
linking...
.\Objects\Template_FreeRTOS.axf: Error: L6200E: Symbol SVC_Handler multiply defined (by port.o and stm32f4xx_it.o).
.\Objects\Template_FreeRTOS.axf: Error: L6200E: Symbol PendSV_Handler multiply defined (by port.o and stm32f4xx_it.o).
.\Objects\Template_FreeRTOS.axf: Error: L6200E: Symbol SysTick_Handler multiply defined (by port.o and stm32f4xx_it.o).
Not enough information to list image symbols.
Not enough information to list the image map.
Finished: 2 information, 0 warning and 3 error messages.
".\Objects\Template_FreeRTOS.axf" - 3 Error(s), 0 Warning(s).
Target not created.
Build Time Elapsed:  00:00:02

又提示port.o与stm32f4xx_it.o有重复定义(.o为编译的目标文件,其实就是对应的.c文件出了问题)

注释掉stm32f4xx_it.c中的SVC_Handler() PendSV_Handler() SysTick_Handler()即可

修改后的stm32f4xx_it.c的110~145行:


/**
 \* @brief This function handles SVCall exception.
 \* @param None
 \* @retval None
 */
//void SVC_Handler(void)
//{
//}

/**
 \* @brief This function handles Debug Monitor exception.
 \* @param None
 \* @retval None
 */
void DebugMon_Handler(void)
{
}

/**
 \* @brief This function handles PendSVC exception.
 \* @param None
 \* @retval None
 */
//void PendSV_Handler(void)
//{
//}

/**
 \* @brief This function handles SysTick Handler.
 \* @param None
 \* @retval None
 */
//void SysTick_Handler(void)
//{
// 
//}

再次进行第4次编译:

......(省略显示若干行)
linking...
.\Objects\Template_FreeRTOS.axf: Error: L6218E: Undefined symbol vApplicationIdleHook (referred from tasks.o).
.\Objects\Template_FreeRTOS.axf: Error: L6218E: Undefined symbol vApplicationStackOverflowHook (referred from tasks.o).
.\Objects\Template_FreeRTOS.axf: Error: L6218E: Undefined symbol vApplicationTickHook (referred from tasks.o).
.\Objects\Template_FreeRTOS.axf: Error: L6218E: Undefined symbol vApplicationMallocFailedHook (referred from heap_4.o).
Not enough information to list image symbols.
Finished: 1 information, 0 warning and 4 error messages.
".\Objects\Template_FreeRTOS.axf" - 4 Error(s), 0 Warning(s).

又提示4个hook函数未定义,

这是因为在"FreeRTOSConfig.h"中定义了这些钩子函数,但未找到函数定义,我们先注释掉这些定义,

就是将configUSE_IDLE_HOOK之类的宏定义定义为0即可,


查看"FreeRTOSConfig.h"的93~108行:

#define configUSE_IDLE_HOOK             1
#define configUSE_TICK_HOOK             1
#define configCPU_CLOCK_HZ              ( SystemCoreClock )
#define configTICK_RATE_HZ              ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES            ( 5 )
#define configMINIMAL_STACK_SIZE        ( ( unsigned short ) 130 )
#define configTOTAL_HEAP_SIZE           ( ( size_t ) ( 75 * 1024 ) )
#define configMAX_TASK_NAME_LEN         ( 10 )
#define configUSE_TRACE_FACILITY        1
#define configUSE_16_BIT_TICKS          0
#define configIDLE_SHOULD_YIELD         1
#define configUSE_MUTEXES               1
#define configQUEUE_REGISTRY_SIZE       8
#define configCHECK_FOR_STACK_OVERFLOW  2
#define configUSE_RECURSIVE_MUTEXES     1
#define configUSE_MALLOC_FAILED_HOOK    1

修改93 94 106 108行的数值为0,即:


#define configUSE_IDLE_HOOK             0
#define configUSE_TICK_HOOK             0
......(省略显示11行)
#define configCHECK_FOR_STACK_OVERFLOW  0
......(省略显示1行)
#define configUSE_MALLOC_FAILED_HOOK    0

再次进行第5次编译:

......(省略显示若干行)
compiling port.c...
compiling heap_4.c...
linking...
Program Size: Code=1880 RO-data=424 RW-data=68 ZI-data=2036  
".\Objects\Template_FreeRTOS.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed:  00:00:01

终于编译ok了,这样基本上算移植成功了,下一篇写个FreeRTOS的基础例程测试一下是否可以正常使用。
获取本文用到的工程代码:
微信公众号回复“freertos工程模板”
压缩包内有3个文件夹:

  1. Template_lib:建立工程需要的库文件,包括STM32库文件(v1.4.0)和FreeRTOS库文件(v9.0.0)
  2. Template_noOS:基于Keil5.15的STM32F407工程模板(未使用FreeRTOS)
  3. Template_FreeRTOS:基于Keil5.15的STM32F407 + FreeRTOS工程模板

猜你喜欢

转载自blog.51cto.com/15060517/2641091