【FreeRTOS】【STM32】02 FreeRTOS porting

Based on
[Wildfire®] "FreeRTOS%20 Kernel Implementation and Application Development Practice - Based on STM32"
Punctual Atomic "STM32F429+FreeRTOS Development Manual _V1.2》

Prepare

Basic projects, such as lighting
FreeRTOS system source code

FreeRTOS porting

The previous chapter has explained what the source code files of Free RTOS are needed for transplantation. FreeRTOS provides us with interface files for microcontrollers with cores such as cortex-m0, m3, m4 and m7. As long as the MCU uses these cores, You can use the interface file inside. Usually there are tutorials on the Internet called "Transplanting a certain RTOS to a certain MCU". In fact, to be precise, it cannot be called a transplant. It should be called an official transplant, because these hardware-related interface files are officially RTOS It's already written, we just use it. The transplantation we talk about in this chapter also uses the official FreeRTOS transplantation. We will talk about these underlying transplantation files later.

The files required for transplantation have been mentioned in the previous chapter. We only need to keep the ones we need.
Insert image description here
Only keil, MemMang and RVDS need to be left in the portable folder. folders.
Insert image description here

Add the appropriate files to the project

1. Copy and add the FreeRTOS source code

Before compiling the files in the keil file, we need to have these files, so we need to copy these files from the source code downloaded from the official website to the project folder of our example.
Create a new folder named FreeRTOS in the basic project
Insert image description here
1. Find all the '.c' files in the "FreeRTOSv9.0.0\FreeRTOS\Source" directory files' and copy them to our newly created src folder.
Insert image description here
2. Open the FreeRTOS V9.0.0 source code, find the "MemMang" folder and "RVDS" folder in the "FreeRTOSv9.0.0\FreeRTOS\Source\portable" directory, and copy them to our In the newly created port folder
Insert image description here
3. Find the "include" folder in the "FreeRTOSv9.0.0\ FreeRTOS\Source" directory. This is the header file and contains the API functions provided by FreeRTOS. Now we have three folders under our FreeRTOS folder.
Insert image description here

2. Add files to the keil project

In the previous step, we just placed the source code of FreeRTOS in the local project directory. After the source code is copied to our project, it needs to be added to keil before it can be used.
Insert image description here

Open the basic project, create new groups FreeRTOS_CORE and FreeRTOS_PORTABLE, and then add files to these two groups.
Insert image description here
FreeRTOS_CORE: The file comes from the source code of FreeRTOS .c文件
FreeRTOS_PORTABLE: There are port.c and heap_4.cTwo files.
  port.c is the file in ARM_CM4F under the RVDS folder. Because STM32F429 is a Cortex-M4 core and has an FPU, select the port.c file in ARM_CM4F. In other words, different hardware interface files need to be selected according to different MCUs.
  heap_4.c is a file related to memory management in the MemMang folder. There are 5 c files in the MemMang folder: heap_1.c, heap_2.c, heap_3.c, heap_4.c and heap_5.c , you can use any one. Here we choose heap_4.c

3. Add the corresponding header file path

After adding the C file in the FreeRTOS source code, you must also add the header file path and header file path of the FreeRTOS source code.
Insert image description here

4. Solve some problems during compilation

1. Found during compilationThe file "FreeRTOSConfig.h" cannot be opened
It can be found in the Demo ported by Free RTOS to stm32F407. The file The folder is CORTEX_M4F_STM32F407ZG-SK. Copy it to the project. The path is arbitrary. I put it in the include folder in the FreeRTOS source code for convenience.
Insert image description here
FreeRTOSConfig.h is the configuration file of FreeRTOS. General operating systems have tailoring and configuration functions. These tailoring and configurations are all completed through a file. Basically, macro definitions are used to complete the system configuration. Configuration and tailoring.
There are similar configuration files when transplanting the FATFS file system.

2. Compile again and find SystemCoreClock undefined error. This is because SystemCoreClock is used in FreeRTOSConfig.h to mark the frequency of the MCU. And the definition here is conditional.
Insert image description here
But here is conditional precompilation, the condition is

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

This conditional compilation needs to be modified. The modified code is as follows

#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
	#include <stdint.h>
	extern uint32_t SystemCoreClock;
#endif

3.Undefined Hook function, hook function, this is because these hook functions are enabled in FreeRTOSConfig.h, but they are not It is caused by not defining these hook functions. Just turn off these hook functions in FreeRTOSConfig.h and define the macros configUSE_IDLE_HOOK, configUSE_TICK_HOOK, configUSE_MALLOC_FAILED_HOOK and configCHECK_FOR_STACK_OVERFLOW as 0.

5.FreeRTOSConfig.h header file modification

As I said before, the FreeRTOSConfig.h file can be added to any folder in the project, just add it to the path. This header file defines macros that tailor the functionality required by the entire FreeRTOS.
Here is the annotated version of FreeRTOSConfig.h from Wildfire Electronics. Some Chinese comments have been added, and the relevant header files have been classified to facilitate the search for macro definitions. For details, see [Wildfire®] "FreeRTOS%20 Kernel Implementation and Application Development Practice—Based on STM32".pdf

(1)Such labels in the code list are detailed in [Wildfire®] "FreeRTOS%20 Kernel Implementation and Application Development Practice—Based on STM32".pdf

Generally speaking, just refer to the files in the official DEMO.

The content of the FreeRTOSConfig.h header file has not been modified much. Specifically: modify the header file of the corresponding development board. If STM32F1 is used, include the F1 header file #include "stm32f10x.h". The same is true for other series. , then include the corresponding header file.
Insert image description here

6. Modify stm32f10x_it.c to prevent repeated definitions

All time-related things in FreeRTOS are handled in the SysTick interrupt service function. It is the heartbeat of FreeRTOS.
PendSV_Handler(), SVC_Handler() and Systick_Handler() are repeatedly defined. This is because port.c and stm32f4xx_it.c There are duplicate functions defined in the file. The three functions PendSV_Handler(), SVC_Handler() and Systick_Handler() in stm32f4xx_it.c are blocked here.

or:

shield

PendSV_Handler() and SVC_Handler() are two repeatedly defined interrupt handling functionsRewriteSysTick_Handler() function

 //systick 中断服务函数 
 void SysTick_Handler(void) 
 {
    
     
 #if (INCLUDE_xTaskGetSchedulerState == 1 ) 
	 if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
    
     
 #endif /* INCLUDE_xTaskGetSchedulerState */ 
 
	 xPortSysTickHandler(); 
 #if (INCLUDE_xTaskGetSchedulerState == 1 ) 
	 } 
 #endif /* INCLUDE_xTaskGetSchedulerState */ 
 } 

7. Modify main.c

#include " FreeRTOS.h"
#include " task.h"

 int main(void)
 {
    
    
 /* 暂时没有在 main 任务里面创建任务应用任务 */
 }

Guess you like

Origin blog.csdn.net/apythonlearner/article/details/133636457