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

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

commit 36aa57f052971e0d0418a4c8bcd924da0c03adae
Author: raiden00pl <[email protected]>
AuthorDate: Sun Nov 15 10:19:14 2026 +0100

    boards/stm32n6570-dk: Add SPI5 on the Arduino connector.
    
    SPI5 (PE15 SCK D13, PH8 MISO D12, PG2 MOSI D11, PA3 chip select D10)
    clocked from PCLK2 and registered as /dev/spi5 in the nsh
    configuration.
    
    Assisted-by: Claude:claude-fable-5-1
    Signed-off-by: raiden00pl <[email protected]>
---
 .../arm/stm32n6/boards/stm32n6570-dk/index.rst     |   8 ++
 .../stm32n6/stm32n6570-dk/configs/nsh/defconfig    |   3 +
 boards/arm/stm32n6/stm32n6570-dk/include/board.h   |  14 +++
 .../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 ++
 .../src/{stm32n6570-dk.h => stm32_spi.c}           | 127 +++++++++++++++------
 .../arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h  |  29 +++++
 8 files changed, 161 insertions(+), 38 deletions(-)

diff --git a/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst 
b/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst
index cc9e3c0497f..92b55872627 100644
--- a/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst
+++ b/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst
@@ -147,6 +147,14 @@ USART1 ttyS0  PE5 TX, PE6 RX                           
ST-LINK VCP, console
 USART2 ttyS1  PD5 TX, PF6 RX, PG5 CTS, PG14 RTS        STMod+ connector
 ====== ====== ======================================== ====================
 
+SPI
+===
+
+SPI5 is wired to the Arduino connector: PE15 SCK (D13), PH8 MISO (D12),
+PG2 MOSI (D11) and PA3 as chip select (D10).  It is registered as
+``/dev/spi5`` and can be exercised with the ``spi`` tool; shorting D11
+to D12 loops the transmitted bytes back.
+
 Clocking
 ========
 
diff --git a/boards/arm/stm32n6/stm32n6570-dk/configs/nsh/defconfig 
b/boards/arm/stm32n6/stm32n6570-dk/configs/nsh/defconfig
index 4b55b93a58f..a37863c3b46 100644
--- a/boards/arm/stm32n6/stm32n6570-dk/configs/nsh/defconfig
+++ b/boards/arm/stm32n6/stm32n6570-dk/configs/nsh/defconfig
@@ -45,6 +45,7 @@ CONFIG_RTC_DRIVER=y
 CONFIG_RTC_NALARMS=2
 CONFIG_RTC_PERIODIC=y
 CONFIG_SCHED_WAITPID=y
+CONFIG_SPITOOL_MAXBUS=5
 CONFIG_STM32N6_DEBUG=y
 CONFIG_STM32_DMA1=y
 CONFIG_STM32_I2C1=y
@@ -52,10 +53,12 @@ CONFIG_STM32_I2C2=y
 CONFIG_STM32_I2C4=y
 CONFIG_STM32_RNG=y
 CONFIG_STM32_RTC=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 7acfbe1f92b..347510e21e1 100644
--- a/boards/arm/stm32n6/stm32n6570-dk/include/board.h
+++ b/boards/arm/stm32n6/stm32n6570-dk/include/board.h
@@ -267,6 +267,20 @@
 #define STM32_I2C2_FREQUENCY     STM32_HSI_FREQUENCY
 #define STM32_I2C4_FREQUENCY     STM32_HSI_FREQUENCY
 
