[FreeRTOS]FreeRTOS porting stm32 detailed steps

Time:2023-10-25

I’m looking for FreeRTOS porting related tutorials especially less, so I want to introduce FreeRTOS porting stm32 detailed steps in great detail, including the download of the source code, source code introduction, system porting, code verification, etc., each step has a corresponding introduction and explanation, I hope it can help you.
[FreeRTOS]FreeRTOS porting stm32 detailed steps



1. FreeRTOS source code downloads

Official website download, warehouse download, mirror download, Baidu.com disk download, private message me


1.1 You can download it from the FreeRTOS website.[FreeRTOS Official Website PortalThe download speed is very slow, however, it is not recommended to use this method to download without going over the wall.

1.1.1 The official website is as follows, click on the upper right corner or below the download portal

[FreeRTOS]FreeRTOS porting stm32 detailed steps


1.1.2 There are two download options when you go to the download page.

  • The first option is FreeRTOS 202212.01, a downloadable packet containing the latest FreeRTOS kernel, FreeRTOS-Plus library and AWS IoT library, as well as sample projects
  • The second option is FreeRTOS 202210.01 LTS, the packet contains the FreeRTOS LTS libraries, which includes the FreeRTOS kernel and IoT libraries, but no sample project

Usually we just download the first one.

[FreeRTOS]FreeRTOS porting stm32 detailed steps


1.2 Direct downloads through the repository.[Warehouse Address Portal]It is also not recommended to use this method, as it is also very slow and cannot even open the web page.


1.3 Download through domestic mirrors, this way of downloading is not affected by the domain name, is the most direct way
[Domestic Mirror Portal]Recommended use


1.3.1 Opening a linked page

[FreeRTOS]FreeRTOS porting stm32 detailed steps


1.3.2 Click download/clone, download successfully

[FreeRTOS]FreeRTOS porting stm32 detailed steps


1.4 Use the source code I uploaded to Baidu.com disk, it is in the official website directly download the data package, it is recommended to use, at least faster than the official website and the warehouse

Link:“Baidu.com
Extract code: sqml


1.5 If you can’t do any of the above, you can send me a private message directly.

The downloaded zip unpacked file yah contains the following:
[FreeRTOS]FreeRTOS porting stm32 detailed steps

Zip 88.3MB, Unzip folder 506MB


2. FreeRTOS source code introduction

The previous article has been very detailed, you can go straight to the previous article to understand the source code structure, can better help you understand the principles of porting and FreeRTOS implementation principles.

Blog Portal:FreeRTOS] Detailed directory structure and roles

It can basically be summarized in the following chart:
[FreeRTOS]FreeRTOS porting stm32 detailed steps


3. FreeRTOS system porting

3.1 Prepare the FreeRTOS source code and a configured STM32 project, I use a previous project that has been successfully implemented LED blinking project

[FreeRTOS]FreeRTOS porting stm32 detailed steps

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.2 Create a FreeRTOS folder in the project that holds all the ported files.

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.3 In the FreeRTOS folder you just created, create new src, inc, and port folders:

  • The src folder is used to store the source code
  • The inc folder is used to store header files
  • The port folder is used to store port platform related files

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.4 Next, we port the FreeRTOS source code to the new folder we created.

3.4.1 First of all, we open the protable folder in the Soure folder in the source code, and find the MemMang folder, this folder is full of memory management files, we only use the heap_4.c file, and copy it to the port folder.

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.4.2 Next, in the portable folder, find the RVDS folder, select the ARM_M3 port.c and portmacro.h files inside, and copy them to the port folder of our project file.

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.4.3 Go back to the source folder and copy all the .c files in this folder to the new src folder of the project.


