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 <nuttx/config.h> + +#include <inttypes.h> +#include <stdint.h> +#include <sys/param.h> +#include <assert.h> +#include <debug.h> + +#include <nuttx/analog/adc.h> +#include <nuttx/spinlock.h> + +#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_FUNCTION FUNCTION_3 +#endif +#ifdef CONFIG_ARCH_CHIP_ESP32S2 +# define esp_configgpio esp32s2_configgpio +# define GPIO_ADC_FUNCTION FUNCTION_2 +#endif +#ifdef CONFIG_ARCH_CHIP_ESP32S3 +# define esp_configgpio esp32s3_configgpio +# define GPIO_ADC_FUNCTION FUNCTION_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 */ + bool initialized; /* ADC unit initialized */ + + union + { + struct esp_adc_oneshot_s os_dev; + struct esp_adc_continuous_s cnt_dev; + }; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void esp_adc_reset(struct adc_dev_s *dev); +static void esp_adc_shutdown(struct adc_dev_s *dev); +static void esp_adc_rxint(struct adc_dev_s *dev, bool enable); +static int esp_adc_setup(struct adc_dev_s *dev); +static int esp_adc_ioctl(struct adc_dev_s *dev, int cmd, + unsigned long arg); +static int esp_adc_bind(struct adc_dev_s *dev, + const struct adc_callback_s *callback); + +static int esp_adc_oneshot_read(struct adc_dev_s *dev); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct adc_ops_s g_adcops = +{ + .ao_bind = esp_adc_bind, + .ao_reset = esp_adc_reset, + .ao_setup = esp_adc_setup, + .ao_shutdown = esp_adc_shutdown, + .ao_rxint = esp_adc_rxint, + .ao_ioctl = esp_adc_ioctl, +}; + +static struct esp_adc_dev_common_s g_adc_common = +{ + .esp_adc_spinlock = SP_UNLOCKED, +}; + +#ifdef CONFIG_ESPRESSIF_ADC_1 +static struct esp_adc_dev_s g_adcpriv1 = +{ + .common = &g_adc_common, + .initialized = false, + .unit = ADC_UNIT_1, + .atten_mode = CONFIG_ESPRESSIF_ADC_1_ATTENUATION, +#ifdef CONFIG_ESPRESSIF_ADC_1_MODE_ONE_SHOT + .mode = ESP_ADC_MODE_ONE_SHOT, +#else + .mode = ESP_ADC_MODE_CONTINUOUS, +#endif +}; + +static struct adc_dev_s g_adcdev1 = +{ + .ad_ops = &g_adcops, + .ad_priv = &g_adcpriv1, +}; +#endif /* CONFIG_ESPRESSIF_ADC_1 */ + +#ifdef CONFIG_ESPRESSIF_ADC_2 +static struct esp_adc_dev_s g_adcpriv2 = +{ + .common = &g_adc_common, + .initialized = false, + .unit = ADC_UNIT_2, + .atten_mode = CONFIG_ESPRESSIF_ADC_2_ATTENUATION, +#ifdef CONFIG_ESPRESSIF_ADC_2_MODE_ONE_SHOT + .mode = ESP_ADC_MODE_ONE_SHOT, +#else + .mode = ESP_ADC_MODE_CONTINUOUS, +#endif +}; + +static struct adc_dev_s g_adcdev2 = +{ + .ad_ops = &g_adcops, + .ad_priv = &g_adcpriv2, +}; +#endif /* CONFIG_ESPRESSIF_ADC_2 */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_adc_bind + * + * Description: + * This function binds the upper-half driver callback to the ADC device. + * + * Input Parameters: + * dev - Pointer to the ADC device structure. + * callback - Pointer to the upper-half driver callback structure. + * + * Returned Value: + * Returns OK on successful binding. + * + ****************************************************************************/ + +static int esp_adc_bind(struct adc_dev_s *dev, + const struct adc_callback_s *callback) +{ + struct esp_adc_dev_s *priv = (struct esp_adc_dev_s *)dev->ad_priv; + + DEBUGASSERT(priv); + + priv->cb = callback; + + return OK; +} + +/**************************************************************************** + * Name: esp_adc_reset + * + * Description: + * This function resets the ADC device. + * + * Input Parameters: + * dev - Pointer to the ADC device structure. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static void esp_adc_reset(struct adc_dev_s *dev) +{ + struct esp_adc_dev_s *priv = (struct esp_adc_dev_s *)dev->ad_priv; + + DEBUGASSERT(priv); +} Review Comment: Why what? Nothing inside? Future use could benefit, right now I didn't see a clear use case. What do you think? -- 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: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org