+/* SPI GPIOs ****************************************************************/
+
+/* SPI5 on the Arduino connector: PE15 SCK (D13), PH8 MISO (D12), PG2 MOSI
+ * (D11); PA3 (D10) is driven as a GPIO chip select by the board code.
+ * The kernel clock is PCLK2 (200 MHz).
+ */
+
+#define GPIO_SPI5_SCK     (GPIO_SPI5_SCK_1 | GPIO_SPEED_50MHZ)
+#define GPIO_SPI5_MISO    (GPIO_SPI5_MISO_4 | GPIO_SPEED_50MHZ)
+#define GPIO_SPI5_MOSI    (GPIO_SPI5_MOSI_4 | GPIO_SPEED_50MHZ)
+
+#define STM32_RCC_CCIPR9_SPI5SEL RCC_CCIPR9_SPI5SEL_PCLK2
+#define STM32_SPI5_FREQUENCY     STM32_PCLK2_FREQUENCY
+
 /* 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 1d098532c37..9a8754811fd 100644
--- a/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt
+++ b/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt
@@ -32,6 +32,10 @@ if(CONFIG_ARCH_BUTTONS)
   list(APPEND SRCS stm32_buttons.c)
 endif()
 
+if(CONFIG_STM32_SPI)
+  list(APPEND SRCS stm32_spi.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 66585c67cca..da47a4d6b1c 100644
--- a/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs
+++ b/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs
@@ -35,6 +35,10 @@ ifeq ($(CONFIG_ARCH_BUTTONS),y)
 CSRCS += stm32_buttons.c
 endif
 
+ifeq ($(CONFIG_STM32_SPI),y)
+CSRCS += stm32_spi.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 4382eac0c54..351277c24c1 100644
--- a/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c
+++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c
@@ -104,6 +104,16 @@ int stm32_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_STM32_SPI
+  /* Initialize the SPI buses and their chip selects */
+
+  stm32_spiinitialize();
+
+#  ifdef CONFIG_SPI_DRIVER
+  stm32_spiregister();
+#  endif
+#endif
+
   UNUSED(ret);
   return OK;
 }
diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h 
b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_spi.c
similarity index 51%
copy from boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h
copy to boards/arm/stm32n6/stm32n6570-dk/src/stm32_spi.c
index 0d012d11f00..92809cd6d7a 100644
--- a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h
+++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_spi.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h
+ * boards/arm/stm32n6/stm32n6570-dk/src/stm32_spi.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,68 +20,119 @@
  *
  ****************************************************************************/
 
-#ifndef __BOARDS_ARM_STM32N6_STM32N6570_DK_SRC_STM32N6570_DK_H
-#define __BOARDS_ARM_STM32N6_STM32N6570_DK_SRC_STM32N6570_DK_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <nuttx/compiler.h>
+
 #include <stdint.h>
+#include <stdbool.h>
+#include <nuttx/debug.h>
+#include <errno.h>
 
-#include "stm32_gpio.h"
+#include <nuttx/spi/spi.h>
+#include <nuttx/spi/spi_transfer.h>
+#include <arch/board/board.h>
 
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
+#include "chip.h"
+#include "stm32.h"
+#include "stm32n6570-dk.h"
 
-/* LED definitions **********************************************************/
+#ifdef CONFIG_STM32_SPI
 
-/* LD1 is active high; LD2 is active low.  Start with both LEDs off. */
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
 
-#define GPIO_LD1       (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_2MHZ | \
-                        GPIO_OUTPUT_CLEAR | GPIO_PORTO | GPIO_PIN1)
-#define GPIO_LD2       (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_2MHZ | \
-                        GPIO_OUTPUT_SET | GPIO_PORTG | GPIO_PIN10)
+#ifdef CONFIG_STM32_SPI5
+static struct spi_dev_s *g_spi5;
+#endif
 
 /****************************************************************************
- * Public Types
+ * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Public Data
+ * Name: stm32_spiinitialize
+ *
+ * Description:
+ *   Initialize the SPI buses and their chip select GPIOs.  The SPI pins
+ *   themselves are configured by the SPI driver.
+ *
  ****************************************************************************/
 
-/* USER1 (B2) on PC13 and TAMP (B4) on PE0, high when pressed */
-
-#define GPIO_BTN_USER  (GPIO_INPUT | GPIO_PULLDOWN | GPIO_EXTI | \
-                        GPIO_PORTC | GPIO_PIN13)
-#define GPIO_BTN_TAMP  (GPIO_INPUT | GPIO_PULLDOWN | GPIO_EXTI | \
-                        GPIO_PORTE | GPIO_PIN0)
-
-#ifndef __ASSEMBLY__
+void stm32_spiinitialize(void)
+{
+#ifdef CONFIG_STM32_SPI5
+  g_spi5 = stm32_spibus_initialize(5);
+  if (g_spi5 == NULL)
+    {
+      spierr("ERROR: Failed to initialize SPI port 5\n");
+    }
+  else
+    {
+      stm32_configgpio(GPIO_SPI5_CS);
+    }
+#endif
+}
 
 /****************************************************************************
- * Public Function Prototypes
+ * Name: stm32_spiregister
+ *
+ * Description:
+ *   Register the SPI character drivers.
+ *
  ****************************************************************************/
 
