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


The following commit(s) were added to refs/heads/master by this push:
     new 68db199d504 arch/arm/imxrt: Add adc driver for IMXRT118x
68db199d504 is described below

commit 68db199d5049a28096cefeff39127eeada487e91
Author: Ari Kimari <[email protected]>
AuthorDate: Tue Nov 3 11:02:30 2026 +0200

    arch/arm/imxrt: Add adc driver for IMXRT118x
    
    Add internal adc driver for imxrt118x
    
    [skip-cmake-parity]
    
    Signed-off-by: Ari Kimari <[email protected]>
---
 arch/arm/src/imxrt/CMakeLists.txt                  |   4 +-
 arch/arm/src/imxrt/Kconfig                         |   5 +
 arch/arm/src/imxrt/Make.defs                       |   4 +-
 arch/arm/src/imxrt/hardware/imxrt_adc.h            |   4 +-
 arch/arm/src/imxrt/hardware/imxrt_adc_ver3.h       |  75 ++++
 .../imxrt/hardware/rt118x/imxrt118x_memorymap.h    |   1 +
 arch/arm/src/imxrt/imxrt_adc_ver3.c                | 499 +++++++++++++++++++++
 arch/arm/src/imxrt/imxrt_clockconfig_ver3.h        |   5 +
 boards/arm/imxrt/imxrt1180-evk/src/Makefile        |   4 +
 boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h |  12 +
 .../src/{imxrt1180-evk.h => imxrt_adc.c}           |  68 ++-
 boards/arm/imxrt/imxrt1180-evk/src/imxrt_bringup.c |  11 +
 12 files changed, 678 insertions(+), 14 deletions(-)

diff --git a/arch/arm/src/imxrt/CMakeLists.txt 
b/arch/arm/src/imxrt/CMakeLists.txt
index f7ecd7a1a2f..c3eee67567a 100644
--- a/arch/arm/src/imxrt/CMakeLists.txt
+++ b/arch/arm/src/imxrt/CMakeLists.txt
@@ -166,7 +166,9 @@ if(CONFIG_IMXRT_USBDEV)
 endif()
 
 if(CONFIG_IMXRT_ADC)
-  if(CONFIG_IMXRT_ADC_VER2)
+  if(CONFIG_IMXRT_ADC_VER3)
+    list(APPEND SRCS imxrt_adc_ver3.c)
+  elseif(CONFIG_IMXRT_ADC_VER2)
     list(APPEND SRCS imxrt_adc_ver2.c)
   else()
     list(APPEND SRCS imxrt_adc_ver1.c)
diff --git a/arch/arm/src/imxrt/Kconfig b/arch/arm/src/imxrt/Kconfig
index 79e072d3d18..17013e26c15 100644
--- a/arch/arm/src/imxrt/Kconfig
+++ b/arch/arm/src/imxrt/Kconfig
@@ -258,6 +258,7 @@ config ARCH_FAMILY_IMXRT118x
        select IMXRT_HAVE_LPUART11
        select IMXRT_HAVE_LPUART12
        select IMXRT_CLOCKCONFIG_VER3
+       select IMXRT_ADC_VER3
        select IMXRT_IOMUX_VER3
        select IMXRT_RGPIO
        select IMXRT_EDMA_VER2
@@ -432,6 +433,10 @@ config IMXRT_ADC_VER2
        bool
        default n
 
+config IMXRT_ADC_VER3
+       bool
+       default n
+
 config IMXRT_CLOCKCONFIG_VER2
        bool
        default n
diff --git a/arch/arm/src/imxrt/Make.defs b/arch/arm/src/imxrt/Make.defs
index fd74819ee10..e05641963ed 100644
--- a/arch/arm/src/imxrt/Make.defs
+++ b/arch/arm/src/imxrt/Make.defs
@@ -171,7 +171,9 @@ CHIP_CSRCS += imxrt_usbdev.c
 endif
 
 ifeq ($(CONFIG_IMXRT_ADC),y)
