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 928d7682ceb326a618ddfa05315b56b7ec669c5b
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Sat Apr 25 16:56:59 2026 -0300

    boards/stm32f4discovery: Add support to MT6816
    
    This commit add support to use MagTek Magnetic Rotary Encoder MT6816
    on STM32F4Discovery board.
    
    Signed-off-by: Alan C. Assis <[email protected]>
---
 boards/arm/stm32/common/include/stm32_mt6816.h     | 75 ++++++++++++++++++
 boards/arm/stm32/common/src/CMakeLists.txt         |  4 +
 boards/arm/stm32/common/src/Make.defs              |  4 +
 boards/arm/stm32/common/src/stm32_mt6816.c         | 91 ++++++++++++++++++++++
 .../stm32f4discovery/configs/mt6816/defconfig      | 56 +++++++++++++
 boards/arm/stm32/stm32f4discovery/include/board.h  |  5 ++
 .../arm/stm32/stm32f4discovery/src/stm32_bringup.c | 14 ++++
 boards/arm/stm32/stm32f4discovery/src/stm32_spi.c  | 11 +++
 8 files changed, 260 insertions(+)

diff --git a/boards/arm/stm32/common/include/stm32_mt6816.h 
b/boards/arm/stm32/common/include/stm32_mt6816.h
new file mode 100644
index 00000000000..2497d13aa9a
--- /dev/null
+++ b/boards/arm/stm32/common/include/stm32_mt6816.h
@@ -0,0 +1,75 @@
+/****************************************************************************
+ * boards/arm/stm32/common/include/stm32_mt6816.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MT6816_H
+#define __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MT6816_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_mt6816_initialize
+ *
+ * Description:
+ *   Initialize the MT6816 encoder driver
+ *
+ ****************************************************************************/
+
+int board_mt6816_initialize(int devno, int spi_busno);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BOARDS_ARM_STM32_COMMON_INCLUDE_STM32_MT6816_H */
diff --git a/boards/arm/stm32/common/src/CMakeLists.txt 
b/boards/arm/stm32/common/src/CMakeLists.txt
index 754fe81ce6e..c7d9426af29 100644
--- a/boards/arm/stm32/common/src/CMakeLists.txt
+++ b/boards/arm/stm32/common/src/CMakeLists.txt
@@ -82,6 +82,10 @@ if(CONFIG_SENSORS_APDS9960)
   list(APPEND SRCS stm32_apds9960.c)
 endif()
 
+if(CONFIG_SENSORS_MT6816)
+  list(APPEND SRCS stm32_mt6816.c)
+endif()
+
 if(CONFIG_SENSORS_ZEROCROSS)
   list(APPEND SRCS stm32_zerocross.c)
 endif()
diff --git a/boards/arm/stm32/common/src/Make.defs 
b/boards/arm/stm32/common/src/Make.defs
index 2839c86bf71..5b1f8556d76 100644
--- a/boards/arm/stm32/common/src/Make.defs
+++ b/boards/arm/stm32/common/src/Make.defs
@@ -86,6 +86,10 @@ ifeq ($(CONFIG_SENSORS_APDS9960),y)
   CSRCS += stm32_apds9960.c
 endif
 
+ifeq ($(CONFIG_SENSORS_MT6816),y)
+  CSRCS += stm32_mt6816.c
+endif
+
 ifeq ($(CONFIG_INPUT_MPR121_KEYPAD),y)
   CSRCS += stm32_mpr121.c
 endif
