STM32之RT Thread

RTOS在嵌入式开发大行其道,RT-Thread有很丰富的外设驱动,遂折腾以下RT-Thread(本文的RT-Thread均指RT-Thread Nano)。

  • 环境: Win10x64, STM32CubeIDE v1.7.0
  • 开发板: DevEBox STM32F407VE

1、STM32F407VE开发板基本参数。

(1) Specifications

  • STM32F407VET6 ARM Cortex M4
  • 168MHz, 210 DMIPS / 1.25 DMIPS / MHz
  • 1.8V - 3.6V operating voltage
  • 8MHz system crystal
  • 32.768KHz RTC crystal
  • 2.54mm pitch pins
  • SWD header
  • 512 KByte Flash, 192 + 4 KByte SRAM
  • 3x SPI, 3x USART, 2x UART, 2x I2S, 3x I2C
  • 1x FSMC, 1x SDIO, 2x CAN
  • 1x USB 2.0 FS / HS controller (with dedicated DMA)
  • 1x USB HS ULPI (for external USB HS PHY)
  • Micro SD
  • Winbond W25Q16 16Mbit SPI Flash
  • 1x 10/100 Ethernet MAC
  • 1x 8 to 12-bit Parallel Camera interface
  • 3x ADC (12-bit / 16-channel)
  • 2x DAC (12-bit)
  • 12x general timers, 2x advanced timers
  • AMS1117-3.3V: 3.3V LDO voltage regulator, max current 800mA
  • Micro USB for power and comms
  • Power LED D1
  • User LED D2 (PA1) active low
  • Reset button, 1x user buttons K0
  • 2x22 side header
  • SPI TFT/OLED header (3V3, GND, MOSI, SCK, CS, MISO, RST, BL)
  • RTC battery header B1 beside SD card
  • M3 mounting holes
  • Dimensions: 40.89mm x 68.59mm

(2) Exposed Port Pins

  • PA0-PA15
  • PB0-PB3, PB5-PB15 (PB4 SPI1_MISO used exclusively with SPI Flash)
  • PC0-PC13 (PC14 OSC32_IN and PC15 OSC32_OUT not broken out)
  • PD0-PD15
  • PE0-PE15

(3) Peripherals

TFT/OLED (J4)

index Pin Usage index Pin Usage
1 - 3V3 5 PB12 CS
2 - GND 6 PB14 MISO
3 PB15 MOSI 7 PC5 RS
4 PB13 SCK 8 PB1 BLK

SPI Flash W25Q16 (U3)

index Pin Usage index Pin Usage
1 PA15 F_CS 5 PB5 SPI1_MOSI
2 PB4 SPI1_MISO 6 PB3 SPI1_SCK
3 WP 3V3 7 HOLD 3V3
4 - GND 8 VCC 3V3

SWD debug (J1)

index Pin Usage index Pin Usage
1 Boot0 4 PA13 SWDIO
2 - 3V3 5 PA14 SWCLK
3 - GND

USB (J5)

index Pin Usage index Pin Usage
1 VCC 5V 4 NC ID
2 PA11 USB_DM 5 - GND
3 PA12 USB_DP

Micro SD (U5)

index Pin Usage index Pin Usage
1 PC10 SDIO_D2 6 - GND
2 PC11 SDIO_D3 7 PC8 SDIO_D0
3 PD2 SDIO_CMD 8 PC9 SDIO_D1
4 - 3V3 9 NC SD_NC
5 PC12 SDIO_SCK

User Button (K1)

index Pin Usage index Pin Usage
1 PA0 WK_UP

User LED (D2)

index Pin Usage index Pin Usage
1 PA1 User LED

Battery (B1)

index Pin Usage index Pin Usage
1 - BAT54C 2 - GND

2、工程创建与修改

使用STM32CubeIDE(以下简称CubeIde)创建工程的过程非常程序化,选芯片->配置外设->配置时钟->配置所用的库,具体过程网上很多,此处略过。以下核心记录RT-Thread的使用方法。

(1) 安装并在工程中导入RT-Thread Nano包,

基本过程请参考RT-Thread 文档。重点是在CubeIde中通过连接https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc安装,并在工程中配置。

(2) CubeIde中配置RT-Thread Nano需要注意的几个问题

  1. 如文档所述, RT-Thread重定义HardFault_HandlerPendSV_HandlerSysTick_Handler 中断函数,为了避免重复定义的问题,需要在NVIC->Code generate中禁止生成Hard fault interrupt, Pendable request, Time base :System tick timer三个函数。实际操作中,如果选择了定时器作为时基,那么只需要禁止生成Hard fault interrupt即可。
  2. 启用RT_USING_HEAPRT_USING_DEVICEfinsh。前两个直接在CubeIde点选即可,最后一个需要打开rtconfig.h编辑。
1
2
3
4
5
6
// <h>Enable FinSH Configuration
// <c1>include shell config
// <i> Select this choice if you using FinSH
#include "finsh_config.h"
// </c>
// </h>
  1. 编译器参数修改,Project->properties->C/C++ Build->Settings->Tool Settings,在MCU GCC Assembler->Miscellaneous添加-Wa,-mimplicit-it=thumbMCU GCC Compiler->Include paths添加相应的头文件(如果没有,貌似nano 3.1.5已经自动添加了5个相关头文件路径)
  2. 修改启动文件Core/Startup/startup_stm32f407vetx.s,第100bl main => bl entry。(注第98bl __libc_init_arrayRT-Thread Studio自带的代码中,是注释的,不过此处保留后,尚未遇到问题)
  3. 修改STM32F407VETX_FLASH.ld文件,开启自动初始化,并使finsh正常工作。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

    . = ALIGN(4);
    _etext = .; /* define a global symbols at end of code */
    /**************添加以下内容*************/
    /* section information for finsh shell */
    . = ALIGN(4);
    __fsymtab_start = .;
    KEEP(*(FSymTab))
    __fsymtab_end = .;
    . = ALIGN(4);
    __vsymtab_start = .;
    KEEP(*(VSymTab))
    __vsymtab_end = .;
    . = ALIGN(4);

    /* section information for initial. */
    . = ALIGN(4);
    __rt_init_start = .;
    KEEP(*(SORT(.rti_fn*)))
    __rt_init_end = .;
    . = ALIGN(4);
    /**************添加内容结束*************/
    } >FLASH

参考

  1. RT-Thread Nano 文档
  2. STM32CubeIDE 使用RTThread-Nano无法自动初始化