Skip to content

Commit

Permalink
Merge pull request #19540 from gschorcht/cpu/stm32/periph/sdmmc
Browse files Browse the repository at this point in the history
cpu/stm32/periph: add low-level SDMMC peripheral driver
  • Loading branch information
benpicco authored Dec 23, 2023
2 parents 5898c80 + b3f3e16 commit 7c2f091
Show file tree
Hide file tree
Showing 27 changed files with 1,264 additions and 38 deletions.
3 changes: 2 additions & 1 deletion boards/stm32f746g-disco/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ config BOARD_STM32F746G_DISCO
select HAS_PERIPH_RTT
select HAS_PERIPH_SPI
select HAS_PERIPH_TIMER
select HAS_PERIPH_SDMMC
select HAS_PERIPH_UART
select HAS_PERIPH_USBDEV
select HAS_PERIPH_USBDEV_HS
Expand All @@ -37,7 +38,7 @@ config BOARD_STM32F746G_DISCO
select HAVE_SAUL_GPIO
select HAVE_STM32_ETH
select HAVE_FT5X06

select HAVE_MTD_SDMMC_DEFAULT

config CLOCK_HSE
default 25000000
Expand Down
11 changes: 11 additions & 0 deletions boards/stm32f746g-disco/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ endif
ifneq (,$(filter periph_fmc,$(USEMODULE)))
FEATURES_REQUIRED += periph_fmc_16bit
endif

# default to using fatfs on SD card
ifneq (,$(filter vfs_default,$(USEMODULE)))
USEMODULE += fatfs_vfs
USEMODULE += mtd
endif

ifneq (,$(filter mtd,$(USEMODULE)))
USEMODULE += mtd_sdmmc_default
USEMODULE += periph_sdmmc
endif
1 change: 1 addition & 0 deletions boards/stm32f746g-disco/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Current hardware support:
| User microphones | - | |
| External Quad-SPI Flash | - | |
| External SDRAM | X | |
| SD Card Interface | x | SDMMC1 on PC8..PC13 and PD2 |

## Flashing the device

