Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Esp32AdcOneShot.hxx
Go to the documentation of this file.
1
35#ifndef _DRIVERS_ESP32ADCONESHOT_HXX_
36#define _DRIVERS_ESP32ADCONESHOT_HXX_
37
38#include "utils/logging.h"
39
40#if defined(__has_include)
41#if __has_include(<driver/adc_types_legacy.h>)
42// include legacy types so ADC1_CHANNEL_0 and ADC1_CHANNEL_0_GPIO_NUM etc are
43// defined, soc/adc_channel.h includes references to these on release/v5.0
44// branch and is planned for update in later relase versions.
45#include <driver/adc_types_legacy.h>
46#endif // __has_include driver/adc_types_legacy.h
47#endif // has_include
48
49#include <esp_adc/adc_oneshot.h>
50#include <soc/adc_channel.h>
51
55template <class Defs> struct Esp32ADCInput : public Defs
56{
57public:
58 using Defs::ATTEN;
59 using Defs::BITS;
60 using Defs::CHANNEL;
61 using Defs::PIN;
62 using Defs::HANDLE;
63
64 static void hw_init()
65 {
66 // due to using #if/#elif/#endif it is not possible to include this in
67 // the ADC_PIN wrapper code.
68 const adc_oneshot_unit_init_cfg_t unit_config =
69 {
70#if CONFIG_IDF_TARGET_ESP32
71 .unit_id = PIN >= 30 ? ADC_UNIT_1 : ADC_UNIT_2,
72#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
73 .unit_id = PIN <= 10 ? ADC_UNIT_1 : ADC_UNIT_2,
74#elif CONFIG_IDF_TARGET_ESP32C3
75 .unit_id = PIN <= 4 ? ADC_UNIT_1 : ADC_UNIT_2,
76#endif
77 .ulp_mode = ADC_ULP_MODE_DISABLE,
78 };
79 const adc_oneshot_chan_cfg_t channel_config =
80 {
81 .atten = ATTEN,
82 .bitwidth = BITS,
83 };
84
86 "[Esp32ADCInput] Configuring ADC%d:%d input pin %d, "
87 "attenuation %d, bits %d",
88 unit_config.unit_id, CHANNEL, PIN, ATTEN, BITS);
89 ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_config, &HANDLE));
90 ESP_ERROR_CHECK(
91 adc_oneshot_config_channel(HANDLE, CHANNEL, &channel_config));
92 }
93
95 static void hw_set_to_safe()
96 {
97 // NO-OP
98 }
99
101 static void set(bool value)
102 {
103 // NO-OP
104 }
105
106 static int sample()
107 {
108 int value = 0;
109 ESP_ERROR_CHECK(adc_oneshot_read(HANDLE, CHANNEL, &value));
110 return value;
111 }
112};
113
196#define ADC_PIN(NAME, ADC_CHANNEL, ATTENUATION, BIT_RANGE) \
197 struct NAME##Defs \
198 { \
199 static const adc_channel_t CHANNEL = (adc_channel_t)ADC_CHANNEL; \
200 static const gpio_num_t PIN = (gpio_num_t)ADC_CHANNEL##_GPIO_NUM; \
201 static const adc_atten_t ATTEN = (adc_atten_t)ATTENUATION; \
202 static const adc_bitwidth_t BITS = (adc_bitwidth_t)BIT_RANGE; \
203 static adc_oneshot_unit_handle_t HANDLE; \
204 public: \
205 static const gpio_num_t pin() \
206 { \
207 return PIN; \
208 } \
209 static const adc_channel_t channel() \
210 { \
211 return CHANNEL; \
212 } \
213 }; \
214 adc_oneshot_unit_handle_t NAME##Defs::HANDLE; \
215 typedef Esp32ADCInput<NAME##Defs> NAME##_Pin
216
217#endif // _DRIVERS_ESP32ADCONESHOT_HXX_
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int VERBOSE
Loglevel that is usually not printed, reporting debugging information.
Definition logging.h:59
Defines an ADC input pin.
static void hw_set_to_safe()
NO-OP.
static void set(bool value)
NO-OP.