This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 324a0cd4379 boards/arm/stm32l4/nucleo-l432kc: Add support for external 
W25 SPI flash.
324a0cd4379 is described below

commit 324a0cd4379f4c808572f518a50309b591768b9f
Author: Daniel P. Carvalho <[email protected]>
AuthorDate: Fri Oct 2 17:37:04 2026 -0300

    boards/arm/stm32l4/nucleo-l432kc: Add support for external W25 SPI flash.
    
    This commit adds board support for connecting an external Winbond W25
    SPI NOR flash memory via SPI1 on the Nucleo-L432KC:
    - Define W25_SPI1_CS on PA11.
    - Configure SPI1 chip select and presence detection in stm32_spi.c.
    - Add stm32_w25initialize() to initialize SPI1, bind the W25 driver,
      and register /dev/mtd0, /dev/mtdblock0 (FTL), and optionally /dev/smart0.
    - Call stm32_w25initialize() during bringup when CONFIG_MTD_W25 is enabled.
    
    Assisted-by: gemini-3.8-flash
    Signed-off-by: Daniel P. Carvalho <[email protected]>
---
 .../arm/stm32l4/nucleo-l432kc/src/CMakeLists.txt   |   4 +
 boards/arm/stm32l4/nucleo-l432kc/src/Make.defs     |   4 +
 .../arm/stm32l4/nucleo-l432kc/src/nucleo-l432kc.h  |  26 +++++
 .../arm/stm32l4/nucleo-l432kc/src/stm32_bringup.c  |  12 ++
 boards/arm/stm32l4/nucleo-l432kc/src/stm32_spi.c   |  42 +++++--
 boards/arm/stm32l4/nucleo-l432kc/src/stm32_w25.c   | 129 +++++++++++++++++++++
 6 files changed, 207 insertions(+), 10 deletions(-)

diff --git a/boards/arm/stm32l4/nucleo-l432kc/src/CMakeLists.txt 
b/boards/arm/stm32l4/nucleo-l432kc/src/CMakeLists.txt
index 374e99abc25..683ac89ad48 100644
--- a/boards/arm/stm32l4/nucleo-l432kc/src/CMakeLists.txt
+++ b/boards/arm/stm32l4/nucleo-l432kc/src/CMakeLists.txt
@@ -52,6 +52,10 @@ if(CONFIG_MTD_AT45DB)
   list(APPEND SRCS stm32_at45db.c)
 endif()
 
+if(CONFIG_MTD_W25)
+  list(APPEND SRCS stm32_w25.c)
+endif()
+
 if(CONFIG_SENSORS_INA226)
   list(APPEND SRCS stm32_ina226.c)
 endif()
diff --git a/boards/arm/stm32l4/nucleo-l432kc/src/Make.defs 
b/boards/arm/stm32l4/nucleo-l432kc/src/Make.defs
index 08b523c21d1..b87bd2d1e25 100644
--- a/boards/arm/stm32l4/nucleo-l432kc/src/Make.defs
+++ b/boards/arm/stm32l4/nucleo-l432kc/src/Make.defs
@@ -54,6 +54,10 @@ ifeq ($(CONFIG_MTD_AT45DB),y)
 CSRCS += stm32_at45db.c
 endif
 
+ifeq ($(CONFIG_MTD_W25),y)
+CSRCS += stm32_w25.c
+endif
+
 ifeq ($(CONFIG_SENSORS_INA226),y)
 CSRCS += stm32_ina226.c
 endif
diff --git a/boards/arm/stm32l4/nucleo-l432kc/src/nucleo-l432kc.h 
b/boards/arm/stm32l4/nucleo-l432kc/src/nucleo-l432kc.h
index 11dfbed2ee9..d9165452be7 100644
--- a/boards/arm/stm32l4/nucleo-l432kc/src/nucleo-l432kc.h
+++ b/boards/arm/stm32l4/nucleo-l432kc/src/nucleo-l432kc.h
@@ -65,6 +65,14 @@
 #  undef HAVE_AT45DB
 #endif
 