Expand Down
1 change: 1 addition & 0 deletions boards/stm32f746g-disco/features-shared.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ FEATURES_PROVIDED += periph_i2c
FEATURES_PROVIDED += periph_ltdc
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_sdmmc
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart
Expand Down
42 changes: 40 additions & 2 deletions boards/stm32f746g-disco/include/periph_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static const dma_conf_t dma_config[] = {
{ .stream = 6 }, /* DMA1 Stream 6 - USART2_TX */
{ .stream = 3 }, /* DMA1 Stream 3 - SPI2_RX */
{ .stream = 4 }, /* DMA1 Stream 4 - SPI2_TX */
{ .stream = 11 }, /* DMA2 Stream 3 - SPI4_RX */
{ .stream = 11 }, /* DMA2 Stream 3 - SPI4_RX Ch5 / SDMMC1 Ch 4 */
{ .stream = 12 }, /* DMA2 Stream 4 - SPI4_TX */
{ .stream = 8 }, /* DMA2 Stream 0 - ETH_TX */
};
Expand Down Expand Up @@ -260,7 +260,8 @@ static const ltdc_conf_t ltdc_config = {
/* values below come from STM32CubeF7 code and differ from the typical
* values mentioned in the RK043FN48H datasheet. Both sets of values work
* with the display.
* See the discussion in https://community.st.com/s/question/0D50X0000BOvdWP/how-to-set-displays-parameters-
* See the discussion in
* https://community.st.com/s/question/0D50X0000BOvdWP/how-to-set-displays-parameters-
*/
.hsync = 41,
.vsync = 10,
Expand Down Expand Up @@ -430,6 +431,43 @@ static const fmc_bank_conf_t fmc_bank_config[] = {
#define FMC_BANK_NUMOF ARRAY_SIZE(fmc_bank_config)
/** @} */

/**
* @name SDIO/SDMMC configuration
* @{
*/

/**
* @brief SDIO/SDMMC static configuration struct
*/
static const sdmmc_conf_t sdmmc_config[] = {
{
.dev = SDMMC1,
.bus = APB2,
.rcc_mask = RCC_APB2ENR_SDMMC1EN,
.cd = GPIO_PIN(PORT_C, 13),
.cd_active = 0, /* CD pin is LOW active */
.cd_mode = GPIO_IN_PU, /* Pull-up R12 not soldered by default */
.clk = { GPIO_PIN(PORT_C, 12), GPIO_AF12 },
.cmd = { GPIO_PIN(PORT_D, 2), GPIO_AF12 },
.dat0 = { GPIO_PIN(PORT_C, 8), GPIO_AF12 },
.dat1 = { GPIO_PIN(PORT_C, 9), GPIO_AF12 },
.dat2 = { GPIO_PIN(PORT_C, 10), GPIO_AF12 },
.dat3 = { GPIO_PIN(PORT_C, 11), GPIO_AF12 },
#ifdef MODULE_PERIPH_DMA
.dma = 5,
.dma_chan = 4,
#endif
.irqn = SDMMC1_IRQn
},
};

/**
* @brief Number of configured SDIO/SDMMC peripherals
*/
#define SDMMC_CONFIG_NUMOF 1

/** @} */

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion boards/stm32f7508-dk/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ config BOARD_STM32F7508_DK
select HAS_PERIPH_LTDC
select HAS_PERIPH_RTC
select HAS_PERIPH_RTT
select HAS_PERIPH_SDMMC
select HAS_PERIPH_SPI
select HAS_PERIPH_TIMER
select HAS_PERIPH_UART
Expand All @@ -34,9 +35,10 @@ config BOARD_STM32F7508_DK
select BOARD_HAS_HSE
select BOARD_HAS_LSE

select HAVE_FT5X06
select HAVE_MTD_SDMMC_DEFAULT
select HAVE_SAUL_GPIO
select HAVE_STM32_ETH
select HAVE_FT5X06

config CLOCK_HSE
default 25000000
Expand Down
2 changes: 2 additions & 0 deletions boards/stm32l496g-disco/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ config BOARD_STM32L496G_DISCO
select HAS_PERIPH_RTC
select HAS_PERIPH_RTT
select HAS_PERIPH_PWM
select HAS_PERIPH_SDMMC
select HAS_PERIPH_SPI
select HAS_PERIPH_SPI_STMOD
select HAS_PERIPH_TIMER
Expand All @@ -43,6 +44,7 @@ config BOARD_STM32L496G_DISCO

select HAVE_SAUL_GPIO
select HAVE_FT5X06
select HAVE_MTD_SDMMC_DEFAULT
select HAVE_ST7789
select HAVE_LCD_PARALLEL_16BIT if MODULE_ST7789
select HAVE_LCD_PARALLEL_LL_MCU if MODULE_ST7789
Expand Down
11 changes: 11 additions & 0 deletions boards/stm32l496g-disco/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ ifneq (,$(filter st7789,$(USEMODULE)))
USEMODULE += lcd_parallel_ll_mcu
FEATURES_REQUIRED += periph_fmc_nor_sram
endif

# default to using fatfs on SD card
ifneq (,$(filter vfs_default,$(USEMODULE)))
USEMODULE += fatfs_vfs
USEMODULE += mtd
endif

ifneq (,$(filter mtd,$(USEMODULE)))
USEMODULE += mtd_sdmmc_default
USEMODULE += periph_sdmmc
endif
1 change: 1 addition & 0 deletions boards/stm32l496g-disco/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ FEATURES_PROVIDED += periph_lpuart
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_rtt
FEATURES_PROVIDED += periph_pwm
FEATURES_PROVIDED += periph_sdmmc
FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_spi_stmod
FEATURES_PROVIDED += periph_timer
Expand Down
2 changes: 1 addition & 1 deletion boards/stm32l496g-disco/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The main features of this board are:
| SAI audio codec | - | |
| External PSRAM | x | Connected to FMC peripheral |
| External Quad-SPI Flash | - | QSPI peripheral is not yet supported |
| SD Card Interface | - | |
| SD Card Interface | - | SDMMC1 on PC8..PC13/PD2 |

## Board Configuration (sorted by peripheral):

Expand Down
61 changes: 48 additions & 13 deletions boards/stm32l496g-disco/include/periph_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static const adc_conf_t adc_config[] = {
{ .pin = GPIO_PIN(PORT_C, 0), .dev = 1, .chan = 13 }, /* A5, ADC12_IN13, SB28 closed */
{ .pin = GPIO_UNDEF, .dev = 0, .chan = 0 }, /* V_REFINT, ADC1_IN0 */
{ .pin = GPIO_UNDEF, .dev = 0, .chan = 18 }, /* V_BAT, ADC1_IN18 */
#ifndef MODULE_PERIPH_DAC
#if !MODULE_PERIPH_DAC
{ .pin = GPIO_PIN(PORT_A, 4), .dev = 0, .chan = 9 }, /* STMOD+_ADC, ADC12_IN9 */
#else
{ .pin = GPIO_UNDEF, .dev = 1, .chan = 17 }, /* DAC1, ADC2_IN17 */
Expand Down Expand Up @@ -153,7 +153,7 @@ static const adc_conf_t adc_config[] = {
*/
static const dac_conf_t dac_config[] = {
{ GPIO_PIN(PORT_A, 4), .chan = 0 }, /* STMod+_ADC pin */
#ifndef MODULE_PERIPH_SPI
#if !MODULE_PERIPH_SPI
{ GPIO_PIN(PORT_A, 5), .chan = 1 }, /* Arduino D13, conflicts with SPI_DEV(0) */
#endif
};
Expand Down Expand Up @@ -397,6 +397,41 @@ static const pwm_conf_t pwm_config[] = {
#define PWM_NUMOF ARRAY_SIZE(pwm_config)
/** @} */

/**
* @name SDIO/SDMMC configuration
* @{
*/

/**
* @brief SDIO/SDMMC static configuration struct
*/
static const sdmmc_conf_t sdmmc_config[] = {
{
.dev = SDMMC1,
.bus = APB2,
.rcc_mask = RCC_APB2ENR_SDMMC1EN,
.cd = GPIO_UNDEF, /* CD is connected to MFX GPIO8 */
.clk = { GPIO_PIN(PORT_C, 12), GPIO_AF12 },
.cmd = { GPIO_PIN(PORT_D, 2), GPIO_AF12 },
.dat0 = { GPIO_PIN(PORT_C, 8), GPIO_AF12 },
.dat1 = { GPIO_PIN(PORT_C, 9), GPIO_AF12 },
.dat2 = { GPIO_PIN(PORT_C, 10), GPIO_AF12 },
.dat3 = { GPIO_PIN(PORT_C, 11), GPIO_AF12 },
#if MODULE_PERIPH_DMA
.dma = 6,
.dma_chan = 7,
#endif
.irqn = SDMMC1_IRQn
},
};

/**
* @brief Number of configured SDIO/SDMMC peripherals
*/
#define SDMMC_CONFIG_NUMOF 1

/** @} */

/**
* @name SPI configuration
*
Expand All @@ -422,14 +457,14 @@ static const spi_conf_t spi_config[] = {
.cs_af = GPIO_AF5,
.rccmask = RCC_APB2ENR_SPI1EN,
.apbbus = APB2,
#if IS_USED(MODULE_PERIPH_DMA)
#if MODULE_PERIPH_DMA
.rx_dma = 0, /* DMA1 Channel 2 */
.rx_dma_chan = 1, /* CxS = 1 */
.tx_dma = 1, /* DMA1 Channel 3 */
.tx_dma_chan = 1, /* CxS = 1 */
#endif
},
#if IS_USED(MODULE_PERIPH_SPI_STMOD)
#if MODULE_PERIPH_SPI_STMOD
{ /* Pmod/STMod+ connector if solder bridges SB4, SB5, SB9 are closed */
.dev = SPI2,
.mosi_pin = GPIO_PIN(PORT_B, 15),
Expand All @@ -442,7 +477,7 @@ static const spi_conf_t spi_config[] = {
.cs_af = GPIO_AF5,
.rccmask = RCC_APB1ENR1_SPI2EN,
.apbbus = APB1,
#if IS_USED(MODULE_PERIPH_DMA)
#if MODULE_PERIPH_DMA
.rx_dma = 2, /* DMA1 Channel 4 */
.rx_dma_chan = 1, /* CxS = 1 */
.tx_dma = 3, /* DMA1 Channel 5 */
Expand Down Expand Up @@ -503,13 +538,13 @@ static const uart_conf_t uart_config[] = {
.tx_af = GPIO_AF7,
.bus = APB1,
.irqn = USART2_IRQn,
#if IS_USED(MODULE_PERIPH_UART_HW_FC)
#if MODULE_PERIPH_UART_HW_FC
.cts_pin = GPIO_UNDEF, /* CTS is not connected */
.rts_pin = GPIO_UNDEF, /* RTS is not connected */
#endif
.type = STM32_USART,
.clk_src = 0, /* Use APB clock */
#if IS_USED(MODULE_PERIPH_DMA)
#if MODULE_PERIPH_DMA
.dma = 4, /* DMA1 Channel 7 */
.dma_chan = 2, /* CxS = 2 */
#endif
Expand All @@ -523,19 +558,19 @@ static const uart_conf_t uart_config[] = {
.tx_af = GPIO_AF8,
.bus = APB12,
.irqn = LPUART1_IRQn,
#if IS_USED(MODULE_PERIPH_UART_HW_FC)
#if MODULE_PERIPH_UART_HW_FC
.cts_pin = GPIO_UNDEF, /* CTS is not connected */
.rts_pin = GPIO_UNDEF, /* RTS is not connected */
#endif
.type = STM32_LPUART,
.clk_src = 0, /* Use APB clock */
#if IS_USED(MODULE_PERIPH_DMA)
#if MODULE_PERIPH_DMA
.dma = 5, /* DMA2 Channel 6 */
.dma_chan = 4, /* CxS = 4 */
#endif
},

#if !IS_USED(MODULE_PERIPH_SPI_STMOD)
#if !MODULE_PERIPH_SPI_STMOD
{ /* Pmod/STMod+ connector if solder bridges SB6, SB7, SB8 are closed (default) */
.dev = USART1,
.rcc_mask = RCC_APB2ENR_USART1EN,
Expand All @@ -545,20 +580,20 @@ static const uart_conf_t uart_config[] = {
.tx_af = GPIO_AF7,
.bus = APB2,
.irqn = USART1_IRQn,
#if IS_USED(MODULE_PERIPH_UART_HW_FC)
#if MODULE_PERIPH_UART_HW_FC
.cts_pin = GPIO_PIN(PORT_G, 11),
.rts_pin = GPIO_PIN(PORT_G, 12),
.cts_af = GPIO_AF7,
.rts_af = GPIO_AF7,
#endif
.type = STM32_USART,
.clk_src = 0, /* Use APB clock */
#if IS_USED(MODULE_PERIPH_DMA)
#if MODULE_PERIPH_DMA
.dma = 2, /* DMA1 Channel 4 */
.dma_chan = 2, /* CxS = 2 */
#endif
},
#endif /* !IS_USED(MODULE_PERIPH_SPI_STMOD) */
#endif /* !MODULE_PERIPH_SPI_STMOD */
};

#define UART_0_ISR (isr_usart2)
Expand Down
6 changes: 6 additions & 0 deletions cpu/stm32/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,10 @@ ifneq (,$(filter periph_fmc_%,$(USEMODULE)))
FEATURES_REQUIRED += periph_fmc
endif

ifneq (,$(filter periph_sdmmc,$(FEATURES_USED)))
FEATURES_REQUIRED += periph_gpio_irq
FEATURES_REQUIRED += periph_sdmmc_clk
FEATURES_OPTIONAL += periph_dma
endif

include $(RIOTCPU)/cortexm_common/Makefile.dep
7 changes: 7 additions & 0 deletions cpu/stm32/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ ifneq (,$(filter $(CPU_FAM),f2 f4 f7 g4 l0 l4 l5 u5 wb))
endif
endif

ifneq (,$(filter $(CPU_FAM),f2 f4 f7 l4))
FEATURES_PROVIDED += periph_sdmmc_auto_clk
FEATURES_PROVIDED += periph_sdmmc_clk
FEATURES_PROVIDED += periph_sdmmc_hs
FEATURES_PROVIDED += periph_sdmmc_mmc
endif

ifneq (,$(filter $(CPU_FAM),f2 f4 f7 g4 l1 l4 mp1))
FEATURES_PROVIDED += cortexm_mpu
endif
Expand Down
5 changes: 3 additions & 2 deletions cpu/stm32/include/clk/f2f4f7/cfg_clock_default_100.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ extern "C" {
#endif
#endif
#ifndef CONFIG_CLOCK_PLL_N
#if IS_USED(MODULE_PERIPH_USBDEV_CLK) && defined(CPU_LINE_STM32F411xE)
#if (IS_USED(MODULE_PERIPH_USBDEV_CLK) || IS_USED(MODULE_PERIPH_SDMMC_CLK)) && \
defined(CPU_LINE_STM32F411xE)
#if IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && (CONFIG_CLOCK_HSE == MHZ(8))
#define CONFIG_CLOCK_PLL_N (96)
#elif IS_ACTIVE(CONFIG_BOARD_HAS_HSE) && (CONFIG_CLOCK_HSE == MHZ(25))
Expand All @@ -60,7 +61,7 @@ extern "C" {
#else
#define CONFIG_CLOCK_PLL_N (50)
#endif
#endif /* MODULE_PERIPH_USBDEV_CLK */
#endif /* MODULE_PERIPH_USBDEV_CLK || MODULE_PERIPH_SDMMC_CLK */
#endif
#ifndef CONFIG_CLOCK_PLL_P
#define CONFIG_CLOCK_PLL_P (2)
Expand Down
6 changes: 3 additions & 3 deletions cpu/stm32/include/clk/f2f4f7/cfg_clock_default_180.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern "C" {
#endif
#endif
#ifndef CONFIG_CLOCK_PLL_N
#if IS_USED(MODULE_PERIPH_USBDEV_CLK) && \
#if (IS_USED(MODULE_PERIPH_USBDEV_CLK) || IS_USED(MODULE_PERIPH_SDMMC_CLK)) && \
(defined(CPU_LINE_STM32F405xx) || defined(CPU_LINE_STM32F407xx) || \
defined(CPU_LINE_STM32F415xx) || defined(CPU_LINE_STM32F417xx) || \
defined(CPU_LINE_STM32F427xx) || defined(CPU_LINE_STM32F429xx) || \
Expand All @@ -68,13 +68,13 @@ extern "C" {
#else
#define CONFIG_CLOCK_PLL_N (90)
#endif
#endif /* MODULE_PERIPH_USBDEV_CLK */
#endif /* MODULE_PERIPH_USBDEV_CLK || MODULE_PERIPH_SDMMC_CLK */
#endif
#ifndef CONFIG_CLOCK_PLL_P
#define CONFIG_CLOCK_PLL_P (2)
#endif
#ifndef CONFIG_CLOCK_PLL_Q
#if IS_USED(MODULE_PERIPH_USBDEV_CLK) && \
#if (IS_USED(MODULE_PERIPH_USBDEV_CLK) || IS_USED(MODULE_PERIPH_SDMMC_CLK)) && \
(defined(CPU_LINE_STM32F405xx) || defined(CPU_LINE_STM32F407xx) || \
defined(CPU_LINE_STM32F415xx) || defined(CPU_LINE_STM32F417xx) || \
defined(CPU_LINE_STM32F427xx) || defined(CPU_LINE_STM32F429xx) || \
Expand Down
Loading

0 comments on commit 7c2f091

Please sign in to comment.