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 d61d4c7ff727ab2a2f185f3805581e586678827c
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Sat Nov 14 18:34:21 2026 -0300

    boards/stm32f4discovery: Add gnsspps configuration.
    
    Add the stm32f4discovery:gnsspps configuration, which synchronizes
    the board to GNSS time with the PPS output of a GNSS receiver:
    
    * The TIM5 capture (32-bit, 84 MHz, CONFIG_STM32_TIM5_CAP) is the
      counter and the PPS input of the PPS disciplined PTP clock
      /dev/ptp0, registered by stm32_ptp_pps_setup().  The PPS is
      captured on PA1, TIM5_CH2.  PA0 (TIM5_CH1) is not used because the
      USER button is connected to it.
    * The NMEA output of the receiver goes to USART3 RX on PB11
      (/dev/ttyS1, 9600 baud) for the time of day.
    * The gnsspps application is enabled, and CONFIG_NSH_MAXARGUMENTS is
      raised to 12 so that all its options can be given in NSH.
    
    The TIM3 capture is only registered as /dev/capture0 when
    CONFIG_STM32_TIM3_CAP is enabled, so that CONFIG_CAPTURE without it
    does not stop the bring-up before the PPS clock is registered.
    
    The connections are described in the board documentation.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Alan Carvalho de Assis <[email protected]>
---
 .../arm/stm32f4/boards/stm32f4discovery/index.rst  | 23 ++++++
 .../stm32f4discovery/configs/gnsspps/defconfig     | 60 +++++++++++++++
 .../arm/stm32f4/stm32f4discovery/include/board.h   | 10 +++
 .../stm32f4/stm32f4discovery/src/CMakeLists.txt    |  4 +
 boards/arm/stm32f4/stm32f4discovery/src/Make.defs  |  4 +
 .../stm32f4/stm32f4discovery/src/stm32_bringup.c   | 12 ++-
 .../stm32f4/stm32f4discovery/src/stm32_ptp_pps.c   | 86 ++++++++++++++++++++++
 .../stm32f4discovery/src/stm32f4discovery.h        | 22 ++++++
 8 files changed, 220 insertions(+), 1 deletion(-)

diff --git 
a/Documentation/platforms/arm/stm32f4/boards/stm32f4discovery/index.rst 
b/Documentation/platforms/arm/stm32f4/boards/stm32f4discovery/index.rst
index 9707b4ab36c..2da00d7b14f 100644
--- a/Documentation/platforms/arm/stm32f4/boards/stm32f4discovery/index.rst
+++ b/Documentation/platforms/arm/stm32f4/boards/stm32f4discovery/index.rst
@@ -1173,6 +1173,29 @@ NOTES:
    OUT mode.  In that mode, the PHY interrupt is not supported.  The NINT
    pin serves as REFLCK0 in that case.
 
+gnsspps
+-------
+
+Synchronizes the board to GNSS time with the PPS output of a GNSS receiver,
+see :doc:`/applications/system/gnsspps/index`.  The free running counter of
+the TIM5 capture (32-bit, 84 MHz) is the counter of a PPS disciplined PTP
+clock, ``/dev/ptp0``, and its channel 2 captures the PPS in hardware.
+
+Connections:
+
+=============  ==================  ============
+GNSS receiver  STM32F4Discovery    Function
+=============  ==================  ============
+PPS            PA1 (P1 pin 12)     TIM5_CH2
+TX (NMEA)      PB11 (P1 pin 35)    USART3_RX
+GND            GND
+=============  ==================  ============
+
+The NMEA port is ``/dev/ttyS1`` at 9600 baud and the console is on USART2
+(PA2/PA3).  Start the servo with::
+
+    nsh> gnsspps -s /dev/ttyS1 &
+
 hciuart
 -------
 