-ifeq ($(CONFIG_IMXRT_ADC_VER2),y)
+ifeq ($(CONFIG_IMXRT_ADC_VER3),y)
+CHIP_CSRCS += imxrt_adc_ver3.c
+else ifeq ($(CONFIG_IMXRT_ADC_VER2),y)
 CHIP_CSRCS += imxrt_adc_ver2.c
 else
 CHIP_CSRCS += imxrt_adc_ver1.c
diff --git a/arch/arm/src/imxrt/hardware/imxrt_adc.h 
b/arch/arm/src/imxrt/hardware/imxrt_adc.h
index a35fd4c2f6d..a4591389a96 100644
--- a/arch/arm/src/imxrt/hardware/imxrt_adc.h
+++ b/arch/arm/src/imxrt/hardware/imxrt_adc.h
@@ -28,7 +28,9 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#ifdef CONFIG_IMXRT_ADC_VER2
+#ifdef CONFIG_IMXRT_ADC_VER3
+#include "imxrt_adc_ver3.h"
+#elif defined(CONFIG_IMXRT_ADC_VER2)
 #include "imxrt_adc_ver2.h"
 #else
 #include "imxrt_adc_ver1.h"
diff --git a/arch/arm/src/imxrt/hardware/imxrt_adc_ver3.h 
b/arch/arm/src/imxrt/hardware/imxrt_adc_ver3.h
new file mode 100644
index 00000000000..030f529c837
--- /dev/null
+++ b/arch/arm/src/imxrt/hardware/imxrt_adc_ver3.h
@@ -0,0 +1,75 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/hardware/imxrt_adc_ver3.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 __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_ADC_VER3_H
+#define __ARCH_ARM_SRC_IMXRT_HARDWARE_IMXRT_ADC_VER3_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <stdint.h>
+#include "hardware/imxrt_memorymap.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define IMXRT_ADC_CTRL_OFFSET       0x010
+#define IMXRT_ADC_STAT_OFFSET       0x014
+#define IMXRT_ADC_IE_OFFSET         0x018
+#define IMXRT_ADC_CFG_OFFSET        0x020
+#define IMXRT_ADC_SWTRIG_OFFSET     0x034
+#define IMXRT_ADC_TCTRL0_OFFSET     0x0a0
+#define IMXRT_ADC_FCTRL0_OFFSET     0x0e0
+#define IMXRT_ADC_CMDL1_OFFSET      0x100
+#define IMXRT_ADC_CMDH1_OFFSET      0x104
+#define IMXRT_ADC_RESFIFO0_OFFSET   0x300
+
+#define IMXRT_ADC_CTRL_ADCEN        (1u << 0)
+#define IMXRT_ADC_CTRL_RST          (1u << 1)
+#define IMXRT_ADC_CTRL_CAL_REQ      (1u << 3)
+#define IMXRT_ADC_CTRL_RSTFIFO0     (1u << 8)
+#define IMXRT_ADC_CTRL_RSTFIFO1     (1u << 9)
+
+#define IMXRT_ADC_STAT_CAL_RDY      (1u << 10)
+#define IMXRT_ADC_IE_FWMIE0         (1u << 0)
+#define IMXRT_ADC_CFG_REFSEL_SHIFT  6
+#define IMXRT_ADC_CFG_PUDLY_SHIFT   16
+#define IMXRT_ADC_CFG_PWREN         (1u << 28)
+
+#define IMXRT_ADC_TCTRL_TCMD(x)     (((uint32_t)(x) & 0xfu) << 24)
+#define IMXRT_ADC_FCTRL_FCOUNT_MASK 0x1fu
+#define IMXRT_ADC_FCTRL_FWMARK(x)   (((uint32_t)(x) & 0xfu) << 16)
+
+#define IMXRT_ADC_CMDL_ADCH(x)      ((uint32_t)(x) & 0x1fu)
+#define IMXRT_ADC_CMDL_CTYPE(x)     (((uint32_t)(x) & 0x3u) << 5)
+#define IMXRT_ADC_CMDL_CTYPE_A      0
+#define IMXRT_ADC_CMDL_CTYPE_B      1
+#define IMXRT_ADC_CMDL_CTYPE_SINGLE 0
+#define IMXRT_ADC_CMDL_MODE_12BIT   0
+#define IMXRT_ADC_CMDL_CSCALE(x)    (((uint32_t)(x) & 0x3u) << 13)
+
+#define IMXRT_ADC_RESFIFO_VALID     (1u << 31)
+#define IMXRT_ADC_RESFIFO_DATA_MASK 0xffffu
+
+#endif
diff --git a/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h 
b/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h
index 622f442275e..25f8b5dc660 100644
--- a/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h
+++ b/arch/arm/src/imxrt/hardware/rt118x/imxrt118x_memorymap.h
@@ -93,6 +93,7 @@
 #define IMXRT_OTFAD1_BASE               (0x425e0000ul)  /* On-The-Fly AES 
