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


The following commit(s) were added to refs/heads/master by this push:
     new e67df47b64 add support for zero-cross driver
e67df47b64 is described below

commit e67df47b64d7e53c0c3daf82117aa9e4a2103d08
Author: halyssonJr <[email protected]>
AuthorDate: Sat May 11 17:37:38 2024 -0300

    add support for zero-cross driver
---
 .../include/esp32_zerocross.h}                     |  83 +++---------
 boards/xtensa/esp32/common/src/Make.defs           |   4 +
 .../src/esp32_zerocross.c}                         | 144 +++++++++++++--------
 .../esp32/esp32-wrover-kit/src/esp32-wrover-kit.h  |   3 +
 .../esp32/esp32-wrover-kit/src/esp32_bringup.c     |  15 +++
 5 files changed, 128 insertions(+), 121 deletions(-)

diff --git a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h 
b/boards/xtensa/esp32/common/include/esp32_zerocross.h
similarity index 56%
copy from boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
copy to boards/xtensa/esp32/common/include/esp32_zerocross.h
index 1b9c715f47..bed369cbe9 100644
--- a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
+++ b/boards/xtensa/esp32/common/include/esp32_zerocross.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
+ * boards/xtensa/esp32/common/include/esp32_zerocross.h
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,45 +18,19 @@
  *
  ****************************************************************************/
 
-#ifndef __BOARDS_XTENSA_ESP32_ESP32_WROVERKIT_SRC_ESP32_WROVERKIT_H
-#define __BOARDS_XTENSA_ESP32_ESP32_WROVERKIT_SRC_ESP32_WROVERKIT_H
+#ifndef __BOARDS_EXTENSA_ESP32_COMMON_INCLUDE_ESPM32_ZEROCROSS_H
+#define __BOARDS_EXTENSA_ESP32_COMMON_INCLUDE_ESPM32_ZEROCROSS_H
 
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <nuttx/compiler.h>
-#include <stdint.h>
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* ESP-WROVER-KIT GPIOs *****************************************************/
-
-/* LEDs */
-
-#define GPIO_LED1  0
-#define GPIO_LED2  2
-#define GPIO_LED3  4
-
-/* BOOT Button */
-
-#define BUTTON_BOOT  0
-
-/* TIMERS */
-
-#define TIMER0 0
-#define TIMER1 1
-#define TIMER2 2
-#define TIMER3 3
-
-/* ONESHOT */
-
-#define ONESHOT_TIMER         1
-#define ONESHOT_RESOLUTION_US 1
-
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -65,54 +39,35 @@
  * Public Data
  ****************************************************************************/
 
-#ifndef __ASSEMBLY__
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
 
 /****************************************************************************
- * Public Function Prototypes
+ * Inline Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: esp32_bringup
- *
- * Description:
- *   Perform architecture-specific initialization
- *
- *   CONFIG_BOARD_LATE_INITIALIZE=y :
- *     Called from board_late_initialize().
- *
- *   CONFIG_BOARD_LATE_INITIALIZE=y && CONFIG_BOARDCTL=y :
- *     Called from the NSH library via board_app_initialize()
- *
+ * Public Function Prototypes
  ****************************************************************************/
 
-int esp32_bringup(void);
-
 /****************************************************************************
- * Name: esp32_mmcsd_initialize
+ * Name: esp32_zerocross_initialize
  *
  * Description:
- *   Initialize SPI-based SD card and card detect thread.
+ *   Initialize and register the zero cross driver
  *
  ****************************************************************************/
 
-int esp32_mmcsd_initialize(int minor);
-
-/****************************************************************************
- * Name: esp32_spiflash_init
- *
- * Description:
- *   Initialize the SPIFLASH and register the MTD device.
- ****************************************************************************/
-
-int esp32_spiflash_init(void);
-
-/****************************************************************************
- * Name: esp32_gpio_init
- ****************************************************************************/
+int board_zerocross_initialize(int devno);
 
-#ifdef CONFIG_DEV_GPIO
-int esp32_gpio_init(void);
+#undef EXTERN
+#ifdef __cplusplus
+}
 #endif
 
