This article introduces porting FreeRTOS on N32F43x
Download FreeRTOS source code
FreeRTOS official download address: https://freertos.org/a00104.html , download FreeRTOS 202112.00 version
FreeRTOSv202112.00.zip
(89.72 MB, downloads: 0)
, there is also a long-term support LTS version
FreeRTOSv202012.04-LTS.zip
(6.69 MB, downloads: 1)
Unzip the downloaded source code package
Mainly look at the Source folder in the FreeRTOS folder. The files in the Source folder are the FreeRTOS source code
Engineering transplantation
Add FreeRTOS source code
Create a new FreeRTOS folder in the project and copy the source code in the Source folder to the FreeRTOS folder in the project
Keep Keil, MemMang and RVDS folders in the portable file, and keep the ARM_CM4F folder in the RVDS folder
Add .h header file
Add the FreeRTOS.h configuration file, open the FreeRTOS source code Demo folder, and copy the FreeRTOSConfig.h file in the chip example of the M4F core to the include folder under the FreeRTOS folder of the project.
Compile the code and it shows that SystemCoreClock is not defined
Because SystemCoreClock is used to mark the frequency of MCU in FreeRTOSConfig.h, there is a conditional compilation in FreeRTOSConfig.h. The following code is valid only when __ICCARM__ is defined
This conditional compilation needs to be modified to #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
After recompiling, it is shown that SVC_Handler() and SysTick_Handler() are repeatedly defined in port.c and n32l43x.c
After recompiling after masking the comments of SVC_Handler() and SysTick_Handler() in n32l43x.c, it shows that 4 Hook functions are undefined. The reason is that these 4 functions are enabled in FreeRTOSConfig.h, but they are not defined.
Change the macro definitions corresponding to these 4 functions in FreeRTOSConfig.h to 0
Compiled
The test designs 3 tasks: start_task, led1_task, led2_task. start_task is used to create two tasks, led1_task and led2_task. led1_task controls LED1 to flash once every 11 seconds, and led2_task controls LED2 to light up for 200ms and off for 800ms in a 1-second cycle. After compiling, memory overflow is displayed
The configTOTAL_HEAP_SIZE macro defined in FreeRTOSConfig.h is 75kB, which exceeds the 32kB of N32L43x.
Set configTOTAL_HEAP_SIZE to 20kB and compile successfully
Software Code
/** 任务优先级 */
#define START_TASK_PRIO 1
#define LED1_TASK_PRIO 2
#define LED2_TASK_PRIO 3
/** 任务堆栈大小 */
#define START_STK_SIZE 128
#define LED1_STK_SIZE 50
#define LED2_STK_SIZE 50
/** 任务句柄 */
TaskHandle_t StartTask_Handler;
TaskHandle_t LED1Task_Handler;
TaskHandle_t LED2Task_Handler;
/** 任务函数 */
void start_task(void *pvParameters);
void led1_task(void *pvParameters);
void led2_task(void *pvParameters);
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] Main program.
*/
int main(void)
{
/*SystemInit() function has been called by startup file startup_n32l43x.s*/
Delay_Init();
hal_ledInit();
//创建开始任务
xTaskCreate((TaskFunction_t )start_task, //任务函数
(const char* )"start_task", //任务名称
(uint16_t )START_STK_SIZE, //任务堆栈大小
(void* )NULL, //传递给任务函数的参数
(UBaseType_t )START_TASK_PRIO, //任务优先级
(TaskHandle_t* )&StartTask_Handler); //任务句柄
vTaskStartScheduler(); //开启任务调度
}
//开始任务任务函数
void start_task(void *pvParameters)
{
taskENTER_CRITICAL(); //进入临界区
//创建LED1任务
xTaskCreate((TaskFunction_t )led1_task,
(const char* )"led1_task",
(uint16_t )LED1_STK_SIZE,
(void* )NULL,
(UBaseType_t )LED1_TASK_PRIO,
(TaskHandle_t* )&LED1Task_Handler);
//创建LED2任务
xTaskCreate((TaskFunction_t )led2_task,
(const char* )"led2_task",
(uint16_t )LED2_STK_SIZE,
(void* )NULL,
(UBaseType_t )LED2_TASK_PRIO,
(TaskHandle_t* )&LED2Task_Handler);
vTaskDelete(StartTask_Handler); //删除开始任务
taskEXIT_CRITICAL(); //退出临界区
}
//LED1任务函数
void led1_task(void *pvParameters)
{
while(1)
{
GPIO_WriteBit(LED1_PORT, LED1_PIN, (Bit_OperateType)(1 - GPIO_ReadOutputDataBit(LED1_PORT, LED1_PIN)));
vTaskDelay(500);
}
}
//LED2任务函数
void led2_task(void *pvParameters)
{
while(1)
{
GPIO_SetBits(LED2_PORT, LED2_PIN);
vTaskDelay(200);
GPIO_ResetBits(LED2_PORT, LED2_PIN);
vTaskDelay(800);
}
}
Running Tests
LED
Test code
N32L43x_FreeRTOS.rar
(721.25 KB, downloads: 9)
|