Decrypt 1 */
 
 #define IMXRT_ADC1_BASE                 (0x42600000ul)  /* LPADC 1 */
+#define IMXRT_ADC2_BASE                 (0x42e00000ul)  /* LPADC 2 */
 #define IMXRT_FLEXPWM1_BASE             (0x42650000ul)  /* FlexPWM 1 */
 #define IMXRT_FLEXPWM2_BASE             (0x42660000ul)  /* FlexPWM 2 */
 #define IMXRT_FLEXPWM3_BASE             (0x42670000ul)  /* FlexPWM 3 */
diff --git a/arch/arm/src/imxrt/imxrt_adc_ver3.c 
b/arch/arm/src/imxrt/imxrt_adc_ver3.c
new file mode 100644
index 00000000000..9ab9db40e11
--- /dev/null
+++ b/arch/arm/src/imxrt/imxrt_adc_ver3.c
@@ -0,0 +1,499 @@
+/****************************************************************************
+ * arch/arm/src/imxrt/imxrt_adc_ver3.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 <stdint.h>
+#include <string.h>
+
+#include <nuttx/analog/adc.h>
+#include <nuttx/analog/ioctl.h>
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+
+#include "arm_internal.h"
+#include "chip.h"
+#include "hardware/imxrt_adc.h"
+#include "hardware/imxrt_pinmux.h"
+#include "imxrt_gpio.h"
+#include "imxrt_periphclks.h"
+
+#ifdef CONFIG_IMXRT_ADC
+#if defined(CONFIG_IMXRT_ADC1) || defined(CONFIG_IMXRT_ADC2)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+#define ADC_MAX_CHANNELS 16
+#define ADC2_MAX_CHANNELS 14
+#define IMXRT_ADC_PIN(n) \
+  IOMUX_PIN(IMXRT_PADCFG(0, 63 + (n), 0, 0, IMXRT_PADCFG_NO_DSYIDX, 0), \
+            IOMUXC_PAD_IBE_OFF, IOMUXC_MUX_SION_OFF)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct imxrt_adc_ver3_s
+{
+  const struct adc_callback_s *cb;
+  uint8_t intf;
+  uint32_t base;
+  int irq;
+  uint8_t initialized;
+  uint8_t nchannels;
+  uint8_t chanlist[ADC_MAX_CHANNELS];
+  uint8_t current;
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int adc_bind(struct adc_dev_s *dev,
+                    const struct adc_callback_s *callback);
+static void adc_reset(struct adc_dev_s *dev);
+static int adc_setup(struct adc_dev_s *dev);
+static void adc_shutdown(struct adc_dev_s *dev);
+static void adc_rxint(struct adc_dev_s *dev, bool enable);
+static int adc_ioctl(struct adc_dev_s *dev, int cmd, unsigned long arg);
+static int adc_interrupt(int irq, void *context, void *arg);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct adc_ops_s g_adcops =
+{
+  .ao_bind = adc_bind,
+  .ao_reset = adc_reset,
+  .ao_setup = adc_setup,
+  .ao_shutdown = adc_shutdown,
+  .ao_rxint = adc_rxint,
+  .ao_ioctl = adc_ioctl,
+};
+
+#ifdef CONFIG_IMXRT_ADC1
+static struct imxrt_adc_ver3_s g_adcpriv1 =
+{
+  .intf = 1,
+  .base = IMXRT_ADC1_BASE,
+  .irq  = IMXRT_IRQ_ADC1
+};
+static struct adc_dev_s g_adcdev1 =
+{
+  &g_adcops,
+  &g_adcpriv1
+};
+
+static const gpio_pinset_t g_adcpinlist1[ADC_MAX_CHANNELS] =
+{
+  IMXRT_ADC_PIN(16),
+  IMXRT_ADC_PIN(14),
+  IMXRT_ADC_PIN(12),
+  IMXRT_ADC_PIN(10),
+  IMXRT_ADC_PIN(8),
+  IMXRT_ADC_PIN(6),
+  IMXRT_ADC_PIN(4),
+  IMXRT_ADC_PIN(2),
+  IMXRT_ADC_PIN(17),
+  IMXRT_ADC_PIN(15),
+  IMXRT_ADC_PIN(13),
+  IMXRT_ADC_PIN(11),
+  IMXRT_ADC_PIN(9),
+  IMXRT_ADC_PIN(7),
+  IMXRT_ADC_PIN(5),
+  IMXRT_ADC_PIN(3),
+};
+#endif
+
+#ifdef CONFIG_IMXRT_ADC2
+static struct imxrt_adc_ver3_s g_adcpriv2 =
+{
+  .intf = 2,
+  .base = IMXRT_ADC2_BASE,
+  .irq  = IMXRT_IRQ_ADC2
+};
+static struct adc_dev_s g_adcdev2 =
+{
+  &g_adcops,
+  &g_adcpriv2
+};
+
+static const gpio_pinset_t g_adcpinlist2[ADC2_MAX_CHANNELS] =
+{
+  IMXRT_ADC_PIN(18),
+  IMXRT_ADC_PIN(20),
+  IMXRT_ADC_PIN(22),
+  IMXRT_ADC_PIN(24),
+  IMXRT_ADC_PIN(26),
+  IMXRT_ADC_PIN(28),
+  IMXRT_ADC_PIN(30),
+  IMXRT_ADC_PIN(19),
+  IMXRT_ADC_PIN(21),
+  IMXRT_ADC_PIN(23),
+  IMXRT_ADC_PIN(25),
+  IMXRT_ADC_PIN(27),
+  IMXRT_ADC_PIN(29),
+  IMXRT_ADC_PIN(31),
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static inline uint32_t adc_getreg(struct imxrt_adc_ver3_s *priv,
+                                  uint32_t offset)
+{
+  return getreg32(priv->base + offset);
+}
+
+static inline void adc_putreg(struct imxrt_adc_ver3_s *priv,
+                              uint32_t offset, uint32_t value)
+{
+  putreg32(value, priv->base + offset);
+}
+
+/****************************************************************************
+ * Name: adc_bind
+ *
+ * Description:
+ *   Bind the upper-half driver callbacks to the lower-half implementation.
+ *   This must be called early in order to receive ADC event notifications.
+ *
+ ****************************************************************************/
+
+static int adc_bind(struct adc_dev_s *dev,
+                    const struct adc_callback_s *callback)
+{
+  ((struct imxrt_adc_ver3_s *)dev->ad_priv)->cb = callback;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: adc_reset
+ *
+ * Description:
+ *   Reset the ADC device.  Called early to initialize the hardware. This
+ *   is called, before adc_setup() and on error conditions.
+ *
+ ****************************************************************************/
+
+static void adc_reset(struct adc_dev_s *dev)
+{
+  struct imxrt_adc_ver3_s *priv = dev->ad_priv;
+
+  if (priv->initialized != 0)
+    {
+      return;
+    }
+
+  if (priv->intf == 1)
+    {
+      imxrt_clockall_adc1();
+    }
+  else
+    {
+      imxrt_clockall_adc2();
+    }
+
+  adc_putreg(priv, IMXRT_ADC_CTRL_OFFSET, IMXRT_ADC_CTRL_RST);
+  adc_putreg(priv, IMXRT_ADC_CTRL_OFFSET, 0);
+  adc_putreg(priv, IMXRT_ADC_CFG_OFFSET, IMXRT_ADC_CFG_PWREN |
+             (0x10u << IMXRT_ADC_CFG_PUDLY_SHIFT));
+  adc_putreg(priv, IMXRT_ADC_FCTRL0_OFFSET, IMXRT_ADC_FCTRL_FWMARK(0));
+  adc_putreg(priv, IMXRT_ADC_IE_OFFSET, 0);
+
+  /* Keep calibration out of the reset path until the gain-result sequence
+   * (GCC/GCR) is implemented.  The SDK examples initialize this way too.
+   */
+
+  adc_putreg(priv, IMXRT_ADC_CTRL_OFFSET, IMXRT_ADC_CTRL_ADCEN);
+
+  /* Pad configuration */
+
+  for (int i = 0; i < priv->nchannels; i++)
+    {
+      DEBUGASSERT(priv->chanlist[i] < ADC_MAX_CHANNELS);
+      if (priv->intf == 1)
+        {
+#ifdef CONFIG_IMXRT_ADC1
+          imxrt_config_gpio(g_adcpinlist1[priv->chanlist[i]]);
+#endif
+        }
+      else
+        {
+#ifdef CONFIG_IMXRT_ADC2
+          imxrt_config_gpio(g_adcpinlist2[priv->chanlist[i]]);
+#endif
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: adc_program
+ *
+ * Description:
+ *   Program the command and trigger registers for one ADC channel.
+ *   Channels are programmed one at a time; the interrupt handler programs
+ *   the next channel after each conversion result.
+ *
+ ****************************************************************************/
+
+static void adc_program(struct imxrt_adc_ver3_s *priv, uint8_t channel)
+{
+  uint8_t hardware_channel = channel;
+  uint8_t side_channels = priv->intf == 2 ? ADC2_MAX_CHANNELS / 2 :
+                                           ADC_MAX_CHANNELS / 2;
+  uint32_t channel_type = IMXRT_ADC_CMDL_CTYPE_A;
+
+  /* ADC1 IDs 0-7 are A0-A7 and IDs 8-15 are B0-B7.
+   * ADC2 IDs 0-6 are A0-A6 and IDs 7-13 are B0-B6.
+   */
+
+  if (channel >= side_channels)
+    {
+      hardware_channel = channel - side_channels;
+      channel_type = IMXRT_ADC_CMDL_CTYPE_B;
+    }
+
+  uint32_t cmdl = IMXRT_ADC_CMDL_ADCH(hardware_channel) |
+                  IMXRT_ADC_CMDL_CTYPE(channel_type) |
+                  IMXRT_ADC_CMDL_CSCALE(1);
+
+  adc_putreg(priv, IMXRT_ADC_CMDL1_OFFSET, cmdl);
+  adc_putreg(priv, IMXRT_ADC_CMDH1_OFFSET, 0);
+  adc_putreg(priv, IMXRT_ADC_TCTRL0_OFFSET, IMXRT_ADC_TCTRL_TCMD(1));
+}
+
+/****************************************************************************
+ * Name: adc_setup
+ *
+ * Description:
+ *   Configure the ADC. This method is called the first time that the ADC
+ *   device is opened.  This will occur when the port is first opened.
+ *   This setup includes configuring and attaching ADC interrupts.
+ *   Interrupts are all disabled upon return.
+ *
+ ****************************************************************************/
+
+static int adc_setup(struct adc_dev_s *dev)
+{
+  struct imxrt_adc_ver3_s *priv = dev->ad_priv;
+  int ret;
+
+  if (priv->initialized++ != 0)
+    {
+      return OK;
+    }
+
+  priv->current = 0;
+  /* Start the sequence with the first channel.  Subsequent channels are
+   * programmed by adc_interrupt() after each conversion.
+   */
+
+  adc_program(priv, priv->chanlist[0]);
+
+  ret = irq_attach(priv->irq, adc_interrupt, dev);
+  if (ret < 0)
+    {
+      priv->initialized--;
+      return ret;
+    }
+
+  up_enable_irq(priv->irq);
+  adc_putreg(priv, IMXRT_ADC_SWTRIG_OFFSET, 1);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: adc_shutdown
+ *
+ * Description:
+ *   Disable the ADC.  This method is called when the ADC device is closed.
+ *   This method reverses the operation the setup method.
+ *
+ ****************************************************************************/
+
+static void adc_shutdown(struct adc_dev_s *dev)
+{
+  struct imxrt_adc_ver3_s *priv = dev->ad_priv;
+
+  if (priv->initialized == 0 || --priv->initialized != 0)
+    {
+      return;
+    }
+
+  adc_putreg(priv, IMXRT_ADC_IE_OFFSET, 0);
+  adc_putreg(priv, IMXRT_ADC_SWTRIG_OFFSET, 0);
+  up_disable_irq(priv->irq);
+  irq_detach(priv->irq);
+}
+
+/****************************************************************************
+ * Name: adc_rxint
+ *
+ * Description:
+ *   Call to enable or disable RX interrupts
+ *
+ ****************************************************************************/
+
+static void adc_rxint(struct adc_dev_s *dev, bool enable)
+{
+  struct imxrt_adc_ver3_s *priv = dev->ad_priv;
+
+  adc_putreg(priv, IMXRT_ADC_IE_OFFSET, enable ? IMXRT_ADC_IE_FWMIE0 : 0);
+}
+
+/****************************************************************************
+ * Name: adc_ioctl
+ *
+ * Description:
+ *   All ioctl calls will be routed through this method.
+ *
+ * Input Parameters:
+ *   dev - pointer to device structure used by the driver
+ *   cmd - command
+ *   arg - arguments passed with command
+ *
+ * Returned Value:
+ *
+ ****************************************************************************/
+
+static int adc_ioctl(struct adc_dev_s *dev, int cmd, unsigned long arg)
+{
+  struct imxrt_adc_ver3_s *priv = dev->ad_priv;
+
+  return cmd == ANIOC_GET_NCHANNELS ? priv->nchannels : -ENOTTY;
+}
+
+/****************************************************************************
+ * Name: adc_interrupt
+ *
+ * Description:
+ *   ADC interrupt handler
+ *
+ ****************************************************************************/
+
+static int adc_interrupt(int irq, void *context, void *arg)
+{
+  struct adc_dev_s *dev = arg;
+  struct imxrt_adc_ver3_s *priv = dev->ad_priv;
+
+  while ((adc_getreg(priv, IMXRT_ADC_FCTRL0_OFFSET) &
+          IMXRT_ADC_FCTRL_FCOUNT_MASK) != 0)
+    {
+      uint32_t result = adc_getreg(priv, IMXRT_ADC_RESFIFO0_OFFSET);
+
+      if ((result & IMXRT_ADC_RESFIFO_VALID) == 0)
+        {
+          break;
+        }
+
+      if (priv->cb != NULL && priv->cb->au_receive != NULL)
+        {
+          priv->cb->au_receive(dev, priv->chanlist[priv->current],
+                               result & IMXRT_ADC_RESFIFO_DATA_MASK);
+        }
+
+      if (++priv->current >= priv->nchannels)
+        {
+          priv->current = 0;
+        }
+
+      adc_program(priv, priv->chanlist[priv->current]);
+      adc_putreg(priv, IMXRT_ADC_SWTRIG_OFFSET, 1);
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: imxrt_adcinitialize
+ *
+ * Description:
+ *   Initialize the adc
+ *
+ * Input Parameters:
+ *   intf      - ADC number (1 or 2)
+ *   chanlist  - The list of channels
+ *   nchannels - Number of channels
+ *
+ * Returned Value:
+ *   Valid can device structure reference on success; a NULL on failure
+ *
+ ****************************************************************************/
+
+struct adc_dev_s *imxrt_adcinitialize(int intf, const uint8_t *chanlist,
+                                      int nchannels)
+{
+  struct adc_dev_s *dev;
+  struct imxrt_adc_ver3_s *priv;
+
+  if (nchannels <= 0 || nchannels > ADC_MAX_CHANNELS || chanlist == NULL)
+    {
+      return NULL;
+    }
+
+  for (int i = 0; i < nchannels; i++)
+    {
+      if (chanlist[i] >= ADC_MAX_CHANNELS ||
+          (intf == 2 && chanlist[i] >= ADC2_MAX_CHANNELS))
+        {
+          return NULL;
+        }
+    }
+
+  switch (intf)
+    {
+#ifdef CONFIG_IMXRT_ADC1
+      case 1:
+        dev = &g_adcdev1;
+        break;
+#endif
+#ifdef CONFIG_IMXRT_ADC2
+      case 2:
+        dev = &g_adcdev2;
+        break;
+#endif
+      default:
+        return NULL;
+    }
+
+  priv = dev->ad_priv;
+  memcpy(priv->chanlist, chanlist, nchannels);
+  priv->nchannels = nchannels;
+  return dev;
+}
+
+#endif
+#endif
diff --git a/arch/arm/src/imxrt/imxrt_clockconfig_ver3.h 
b/arch/arm/src/imxrt/imxrt_clockconfig_ver3.h
index 24811775dab..490361cf243 100644
--- a/arch/arm/src/imxrt/imxrt_clockconfig_ver3.h
+++ b/arch/arm/src/imxrt/imxrt_clockconfig_ver3.h
@@ -34,6 +34,11 @@
 
 #include "hardware/rt118x/imxrt118x_ccm.h"
 
+#define imxrt_clockall_adc1() imxrt_ccm_gate_on(CCM_LPCG_ADC1, true)
+#define imxrt_clockall_adc2() imxrt_ccm_gate_on(CCM_LPCG_ADC2, true)
+#define imxrt_clockoff_adc1() imxrt_ccm_gate_on(CCM_LPCG_ADC1, false)
+#define imxrt_clockoff_adc2() imxrt_ccm_gate_on(CCM_LPCG_ADC2, false)
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
diff --git a/boards/arm/imxrt/imxrt1180-evk/src/Makefile 
b/boards/arm/imxrt/imxrt1180-evk/src/Makefile
index 1edecdcaa63..b0ad8287aa7 100644
--- a/boards/arm/imxrt/imxrt1180-evk/src/Makefile
+++ b/boards/arm/imxrt/imxrt1180-evk/src/Makefile
@@ -24,6 +24,10 @@ include $(TOPDIR)/Make.defs
 
 CSRCS  = imxrt_boot.c imxrt_bringup.c imxrt_appinit.c
 
+ifeq ($(CONFIG_IMXRT_ADC),y)
+CSRCS += imxrt_adc.c
+endif
+
 ifeq ($(CONFIG_ARCH_CORTEXM33),y)
 CSRCS += imxrt_clockconfig_m33.c
 else
diff --git a/boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h 
b/boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h
index 13229d8d6fd..610c1e50b5a 100644
--- a/boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h
+++ b/boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h
@@ -42,4 +42,16 @@ int imxrt_bringup(void);
 int imxrt_dma_alloc_init(void);
 #endif
 
+/****************************************************************************
+ * Name: imxrt_adc_initialize
+ *
+ * Description:
+ *   Initialize ADC drivers
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_IMXRT_ADC
+int imxrt_adc_initialize(void);
+#endif
+
 #endif /* __BOARDS_ARM_IMXRT_IMXRT1180_EVK_SRC_IMXRT1180_EVK_H */
diff --git a/boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h 
b/boards/arm/imxrt/imxrt1180-evk/src/imxrt_adc.c
similarity index 60%
copy from boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h
copy to boards/arm/imxrt/imxrt1180-evk/src/imxrt_adc.c
index 13229d8d6fd..5c915348168 100644
--- a/boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h
+++ b/boards/arm/imxrt/imxrt1180-evk/src/imxrt_adc.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * boards/arm/imxrt/imxrt1180-evk/src/imxrt1180-evk.h
+ * boards/arm/imxrt/imxrt1180-evk/src/imxrt_adc.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,26 +20,72 @@
  *
  ****************************************************************************/
 
-#ifndef __BOARDS_ARM_IMXRT_IMXRT1180_EVK_SRC_IMXRT1180_EVK_H
-#define __BOARDS_ARM_IMXRT_IMXRT1180_EVK_SRC_IMXRT1180_EVK_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <nuttx/compiler.h>
 
-#include <stdint.h>
+#include <errno.h>
+#include <stdbool.h>
+
+#include <nuttx/analog/adc.h>
+#include <nuttx/board.h>
+
+#include "imxrt_adc.h"
 
 /****************************************************************************
- * Public Function Prototypes
+ * Public Functions
  ****************************************************************************/
 
-int imxrt_bringup(void);
+#ifdef CONFIG_IMXRT_ADC
+
+int imxrt_adc_initialize(void)
+{
+  static bool initialized;
+  struct adc_dev_s *adc;
+  int ret;
+
+  static const uint8_t chanlist[] =
+    {
+      0
+    };
+
+  if (initialized)
+    {
+      return OK;
+    }
 
-#if defined(CONFIG_USBDEV_DMAMEMORY) || defined(CONFIG_FAT_DMAMEMORY)
-int imxrt_dma_alloc_init(void);
+#ifdef CONFIG_IMXRT_ADC1
+  adc = imxrt_adcinitialize(1, chanlist, 1);
+  if (adc == NULL)
+    {
+      return -ENODEV;
+    }
+
+  ret = adc_register("/dev/adc1", adc);
+  if (ret < 0)
+    {
+      return ret;
+    }
+#endif
+
+#ifdef CONFIG_IMXRT_ADC2
+  adc = imxrt_adcinitialize(2, chanlist, 1);
+  if (adc == NULL)
+    {
+      return -ENODEV;
+    }
+
+  ret = adc_register("/dev/adc2", adc);
+  if (ret < 0)
+    {
+      return ret;
+    }
 #endif
 
-#endif /* __BOARDS_ARM_IMXRT_IMXRT1180_EVK_SRC_IMXRT1180_EVK_H */
+  initialized = true;
+  return OK;
+}
+
+#endif
diff --git a/boards/arm/imxrt/imxrt1180-evk/src/imxrt_bringup.c 
b/boards/arm/imxrt/imxrt1180-evk/src/imxrt_bringup.c
index 910b03537f5..9aeaafafd82 100644
--- a/boards/arm/imxrt/imxrt1180-evk/src/imxrt_bringup.c
+++ b/boards/arm/imxrt/imxrt1180-evk/src/imxrt_bringup.c
@@ -41,6 +41,9 @@
 #include "imxrt_gpio.h"
 #include "imxrt_usdhc.h"
 #include "imxrt1180-evk.h"
+#ifdef CONFIG_IMXRT_ADC
+#  include "imxrt_adc.h"
+#endif
 #include <arch/board/board.h>  /* Must always be included last */
 
 /****************************************************************************
@@ -93,6 +96,14 @@ int imxrt_bringup(void)
 {
   int ret = OK;
 
+#ifdef CONFIG_IMXRT_ADC
+  ret = imxrt_adc_initialize();
+  if (ret < 0)
+    {
+      syslog(LOG_ERR, "ERROR: imxrt_adc_initialize() failed: %d\n", ret);
+    }
+#endif
+
 #if defined(CONFIG_USBDEV_DMAMEMORY) || defined(CONFIG_FAT_DMAMEMORY)
   ret = imxrt_dma_alloc_init();
   if (ret < 0)

Reply via email to