前言
以前在clion中使用rt-thread的方式
1. 使用的cubemx生成的方式: 这种方式只能使用rt-thread的内核版本
2. 自己去把rt-thread的源码拷贝到对应的工程中,再编写对应的CMakelists文件进行管理
思考
- 我的想法是通过rt-studio创建项目,然后通过工具转成cmakel编译的工具,然后根据官方提供的工具env来实现这个功能
概述
- 本次是最初的构建方案,可能在一些方面做的不是很好,后面学会了更好的解决方案再来处理.
- 本次是需要编写一些SConscript脚本文件(这里是参考rt-thread的官方源码进行的)
- 本次是使用的是stm32f407zgt6为硬件环境
- 本次使用的版本为rt-thread 5.0.2
使用RT-Studio创建一个项目文件
项目创建
处理报错问题
测试项目程序
Env的配置(参考官网,这里不做说明)
使用Env生成cmake构建工具
scons --target=cmake
使用Clion打开
修改py脚本文件
原始的脚本文件
这里我们直接去rt-thread源码找一个f4的参考源码文件进行修改
修改后的py脚本文件
import os
# toolchains options
ARCH = 'arm'
CPU = 'cortex-m4'
CROSS_TOOL = 'gcc'
# cross_tool provides the cross compiler
# EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR
PLATFORM = 'gcc'
EXEC_PATH = ''
if os.getenv('RTT_EXEC_PATH'):
EXEC_PATH = os.getenv('RTT_EXEC_PATH')
BUILD = 'debug'
PLATFORM = 'gcc'
if PLATFORM == 'gcc':
# toolchains
PREFIX = 'arm-none-eabi-'
CC = PREFIX + 'gcc'
AS = PREFIX + 'gcc'
AR = PREFIX + 'ar'
CXX = PREFIX + 'g++'
LINK = PREFIX + 'gcc'
TARGET_EXT = 'elf'
SIZE = PREFIX + 'size'
OBJDUMP = PREFIX + 'objdump'
OBJCPY = PREFIX + 'objcopy'
DEVICE = ' -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections'
CFLAGS = DEVICE + ' -Dgcc'
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -Wa,-mimplicit-it=thumb '
LFLAGS = DEVICE + ' -Wl,--gc-sections,-Map=rtthread.map,-cref,-u,Reset_Handler -T linkscripts/STM32F407ZG/link.lds'
CPATH = ''
LPATH = ''
if BUILD == 'debug':
CFLAGS += ' -O0 -gdwarf-2 -g'
AFLAGS += ' -gdwarf-2'
else:
CFLAGS += ' -O2'
CXXFLAGS = CFLAGS
POST_ACTION = OBJCPY + ' -O binary $TARGET rtthread.bin\n' + SIZE + ' $TARGET \n'
在Env控制台,再次执行生成命令
scons --target=cmake
构建项目
创建SConscript文件来管理HAL库相关文件
内容填写(这里我们依然可以参考rt-thread源码提供的)
完整配置内容
import rtconfig
from building import *
# get current directory
cwd = GetCurrentDir()
# The set of source files associated with this SConscript file.
src = Split('''
CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cec.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_crc.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cryp_ex.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c
STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
''')
if GetDepend(['RT_USING_SERIAL']) or GetDepend(['RT_USING_NANO', 'RT_USING_CONSOLE']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_usart.c']
if GetDepend(['RT_USING_I2C']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c']
if GetDepend(['RT_USING_SPI']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_qspi.c']
if GetDepend(['RT_USING_USB']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pccard.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd_ex.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_hcd.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c']
if GetDepend(['RT_USING_CAN']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c']
if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']) or GetDepend(['RT_USING_PULSE_ENCODER']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_lptim.c']
if GetDepend(['BSP_USING_ETH']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c']
if GetDepend(['RT_USING_ADC']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c']
if GetDepend(['RT_USING_DAC']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c']
if GetDepend(['RT_USING_RTC']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c']
if GetDepend(['RT_USING_WDT']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_wwdg.c']
if GetDepend(['RT_USING_SDIO']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c']
if GetDepend(['RT_USING_AUDIO']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2s_ex.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sai_ex.c']
if GetDepend(['RT_USING_MTD_NOR']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nor.c']
if GetDepend(['RT_USING_MTD_NAND']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_nand.c']
if GetDepend(['BSP_USING_FMC']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fmc.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_fsmc.c']
if GetDepend(['BSP_USING_SDRAM']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sdram.c']
if GetDepend(['BSP_USING_EXT_FMC_IO']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sram.c']
if GetDepend(['BSP_USING_ON_CHIP_FLASH']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c']
if GetDepend(['BSP_USING_LTDC']):
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc_ex.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma2d.c']
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dsi.c']
path = [cwd + '/STM32F4xx_HAL_Driver/Inc',
cwd + '/CMSIS/Device/ST/STM32F4xx/Include']
# 在原有的基础上增加CMSIS相关的头文件目录
path += [cwd + '/CMSIS/Include']
# 添加启动文件
src += ['CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/startup_stm32f407xx.S']
CPPDEFINES = ['USE_HAL_DRIVER']
group = DefineGroup('Libraries', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)
Return('group')
同理创建SConsript来管理rt-thread的drivers目录
内容编写参考rt-thread源码提供的
完整配置内容
Import('RTT_ROOT')
Import('rtconfig')
from building import *
import os
cwd = GetCurrentDir()
group = []
src = []
path = [cwd]
if GetDepend(['RT_USING_PIN']):
src += ['drv_gpio.c']
if GetDepend(['RT_USING_SERIAL']):
if GetDepend(['RT_USING_SERIAL_V2']):
src += ['drv_usart_v2.c']
else:
src += ['drv_usart.c']
if GetDepend(['BSP_USING_TIM']):
src += ['drv_tim.c']
if GetDepend(['BSP_USING_PWM']):
src += ['drv_pwm.c', 'drv_tim.c']
if GetDepend(['RT_USING_SPI']):
src += ['drv_spi.c']
if GetDepend(['RT_USING_QSPI']):
src += ['drv_qspi.c']
if GetDepend('RT_USING_SPI_BITOPS'):
src += ['drv_soft_spi.c']
if GetDepend(['RT_USING_I2C', 'RT_USING_I2C_BITOPS']):
if GetDepend('BSP_USING_I2C1') or GetDepend('BSP_USING_I2C2') or GetDepend('BSP_USING_I2C3') or GetDepend('BSP_USING_I2C4'):
src += ['drv_soft_i2c.c']
if GetDepend(['RT_USING_I2C']):
if GetDepend('BSP_USING_HARD_I2C1') or GetDepend('BSP_USING_HARD_I2C2') or GetDepend('BSP_USING_HARD_I2C3') or GetDepend('BSP_USING_HARD_I2C4'):
src += ['drv_hard_i2c.c']
if GetDepend(['BSP_USING_ETH', 'RT_USING_LWIP']):
src += ['drv_eth.c']
if GetDepend(['RT_USING_ADC']):
src += ['drv_adc.c']
if GetDepend(['RT_USING_DAC']):
src += ['drv_dac.c']
if GetDepend(['RT_USING_CAN']):
src += ['drv_can.c']
if GetDepend(['RT_USING_PM', 'SOC_SERIES_STM32L4']):
src += ['drv_pm.c']
src += ['drv_lptim.c']
if GetDepend('BSP_USING_SDRAM'):
src += ['drv_sdram.c']
if GetDepend(['BSP_USING_NAND1']):
src += ['drv_nand.c']
if GetDepend('BSP_USING_LCD'):
src += ['drv_lcd.c']
if GetDepend('BSP_USING_LCD_MIPI'):
src += ['drv_lcd_mipi.c']
if GetDepend('BSP_USING_ONCHIP_RTC'):
src += ['drv_rtc.c']
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32G0']):
src += [os.path.join('drv_flash', 'drv_flash_g0.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32F0']):
src += [os.path.join('drv_flash', 'drv_flash_f0.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32F1']):
src += [os.path.join('drv_flash', 'drv_flash_f1.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32F2']):
src += [os.path.join('drv_flash', 'drv_flash_f2.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32F4']):
src += [os.path.join('drv_flash', 'drv_flash_f4.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32F7']):
src += [os.path.join('drv_flash', 'drv_flash_f7.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32L4']):
src += [os.path.join('drv_flash', 'drv_flash_l4.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32H7']):
src += [os.path.join('drv_flash', 'drv_flash_h7.c')]
if GetDepend(['BSP_USING_ON_CHIP_FLASH', 'SOC_SERIES_STM32WB']):
src += [os.path.join('drv_flash', 'drv_flash_wb.c')]
if GetDepend('RT_USING_HWCRYPTO'):
src += ['drv_crypto.c']
if GetDepend(['BSP_USING_WDT']):
src += ['drv_wdt.c']
if GetDepend(['BSP_USING_SDIO']):
if GetDepend('SOC_SERIES_STM32H7') or GetDepend('SOC_SERIES_STM32F7') or GetDepend('SOC_SERIES_STM32L4') or GetDepend('SOC_SERIES_STM32L5'):
src += ['drv_sdmmc.c']
else:
src += ['drv_sdio.c']
if GetDepend(['BSP_USING_USBD']):
src += ['drv_usbd.c']
if GetDepend(['BSP_USING_PULSE_ENCODER']):
src += ['drv_pulse_encoder.c']
if GetDepend(['BSP_USING_USBH']):
src += ['drv_usbh.c']
path += [os.path.join(cwd, 'config')]
if GetDepend('BSP_USING_ON_CHIP_FLASH'):
path += [os.path.join(cwd, 'drv_flash')]
# 增加头文件路径
path += [os.path.join(cwd)]
path += [os.path.join(cwd,'include')]
# 增加公共部分源文件
src += ['board.c','drv_common.c','drv_clk.c']
# 增加相关宏定义
CPPDEFINES = ['STM32F407xx','SOC_SERIES_STM32F4']
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)
Return('group')
对于application目录的SConscript文件配置
rt-thread源码工程
自己创建的工程
在Env控制台,再次执行生成命令
scons --target=cmake
使用Clion进行构建
配置openocd进行程序下载测试
openocd 配置文件
配置烧录环境
烧录(ok)
额外补充
项目名称修改
- 生成的项目名称都为rtthread,这里需要修改cmake.py脚本文件
- 修改后
3. 在Env控制台中重新构建一次
scons --target=cmake
总结
- 如果在rt-studio中开启某个组件或软件包的时候,都需要在Env控制台中构建一下,用于动态更新CMakelists文件中的配置
- 个人的理解scons就是通过python脚本读取相应的配置文件,然后解析,安装指定的格式生成对应的构建配置文件