[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.4.4 Include all FreeRTOS header files in the source folder and add them to the new inc folder of the project.

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.4.5 Add a configuration file, find the CORTEX_STM32F103_Keil folder in the demos folder in the FreeRTOS folder, and copy the FreerRTOSConfig.h file inside to the newly created FreeRTOS folder.

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.5 Then we add the file paths, open the keil software and click the magic wand, enter C/C++, add the file paths in Include Paths, add all the paths of FreeRTO, src, inc and port.

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.6 New Group Add all the files to the project, click the icon of the three grids next to the Magic Wand, select New Group, we create three new groups FreeRTOS_src, FreeRTOS_inc, FreeRTOS_port, add all the .c files in the project folder src to FreeRTOS_src, add all the .h files in the project folder inc to FreeRTOS_inc (excluding .readme), add the files (heap_4.c, readme) in the project folder port. c files in the project folder src to FreeRTOS_src, add all .h files in the project folder inc to FreeRTOS_inc (excluding .readme), add the files in the project folder port (heap_4.c, port.c) to FreeRTOS_port, and add the configuration file FreeRTOSConfig.h to the FreeRTOS_inc group.

[FreeRTOS]FreeRTOS porting stm32 detailed steps

[FreeRTOS]FreeRTOS porting stm32 detailed steps

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.7 Complete the macro definitions for the interrupt functions, in FreeRTOSConfig.h, and add in the conditional compilation

#define xPortPendSVHandler PendSV_Handler
#define vPortSVCHandler SVC_Handler
#define xPortSysTickHandler SysTick_Handler

[FreeRTOS]FreeRTOS porting stm32 detailed steps


3.8 Commenting out the three interrupt null functions in the STM library function

Separately:

void SVC_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);

[FreeRTOS]FreeRTOS porting stm32 detailed steps

At this point, the system migration has been fully completed


4. System code validation

4.1 First add the header files FreeRTOS.h and task.h to the main file.

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "FreeRTOS.H"
#include "task.h"

4.2 We verify this directly using the code for the blinking LED by first creating a task function

static void task1(void *arg)
{
	while(1)
	{
		GPIO_ResetBits(GPIOC, GPIO_Pin_13);
		vTaskDelay(500);
		GPIO_SetBits(GPIOC, GPIO_Pin_13);
		vTaskDelay(500);
	}
}

4.3 Creating a Task and Turning on the Task Scheduler in the Main Function

xTaskCreate(task1,"task1",512,NULL,2,&task1Handler);
	vTaskStartScheduler();

4.4 Of course, to use the LED must be initialized serial port, we take GPIO_Pin_13 in GPIOC for example

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

4.5 The entire code in the main file is

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "FreeRTOS.H"
#include "task.h"

TaskHandle_t task1Handler;

static void task1(void *arg)
{
	while(1)
	{
		GPIO_ResetBits(GPIOC, GPIO_Pin_13);
		vTaskDelay(500);
		GPIO_SetBits(GPIOC, GPIO_Pin_13);
		vTaskDelay(500);
	}
}
int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	xTaskCreate(task1,"task1",512,NULL,2,&task1Handler);
	vTaskStartScheduler();
	
}

FreeRTOS common function transfer port:FreeRTOS] Frequently Used Functions Summary


4.6 After finishing the compilation, you can burn it to the development board for verification.


4.7 Or use keil’s own debugger for debugging
4.7.1 First, find the debug option in the Magic Wand, select use simulate, and the following should also be configured to the corresponding microcontroller model

[FreeRTOS]FreeRTOS porting stm32 detailed steps


4.7.2 Click on the Debug button to add a logic analyzer to the top

[FreeRTOS]FreeRTOS porting stm32 detailed steps


4.7.3 Click setup of the logic analyzer to add the signal, we are using stm32f103c8t6, so the signal is PORTC.13, different development boards can view the configuration of the serial port and the corresponding registers of the microcontroller, pay attention to the need to select the signal type as bit

[FreeRTOS]FreeRTOS porting stm32 detailed steps


4.7.4 Running at full speed gives an accurate waveform of the serial port

[FreeRTOS]FreeRTOS porting stm32 detailed steps

You can also create multiple tasks to view the execution of waveform analysis tasks


Project Engineering Complete Code Transfer Port:
Link:FreeRTOS Sample Project
Extract code: k8uk

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]