+#ifdef CONFIG_SPI_DRIVER
+void stm32_spiregister(void)
+{
+#ifdef CONFIG_STM32_SPI5
+  int ret;
+
+  if (g_spi5 != NULL)
+    {
+      ret = spi_register(g_spi5, 5);
+      if (ret < 0)
+        {
+          spierr("ERROR: Failed to register SPI port 5: %d\n", ret);
+        }
+    }
+#endif
+}
+#endif
+
 /****************************************************************************
- * Name: stm32_bringup
+ * Name: stm32_spi5select, stm32_spi5status and stm32_spi5cmddata
  *
  * Description:
- *   Perform architecture-specific initialization
- *
- *   CONFIG_BOARD_LATE_INITIALIZE=y :
- *     Called from board_late_initialize().
- *
- *   CONFIG_BOARD_LATE_INITIALIZE=n && CONFIG_BOARDCTL=y :
- *     Called from the NSH library
+ *   Board-specific chip select, status and command/data callbacks for the
+ *   SPI5 bus.  The only chip select is the Arduino D10 line.
  *
  ****************************************************************************/
 
-int stm32_bringup(void);
-
-#endif /* __ASSEMBLY__ */
-#endif /* __BOARDS_ARM_STM32N6_STM32N6570_DK_SRC_STM32N6570_DK_H */
+#ifdef CONFIG_STM32_SPI5
+void stm32_spi5select(struct spi_dev_s *dev, uint32_t devid,
+                      bool selected)
+{
+  spiinfo("devid: %08" PRIx32 " CS: %s\n", devid,
+          selected ? "assert" : "de-assert");
+
+  stm32_gpiowrite(GPIO_SPI5_CS, !selected);
+}
+
+uint8_t stm32_spi5status(struct spi_dev_s *dev, uint32_t devid)
+{
+  return 0;
+}
+
+#ifdef CONFIG_SPI_CMDDATA
+int stm32_spi5cmddata(struct spi_dev_s *dev, uint32_t devid, bool cmd)
+{
+  return -ENODEV;
+}
+#endif
+#endif /* CONFIG_STM32_SPI5 */
+
+#endif /* CONFIG_STM32_SPI */
diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h 
b/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h
index 0d012d11f00..2d7bcf11bf1 100644
--- a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h
+++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h
@@ -61,6 +61,11 @@
 #define GPIO_BTN_TAMP  (GPIO_INPUT | GPIO_PULLDOWN | GPIO_EXTI | \
                         GPIO_PORTE | GPIO_PIN0)
 
+/* SPI5 chip select on PA3 (Arduino D10), active low */
+
+#define GPIO_SPI5_CS   (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_50MHZ | \
+                        GPIO_OUTPUT_SET | GPIO_PORTA | GPIO_PIN3)
+
 #ifndef __ASSEMBLY__
 
 /****************************************************************************
@@ -83,5 +88,29 @@
 
 int stm32_bringup(void);
 
+/****************************************************************************
+ * Name: stm32_spiinitialize
+ *
+ * Description:
+ *   Initialize the SPI buses and their chip select GPIOs.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_STM32_SPI
+void stm32_spiinitialize(void);
+#endif
+
+/****************************************************************************
+ * Name: stm32_spiregister
+ *
+ * Description:
+ *   Register the SPI character drivers.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SPI_DRIVER
+void stm32_spiregister(void);
+#endif
+
 #endif /* __ASSEMBLY__ */
 #endif /* __BOARDS_ARM_STM32N6_STM32N6570_DK_SRC_STM32N6570_DK_H */

Reply via email to