diff --git a/boards/arm/stm32/common/src/stm32_mt6816.c 
b/boards/arm/stm32/common/src/stm32_mt6816.c
new file mode 100644
index 00000000000..834e1203850
--- /dev/null
+++ b/boards/arm/stm32/common/src/stm32_mt6816.c
@@ -0,0 +1,91 @@
+/****************************************************************************
+ * boards/arm/stm32/common/src/stm32_mt6816.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 <stdio.h>
+
+#include <nuttx/sensors/qencoder.h>
+#include <nuttx/sensors/mt6816.h>
+#include <arch/board/board.h>
+#include <nuttx/spi/spi.h>
+
+#include "chip.h"
+#include "stm32.h"
+#include "stm32_spi.h"
+#include "arm_internal.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: board_mt6816_initialize
+ *
+ * Description:
+ *   Initialize the MT6816 encoder driver
+ *
+ ****************************************************************************/
+
+int board_mt6816_initialize(int devno, int spi_busno)
+{
+  struct spi_dev_s *spi;
+  struct qe_lowerhalf_s *dev;
+  char qe_path[12];
+  int ret = OK;
+
+  spi = stm32_spibus_initialize(spi_busno);
+  if (!spi)
+    {
+      return -ENODEV;
+    }
+
+  dev = mt6816_initialize(spi, (uint16_t) devno);
+  if (dev == NULL)
+    {
+      snerr("ERROR: Failed to initialize MT6816 at SPI%d\n", spi_busno);
+      ret = -ENODEV;
+    }
+  else
+    {
+      snprintf(qe_path, sizeof(qe_path), "/dev/qe%d", devno);
+      ret = qe_register(qe_path, dev);
+      if (ret < 0)
+        {
+          snerr("ERROR: Failed to register MT6816 qe%d driver: %d\n",
+                devno, ret);
+          ret = -ENODEV;
+        }
+    }
+
+  return ret;
+}
diff --git a/boards/arm/stm32/stm32f4discovery/configs/mt6816/defconfig 
b/boards/arm/stm32/stm32f4discovery/configs/mt6816/defconfig
new file mode 100644
index 00000000000..722c5b12b9f
--- /dev/null
+++ b/boards/arm/stm32/stm32f4discovery/configs/mt6816/defconfig
@@ -0,0 +1,56 @@
+#
+# 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_FPU is not set
+# CONFIG_DEBUG_ERROR 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_COMMON=y
+CONFIG_ARCH_BOARD_STM32F4_DISCOVERY=y
+CONFIG_ARCH_BUTTONS=y
+CONFIG_ARCH_CHIP="stm32"
+CONFIG_ARCH_CHIP_STM32=y
+CONFIG_ARCH_CHIP_STM32F407VG=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_BOARD_LATE_INITIALIZE=y
+CONFIG_BOARD_LOOPSPERMSEC=16717
+CONFIG_BUILTIN=y
+CONFIG_DEBUG_FEATURES=y
+CONFIG_DEBUG_SENSORS=y
+CONFIG_EXAMPLES_HELLO=y
+CONFIG_EXAMPLES_QENCODER=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_READLINE=y
+CONFIG_PREALLOC_TIMERS=4
+CONFIG_RAM_SIZE=114688
+CONFIG_RAM_START=0x20000000
+CONFIG_RAW_BINARY=y
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_WAITPID=y
+CONFIG_SENSORS=y
+CONFIG_SENSORS_MT6816=y
+CONFIG_START_DAY=6
+CONFIG_START_MONTH=12
+CONFIG_START_YEAR=2011
+CONFIG_STM32_JTAG_SW_ENABLE=y
+CONFIG_STM32_PWR=y
+CONFIG_STM32_SPI1=y
+CONFIG_STM32_USART2=y
+CONFIG_SYSTEM_NSH=y
+CONFIG_USART2_RXBUFSIZE=128
+CONFIG_USART2_SERIAL_CONSOLE=y
+CONFIG_USART2_TXBUFSIZE=128
diff --git a/boards/arm/stm32/stm32f4discovery/include/board.h 
b/boards/arm/stm32/stm32f4discovery/include/board.h
index 4f850570856..2b2d37418c4 100644
--- a/boards/arm/stm32/stm32f4discovery/include/board.h
+++ b/boards/arm/stm32/stm32f4discovery/include/board.h
@@ -455,6 +455,11 @@
 
 #define BOARD_MPR121_GPIO_INT GPIO_MPR121_INT
 
+/* Pin for Magnetic Encoder MT6816 */
+
+#define GPIO_CS_MT6816      (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
+                             GPIO_OUTPUT_SET|GPIO_PORTE|GPIO_PIN3)
+
 /* LIS3DSH */
 
 #define GPIO_LIS3DSH_EXT0 \
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c 
b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
index 714945164bd..272cc5206ef 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
@@ -60,6 +60,10 @@
 #include "stm32_apds9960.h"
 #endif
 
+#ifdef CONFIG_SENSORS_MT6816
+#include "stm32_mt6816.h"
+#endif
+
 #ifdef CONFIG_INPUT_MPR121_KEYPAD
 #include "stm32_mpr121.h"
 #endif
@@ -268,6 +272,16 @@ int stm32_bringup(void)
   board_zerocross_initialize(0);
 #endif
 
+#ifdef CONFIG_SENSORS_MT6816
+  /* Initialize MT6816 as /dev/qe0 on SPI1 */
+
+  ret = board_mt6816_initialize(0, 1);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: board_mt6816_initialize failed: %d\n", ret);
+    }
+#endif
+
 #ifdef CONFIG_LEDS_MAX7219
   ret = stm32_max7219init("/dev/numdisp0");
   if (ret < 0)
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c 
b/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c
index 3dad8ada29d..a1026359b94 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_spi.c
@@ -77,6 +77,10 @@ void weak_function stm32_spidev_initialize(void)
   stm32_configgpio(GPIO_CS_MFRC522);  /* MFRC522 chip select */
 #endif
 
+#if defined(CONFIG_STM32_SPI1) && defined(CONFIG_SENSORS_MT6816)
+  stm32_configgpio(GPIO_CS_MT6816);
+#endif
+
 #if defined(CONFIG_STM32_SPI2) && defined(CONFIG_SENSORS_MAX31855)
   stm32_configgpio(GPIO_MAX31855_CS); /* MAX31855 chip select */
 #endif
@@ -192,6 +196,13 @@ void stm32_spi1select(struct spi_dev_s *dev, uint32_t 
devid,
     }
 #endif
 
+#if defined (CONFIG_SENSORS_MT6816)
+  if (devid == SPIDEV_MAG_ENCODER(0))
+    {
+      stm32_gpiowrite(GPIO_CS_MT6816, !selected);
+    }
+#endif
+
 #if defined(CONFIG_CL_MFRC522)
   if (devid == SPIDEV_CONTACTLESS(0))
     {

Reply via email to