Skip to content

Commit 7a5da68

Browse files
committed
cpu/esp32: replace deprecated ADC peripheral driver
1 parent cb23469 commit 7a5da68

File tree

10 files changed

+330
-155
lines changed

10 files changed

+330
-155
lines changed

cpu/esp32/doc_esp32c3.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ total:
8080
The maximum number of ADC channels #ADC_NUMOF_MAX is 6.
8181

8282
@note
83-
- ADC2 is also used by the WiFi module. The GPIOs connected to ADC2 are
84-
therefore not available as ADC channels if the modules `esp_wifi` or
83+
- According to the ESP32-C2 Errata Sheet, ADC2 with GPIO5 as ADC channel may
84+
not work correctly. By default it is still possible to use it anyway.
85+
Set `CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3` to 0 if you do not want to use
86+
it and want to activate the configuration check for this channel.
87+
- ADC2 is also used by the WiFi module. GPIO5 connected to ADC2 is
88+
therefore not available as ADC channels if the modules `esp_wifi*` or
8589
`esp_now` are used.
8690
- Vref can be read with function #adc_line_vref_to_gpio at GPIO5.
8791

cpu/esp32/esp-idf/adc/Makefile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ MODULE = esp_idf_adc
22

33
# source files to be compiled for this module
44
ESP32_SDK_SRC = \
5-
components/driver/deprecated/adc_legacy.c \
5+
components/esp_adc/adc_cali.c \
6+
components/esp_adc/adc_oneshot.c \
67
components/esp_hw_support/adc_share_hw_ctrl.c \
78
components/hal/adc_hal_common.c \
9+
components/hal/adc_oneshot_hal.c \
810
components/hal/rtc_io_hal.c \
911
components/soc/$(CPU_FAM)/adc_periph.c \
1012
#
@@ -17,11 +19,21 @@ ifeq (esp32s2,$(CPU_FAM))
1719
ESP32_SDK_SRC += components/efuse/esp32s2/esp_efuse_rtc_table.c
1820
endif
1921

22+
ifneq (,$(filter esp32 esp32s2,$(CPU_FAM)))
23+
ESP32_SDK_SRC += components/esp_adc/$(CPU_FAM)/adc_cali_line_fitting.c
24+
else
25+
ESP32_SDK_SRC += components/esp_adc/adc_cali_curve_fitting.c
26+
endif
27+
28+
ifneq (,$(filter esp32h2 esp32s3 esp32c3 esp32c6,$(CPU_FAM)))
29+
ESP32_SDK_SRC += components/esp_adc/$(CPU_FAM)/curve_fitting_coefficients.c
30+
endif
31+
2032
include $(RIOTBASE)/Makefile.base
2133

2234
ESP32_SDK_BIN = $(BINDIR)/$(MODULE)
2335

24-
INCLUDES += -I$(ESP32_SDK_DIR)/components/driver/deprecated
36+
INCLUDES += -I$(ESP32_SDK_DIR)/components/esp_adc/interface
2537

2638
include ../esp_idf.mk
2739
include ../esp_idf_cflags.mk

