From bd92e95160644d843f52b6629256c85e5bd4ae2a Mon Sep 17 00:00:00 2001 From: fuzhibo Date: Thu, 11 Jun 2020 22:05:18 +0800 Subject: [PATCH] driver(adc): add adc-dma code for esp32 --- components/driver/esp32/adc.c | 34 +++- components/driver/esp32s2/adc.c | 2 +- .../driver/esp32s2/include/driver/adc.h | 23 --- components/driver/include/driver/adc_common.h | 77 +++++-- components/soc/include/hal/adc_hal.h | 16 ++ components/soc/include/hal/adc_types.h | 188 ++++++++++-------- .../soc/soc/esp32/include/soc/adc_caps.h | 4 +- components/soc/src/esp32/adc_hal.c | 6 +- .../soc/src/esp32/include/hal/adc_hal.h | 33 --- components/soc/src/esp32/include/hal/adc_ll.h | 35 +--- .../soc/src/esp32s2/include/hal/adc_hal.h | 17 -- 11 files changed, 211 insertions(+), 224 deletions(-) diff --git a/components/driver/esp32/adc.c b/components/driver/esp32/adc.c index cdd4448c6c..351601968e 100644 --- a/components/driver/esp32/adc.c +++ b/components/driver/esp32/adc.c @@ -40,7 +40,6 @@ #define ADC_MAX_MEAS_NUM_DEFAULT (255) #define ADC_MEAS_NUM_LIM_DEFAULT (1) -#define SAR_ADC_CLK_DIV_DEFUALT (2) #define DIG_ADC_OUTPUT_FORMAT_DEFUALT (ADC_DIGI_FORMAT_12BIT) #define DIG_ADC_ATTEN_DEFUALT (ADC_ATTEN_DB_11) @@ -94,14 +93,13 @@ esp_err_t adc_i2s_mode_init(adc_unit_t adc_unit, adc_channel_t channel) ADC_CHANNEL_CHECK(ADC_NUM_2, channel); } - adc_hal_digi_pattern_table_t adc1_pattern[1]; - adc_hal_digi_pattern_table_t adc2_pattern[1]; - adc_hal_digi_config_t dig_cfg = { + adc_digi_pattern_table_t adc1_pattern[1]; + adc_digi_pattern_table_t adc2_pattern[1]; + adc_digi_config_t dig_cfg = { .conv_limit_en = ADC_MEAS_NUM_LIM_DEFAULT, .conv_limit_num = ADC_MAX_MEAS_NUM_DEFAULT, - .clk_div = SAR_ADC_CLK_DIV_DEFUALT, .format = DIG_ADC_OUTPUT_FORMAT_DEFUALT, - .conv_mode = (adc_hal_digi_convert_mode_t)adc_unit, + .conv_mode = (adc_digi_convert_mode_t)adc_unit, }; if (adc_unit & ADC_UNIT_1) { @@ -127,6 +125,30 @@ esp_err_t adc_i2s_mode_init(adc_unit_t adc_unit, adc_channel_t channel) return ESP_OK; } +esp_err_t adc_digi_init(void) +{ + ADC_ENTER_CRITICAL(); + adc_hal_digi_init(); + ADC_EXIT_CRITICAL(); + return ESP_OK; +} + +esp_err_t adc_digi_deinit(void) +{ + ADC_ENTER_CRITICAL(); + adc_hal_digi_deinit(); + ADC_EXIT_CRITICAL(); + return ESP_OK; +} + +esp_err_t adc_digi_controller_config(const adc_digi_config_t *config) +{ + ADC_ENTER_CRITICAL(); + adc_hal_digi_controller_config(config); + ADC_EXIT_CRITICAL(); + return ESP_OK; +} + /*--------------------------------------------------------------- RTC controller setting ---------------------------------------------------------------*/ diff --git a/components/driver/esp32s2/adc.c b/components/driver/esp32s2/adc.c index 937785248f..7eaecace38 100644 --- a/components/driver/esp32s2/adc.c +++ b/components/driver/esp32s2/adc.c @@ -72,7 +72,7 @@ esp_err_t adc_digi_init(void) esp_err_t adc_digi_deinit(void) { ADC_ENTER_CRITICAL(); - adc_hal_digi_init(); + adc_hal_digi_deinit(); ADC_EXIT_CRITICAL(); return ESP_OK; } diff --git a/components/driver/esp32s2/include/driver/adc.h b/components/driver/esp32s2/include/driver/adc.h index 65bda25c27..10c201e60b 100644 --- a/components/driver/esp32s2/include/driver/adc.h +++ b/components/driver/esp32s2/include/driver/adc.h @@ -44,29 +44,6 @@ esp_err_t adc_arbiter_config(adc_unit_t adc_unit, adc_arbiter_t *config); /*--------------------------------------------------------------- Digital controller setting ---------------------------------------------------------------*/ -/** - * @brief ADC digital controller initialization. - * @return - * - ESP_OK Success - */ -esp_err_t adc_digi_init(void); - -/** - * @brief ADC digital controller deinitialization. - * @return - * - ESP_OK Success - */ -esp_err_t adc_digi_deinit(void); - -/** - * @brief Setting the digital controller. - * - * @param config Pointer to digital controller paramter. Refer to `adc_digi_config_t`. - * - * @return - * - ESP_OK Success - */ -esp_err_t adc_digi_controller_config(const adc_digi_config_t *config); /** * @brief Enable digital controller to trigger the measurement. diff --git a/components/driver/include/driver/adc_common.h b/components/driver/include/driver/adc_common.h index 2c426b71d6..c8c7f0cbab 100644 --- a/components/driver/include/driver/adc_common.h +++ b/components/driver/include/driver/adc_common.h @@ -84,6 +84,35 @@ typedef enum { ADC_ENCODE_MAX, } adc_i2s_encode_t; +/*--------------------------------------------------------------- + Common setting +---------------------------------------------------------------*/ + +/** + * @brief Enable ADC power + */ +void adc_power_on(void); + +/** + * @brief Power off SAR ADC + * This function will force power down for ADC + */ +void adc_power_off(void); + +/** + * @brief Initialize ADC pad + * @param adc_unit ADC unit index + * @param channel ADC channel index + * @return + * - ESP_OK success + * - ESP_ERR_INVALID_ARG Parameter error + */ +esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel); + +/*--------------------------------------------------------------- + RTC controller setting +---------------------------------------------------------------*/ + /** * @brief Get the GPIO number of a specific ADC1 channel. * @@ -173,27 +202,6 @@ esp_err_t adc1_config_width(adc_bits_width_t width_bit); */ int adc1_get_raw(adc1_channel_t channel); -/** - * @brief Enable ADC power - */ -void adc_power_on(void); - -/** - * @brief Power off SAR ADC - * This function will force power down for ADC - */ -void adc_power_off(void); - -/** - * @brief Initialize ADC pad - * @param adc_unit ADC unit index - * @param channel ADC channel index - * @return - * - ESP_OK success - * - ESP_ERR_INVALID_ARG Parameter error - */ -esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel); - /** * @brief Set ADC data invert * @param adc_unit ADC unit index @@ -345,6 +353,33 @@ esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio); * - ESP_ERR_INVALID_ARG: Unsupported GPIO */ esp_err_t adc2_vref_to_gpio(gpio_num_t gpio) __attribute__((deprecated)); +/*--------------------------------------------------------------- + Digital controller setting +---------------------------------------------------------------*/ + +/** + * @brief ADC digital controller initialization. + * @return + * - ESP_OK Success + */ +esp_err_t adc_digi_init(void); + +/** + * @brief ADC digital controller deinitialization. + * @return + * - ESP_OK Success + */ +esp_err_t adc_digi_deinit(void); + +/** + * @brief Setting the digital controller. + * + * @param config Pointer to digital controller paramter. Refer to `adc_digi_config_t`. + * + * @return + * - ESP_OK Success + */ +esp_err_t adc_digi_controller_config(const adc_digi_config_t *config); #ifdef __cplusplus } diff --git a/components/soc/include/hal/adc_hal.h b/components/soc/include/hal/adc_hal.h index e497149e61..3a0a5a5c55 100644 --- a/components/soc/include/hal/adc_hal.h +++ b/components/soc/include/hal/adc_hal.h @@ -185,6 +185,22 @@ int adc_hal_convert(adc_ll_num_t adc_n, int channel, int *value); /*--------------------------------------------------------------- Digital controller setting ---------------------------------------------------------------*/ +/** + * Digital controller initialization. + */ +void adc_hal_digi_init(void); + +/** + * Digital controller deinitialization. + */ +void adc_hal_digi_deinit(void); + +/** + * Setting the digital controller. + * + * @param cfg Pointer to digital controller paramter. + */ +void adc_hal_digi_controller_config(const adc_digi_config_t *cfg); /** * Reset the pattern table pointer, then take the measurement rule from table header in next measurement. diff --git a/components/soc/include/hal/adc_types.h b/components/soc/include/hal/adc_types.h index 5f3d5a134c..ae6283c73b 100644 --- a/components/soc/include/hal/adc_types.h +++ b/components/soc/include/hal/adc_types.h @@ -89,6 +89,49 @@ typedef enum { ADC_WIDTH_MAX, } adc_bits_width_t; +/** + * @brief ADC digital controller (DMA mode) work mode. + * + * @note The conversion mode affects the sampling frequency: + * SINGLE_UNIT_1: When the measurement is triggered, only ADC1 is sampled once. + * SINGLE_UNIT_2: When the measurement is triggered, only ADC2 is sampled once. + * BOTH_UNIT : When the measurement is triggered, ADC1 and ADC2 are sampled at the same time. + * ALTER_UNIT : When the measurement is triggered, ADC1 or ADC2 samples alternately. + */ +typedef enum { + ADC_CONV_SINGLE_UNIT_1 = 1, /*!< SAR ADC 1. */ + ADC_CONV_SINGLE_UNIT_2 = 2, /*!< SAR ADC 2. */ + ADC_CONV_BOTH_UNIT = 3, /*!< SAR ADC 1 and 2. */ + ADC_CONV_ALTER_UNIT = 7, /*!< SAR ADC 1 and 2 alternative mode. */ + ADC_CONV_UNIT_MAX, +} adc_digi_convert_mode_t; + +/** + * @brief ADC digital controller (DMA mode) conversion rules setting. + */ +typedef struct { + union { + struct { + uint8_t atten: 2; /*!< ADC sampling voltage attenuation configuration. + 0: input voltage * 1; + 1: input voltage * 1/1.34; + 2: input voltage * 1/2; + 3: input voltage * 1/3.6. */ +#ifdef CONFIG_IDF_TARGET_ESP32 + uint8_t bit_width: 2; /*!< ADC resolution. +- 0: 9 bit; +- 1: 10 bit; +- 2: 11 bit; +- 3: 12 bit. */ +#elif CONFIG_IDF_TARGET_ESP32S2 + uint8_t reserved: 2; /*!< reserved0 */ +#endif + uint8_t channel: 4; /*!< ADC channel index. */ + }; + uint8_t val; + }; +} adc_digi_pattern_table_t; + /** * @brief ADC digital controller (DMA mode) output data format option. */ @@ -141,6 +184,64 @@ typedef struct { uint32_t div_a; /*!clk_div); /* Single channel mode or multi channel mode. */ adc_ll_digi_set_convert_mode(cfg->conv_mode); if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) { diff --git a/components/soc/src/esp32/include/hal/adc_hal.h b/components/soc/src/esp32/include/hal/adc_hal.h index 1bd84bd217..4755054408 100644 --- a/components/soc/src/esp32/include/hal/adc_hal.h +++ b/components/soc/src/esp32/include/hal/adc_hal.h @@ -31,22 +31,6 @@ extern "C" { #endif -typedef struct { - bool conv_limit_en; /*!