目录
参考链接
前言
一、外设驱动
1、片上外设
2、板载外设
二、外设驱动添加
1、配置CubeMX工程
2、修改Kconfig
3、添加ports文件,修改SConscript
三、外设配置总结
STM32 系列外设驱动添加指南
对一个 BSP 而言,有如下三类驱动:
对于要添加的板卡,有如下外设:
序号 |
驱动 |
简介 |
1 |
SDIO |
通过 SDIO 读写TF卡 |
2 |
ETH PHY |
以太网 |
3 |
USB PHY |
USB |
4 |
LCD |
显示屏 |
添加步骤:
打开 \board\CubeMX_Config 目录下的CubeMX工程,按照原理图配置对应外设的引脚,完成外设时钟使能和管脚配置的工作并生成工程。外设初始化,中断配置,DMA配置等等则由 RT-Thread 提供的驱动文件来完成。也就是说,虽然 STM32CubeMX 生成了多个文件用来初始化外设,但 RT-Thread 只使用了 STM32CubeMX 生成的 stm32fxx_hal_msp.c 文件(存放了在 CubeMX 工具中开启的外设驱动的配置代码)和 stm32fxx_hal_conf.h文件(提供的宏开关会决定 HAL 库将哪些外设驱动添加到工程中)。最后配置完成的外设有:
修改\board\Kconfig文件,这样就可以通过menuconfig来选择需要使用的外设了
menu "Hardware Drivers Config"
config SOC_STM32F429IG
bool
select SOC_SERIES_STM32F4
select RT_USING_COMPONENTS_INIT
select RT_USING_USER_MAIN
default y
menu "Onboard Peripheral Drivers"
config BSP_USING_COM1
bool "Enable USART1"
select BSP_USING_UART
select BSP_USING_UART1
default n
config BSP_USING_COM6
bool "Enable USART6"
select BSP_USING_UART
select BSP_USING_UART6
default n
config BSP_USING_COM7
bool "Enable USART7"
select BSP_USING_UART
select BSP_USING_UART7
default y
config BSP_USING_SDRAM
bool "Enable SDRAM"
select BSP_USING_FMC
default n
config BSP_USING_SPI_FLASH
bool "Enable SOFT-SPI FLASH (W25Q64)"
select BSP_USING_SOFT_SPI
select RT_USING_SFUD
select RT_SFUD_USING_SFDP
default n
config PHY_USING_LAN8720A
bool
config BSP_USING_ETH
bool "Enable Ethernet (ETH_RST is PH3)"
select RT_USING_LWIP
select PHY_USING_LAN8720A
default n
config BSP_USING_SDCARD
bool "Enable SDCARD (sdio)"
select BSP_USING_SDIO
select RT_USING_DFS
select RT_USING_DFS_ELMFAT
default n
config BSP_USING_LCD
bool "Enable RGB-LCD"
select BSP_USING_LTDC
select BSP_USING_SDRAM
default n
config BSP_USING_TOUCH
bool "Enable TOUCH Driver(TSC2046)"
default n
endmenu
menu "On-chip Peripheral Drivers"
config BSP_USING_GPIO
bool "Enable GPIO"
select RT_USING_PIN
default y
menuconfig BSP_USING_UART
bool "Enable UART"
default y
select RT_USING_SERIAL
if BSP_USING_UART
config BSP_USING_UART1
bool "Enable UART1"
default n
config BSP_UART1_RX_USING_DMA
bool "Enable UART1 RX DMA"
depends on BSP_USING_UART1 && RT_SERIAL_USING_DMA
default n
config BSP_USING_UART6
bool "Enable UART6"
default n
config BSP_UART6_RX_USING_DMA
bool "Enable UART6 RX DMA"
depends on BSP_USING_UART6 && RT_SERIAL_USING_DMA
default n
config BSP_USING_UART7
bool "Enable UART7"
default y
config BSP_UART7_RX_USING_DMA
bool "Enable UART7 RX DMA"
depends on BSP_USING_UART7 && RT_SERIAL_USING_DMA
default n
endif
config BSP_USING_ON_CHIP_FLASH
bool "Enable on-chip FLASH"
default n
menuconfig BSP_USING_SOFT_SPI
bool "Enable SOFT SPI BUS"
default n
select RT_USING_SOFT_SPI
if BSP_USING_SOFT_SPI
config BSP_USING_SOFT_SPI1
bool "Enable SOFT SPI1:CS(PG10) SCK(PG12) MISO(PF8) MOSI(PF9)"
default n
endif
menuconfig BSP_USING_SPI
bool "Enable SPI BUS"
default n
select RT_USING_SPI
if BSP_USING_SPI
config BSP_USING_SPI1
bool "Enable SPI1 BUS(extended use!!!)"
default n
config BSP_SPI1_TX_USING_DMA
bool "Enable SPI1 TX DMA"
depends on BSP_USING_SPI1
default n
config BSP_SPI1_RX_USING_DMA
bool "Enable SPI1 RX DMA"
depends on BSP_USING_SPI1
select BSP_SPI1_TX_USING_DMA
default n
config BSP_USING_SPI2
bool "Enable SPI2 BUS(use to extend uart!!!)"
default n
config BSP_SPI2_TX_USING_DMA
bool "Enable SPI2 TX DMA"
depends on BSP_USING_SPI2
default n
config BSP_SPI2_RX_USING_DMA
bool "Enable SPI2 RX DMA"
depends on BSP_USING_SPI2
select BSP_SPI2_TX_USING_DMA
default n
config BSP_USING_SPI4
bool "Enable SPI4 BUS(extended use!!!)"
default n
config BSP_SPI4_TX_USING_DMA
bool "Enable SPI4 TX DMA"
depends on BSP_USING_SPI4
default n
config BSP_SPI4_RX_USING_DMA
bool "Enable SPI4 RX DMA"
depends on BSP_USING_SPI4
select BSP_SPI4_TX_USING_DMA
default n
endif
menuconfig BSP_USING_I2C1
bool "Enable I2C1 BUS (software simulation,extended use!!!)"
default n
select RT_USING_I2C
select RT_USING_I2C_BITOPS
select RT_USING_PIN
if BSP_USING_I2C1
comment "Notice: PB8(SCL) --> 167; PB9(SDA) --> 168"
config BSP_I2C1_SCL_PIN
int "I2C1 scl pin number"
range 1 176
default 167
config BSP_I2C1_SDA_PIN
int "I2C1 sda pin number"
range 1 176
default 168
endif
menuconfig BSP_USING_I2C2
bool "Enable I2C2 BUS (software simulation,use to RTC(DS1340))"
default n
select RT_USING_I2C
select RT_USING_I2C_BITOPS
select RT_USING_PIN
if BSP_USING_I2C2
comment "Notice: PH4(SCL) --> 45; PH5(SDA) --> 46"
config BSP_I2C2_SCL_PIN
int "I2C2 scl pin number"
range 1 176
default 45
config BSP_I2C2_SDA_PIN
int "I2C2 sda pin number"
range 1 176
default 46
endif
menuconfig BSP_USING_I2C3
bool "Enable I2C3 BUS (software simulation,extended use!!!)"
default n
select RT_USING_I2C
select RT_USING_I2C_BITOPS
select RT_USING_PIN
if BSP_USING_I2C3
comment "Notice: PA8(SCL) --> 119; PH8(SDA)--> 85"
config BSP_I2C3_SCL_PIN
int "I2C3 scl pin number"
range 1 176
default 119
config BSP_I2C3_SDA_PIN
int "I2C3 sda pin number"
range 1 176
default 85
endif
menuconfig BSP_USING_TIM
bool "Enable timer"
default n
select RT_USING_HWTIMER
if BSP_USING_TIM
config BSP_USING_TIM11
bool "Enable TIM11"
default n
config BSP_USING_TIM13
bool "Enable TIM13"
default n
config BSP_USING_TIM14
bool "Enable TIM14"
default n
endif
menuconfig BSP_USING_ADC
bool "Enable ADC"
default n
select RT_USING_ADC
if BSP_USING_ADC
config BSP_USING_ADC1
bool "Enable ADC1"
default n
endif
menuconfig BSP_USING_ONCHIP_RTC
bool "Enable RTC"
select RT_USING_RTC
select RT_USING_LIBC
default n
if BSP_USING_ONCHIP_RTC
choice
prompt "Select clock source"
default BSP_RTC_USING_LSE
config BSP_RTC_USING_LSE
bool "RTC USING LSE"
config BSP_RTC_USING_LSI
bool "RTC USING LSI"
endchoice
endif
config BSP_USING_WDT
bool "Enable Watchdog Timer"
select RT_USING_WDT
default n
menuconfig BSP_USING_USBH
bool "Enable USB Host"
select RT_USING_USB_HOST
default n
if BSP_USING_USBH
menuconfig RT_USBH_MSTORAGE
bool "Enable Udisk Drivers"
default n
if RT_USBH_MSTORAGE
config UDISK_MOUNTPOINT
string "Udisk mount dir"
default "/"
endif
endif
config BSP_USING_SDIO
bool "Enable SDIO"
select RT_USING_SDIO
select RT_USING_DFS
default n
config BSP_USING_FMC
bool
default n
config BSP_USING_LTDC
bool
default n
source "./libraries/HAL_Drivers/Kconfig"
endmenu
menu "Board extended module Drivers"
menuconfig RT_STEMWIN
bool "Enable STemWin"
default n
if RT_STEMWIN
config RT_STEMWIN_DEMO
bool "Enable STemWin Demo"
default n
endif
menuconfig RT_TOUCHGFX
bool "Enable TouchGFX"
default n
endmenu
endmenu
使用CubeMX配置完工程后,只是完成了外设时钟使能和管脚配置。外设初始化,中断配置,DMA配置等等则由 RT-Thread 提供的驱动文件(\libraries\HAL_Drivers目录下)来完成的,这些驱动配置有的需要根据使用情况修改配置参数,这些配置参数文件就存放在\board\ports文件夹下。
添加完后,还需要根据menuconfig开启的外设来选择需要包含的配置文件,也就是还需要修改\board\SConscript 构建脚本,这样在编译的时候才会将相应的文件加入工程进行编译
import os
import rtconfig
from building import *
Import('SDK_LIB')
cwd = GetCurrentDir()
# add general drivers
src = Split('''
board.c
CubeMX_Config/Src/stm32f4xx_hal_msp.c
''')
if GetDepend(['BSP_USING_ETH']):
src += Glob('ports/phy_reset.c')
if GetDepend(['BSP_USING_SPI_FLASH']):
src += Glob('ports/spi_flash_init.c')
if GetDepend(['BSP_USING_SDCARD']):
src += Glob('ports/sdcard_port.c')
if GetDepend(['BSP_USING_AUDIO']):
src += Glob('ports/audio/drv_wm8978.c')
src += Glob('ports/audio/drv_sound.c')
if GetDepend(['BSP_USING_AUDIO_RECORD']):
src += Glob('ports/audio/drv_mic.c')
path = [cwd]
path += [cwd + '/CubeMX_Config/Inc']
path += [cwd + '/ports']
if GetDepend(['BSP_USING_AUDIO']):
path += [cwd + '/ports/audio']
startup_path_prefix = SDK_LIB
if rtconfig.CROSS_TOOL == 'gcc':
src += [startup_path_prefix + '/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/startup_stm32f429xx.s']
elif rtconfig.CROSS_TOOL == 'keil':
src += [startup_path_prefix + '/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Source/Templates/arm/startup_stm32f429xx.s']
elif rtconfig.CROSS_TOOL == 'iar':
src += [startup_path_prefix + '/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Source/Templates/iar/startup_stm32f429xx.s']
CPPDEFINES = ['STM32F429xx']
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)
Return('group')
想要在 BSP 中添加更多驱动时,需要使用 CubeMX 工具来配置这些外设。对于绝大多数驱动的配置,只需要在工具中使能相应的外设即可。但是对于一些复杂的外设,则需要更多的配置内容。下表展示了不同驱动在 CubeMX 工具配置步骤的总结:
序号 |
驱动 |
CubeMx 工程中的配置情况(加粗部分为必做步骤) |
1 |
GPIO |
无需任何操作 |
2 |
UART |
开启该外设 ,然后配置所需要的引脚(或者使用默认引脚) |
3 |
SPI |
开启该外设 ,然后配置所需要的引脚(或者使用默认引脚) |
4 |
I2C |
依赖于PIN 驱动,无需任何操作 |
5 |
TIMER |
使能 internal Clock 时钟 ,详细内容可参考5.3章节 |
7 |
PWM |
首先使能 internal Clock 时钟,然后为 channelx 选项选择PWM Generation CHx, 最后配置所需要的引脚(或者使用默认引脚) ,详细内容可参考5.3章节 |
8 |
ADC |
开启该外设,然后选择使用的通道 ,详细内容可参考5.3章节 |
9 |
RTC |
开启该外设,然后在时钟树状图里将 RTC 选择为 LSE 时钟 |
10 |
Watchdog |
开启该外设 |
11 |
EMAC |
配置 ETH 外设的工作模式(一般为 RMII 模式) |
12 |
SDRAM |
需要根据板载的 SDRAM 型号配置片选脚,地址线,数据线等 |
13 |
SDIO |
开启该外设,配置引脚(或者使用默认引脚),SDIO会改变时钟结构,故需重新配置时钟并修改board.c |