-#endif /* __ASSEMBLY__ */
-#endif /* __BOARDS_XTENSA_ESP32_ESP32_WROVERKIT_SRC_ESP32_WROVERKIT_H */
+#endif /* __BOARDS_EXTENSA_ESP32_COMMON_INCLUDE_ESPM32_ZEROCROSS_H */
diff --git a/boards/xtensa/esp32/common/src/Make.defs 
b/boards/xtensa/esp32/common/src/Make.defs
index a7f05e7097..6ff049752f 100644
--- a/boards/xtensa/esp32/common/src/Make.defs
+++ b/boards/xtensa/esp32/common/src/Make.defs
@@ -148,6 +148,10 @@ endif
   CSRCS += esp32_board_dac.c
 #endif
 
+ifeq ($(CONFIG_SENSORS_ZEROCROSS),y)
+  CSRCS += esp32_zerocross.c
+endif
+
 DEPPATH += --dep-path src
 VPATH += :src
 CFLAGS += 
${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src
diff --git a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h 
b/boards/xtensa/esp32/common/src/esp32_zerocross.c
similarity index 51%
copy from boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
copy to boards/xtensa/esp32/common/src/esp32_zerocross.c
index 1b9c715f47..45a945508b 100644
--- a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
+++ b/boards/xtensa/esp32/common/src/esp32_zerocross.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
+ * boards/xtensa/esp32/common/src/esp32_zerocross.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,101 +18,131 @@
  *
  ****************************************************************************/
 
-#ifndef __BOARDS_XTENSA_ESP32_ESP32_WROVERKIT_SRC_ESP32_WROVERKIT_H
-#define __BOARDS_XTENSA_ESP32_ESP32_WROVERKIT_SRC_ESP32_WROVERKIT_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <nuttx/compiler.h>
+
 #include <stdint.h>
+#include <assert.h>
+#include <debug.h>
 
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
+#include <nuttx/irq.h>
+#include <nuttx/arch.h>
+#include <arch/board/board.h>
+#include <nuttx/sensors/zerocross.h>
 
-/* ESP-WROVER-KIT GPIOs *****************************************************/
+#include "esp32_gpio.h"
+#include "hardware/esp32_gpio_sigmap.h"
+#include "esp32-wrover-kit.h"
+#include "esp32_zerocross.h"
 
-/* LEDs */
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
 
-#define GPIO_LED1  0
-#define GPIO_LED2  2
-#define GPIO_LED3  4
+static void zcross_enable(const struct zc_lowerhalf_s *lower,
+                          zc_interrupt_t handler, void *arg);
 
-/* BOOT Button */
+static int zcross_interrupt(int irq, void *context, void *arg);
 
-#define BUTTON_BOOT  0
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
 
-/* TIMERS */
+/* Current interrupt handler and argument */
 
-#define TIMER0 0
-#define TIMER1 1
-#define TIMER2 2
-#define TIMER3 3
+static zc_interrupt_t g_zcrosshandler;
+static void *g_zcrossarg;
 
-/* ONESHOT */
+/* This is the zero cross lower half driver interface */
 
-#define ONESHOT_TIMER         1
-#define ONESHOT_RESOLUTION_US 1
+static struct zc_lowerhalf_s g_zcrosslower =
+{
+    .zc_enable = zcross_enable,
+};
 
 /****************************************************************************
- * Public Types
+ * Private Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Public Data
+ * Name: zcross_enable
+ *
+ * Description:
+ *   Enable interrupts on the selected zero cross pin.  And empty
+ *   set will disable all interrupts.
+ *
  ****************************************************************************/
 
-#ifndef __ASSEMBLY__
+static void zcross_enable(const struct zc_lowerhalf_s *lower,
+                          zc_interrupt_t handler, void *arg)
+{
+  irqstate_t flags;
+  int irq = ESP32_PIN2IRQ(GPIO_ZERO_CROSS_IRQ);
+  int ret;
 
-/****************************************************************************
- * Public Function Prototypes
- ****************************************************************************/
+  flags = enter_critical_section();
+
+  if (handler)
+    {
+      g_zcrosshandler = handler;
+      g_zcrossarg = arg;
+    }
+
+  /* Start with all interrupts disabled */
+
+  esp32_gpioirqdisable(irq);
+
+  ret = irq_attach(irq, zcross_interrupt, NULL);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: zcross_enable() failed: %d\n", ret);
+      leave_critical_section(flags);
+    }
+
+  esp32_gpioirqenable(irq, RISING);
+
+  leave_critical_section(flags);
+}
 
 /****************************************************************************
- * Name: esp32_bringup
+ * Name: zcross_interrupt
  *
  * Description:
- *   Perform architecture-specific initialization
- *
- *   CONFIG_BOARD_LATE_INITIALIZE=y :
- *     Called from board_late_initialize().
- *
- *   CONFIG_BOARD_LATE_INITIALIZE=y && CONFIG_BOARDCTL=y :
- *     Called from the NSH library via board_app_initialize()
+ *   Zero Cross interrupt handler
  *
  ****************************************************************************/
 