+/* Check if we can support W25 FLASH file system */
+
+#if !defined(CONFIG_STM32_SPI1) || !defined(CONFIG_MTD_W25)
+#  undef HAVE_W25
+#else
+#  define HAVE_W25 1
+#endif
+
 /* LED.  User LD3: the green LED is a user LED connected to Arduino signal
  * D13 corresponding to MCU I/O PB3 (pin 26)
  * target.
@@ -86,6 +94,12 @@
       GPIO_SPEED_50MHz)
 #endif
 
+#ifdef CONFIG_MTD_W25
+#  define W25_SPI1_CS \
+     (GPIO_PORTA | GPIO_PIN11 | GPIO_OUTPUT_SET | GPIO_OUTPUT | GPIO_PUSHPULL 
| \
+      GPIO_SPEED_50MHz)
+#endif
+
 /* GPIO pins used by the GPIO Subsystem */
 
 #define BOARD_NGPIOIN     1 /* Amount of GPIO Input pins */
@@ -235,6 +249,18 @@ int stm32_dac7571initialize(const char *devpath);
 int stm32_at45dbinitialize(int minor);
 #endif
 
+/****************************************************************************
+ * Name: stm32_w25initialize
+ *
+ * Description:
+ *   Initialize and register the W25 driver.
+ *
+ ****************************************************************************/
+
+#ifdef HAVE_W25
+int stm32_w25initialize(int minor);
+#endif
+
 /****************************************************************************
  * Name: stm32_ina226initialize
  *
diff --git a/boards/arm/stm32l4/nucleo-l432kc/src/stm32_bringup.c 
b/boards/arm/stm32l4/nucleo-l432kc/src/stm32_bringup.c
index 48ac6fff613..fe5389a2be6 100644
--- a/boards/arm/stm32l4/nucleo-l432kc/src/stm32_bringup.c
+++ b/boards/arm/stm32l4/nucleo-l432kc/src/stm32_bringup.c
@@ -210,6 +210,18 @@ int stm32_bringup(void)
     }
 #endif
 
+#ifdef HAVE_W25
+  /* Initialize and register the W25 FLASH. */
+
+  ret = stm32_w25initialize(0);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: Failed to initialize W25 minor %d: %d\n",
+             0, ret);
+      return ret;
+    }
+#endif
+
 #ifdef CONFIG_PWM
   /* Initialize PWM and register the PWM device. */
 
diff --git a/boards/arm/stm32l4/nucleo-l432kc/src/stm32_spi.c 
b/boards/arm/stm32l4/nucleo-l432kc/src/stm32_spi.c
index f52a6b7bbbf..7474965c83b 100644
--- a/boards/arm/stm32l4/nucleo-l432kc/src/stm32_spi.c
+++ b/boards/arm/stm32l4/nucleo-l432kc/src/stm32_spi.c
@@ -68,24 +68,28 @@ struct spi_dev_s *g_spi2;
  *
  ****************************************************************************/
 
+#ifdef CONFIG_SPI_DRIVER
 void stm32_spiregister(void)
 {
 #ifdef CONFIG_STM32_SPI1
-      int ret = spi_register(g_spi1, 1);
-      if (ret < 0)
-        {
-          spierr("ERROR: FAILED to register driver of SPI port 1\n");
-        }
+  int ret = spi_register(g_spi1, 1);
+
+  if (ret < 0)
+    {
+      spierr("ERROR: FAILED to register driver of SPI port 1\n");
+    }
 #endif
 
 #ifdef CONFIG_STM32_SPI2
-      int ret = spi_register(g_spi2, 2);
-      if (ret < 0)
-        {
-          spierr("ERROR: FAILED to register driver of SPI port 2\n");
-        }
+  int ret = spi_register(g_spi2, 2);
+
+  if (ret < 0)
+    {
+      spierr("ERROR: FAILED to register driver of SPI port 2\n");
+    }
 #endif
 }
+#endif
 
 /****************************************************************************
  * Name: stm32_spiinitialize
@@ -116,6 +120,10 @@ void stm32_spiinitialize(void)
 #ifdef CONFIG_MTD_AT45DB
   stm32_configgpio(AT45DB_SPI1_CS);      /* FLASH chip select */
 #endif
