Re: [PR] Add ADC support on ESP32|S2|S3|C3|C6|H2 [nuttx]

2025-04-16 Thread via GitHub


xiaoxiang781216 merged PR #16228:
URL: https://github.com/apache/nuttx/pull/16228


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] Add ADC support on ESP32|S2|S3|C3|C6|H2 [nuttx]

2025-04-16 Thread via GitHub


acassis commented on code in PR #16228:
URL: https://github.com/apache/nuttx/pull/16228#discussion_r2047110439


##
arch/xtensa/src/common/espressif/esp_adc.c:
##
@@ -0,0 +1,844 @@
+/
+ * arch/xtensa/src/common/espressif/esp_adc.c
+ *
+ * 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include "esp_adc.h"
+
+#include "adc_cali_interface.h"
+#include "esp_adc/adc_cali_scheme.h"
+#include "esp_private/adc_share_hw_ctrl.h"
+#include "esp_private/esp_sleep_internal.h"
+#include "esp_private/periph_ctrl.h"
+#include "esp_private/sar_periph_ctrl.h"
+#include "hal/adc_types.h"
+#include "hal/adc_oneshot_hal.h"
+#include "hal/adc_ll.h"
+#include "hal/sar_ctrl_ll.h"
+#include "soc/adc_periph.h"
+#include "soc/periph_defs.h"
+#include "esp_clk_tree.h"
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#include "esp32_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S2)
+#include "esp32s2_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S3)
+#include "esp32s3_gpio.h"
+#endif
+
+/
+ * Pre-processor Definitions
+ /
+
+#ifdef CONFIG_ESPRESSIF_ADC_1_MODE_CONTINUOUS
+#  error "Continuous mode not implemented"
+#endif
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#  define esp_configgpio   esp32_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_3
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S2
+#  define esp_configgpio   esp32s2_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S3
+#  define esp_configgpio   esp32s3_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+
+#define ESP_ADC_BITWIDTH_DEFAULT ADC_BITWIDTH_DEFAULT
+
+/* Default internal reference voltage */
+
+#define ADC_ESP32_DEFAULT_VREF_INTERNAL 1100
+
+#define ADC_GET_IO_NUM(unit, channel) (adc_channel_io_map[unit][channel])
+#define COUNT_NON_ZERO(arr, len) ({  \
+size_t _count = 0;   \
+for (size_t _i = 0; _i < (len); _i++) \
+if ((arr)[_i] != 0) _count++; \
+_count;   \
+})
+
+/
+ * Private Types
+ /
+
+enum esp_adc_mode_e
+{
+  ESP_ADC_MODE_ONE_SHOT = 0,
+  ESP_ADC_MODE_CONTINUOUS,
+};
+
+struct esp_adc_dev_common_s
+{
+  spinlock_t esp_adc_spinlock;
+  bool initialized; /* ADC peripheral initialized */
+};
+
+struct esp_adc_oneshot_ch_s
+{
+  uint8_t channel; /* Channel number */
+  adc_cali_handle_t  cali_handle;  /* Handle for calibration */
+  bool calibrated; /* Channel has been calibrated */
+};
+
+/* One-shot ADC device struct */
+
+struct esp_adc_oneshot_s
+{
+  adc_oneshot_hal_ctx_t hal;   /* ADC unit low-level context */
+  struct esp_adc_oneshot_ch_s ch_list[SOC_ADC_MAX_CHANNEL_NUM];
+};
+
+/* Continuous ADC device struct */
+
+struct esp_adc_continuous_s
+{
+};
+
+/* Generic ADC unit struct to be used by one-shot or continuous mode */
+
+struct esp_adc_dev_s
+{
+  struct adc_dev_s*upper_dev;  /* Upper-half ADC reference */
+  const struct adc_callback_s *cb; /* Upper driver callback */
+
+  struct esp_adc_dev_common_s *common; /* Common ADC driver data */
+
+  enum esp_adc_mode_e mode;  /* ADC mode */
+  adc_atten_t atten_mode;/* Attenuation paramenter */
+  uint32_t atten_k;  /* Attenuation factor */
+  uint8_t channels;  /* Total channels for this ADC */
+  uint8_t unit;  /* ADC unit number */
+  boolinitialized;   /* ADC unit initialized */
+
+  union
+{
+  struct esp_adc_oneshot_s os_dev;
+  struct esp_adc_continuous_s cnt_dev;
+};
+};
+
+/**

Re: [PR] Add ADC support on ESP32|S2|S3|C3|C6|H2 [nuttx]

2025-04-16 Thread via GitHub


fdcavalcanti commented on code in PR #16228:
URL: https://github.com/apache/nuttx/pull/16228#discussion_r2047073557


##
arch/xtensa/src/common/espressif/esp_adc.c:
##
@@ -0,0 +1,844 @@
+/
+ * arch/xtensa/src/common/espressif/esp_adc.c
+ *
+ * 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include "esp_adc.h"
+
+#include "adc_cali_interface.h"
+#include "esp_adc/adc_cali_scheme.h"
+#include "esp_private/adc_share_hw_ctrl.h"
+#include "esp_private/esp_sleep_internal.h"
+#include "esp_private/periph_ctrl.h"
+#include "esp_private/sar_periph_ctrl.h"
+#include "hal/adc_types.h"
+#include "hal/adc_oneshot_hal.h"
+#include "hal/adc_ll.h"
+#include "hal/sar_ctrl_ll.h"
+#include "soc/adc_periph.h"
+#include "soc/periph_defs.h"
+#include "esp_clk_tree.h"
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#include "esp32_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S2)
+#include "esp32s2_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S3)
+#include "esp32s3_gpio.h"
+#endif
+
+/
+ * Pre-processor Definitions
+ /
+
+#ifdef CONFIG_ESPRESSIF_ADC_1_MODE_CONTINUOUS
+#  error "Continuous mode not implemented"
+#endif
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#  define esp_configgpio   esp32_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_3
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S2
+#  define esp_configgpio   esp32s2_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S3
+#  define esp_configgpio   esp32s3_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+
+#define ESP_ADC_BITWIDTH_DEFAULT ADC_BITWIDTH_DEFAULT
+
+/* Default internal reference voltage */
+
+#define ADC_ESP32_DEFAULT_VREF_INTERNAL 1100
+
+#define ADC_GET_IO_NUM(unit, channel) (adc_channel_io_map[unit][channel])
+#define COUNT_NON_ZERO(arr, len) ({  \
+size_t _count = 0;   \
+for (size_t _i = 0; _i < (len); _i++) \
+if ((arr)[_i] != 0) _count++; \
+_count;   \
+})
+
+/
+ * Private Types
+ /
+
+enum esp_adc_mode_e
+{
+  ESP_ADC_MODE_ONE_SHOT = 0,
+  ESP_ADC_MODE_CONTINUOUS,
+};
+
+struct esp_adc_dev_common_s
+{
+  spinlock_t esp_adc_spinlock;
+  bool initialized; /* ADC peripheral initialized */
+};
+
+struct esp_adc_oneshot_ch_s
+{
+  uint8_t channel; /* Channel number */
+  adc_cali_handle_t  cali_handle;  /* Handle for calibration */
+  bool calibrated; /* Channel has been calibrated */
+};
+
+/* One-shot ADC device struct */
+
+struct esp_adc_oneshot_s
+{
+  adc_oneshot_hal_ctx_t hal;   /* ADC unit low-level context */
+  struct esp_adc_oneshot_ch_s ch_list[SOC_ADC_MAX_CHANNEL_NUM];
+};
+
+/* Continuous ADC device struct */
+
+struct esp_adc_continuous_s
+{
+};
+
+/* Generic ADC unit struct to be used by one-shot or continuous mode */
+
+struct esp_adc_dev_s
+{
+  struct adc_dev_s*upper_dev;  /* Upper-half ADC reference */
+  const struct adc_callback_s *cb; /* Upper driver callback */
+
+  struct esp_adc_dev_common_s *common; /* Common ADC driver data */
+
+  enum esp_adc_mode_e mode;  /* ADC mode */
+  adc_atten_t atten_mode;/* Attenuation paramenter */
+  uint32_t atten_k;  /* Attenuation factor */
+  uint8_t channels;  /* Total channels for this ADC */
+  uint8_t unit;  /* ADC unit number */
+  boolinitialized;   /* ADC unit initialized */
+
+  union
+{
+  struct esp_adc_oneshot_s os_dev;
+  struct esp_adc_continuous_s cnt_dev;
+};
+};
+
+/*

Re: [PR] Add ADC support on ESP32|S2|S3|C3|C6|H2 [nuttx]

2025-04-16 Thread via GitHub


fdcavalcanti commented on code in PR #16228:
URL: https://github.com/apache/nuttx/pull/16228#discussion_r2047073557


##
arch/xtensa/src/common/espressif/esp_adc.c:
##
@@ -0,0 +1,844 @@
+/
+ * arch/xtensa/src/common/espressif/esp_adc.c
+ *
+ * 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include "esp_adc.h"
+
+#include "adc_cali_interface.h"
+#include "esp_adc/adc_cali_scheme.h"
+#include "esp_private/adc_share_hw_ctrl.h"
+#include "esp_private/esp_sleep_internal.h"
+#include "esp_private/periph_ctrl.h"
+#include "esp_private/sar_periph_ctrl.h"
+#include "hal/adc_types.h"
+#include "hal/adc_oneshot_hal.h"
+#include "hal/adc_ll.h"
+#include "hal/sar_ctrl_ll.h"
+#include "soc/adc_periph.h"
+#include "soc/periph_defs.h"
+#include "esp_clk_tree.h"
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#include "esp32_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S2)
+#include "esp32s2_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S3)
+#include "esp32s3_gpio.h"
+#endif
+
+/
+ * Pre-processor Definitions
+ /
+
+#ifdef CONFIG_ESPRESSIF_ADC_1_MODE_CONTINUOUS
+#  error "Continuous mode not implemented"
+#endif
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#  define esp_configgpio   esp32_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_3
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S2
+#  define esp_configgpio   esp32s2_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S3
+#  define esp_configgpio   esp32s3_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+
+#define ESP_ADC_BITWIDTH_DEFAULT ADC_BITWIDTH_DEFAULT
+
+/* Default internal reference voltage */
+
+#define ADC_ESP32_DEFAULT_VREF_INTERNAL 1100
+
+#define ADC_GET_IO_NUM(unit, channel) (adc_channel_io_map[unit][channel])
+#define COUNT_NON_ZERO(arr, len) ({  \
+size_t _count = 0;   \
+for (size_t _i = 0; _i < (len); _i++) \
+if ((arr)[_i] != 0) _count++; \
+_count;   \
+})
+
+/
+ * Private Types
+ /
+
+enum esp_adc_mode_e
+{
+  ESP_ADC_MODE_ONE_SHOT = 0,
+  ESP_ADC_MODE_CONTINUOUS,
+};
+
+struct esp_adc_dev_common_s
+{
+  spinlock_t esp_adc_spinlock;
+  bool initialized; /* ADC peripheral initialized */
+};
+
+struct esp_adc_oneshot_ch_s
+{
+  uint8_t channel; /* Channel number */
+  adc_cali_handle_t  cali_handle;  /* Handle for calibration */
+  bool calibrated; /* Channel has been calibrated */
+};
+
+/* One-shot ADC device struct */
+
+struct esp_adc_oneshot_s
+{
+  adc_oneshot_hal_ctx_t hal;   /* ADC unit low-level context */
+  struct esp_adc_oneshot_ch_s ch_list[SOC_ADC_MAX_CHANNEL_NUM];
+};
+
+/* Continuous ADC device struct */
+
+struct esp_adc_continuous_s
+{
+};
+
+/* Generic ADC unit struct to be used by one-shot or continuous mode */
+
+struct esp_adc_dev_s
+{
+  struct adc_dev_s*upper_dev;  /* Upper-half ADC reference */
+  const struct adc_callback_s *cb; /* Upper driver callback */
+
+  struct esp_adc_dev_common_s *common; /* Common ADC driver data */
+
+  enum esp_adc_mode_e mode;  /* ADC mode */
+  adc_atten_t atten_mode;/* Attenuation paramenter */
+  uint32_t atten_k;  /* Attenuation factor */
+  uint8_t channels;  /* Total channels for this ADC */
+  uint8_t unit;  /* ADC unit number */
+  boolinitialized;   /* ADC unit initialized */
+
+  union
+{
+  struct esp_adc_oneshot_s os_dev;
+  struct esp_adc_continuous_s cnt_dev;
+};
+};
+
+/*

Re: [PR] Add ADC support on ESP32|S2|S3|C3|C6|H2 [nuttx]

2025-04-16 Thread via GitHub


acassis commented on code in PR #16228:
URL: https://github.com/apache/nuttx/pull/16228#discussion_r2046769819


##
arch/xtensa/src/common/espressif/esp_adc.c:
##
@@ -0,0 +1,844 @@
+/
+ * arch/xtensa/src/common/espressif/esp_adc.c
+ *
+ * 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 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#include "esp_adc.h"
+
+#include "adc_cali_interface.h"
+#include "esp_adc/adc_cali_scheme.h"
+#include "esp_private/adc_share_hw_ctrl.h"
+#include "esp_private/esp_sleep_internal.h"
+#include "esp_private/periph_ctrl.h"
+#include "esp_private/sar_periph_ctrl.h"
+#include "hal/adc_types.h"
+#include "hal/adc_oneshot_hal.h"
+#include "hal/adc_ll.h"
+#include "hal/sar_ctrl_ll.h"
+#include "soc/adc_periph.h"
+#include "soc/periph_defs.h"
+#include "esp_clk_tree.h"
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#include "esp32_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S2)
+#include "esp32s2_gpio.h"
+#elif defined(CONFIG_ARCH_CHIP_ESP32S3)
+#include "esp32s3_gpio.h"
+#endif
+
+/
+ * Pre-processor Definitions
+ /
+
+#ifdef CONFIG_ESPRESSIF_ADC_1_MODE_CONTINUOUS
+#  error "Continuous mode not implemented"
+#endif
+
+#ifdef CONFIG_ARCH_CHIP_ESP32
+#  define esp_configgpio   esp32_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_3
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S2
+#  define esp_configgpio   esp32s2_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+#ifdef CONFIG_ARCH_CHIP_ESP32S3
+#  define esp_configgpio   esp32s3_configgpio
+#  define GPIO_ADC_FUNCTIONFUNCTION_2
+#endif
+
+#define ESP_ADC_BITWIDTH_DEFAULT ADC_BITWIDTH_DEFAULT
+
+/* Default internal reference voltage */
+
+#define ADC_ESP32_DEFAULT_VREF_INTERNAL 1100
+
+#define ADC_GET_IO_NUM(unit, channel) (adc_channel_io_map[unit][channel])
+#define COUNT_NON_ZERO(arr, len) ({  \
+size_t _count = 0;   \
+for (size_t _i = 0; _i < (len); _i++) \
+if ((arr)[_i] != 0) _count++; \
+_count;   \
+})
+
+/
+ * Private Types
+ /
+
+enum esp_adc_mode_e
+{
+  ESP_ADC_MODE_ONE_SHOT = 0,
+  ESP_ADC_MODE_CONTINUOUS,
+};
+
+struct esp_adc_dev_common_s
+{
+  spinlock_t esp_adc_spinlock;
+  bool initialized; /* ADC peripheral initialized */
+};
+
+struct esp_adc_oneshot_ch_s
+{
+  uint8_t channel; /* Channel number */
+  adc_cali_handle_t  cali_handle;  /* Handle for calibration */
+  bool calibrated; /* Channel has been calibrated */
+};
+
+/* One-shot ADC device struct */
+
+struct esp_adc_oneshot_s
+{
+  adc_oneshot_hal_ctx_t hal;   /* ADC unit low-level context */
+  struct esp_adc_oneshot_ch_s ch_list[SOC_ADC_MAX_CHANNEL_NUM];
+};
+
+/* Continuous ADC device struct */
+
+struct esp_adc_continuous_s
+{
+};
+
+/* Generic ADC unit struct to be used by one-shot or continuous mode */
+
+struct esp_adc_dev_s
+{
+  struct adc_dev_s*upper_dev;  /* Upper-half ADC reference */
+  const struct adc_callback_s *cb; /* Upper driver callback */
+
+  struct esp_adc_dev_common_s *common; /* Common ADC driver data */
+
+  enum esp_adc_mode_e mode;  /* ADC mode */
+  adc_atten_t atten_mode;/* Attenuation paramenter */
+  uint32_t atten_k;  /* Attenuation factor */
+  uint8_t channels;  /* Total channels for this ADC */
+  uint8_t unit;  /* ADC unit number */
+  boolinitialized;   /* ADC unit initialized */
+
+  union
+{
+  struct esp_adc_oneshot_s os_dev;
+  struct esp_adc_continuous_s cnt_dev;
+};
+};
+
+/**