cpu/esp32/include/adc_arch.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ typedef enum {
5959
#define ADC_ATTENUATION_11_DB ADC_ATTENUATION_12_DB
6060

6161
/**
62-
* @brief Set the attenuation for the ADC line. Default attenuation is 11 dB.
62+
* @brief Set the attenuation for the ADC line. Default attenuation is 12 dB.
6363
*
6464
* For each ADC line, an attenuation of the input signal can be defined
6565
* separately. This results in different full ranges of the measurable voltage
6666
* at the input. The attenuation can be set to 0 dB, 3 dB, 6 dB and 12 dB,
67-
* with 11 dB being the standard attenuation. Since an ADC input is measured
67+
* with 12 dB being the standard attenuation. Since an ADC input is measured
6868
* against a reference voltage Vref of 1.1 V, approximately the following
6969
* measurement ranges are given when using a corresponding attenuation:
7070
*
@@ -80,33 +80,51 @@ typedef enum {
8080
* </center>
8181
*
8282
* @note: The reference voltage Vref can vary from ADC unit to ADC unit in
83-
* the range of 1.0V and 1.2V. The Vref of a unit can be routed with
84-
* function *adc_vref_to_gpio* to a GPIO pin.
83+
* the range of 1.0V and 1.2V. Use function adc_get_vrange to get the voltage
84+
* range of samples for the configured attenuation.
8585
*
8686
* @param line ADC line for which the attenuation is set
87-
* @param atten Attenuation, see type definition of *adc_attenuation_t
87+
* @param atten Attenuation, see type definition of adc_attenuation_t
8888
* @return 0 on success
8989
* @return -1 on error
9090
*/
9191
int adc_set_attenuation(adc_t line, adc_atten_t atten);
9292

93+
/**
94+
* @brief Get the voltage range of samples for configured attenution
95+
*
96+
* After initializing an ADC line or reconfiguring the attenuation of an ADC
97+
* line with adc_set_attenuation, the adc_get_vrange can be used to determine
98+
* the voltage range in which the raw sample values will lie. You can use these
99+
* min and max values to calculate the voltage from the raw sample values.
100+
*
101+
* @param line ADC line
102+
* @param min voltage for a raw sample value of 0
103+
* @param max voltage for the maximum raw sample
104+
*/
105+
void adc_get_vrange(adc_t line, int *min, int *max);
106+
93107
/**
94108
* @brief Output reference voltage of a ADC line to GPIO n
95109
*
96110
* The Vref of the ADC unit of the given ADC line is routed to a GPIO pin n.
97111
* This allows to measure the Vref used by the ADC unit to adjusted the
98112
* results of the conversions accordingly.
99113
*
114+
* @deprecated The function is not supported any longer, use adc_get_vref to get
115+
* the reference voltage for the configured attenutation.
116+
*
100117
* @note
101118
* - The given GPIO must be a valid ADC channel of ADC2 unit.
102-
* - For ESP32 and ESP32C3, the given ADC line has to be a channel of ADC2 unit.
119+
* - For ESP32, only the internal reference voltage of ADC2 is given.
103120
*
104121
* @param line ADC line for which Vref of its ADC unit is routed to the GPIO
105122
* @param gpio GPIO to which Vref is routed (ADC2 channel GPIOs only)
106123
*
107124
* @return 0 on success
108125
* @return -1 on error
109126
*/
127+
__attribute__((__deprecated__))
110128
int adc_line_vref_to_gpio(adc_t line, gpio_t gpio);
111129

112130
#if defined(CPU_FAM_ESP32)

cpu/esp32/include/adc_arch_private.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ extern "C" {
3333
#define RTCIO_GPIO(n) n /* n-th RTCIO GPIO */
3434
#define RTCIO_NA UINT8_MAX /* RTCIO pin not available */
3535

36-
#define ADC_UNIT_MAX ((adc_unit_t)255) /* Invalid ADC unit ID */
36+
#define ADC_UNIT_INV ((adc_unit_t)255) /* Invalid ADC unit ID */
37+
#define ADC_CHANNEL_INV SOC_ADC_MAX_CHANNEL_NUM /* Invalid ADC channel ID */
3738

38-
#define ADC_CHANNEL_MAX SOC_ADC_MAX_CHANNEL_NUM /* Invalid ADC channel ID */
3939
#define ADC1_CHANNEL_MAX (SOC_ADC_CHANNEL_NUM(ADC_UNIT_1)) /* Number of channels of ADC1 */
4040
#define ADC2_CHANNEL_MAX (SOC_ADC_CHANNEL_NUM(ADC_UNIT_2)) /* Number of channels of ADC2 */
41+
#define ADC_CHANNEL_MAX(u) ((u == ADC_UNIT_1) ? ADC1_CHANNEL_MAX : ADC2_CHANNEL_MAX)
4142

4243
/**
4344
* @brief ADC hardware descriptor (for internal use only)

cpu/esp32/include/sdkconfig_esp32c3.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ extern "C" {
155155
#define CONFIG_BT_NIMBLE_COEX_PHY_CODED_TX_RX_TLIM_DIS 1
156156
#endif
157157

158+
/* According to the ESP32-C3 Errata Sheet ADC2 does not work correctly.
159+
* To use ADC2 and GPIO5 as ADC channel, CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3
160+
* has to be set (default). */
161+
#ifndef CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3
162+
#define CONFIG_ADC_ONESHOT_FORCE_USE_ADC2_ON_C3 1
163+
#endif
164+
158165
#ifdef __cplusplus
159166
}
160167
#endif

0 commit comments

Comments
 (0)