首页 > 其他分享 >手动移植FreeRTOS到stm32

手动移植FreeRTOS到stm32

时间:2024-09-22 21:38:19浏览次数:1  
标签:PROJECT CMAKE NAME FreeRTOS stm32 Add include 移植

手动移植FreeRTOS

stm32cubemx内置了freertos,如果不想手动移植,可以参考这个视频:

https://www.bilibili.com/video/BV1To4UeVEHA/

但手动移植可以加深自己的理解,可以参考下面这个视频:
https://www.bilibili.com/video/BV1vUpje9Ej2?p=11

以下内容是手动移植的过程记录

使用stm32cubemx创建裸机项目

注意:在SYS设置timebase source时(即HAL_Delay的时钟源),不能使用SysTick,因为SysTick被FreeRTOS使用了,可以使用其他的(如TIM1、TIM2……)

另外,假设使用了TIM1作为timebase source,记得修改其优先级为较高或最高,比如0或1

Systick的优先级由FreeRTOS设置,我们不用管

移植FreeRTOS

主要包含以下几个步骤:

  1. 下载FreeRTOS

    (我这里选择了9.0.0版本)

  2. 将必要的源码复制到自己的裸机项目中,包括

    • 内核源码(位于FreeRTOS\Source)
    • port代码(位于FreeRTOS\Source\portable)
    • 内存管理代码(位于FreeRTOS\Source\portable\MemMang)
    • FreeRTOSConfig.h头文件(从Demo中寻找适合自己开发板的)
  3. 修改 FreeRTOSConfig.h 文件

  4. 修改 stm32f1xx_it.c文件

接下来依次介绍

第一步,下载,本项目下载的是V9.0.0版本

第二步,在裸机项目目录中新建文件夹FreeRTOS,然后把下面的文件都复制过去
image-20240922200302641

进入【复制的portable文件夹】内,保留2个文件夹,一个是MemMang文件夹,另一个根据自己的编译工具选择,如果是gcc则保留GCC,如果是keil则保留RVDS(注意Keil的移植代码放到RVDS里了),本项目使用GCC编译,所以保留了GCC和MenMang文件夹,其他的都删除了
image-20240922201041846

再进入GCC文件夹内,选择适合自己开发板芯片的部分保留,其他都删除,比如本项目用的是STM32f103,是ARM_Cortex3架构的,所以保留ARM_CM3

然后MemMang文件夹内把heap_4.c保留,其他都删除

最后,还有一个FreeRTOSConfig.h头文件,FreeRTOS内置了很多代码示例,在Demo文件夹内,适配了很多不同的芯片,选择适合自己芯片的示例,进去后找到FreeRTOSConfig.h头文件并把它复制到自己的项目中,我这里是复制到了Core/Inc文件夹下

第三步,修改 FreeRTOSConfig.h 文件

主要是下面2个地方
image-20240922201844776

image-20240922201945892

关于FreeRTOSConfig.h可以从官方文档了解:定制 - FreeRTOS™

修改后的FreeRTOSConfig.h完整代码如下:

/*
    FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
    All rights reserved

    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.

    This file is part of the FreeRTOS distribution.

    FreeRTOS is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License (version 2) as published by the
    Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.

    ***************************************************************************
    >>!   NOTE: The modification to the GPL is included to allow you to     !<<
    >>!   distribute a combined work that includes FreeRTOS without being   !<<
    >>!   obliged to provide the source code for proprietary components     !<<
    >>!   outside of the FreeRTOS kernel.                                   !<<
    ***************************************************************************

    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  Full license text is available on the following
    link: http://www.freertos.org/a00114.html

    ***************************************************************************
     *                                                                       *
     *    FreeRTOS provides completely free yet professionally developed,    *
     *    robust, strictly quality controlled, supported, and cross          *
     *    platform software that is more than just the market leader, it     *
     *    is the industry's de facto standard.                               *
     *                                                                       *
     *    Help yourself get started quickly while simultaneously helping     *
     *    to support the FreeRTOS project by purchasing a FreeRTOS           *
     *    tutorial book, reference manual, or both:                          *
     *    http://www.FreeRTOS.org/Documentation                              *
     *                                                                       *
    ***************************************************************************

    http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
    the FAQ page "My application does not run, what could be wrong?".  Have you
    defined configASSERT()?

    http://www.FreeRTOS.org/support - In return for receiving this top quality
    embedded software for free we request you assist our global community by
    participating in the support forum.

    http://www.FreeRTOS.org/training - Investing in training allows your team to
    be as productive as possible as early as possible.  Now you can receive
    FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
    Ltd, and the world's leading authority on the world's leading RTOS.

    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
    including FreeRTOS+Trace - an indispensable productivity tool, a DOS
    compatible FAT file system, and our tiny thread aware UDP/IP stack.

    http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
    Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.

    http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
    Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
    licenses offer ticketed support, indemnification and commercial middleware.

    http://www.SafeRTOS.com - High Integrity Systems also provide a safety
    engineered and independently SIL3 certified version for use in safety and
    mission critical applications that require provable dependability.

    1 tab == 4 spaces!
*/

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

/* Library includes. */
// #include "stm"

/*-----------------------------------------------------------
 * Application specific definitions.
 *
 * These definitions should be adjusted for your particular hardware and
 * application requirements.
 *
 * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
 * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. 
 *
 * See http://www.freertos.org/a00110.html.
 *----------------------------------------------------------*/

#define configUSE_PREEMPTION        1
#define configUSE_IDLE_HOOK         0
#define configUSE_TICK_HOOK         0
#define configCPU_CLOCK_HZ          ( ( unsigned long ) 72000000 )  
#define configTICK_RATE_HZ          ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES        ( 5 )
#define configMINIMAL_STACK_SIZE    ( ( unsigned short ) 128 )
#define configTOTAL_HEAP_SIZE       ( ( size_t ) ( 4 * 1024 ) )
#define configMAX_TASK_NAME_LEN     ( 16 )
#define configUSE_TRACE_FACILITY    0
#define configUSE_16_BIT_TICKS      0
#define configIDLE_SHOULD_YIELD     1
#define configUSE_MUTEXES           1

/* Co-routine definitions. */
#define configUSE_CO_ROUTINES       0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */

#define INCLUDE_vTaskPrioritySet        1
#define INCLUDE_uxTaskPriorityGet       1
#define INCLUDE_vTaskDelete             1
#define INCLUDE_vTaskCleanUpResources   0
#define INCLUDE_vTaskSuspend            1
#define INCLUDE_vTaskDelayUntil         1
#define INCLUDE_vTaskDelay              1

/* This is the raw value as per the Cortex-M3 NVIC.  Values can be 255
(lowest) to 0 (1?) (highest). */
#define configKERNEL_INTERRUPT_PRIORITY         255
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY    191 /* equivalent to 0xb0, or priority 11. */


/* This is the value being used as per the ST library which permits 16
priority values, 0 to 15.  This must correspond to the
configKERNEL_INTERRUPT_PRIORITY setting.  Here 15 corresponds to the lowest
NVIC value of 255. */
#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY 15


#define xPortPendSVHandler PendSV_Handler
#define vPortSVCHandler SVC_Handler
#define INCLUDE_xTaskGetSchedulerState          1

#endif /* FREERTOS_CONFIG_H */


第四步,修改 stm32f1xx_it.c文件

首先找到PendSV_Handler和SVC_Handler这2个函数并注释掉(这两函数由FreeRTOS帮我们写好了),然后找到SysTick函数,修改为如下:

void SysTick_Handler(void)
{
    if (xTaskGetSchedulerState != taskSCHEDULER_NOT_STARTED)
    {
        xPortSysTickHandler();
    }
}

创建自己的task

在项目根目录下新建application文件夹,添加led.h和led.c这2个文件,内容分别如下:

led.h

#ifndef LED_H__
#define LED_H__