-int esp32_bringup(void);
+static int zcross_interrupt(int irq, void *context, void *arg)
+{
+  DEBUGASSERT(g_zcrosshandler != NULL);
+  if (g_zcrosshandler)
+    {
+      g_zcrosshandler(&g_zcrosslower, g_zcrossarg);
+    }
+
+  return OK;
+}
 
 /****************************************************************************
- * Name: esp32_mmcsd_initialize
- *
- * Description:
- *   Initialize SPI-based SD card and card detect thread.
- *
+ * Public Functions
  ****************************************************************************/
 
-int esp32_mmcsd_initialize(int minor);
-
 /****************************************************************************
- * Name: esp32_spiflash_init
+ * Name: esp32_zerocross_initialize
  *
  * Description:
- *   Initialize the SPIFLASH and register the MTD device.
+ *   Initialize and register the zero cross driver
+ *
  ****************************************************************************/
 
-int esp32_spiflash_init(void);
-
-/****************************************************************************
- * Name: esp32_gpio_init
- ****************************************************************************/
+int board_zerocross_initialize(int devno)
+{
+  esp32_configgpio(GPIO_ZERO_CROSS_IRQ, INPUT_FUNCTION_3 | PULLUP);
 
-#ifdef CONFIG_DEV_GPIO
-int esp32_gpio_init(void);
-#endif
+  /* Register the zero cross device as /dev/zc0 */
 
-#endif /* __ASSEMBLY__ */
-#endif /* __BOARDS_XTENSA_ESP32_ESP32_WROVERKIT_SRC_ESP32_WROVERKIT_H */
+  return zc_register("/dev/zc0", &g_zcrosslower);
+}
diff --git a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h 
b/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
index 1b9c715f47..f1b040f0c8 100644
--- a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
+++ b/boards/xtensa/esp32/esp32-wrover-kit/src/esp32-wrover-kit.h
@@ -57,6 +57,9 @@
 #define ONESHOT_TIMER         1
 #define ONESHOT_RESOLUTION_US 1
 
+/* Zero Cross */
+#define GPIO_ZERO_CROSS_IRQ 22
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
diff --git a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_bringup.c 
b/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_bringup.c
index b5261dc59c..342b518e68 100644
--- a/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_bringup.c
+++ b/boards/xtensa/esp32/esp32-wrover-kit/src/esp32_bringup.c
@@ -105,6 +105,10 @@
 #  include "esp32_lcd_backpack.h"
 #endif
 
+#ifdef CONFIG_SENSORS_ZEROCROSS
+#   include "esp32_zerocross.h"
+#endif
+
 #include "esp32-wrover-kit.h"
 
 /****************************************************************************
@@ -385,6 +389,17 @@ int esp32_bringup(void)
     }
 #endif
 
+#ifdef CONFIG_SENSORS_ZEROCROSS
+  /* Register Zero Cross Driver */
+
+  ret = board_zerocross_initialize(0);
+  if (ret < 0)
+    {
+      syslog(LOG_ERR,
+            "ERROR: board_zerocross_initialize() failed: %d\n", ret);
+    }
+#endif
+
   /* If we got here then perhaps not all initialization was successful, but
    * at least enough succeeded to bring-up NSH with perhaps reduced
    * capabilities.

Reply via email to