This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 40e9c83d50212f273a22e44d8dedcf53cca81a92 Author: raiden00pl <[email protected]> AuthorDate: Wed Nov 18 11:39:01 2026 +0100 boards/stm32n6570-dk: Add the microSD slot and the sdcard configuration. The microSD slot is on SDMMC2 with card detect on PN12 and card power on PQ7. The sdcard configuration adds it to nsh with FAT. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: raiden00pl <[email protected]> --- .../arm/stm32n6/boards/stm32n6570-dk/index.rst | 22 ++++ .../stm32n6/stm32n6570-dk/configs/sdcard/defconfig | 76 ++++++++++++ boards/arm/stm32n6/stm32n6570-dk/include/board.h | 44 ++++++- .../arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt | 4 + boards/arm/stm32n6/stm32n6570-dk/src/Make.defs | 4 + .../arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c | 10 ++ boards/arm/stm32n6/stm32n6570-dk/src/stm32_sdmmc.c | 129 +++++++++++++++++++++ .../arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h | 24 ++++ 8 files changed, 312 insertions(+), 1 deletion(-) diff --git a/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst b/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst index 9d67906bf76..bb878f9b739 100644 --- a/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst +++ b/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst @@ -191,6 +191,22 @@ IMX335 held in reset (PC8) and registers the sensor as the V4L2 depth camera ``/dev/video1`` (Z16, 8x8 or 4x4). INT uses EXTI0, so the TAMP button has no interrupt when the sensor is enabled. +microSD +======= + +The microSD slot is on SDMMC2 (4-bit, IDMA): PC2 CK, PC3 CMD, PC4-PC5 D0-D1, +PC0 D2, PE4 D3 (AF11), card detect PN12 (low with a card) and card power +enable PQ7 (high). The kernel clock is IC4 = 200 MHz, giving 400 kHz for +identification and 25 MHz for transfers. The card is ``/dev/mmcsd0``; +insertion and removal are reported through the card detect interrupt. +With a FAT formatted card:: + + nsh> mount -t vfat /dev/mmcsd0 /mnt + nsh> echo hello > /mnt/test.txt + nsh> cat /mnt/test.txt + nsh> dd if=/dev/zero of=/mnt/big.bin bs=4096 count=256 + nsh> umount /mnt + Clocking ======== @@ -233,3 +249,9 @@ lvgl ``fb`` plus the touch controller, LVGL and its widgets demo. Start it with ``lvgldemo &``. The image is close to the 511 KiB limit of the boot ROM, so LVGL logging and extra fonts are left out. + +sdcard +------ + +NuttShell with the microSD slot on SDMMC2 and the FAT file system, see +`microSD`_. diff --git a/boards/arm/stm32n6/stm32n6570-dk/configs/sdcard/defconfig b/boards/arm/stm32n6/stm32n6570-dk/configs/sdcard/defconfig new file mode 100644 index 00000000000..af85524ba55 --- /dev/null +++ b/boards/arm/stm32n6/stm32n6570-dk/configs/sdcard/defconfig @@ -0,0 +1,76 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_ARCH_LEDS is not set +# CONFIG_MMCSD_HAVE_WRITEPROTECT is not set +# CONFIG_MMCSD_MMCSUPPORT is not set +# CONFIG_SPI_CALLBACK is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="stm32n6570-dk" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_STM32N6570_DK=y +CONFIG_ARCH_BUTTONS=y +CONFIG_ARCH_CHIP="stm32n6" +CONFIG_ARCH_CHIP_STM32=y +CONFIG_ARCH_CHIP_STM32N657X0=y +CONFIG_ARCH_CHIP_STM32N6=y +CONFIG_ARCH_INTERRUPTSTACK=4096 +CONFIG_ARCH_IRQBUTTONS=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARD_LOOPSPERMSEC=84000 +CONFIG_BUILTIN=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_EXAMPLES_ALARM=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_I2C=y +CONFIG_IDLETHREAD_STACKSIZE=4096 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INPUT=y +CONFIG_INPUT_BUTTONS=y +CONFIG_INPUT_BUTTONS_LOWER=y +CONFIG_MMCSD=y +CONFIG_MMCSD_SDIO=y +CONFIG_MMCSD_SDIOWAIT_WRCOMPLETE=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_READLINE=y +CONFIG_RAM_SIZE=2620416 +CONFIG_RAM_START=0x34180400 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_RTC_ALARM=y +CONFIG_RTC_DATETIME=y +CONFIG_RTC_DRIVER=y +CONFIG_RTC_NALARMS=2 +CONFIG_RTC_PERIODIC=y +CONFIG_SCHED_HPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SDMMC2_SDIO_PULLUP=y +CONFIG_SPITOOL_MAXBUS=5 +CONFIG_STM32N6_DEBUG=y +CONFIG_STM32_DMA1=y +CONFIG_STM32_I2C1=y +CONFIG_STM32_I2C2=y +CONFIG_STM32_I2C4=y +CONFIG_STM32_RNG=y +CONFIG_STM32_RTC=y +CONFIG_STM32_SDMMC2=y +CONFIG_STM32_SPI5=y +CONFIG_STM32_USART1=y +CONFIG_STM32_USART2=y +CONFIG_SYSTEM_I2CTOOL=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_SPITOOL=y +CONFIG_USART1_SERIAL_CONSOLE=y +CONFIG_USERLED=y +CONFIG_USERLED_LOWER=y diff --git a/boards/arm/stm32n6/stm32n6570-dk/include/board.h b/boards/arm/stm32n6/stm32n6570-dk/include/board.h index caac04b8873..ebc7ad78839 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/include/board.h +++ b/boards/arm/stm32n6/stm32n6570-dk/include/board.h @@ -57,8 +57,9 @@ * IC2 /6 = 400 MHz \ * IC6 /8 = 300 MHz > SYSCLK components (SYSSW IC2_IC6_IC11) * IC11 /6 = 400 MHz / + * IC4 /12 = 200 MHz -> SDMMC2 kernel clock * IC16 /96 = 25 MHz -> LTDC pixel clock - * PLL2 = HSI / 2 * 48 = 1536 MHz (audio and SDMMC kernel clocks) + * PLL2 = HSI / 2 * 48 = 1536 MHz (audio kernel clocks) * PLL3 = HSE / 3 * 125 = 2000 MHz (LTDC) * PLL4 = HSI / 4 * 75 = 1200 MHz (I2C, LTDC) * HPRE /2 = 200 MHz -> HCLK @@ -110,6 +111,11 @@ #define STM32_IC16_SRC RCC_ICCFGR_SEL_PLL1 #define STM32_IC16_DIV 96 +/* IC4 = PLL1 / 12 = 200 MHz -> SDMMC kernel clock */ + +#define STM32_IC4_SRC RCC_ICCFGR_SEL_PLL1 +#define STM32_IC4_DIV 12 + #define STM32_CPUCLK_FREQUENCY (STM32_PLL1_FREQUENCY / STM32_IC1_DIV) #define STM32_SYSCLK_FREQUENCY (STM32_PLL1_FREQUENCY / STM32_IC2_DIV) #define STM32_RCC_CFGR2_HPRE RCC_CFGR2_HPRE_SYSCLKd2 @@ -125,6 +131,7 @@ * IC2 /8 = 100 MHz \ * IC6 /12 = 66.7 MHz > SYSCLK components (SYSSW IC2_IC6_IC11) * IC11 /8 = 100 MHz / + * IC4 /4 = 200 MHz -> SDMMC2 kernel clock * HPRE /2 = 50 MHz -> HCLK * PPREx /1 = 50 MHz -> PCLK1/2/4/5 * TIMPRE /1 = 100 MHz -> timer kernel clock @@ -151,6 +158,11 @@ #define STM32_IC16_SRC RCC_ICCFGR_SEL_PLL1 #define STM32_IC16_DIV 32 +/* IC4 = PLL1 / 4 = 200 MHz -> SDMMC kernel clock */ + +#define STM32_IC4_SRC RCC_ICCFGR_SEL_PLL1 +#define STM32_IC4_DIV 4 + #define STM32_CPUCLK_FREQUENCY (STM32_PLL1_FREQUENCY / STM32_IC1_DIV) #define STM32_SYSCLK_FREQUENCY (STM32_PLL1_FREQUENCY / STM32_IC2_DIV) #define STM32_RCC_CFGR2_HPRE RCC_CFGR2_HPRE_SYSCLKd2 @@ -212,6 +224,10 @@ #define BOARD_PWR_VDDIO (PWR_SVMCR3_VDDIO2SV | PWR_SVMCR3_VDDIO3SV | \ PWR_SVMCR3_VDDIO2VRSEL | PWR_SVMCR3_VDDIO3VRSEL) +/* VDDIO5 supplies the microSD pads (SDMMC2) in its 3.3 V range */ + +#define BOARD_PWR_VDDIO5 PWR_SVMCR2_VDDIO5SV + /* LED definitions **********************************************************/ /* LD1 (green, PO1) is active high; LD2 (red, PG10) is active low. */ @@ -388,6 +404,32 @@ #define BOARD_LTDC_GCR_VSPOL 0 /* VSYNC active low */ #define BOARD_LTDC_GCR_HSPOL 0 /* HSYNC active low */ +/* SDMMC2 GPIOs *************************************************************/ + +/* microSD slot: PC2 CK, PC3 CMD, PC4 D0, PC5 D1, PC0 D2, PE4 D3 (AF11). + * The kernel clock is IC4 (200 MHz): + * + * SDMMC_CK = 200 MHz / (2 * CLKDIV) + * init CLKDIV 250 -> 400 kHz + * xfer CLKDIV 4 -> 25 MHz (default speed) + * HS CLKDIV 2 -> 50 MHz (CONFIG_SDMMC2_SD_HS_MODE) + */ + +#define GPIO_SDMMC2_CK (GPIO_SDMMC2_CK_1 | GPIO_SPEED_100MHZ) +#define GPIO_SDMMC2_CMD (GPIO_SDMMC2_CMD_2 | GPIO_SPEED_100MHZ) +#define GPIO_SDMMC2_D0 (GPIO_SDMMC2_D0_2 | GPIO_SPEED_100MHZ) +#define GPIO_SDMMC2_D1 (GPIO_SDMMC2_D1_1 | GPIO_SPEED_100MHZ) +#define GPIO_SDMMC2_D2 (GPIO_SDMMC2_D2_2 | GPIO_SPEED_100MHZ) +#define GPIO_SDMMC2_D3 (GPIO_SDMMC2_D3_2 | GPIO_SPEED_100MHZ) + +#define STM32_RCC_CCIPR8_SDMMC2SEL RCC_CCIPR8_SDMMC2SEL_IC4 +#define STM32_SDMMC_CLK (STM32_PLL1_FREQUENCY / STM32_IC4_DIV) + +#define STM32_SDMMC_INIT_CLKDIV (250 << STM32_SDMMC_CLKCR_CLKDIV_SHIFT) +#define STM32_SDMMC_MMCXFR_CLKDIV (4 << STM32_SDMMC_CLKCR_CLKDIV_SHIFT) +#define STM32_SDMMC_SDXFR_CLKDIV (4 << STM32_SDMMC_CLKCR_CLKDIV_SHIFT) +#define STM32_SDMMC_SDXFR_HS_CLKDIV (2 << STM32_SDMMC_CLKCR_CLKDIV_SHIFT) + /* USART1 GPIOs *************************************************************/ /* USART1 (ST-LINK Virtual COM Port): PE5=TX (AF7), PE6=RX (AF7) diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt b/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt index 57d7d13e2b1..85afaf50fbe 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt +++ b/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt @@ -44,6 +44,10 @@ if(CONFIG_VIDEO_VL53L5CX) list(APPEND SRCS stm32_camera.c stm32_tof.c) endif() +if(CONFIG_STM32_SDMMC2) + list(APPEND SRCS stm32_sdmmc.c) +endif() + target_sources(board PRIVATE ${SRCS}) set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/flash.ld") diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs b/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs index 0a450b2c9db..a4f35c5929a 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs +++ b/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs @@ -47,6 +47,10 @@ ifeq ($(CONFIG_VIDEO_VL53L5CX),y) CSRCS += stm32_camera.c stm32_tof.c endif +ifeq ($(CONFIG_STM32_SDMMC2),y) +CSRCS += stm32_sdmmc.c +endif + DEPPATH += --dep-path board VPATH += :board CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c index 5ddfd8ebfc0..b796fdd50ff 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c +++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c @@ -120,6 +120,16 @@ int stm32_bringup(void) } #endif +#if defined(CONFIG_STM32_SDMMC2) && defined(CONFIG_MMCSD_SDIO) + /* Initialize the microSD slot */ + + ret = stm32_sdio_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: stm32_sdio_initialize() failed: %d\n", ret); + } +#endif + #ifdef CONFIG_STM32_SPI /* Initialize the SPI buses and their chip selects */ diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32_sdmmc.c b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_sdmmc.c new file mode 100644 index 00000000000..a80862dcf35 --- /dev/null +++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_sdmmc.c @@ -0,0 +1,129 @@ +/**************************************************************************** + * boards/arm/stm32n6/stm32n6570-dk/src/stm32_sdmmc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdbool.h> +#include <debug.h> +#include <errno.h> + +#include <nuttx/arch.h> +#include <nuttx/sdio.h> +#include <nuttx/mmcsd.h> + +#include "stm32_gpio.h" +#include "stm32_exti.h" +#include "stm32_sdmmc.h" +#include "stm32n6570-dk.h" + +#ifdef CONFIG_MMCSD_SDIO + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct sdio_dev_s *g_sdio_dev; +static bool g_sd_inserted; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_ncd_interrupt + * + * Description: + * Card detect interrupt handler. + * + ****************************************************************************/ + +static int stm32_ncd_interrupt(int irq, void *context, void *arg) +{ + bool present = !stm32_gpioread(GPIO_SDMMC2_NCD); + + if (present != g_sd_inserted) + { + g_sd_inserted = present; + sdio_mediachange(g_sdio_dev, present); + } + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_sdio_initialize + * + * Description: + * Power the microSD slot, bind SDMMC2 to the MMC/SD driver as + * /dev/mmcsd0 and track the card detect switch. + * + ****************************************************************************/ + +int stm32_sdio_initialize(void) +{ + int ret; + + /* Power the card and give the supply time to settle */ + + stm32_configgpio(GPIO_SDMMC2_PWR_EN); + stm32_gpiowrite(GPIO_SDMMC2_PWR_EN, true); + up_mdelay(10); + + g_sdio_dev = sdio_initialize(SDIO_SLOTNO); + if (g_sdio_dev == NULL) + { + ferr("ERROR: Failed to initialize SDIO slot %d\n", SDIO_SLOTNO); + return -ENODEV; + } + + ret = mmcsd_slotinitialize(SDIO_MINOR, g_sdio_dev); + if (ret < 0) + { + ferr("ERROR: Failed to bind SDIO to the MMC/SD driver: %d\n", ret); + return ret; + } + + /* Report the current card state and follow the card detect switch */ + + stm32_configgpio(GPIO_SDMMC2_NCD); + g_sd_inserted = !stm32_gpioread(GPIO_SDMMC2_NCD); + sdio_mediachange(g_sdio_dev, g_sd_inserted); + + ret = stm32_gpiosetevent(GPIO_SDMMC2_NCD, true, true, true, + stm32_ncd_interrupt, NULL); + if (ret < 0) + { + ferr("ERROR: Failed to attach card detect interrupt: %d\n", ret); + } + + return OK; +} + +#endif /* CONFIG_MMCSD_SDIO */ diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h b/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h index a6de5e0ba8b..def7b66112d 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h +++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h @@ -109,6 +109,18 @@ #define TOF_I2C_ADDR 0x29 #define TOF_DEVPATH "/dev/video1" +/* microSD slot: card detect on PN12 (low when a card is inserted) and + * card power enable on PQ7 (active high). + */ + +#define GPIO_SDMMC2_NCD (GPIO_INPUT | GPIO_FLOAT | GPIO_EXTI | \ + GPIO_PORTN | GPIO_PIN12) +#define GPIO_SDMMC2_PWR_EN (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_2MHZ | \ + GPIO_OUTPUT_CLEAR | GPIO_PORTQ | GPIO_PIN7) + +#define SDIO_SLOTNO 0 +#define SDIO_MINOR 0 + #ifndef __ASSEMBLY__ /**************************************************************************** @@ -179,5 +191,17 @@ void stm32_camera_power(void); int stm32_tof_initialize(void); #endif +/**************************************************************************** + * Name: stm32_sdio_initialize + * + * Description: + * Initialize the microSD slot and register /dev/mmcsd0. + * + ****************************************************************************/ + +#if defined(CONFIG_STM32_SDMMC2) && defined(CONFIG_MMCSD_SDIO) +int stm32_sdio_initialize(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_STM32N6_STM32N6570_DK_SRC_STM32N6570_DK_H */