void task_led(void *argument);

#endif

led.c

#include "led.h"
#include "main.h"
#include "stm32f1xx_hal.h"
#include "FreeRTOS.h"
#include "task.h"

void task_led(void *argument){
    // taskENTER_CRITICAL();  进入临界区的代码会被保护(不会被打断)
    while(1){
        HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_RESET);
        vTaskDelay(pdMS_TO_TICKS(500));  // 500ms
        
        HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_SET);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

main.c的main函数如下

// include ...
#include "FreeRTOS.h"  // 必须在include "task" 前面
#include "task.h"
#include "led.h"

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

    // 创建任务
    xTaskCreate(task_led, "ledTask", 128, NULL, 2, NULL);

    // 启动任务调度器,调度器自己会管理要运行哪个任务
    // 启动rtos调度器后将不会退出了
    vTaskStartScheduler();   
}

cmakelists修改

  1. 通过 target_sources 添加.c源码文件,主要是FreeRTOS的代码和自己创建的代码文件

    # Add sources to executable
    file(GLOB src_rtos FreeRTOS/*.c)
    target_sources(${CMAKE_PROJECT_NAME} PRIVATE
        # Add user sources here
        ${src_rtos}
        FreeRTOS/portable/MemMang/heap_4.c
        FreeRTOS/portable/GCC/ARM_CM3/port.c
        application/led.c
    )
    
  2. 通过 target_include_directories.h 头文件所在目录添加进来,主要是FreeRTOS的头文件和自己创建的头文件

    # Add include paths
    target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
        # Add user defined include paths
        FreeRTOS/include
        FreeRTOS/portable/GCC/ARM_CM3
        application
        
    )
    
  3. 生成hex文件

    add_custom_command(
        TARGET ${CMAKE_PROJECT_NAME}  POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -O ihex "${CMAKE_PROJECT_NAME}.elf" "${CMAKE_PROJECT_NAME}.hex"
    )
    

完整cmakelists.txt如下:

cmake_minimum_required(VERSION 3.22)

#
# This file is generated only once,
# and is not re-generated if converter is called multiple times.
#
# User is free to modify the file as much as necessary
#

# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)


# Define the build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# Set the project name
set(CMAKE_PROJECT_NAME 1_rtos_led2)

# Include toolchain file
include("cmake/gcc-arm-none-eabi.cmake")

# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Enable CMake support for ASM and C languages
enable_language(C ASM)

# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})

# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})

# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined library search paths
)

# Add sources to executable
file(GLOB src_rtos FreeRTOS/*.c)
# aux_source_directory(src_rtos FreeRTOS)
message(${src_rtos})
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user sources here
    ${src_rtos}
    FreeRTOS/portable/MemMang/heap_4.c
    FreeRTOS/portable/GCC/ARM_CM3/port.c
    application/led.c
)

# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined include paths
    FreeRTOS/include
    FreeRTOS/portable/GCC/ARM_CM3
    application
    
)

# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
    # Add user defined symbols
)

# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
    stm32cubemx

    # Add user defined libraries
)

add_custom_command(
    TARGET ${CMAKE_PROJECT_NAME}  POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex "${CMAKE_PROJECT_NAME}.elf" "${CMAKE_PROJECT_NAME}.hex"
)

这样就完成了,可以进行编译了。

image-20240922212320683

完整项目下载

https://gitcode.net/m0_46079750/stm32_freertos_led

标签:PROJECT,CMAKE,NAME,FreeRTOS,stm32,Add,include,移植
From: https://www.cnblogs.com/ajream/p/18425917

相关文章

  • 用Eide下配合Cubemx配置stm32环境
    PS:本篇为个人学习的记录,一是方便回忆,二是相同时方便给像我一样的小白一点建议。本文默认已安装好STM32Cubemx和VSCode,以及VsCode下的EideCubemx部分选择好需要使用的对应单片机创建工程。在ProjectManager选项下选择Toolchain/IDE下的makefile方式来创建工程。什么是ma......
  • 电赛毕设:基于stm32的非接触式交流电流检测装置
    需求如下:设计一种非接触式的交流电流检查装置1、基本要求:(1)由任意波信号发生器产生的信号经功率放大电路驱动后,通过导线连接1002电阻负载,形成一电流环路:(2)设计一采用非接触式传感的电流信号检测装置。2、发挥要求:(1)检测环路电流信号的幅度及频率(2)将信号的参数显示出来......
  • (六)Protues仿真STM32单片机控制8x8LED显示
    (六)Protues仿真STM32单片机控制8x8LED显示–ARMFUN1,配置CUBEMX,将PA0~7,PAB0~7配置为GPIOOUTPUT模式2,GPIOA负责8bit数据,高电平有效,GPIOB负责行选则,低电平有效,编写行刷新函数voiddisp_set_row(unsignedchardat,charsel){ GPIOB->ODR=0xff;//关闭行选,防止将数据......
  • STM32流水灯程序代码及解析:三种实现方式
    STM32流水灯程序代码及解析:三种实现方式在这篇文章中,我们将介绍三种方式来实现STM32的流水灯程序,包括使用HAL库、标准库和直接操作寄存器的方法。通过这三种不同的方式。1.硬件准备STM32开发板(如STM32F4或STM32F1系列)若干LED灯(通常是4个)适当的电阻连接线2.接线图将L......
  • stm32 FLASH闪存(读写内部FLASH&读取芯片ID)
    理论1.FLASH简介STM32F1系列的FLASH包含程序存储器、系统存储器和选项字节三个部分,通过闪存存储器接口(外设)(FLASH管理员)可以对程序存储器和选项字节进行擦除和编程读写FLASH的用途:   利用程序存储器的剩余空间来保存掉电不丢失的用户数据    通过在程序中编......
  • 学习STM32的震动开关
    学习STM32的震动开关在本文中,我将详细介绍如何使用STM32微控制器来实现一个震动开关。震动开关是一种能够检测物体是否发生震动的传感器,通常用于安防系统、智能家居等领域。我们将使用STM32的GPIO模块和外部中断功能来实现震动开关的功能。前期准备在开始之前,我们需要准备以......
  • STM32实现简单的智能办公系统
    为了实现一个简单的智能办公系统,我们可以利用STM32微控制器和一些外设来实现各种功能。在本文中,我将介绍如何通过STM32来控制温度和湿度传感器、人体红外传感器,以及通过无线通信来实现报警和监控功能。首先,我们需要准备一些硬件设备,包括:STM32微控制器开发板:本例中使用的是S......
  • 学习STM32的OLED显示屏控制
    引言:OLED(OrganicLightEmittingDiode)屏幕是一种新型的显示技术,它使用有机材料作为发光器件,具有高对比度、快速响应、广视角和低功耗等特点。STM32是一家全球领先的微控制器制造商,其产品家族中包括了多种型号的ARMCortex-M系列的微控制器。在本文中,我们将介绍如何使用STM32......
  • 学习STM32的火焰传感器
    火焰传感器是一种用于检测火焰存在的电子设备。它可以广泛应用于火灾检测、火焰监控和安全预警等领域。本文将详细介绍如何使用STM32开发板和火焰传感器来实现火焰检测功能,并给出相应的代码案例。一、硬件准备首先,需要准备以下硬件设备:STM32开发板(如STM32F103C8T6)火焰传感器......
  • 单片机毕业论文 怎么写 STM32单片机毕业论文 单片机毕设设计论文怎么写 单片机编程 单
    单片机毕业论文怎么写引言单片机毕业论文怎么写?这个问题看似复杂,但只要掌握一些关键技巧,就能轻松应对。论文的本质无非是用一种结构化的方式展示你对单片机的理解、设计、实现和思考。接下来,我们用通俗幽默的方式,逐步解析如何撰写一篇优秀的单片机毕业论文。1.开题报告......