diff --git a/boards/arm/stm32f4/stm32f4discovery/configs/gnsspps/defconfig 
b/boards/arm/stm32f4/stm32f4discovery/configs/gnsspps/defconfig
new file mode 100644
index 00000000000..72c564fa227
--- /dev/null
+++ b/boards/arm/stm32f4/stm32f4discovery/configs/gnsspps/defconfig
@@ -0,0 +1,60 @@
+#
+# 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_LIBC_FLOATINGPOINT is not set
+# CONFIG_NSH_ARGCAT is not set
+# CONFIG_NSH_CMDOPT_HEXDUMP is not set
+CONFIG_ARCH="arm"
+CONFIG_ARCH_BOARD="stm32f4discovery"
+CONFIG_ARCH_BOARD_STM32F4_DISCOVERY=y
+CONFIG_ARCH_BUTTONS=y
+CONFIG_ARCH_CHIP="stm32f4"
+CONFIG_ARCH_CHIP_STM32=y
+CONFIG_ARCH_CHIP_STM32F407VG=y
+CONFIG_ARCH_CHIP_STM32F4=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_BOARD_LOOPSPERMSEC=16717
+CONFIG_BUILTIN=y
+CONFIG_CAPTURE=y
+CONFIG_CAPTURE_NOTIFY=y
+CONFIG_CLOCK_ADJTIME=y
+CONFIG_EXAMPLES_HELLO=y
+CONFIG_FS_PROCFS=y
+CONFIG_HAVE_CXX=y
+CONFIG_HAVE_CXXINITIALIZE=y
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INTELHEX_BINARY=y
+CONFIG_LINE_MAX=64
+CONFIG_MM_REGIONS=2
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_FILEIOSIZE=512
+CONFIG_NSH_MAXARGUMENTS=12
+CONFIG_NSH_READLINE=y
+CONFIG_PREALLOC_TIMERS=4
+CONFIG_PTP_PPS=y
+CONFIG_RAM_SIZE=114688
+CONFIG_RAM_START=0x20000000
+CONFIG_RAW_BINARY=y
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_WAITPID=y
+CONFIG_SERIAL_TERMIOS=y
+CONFIG_START_YEAR=2026
+CONFIG_STM32_JTAG_SW_ENABLE=y
+CONFIG_STM32_PWR=y
+CONFIG_STM32_SPI1=y
+CONFIG_STM32_TIM5=y
+CONFIG_STM32_TIM5_CAP=y
+CONFIG_STM32_TIM5_CHANNEL=2
+CONFIG_STM32_TIM5_CLOCK=84000000
+CONFIG_STM32_USART2=y
+CONFIG_STM32_USART3=y
+CONFIG_SYSTEM_GNSSPPS=y
+CONFIG_SYSTEM_NSH=y
+CONFIG_USART2_RXBUFSIZE=128
+CONFIG_USART2_SERIAL_CONSOLE=y
+CONFIG_USART2_TXBUFSIZE=128
+CONFIG_USART3_BAUD=9600
diff --git a/boards/arm/stm32f4/stm32f4discovery/include/board.h 
b/boards/arm/stm32f4/stm32f4discovery/include/board.h
index 5dfe6f543ae..3eece5d6cf5 100644
--- a/boards/arm/stm32f4/stm32f4discovery/include/board.h
+++ b/boards/arm/stm32f4/stm32f4discovery/include/board.h
@@ -294,6 +294,16 @@
 #define GPIO_USART6_RX    (GPIO_USART6_RX_1|GPIO_SPEED_100MHz)     /* PC7 
(also I2S3_MCK and P2 pin 48) */
 #define GPIO_USART6_TX    (GPIO_USART6_TX_1|GPIO_SPEED_100MHz)     /* PC6 
(also P2 pin 47) */
 
+/* GNSS PPS input (CONFIG_PTP_PPS on the TIM5 capture, channel 2)
+ *
+ * The PPS output of a GNSS receiver goes to PA1 (P1 pin 12), TIM5 CH2.
+ * PA0 (TIM5 CH1) is avoided because the USER button and its RC filter
+ * are connected to it.  The NMEA output of the receiver can go to
+ * USART3 RX on PB11 (P1 pin 35).
+ */
+
+#define GPIO_TIM5_CH2IN   GPIO_TIM5_CH2IN_1                        /* PA1 */
+
 /* PWM
  *
  * The STM32F4 Discovery has no real on-board PWM devices, but the board
diff --git a/boards/arm/stm32f4/stm32f4discovery/src/CMakeLists.txt 
b/boards/arm/stm32f4/stm32f4discovery/src/CMakeLists.txt
index 33c96c8894d..3981d7a43e0 100644
--- a/boards/arm/stm32f4/stm32f4discovery/src/CMakeLists.txt
+++ b/boards/arm/stm32f4/stm32f4discovery/src/CMakeLists.txt
@@ -84,6 +84,10 @@ if(CONFIG_RGBLED)
   list(APPEND SRCS stm32_rgbled.c)
 endif()
 
+if(CONFIG_PTP_PPS)
+  list(APPEND SRCS stm32_ptp_pps.c)
+endif()
+
 if(CONFIG_PWM)
   list(APPEND SRCS stm32_pwm.c)
 endif()
diff --git a/boards/arm/stm32f4/stm32f4discovery/src/Make.defs 
b/boards/arm/stm32f4/stm32f4discovery/src/Make.defs
index 9d9deece275..95914af30d0 100644
--- a/boards/arm/stm32f4/stm32f4discovery/src/Make.defs
+++ b/boards/arm/stm32f4/stm32f4discovery/src/Make.defs
@@ -96,6 +96,10 @@ ifeq ($(CONFIG_CAPTURE),y)
 CSRCS += stm32_capture.c
 endif
 
+ifeq ($(CONFIG_PTP_PPS),y)
+CSRCS += stm32_ptp_pps.c
+endif
+
 ifeq ($(CONFIG_ARCH_CUSTOM_PMINIT),y)
 CSRCS += stm32_pm.c
 endif
diff --git a/boards/arm/stm32f4/stm32f4discovery/src/stm32_bringup.c 
b/boards/arm/stm32f4/stm32f4discovery/src/stm32_bringup.c
index d6fd03eb589..d1cfc497b3d 100644
--- a/boards/arm/stm32f4/stm32f4discovery/src/stm32_bringup.c
+++ b/boards/arm/stm32f4/stm32f4discovery/src/stm32_bringup.c
@@ -437,7 +437,7 @@ int stm32_bringup(void)
     }
 #endif
 
-#ifdef CONFIG_CAPTURE
+#if defined(CONFIG_CAPTURE) && defined(CONFIG_STM32_TIM3_CAP)
   /* Initialize Capture and register the Capture driver. */
 
   ret = stm32_capture_setup("/dev/capture0");
