Porting FreeRTOS Code

1. Introduction to FreeRTOS FilesThe official website of FreeRTOS:https://freertos.org/zh-cn-cmn-sThe latest code and related documentation can be obtained from the FreeRTOS official website.The path to obtain historical versions of FreeRTOS code:https://sourceforge.net/projects/freertos/files/FreeRTOS/This article uses version V9.0.0.Preparation of the IM94 series Demo project.The contents of the FreeRTOS files are shown in the figure below:Porting FreeRTOS CodePorting FreeRTOS CodePorting FreeRTOS Code2. Porting FreeRTOS1. Copy the FreeRTOS folderDirectly copy the FreeRTOS folder into the Demo project folder.Select the corresponding port.c, portmacro.h, and FreeRTOSConfig.h files based on the chip’s core model and compiler environment.This example uses the domestic chip manufacturer IM942KALB series automotive-grade MCU, with an ARM M4F core,and the compiler used is arm-none-eabi-gcc, thus selecting the relevant files as shown in the figure below:Porting FreeRTOS CodePorting FreeRTOS CodeThe final directory of the FreeRTOS source files in the project is as follows:Porting FreeRTOS Code2. Modify related interrupt filesFirst, it is necessary to disable the definitions of SysTick_Handler, SVC_Handler, and PendSV_Handler in the Demo project.Directly use these three interrupt functions from the FreeRTOS source code.Porting FreeRTOS CodeSysTick_Handler: Mainly implements task scheduling processing. The source code is as follows:

void xPortSysTickHandler( void ){ /* The SysTick runs at the lowest interrupt priority, so when this interrupt executes all interrupts must be unmasked.  There is therefore no need to save and then restore the interrupt mask value as its value is already known. */ portDISABLE_INTERRUPTS(); { /* Increment the RTOS tick. */ if( xTaskIncrementTick() != pdFALSE ) { /* A context switch is required.  Context switching is performed in the PendSV interrupt.  Pend the PendSV interrupt. */ portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; } } portENABLE_INTERRUPTS(); }

PendSV_Handler:As the core of task switching in FreeRTOS, it is executed when the PendSV interrupt is triggered (usually by SysTick or manually triggered), achieving seamless task switching. The source code is as follows:

void xPortPendSVHandler(void){ /* Bare function declaration (compiler does not generate function entry/exit code) */ __asm volatile ( 

Leave a Comment