+
+#ifdef CONFIG_MTD_W25
+  stm32_configgpio(W25_SPI1_CS);         /* FLASH chip select */
+#endif
 #endif
 
 #ifdef CONFIG_STM32_SPI2
@@ -174,10 +182,24 @@ void stm32_spi1select(struct spi_dev_s *dev, uint32_t 
devid,
       stm32_gpiowrite(AT45DB_SPI1_CS, !selected);
     }
 #endif
+
+#ifdef CONFIG_MTD_W25
+  if (devid == SPIDEV_FLASH(0))
+    {
+      stm32_gpiowrite(W25_SPI1_CS, !selected);
+    }
+#endif
 }
 
 uint8_t stm32_spi1status(struct spi_dev_s *dev, uint32_t devid)
 {
+#ifdef CONFIG_MTD_W25
+  if (devid == SPIDEV_FLASH(0))
+    {
+      return SPI_STATUS_PRESENT;
+    }
+#endif
+
   return 0;
 }
 #endif
diff --git a/boards/arm/stm32l4/nucleo-l432kc/src/stm32_w25.c 
b/boards/arm/stm32l4/nucleo-l432kc/src/stm32_w25.c
new file mode 100644
index 00000000000..5682f497367
--- /dev/null
+++ b/boards/arm/stm32l4/nucleo-l432kc/src/stm32_w25.c
@@ -0,0 +1,129 @@
+/****************************************************************************
+ * boards/arm/stm32l4/nucleo-l432kc/src/stm32_w25.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 <stdio.h>
+#include <errno.h>
+#include <nuttx/debug.h>
+
+#include <nuttx/spi/spi.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/fs/smart.h>
+
+#include <sys/mount.h>
+
+#include "stm32l4_spi.h"
+#include "nucleo-l432kc.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef CONFIG_STM32_SPI1
+#  error "W25 driver requires CONFIG_STM32_SPI1 to be enabled"
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const int W25_SPI_PORT = 1;
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm32_w25initialize
+ *
+ * Description:
+ *   Initialize and configure the W25 SPI Serial Flash Memory.
+ *
+ ****************************************************************************/
+
+int stm32_w25initialize(int minor)
+{
+  FAR struct spi_dev_s *spi;
+  FAR struct mtd_dev_s *mtd;
+  static bool initialized = false;
+  int ret = OK;
+
+  if (!initialized)
+    {
+      finfo("INFO: Initializing W25\n");
+
+      spi = stm32_spibus_initialize(W25_SPI_PORT);
+      if (spi == NULL)
+        {
+          ferr("ERROR: Failed to initialize SPI port %d\n", W25_SPI_PORT);
+          return -ENODEV;
+        }
+
+      mtd = w25_initialize(spi);
+      if (mtd == NULL)
+        {
+          ferr("ERROR: Failed to initialize W25\n");
+          return -ENODEV;
+        }
+
+#if !defined(CONFIG_DISABLE_MOUNTPOINT)
+      ret = register_mtddriver("/dev/mtd0", mtd, 0777, NULL);
+      if (ret < 0)
+        {
+          ferr("ERROR: Failed to register /dev/mtd0: %d\n", ret);
+          return ret;
+        }
+
+      ret = ftl_initialize(0, mtd);
+      if (ret < 0)
+        {
+          ferr("ERROR: Failed to initialize FTL: %d\n", ret);
+        }
+#endif
+
+#if defined(CONFIG_FS_SMARTFS)
+      finfo("Initialize SMARTFS on /dev/smart0\n");
+      ret = smart_initialize(0, mtd, NULL);
+      if (ret < 0)
+        {
+          ferr("ERROR: Failed to initialize SMARTFS: %d\n", ret);
+        }
+      else
+        {
+          /* Try to mount SMARTFS if already formatted */
+
+          mount("/dev/smart0", "/mnt/w25", "smartfs", 0, NULL);
+        }
+#endif
+
+      initialized = true;
+    }
+
+  return OK;
+}

Reply via email to