@@ -729,5 +729,15 @@ int stm32_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_PTP_PPS
+  /* PPS disciplined PTP clock: TIM5 counter, GNSS PPS on PA1 (TIM5_CH2) */
+
+  ret = stm32_ptp_pps_setup(0);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: stm32_ptp_pps_setup failed: %d\n", ret);
+    }
+#endif
+
   return ret;
 }
diff --git a/boards/arm/stm32f4/stm32f4discovery/src/stm32_ptp_pps.c 
b/boards/arm/stm32f4/stm32f4discovery/src/stm32_ptp_pps.c
new file mode 100644
index 00000000000..bf0c39ed9f5
--- /dev/null
+++ b/boards/arm/stm32f4/stm32f4discovery/src/stm32_ptp_pps.c
@@ -0,0 +1,86 @@
+/****************************************************************************
+ * boards/arm/stm32f4/stm32f4discovery/src/stm32_ptp_pps.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 <errno.h>
+
+#include <nuttx/debug.h>
+#include <nuttx/timers/capture.h>
+#include <nuttx/timers/ptp_pps.h>
+#include <arch/board/board.h>
+
+#include "stm32_capture.h"
+#include "stm32f4discovery.h"
+
+#ifdef CONFIG_PTP_PPS
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#ifndef CONFIG_STM32_TIM5_CAP
+#  error "The PPS clock needs CONFIG_STM32_TIM5_CAP"
+#endif
+
+#if CONFIG_STM32_TIM5_CHANNEL != STM32F4DISCOVERY_PPSCHANNEL
+#  error "The PPS is on TIM5 channel 2, set CONFIG_STM32_TIM5_CHANNEL=2"
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm32_ptp_pps_setup
+ *
+ * Description:
+ *   Register a PPS disciplined PTP clock on the TIM5 capture.  The free
+ *   running TIM5 counter is the counter of the clock, the PPS on PA1
+ *   (TIM5 CH2) is its external time stamp input 0.
+ *
+ * Input parameters:
+ *   devno - The device number, the clock is registered as /dev/ptp<devno>
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int stm32_ptp_pps_setup(int devno)
+{
+  struct cap_lowerhalf_s *capture;
+
+  capture = stm32_cap_initialize(STM32F4DISCOVERY_PPSTIMER);
+  if (capture == NULL)
+    {
+      return -ENODEV;
+    }
+
+  return ptp_pps_register(capture, CONFIG_STM32_TIM5_CLOCK, devno);
+}
+
+#endif /* CONFIG_PTP_PPS */
diff --git a/boards/arm/stm32f4/stm32f4discovery/src/stm32f4discovery.h 
b/boards/arm/stm32f4/stm32f4discovery/src/stm32f4discovery.h
index c0f531409ad..b4272103f36 100644
--- a/boards/arm/stm32f4/stm32f4discovery/src/stm32f4discovery.h
+++ b/boards/arm/stm32f4/stm32f4discovery/src/stm32f4discovery.h
@@ -271,6 +271,16 @@
 #define STM32F4DISCOVERY_CAPTURETIMER   3
 #define STM32F4DISCOVERY_CAPTURECHANNEL 2
 
+/* PPS disciplined PTP clock
+ *
+ * The PPS output of a GNSS receiver can be captured by TIM5 CH2 on PA1.
+ * TIM5 is a 32-bit timer, its free running counter is the counter of the
+ * clock.
+ */
+
+#define STM32F4DISCOVERY_PPSTIMER       5
+#define STM32F4DISCOVERY_PPSCHANNEL     2
+
 /* SPI chip selects */
 
 #define GPIO_CS_MEMS      (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
@@ -611,6 +621,18 @@ int stm32_pwm_setup(void);
 int stm32_capture_setup(const char *devpath);
 #endif
 
+/****************************************************************************
+ * Name: stm32_ptp_pps_setup
+ *
+ * Description:
+ *  Register a PPS disciplined PTP clock on the TIM5 capture
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_PTP_PPS
+int stm32_ptp_pps_setup(int devno);
+#endif
+
 /****************************************************************************
  * Name: stm32_can_setup
  *

Reply via email to