libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::ESP32ADC Class Reference

Classes

class  Channel
 

Public Types

enum class  ContinuousInitResult : uint8_t { STARTED = 0 , UNSUPPORTED , FAILED }
 

Public Member Functions

 ESP32ADC (adc_unit_t unit, const adc_channel_t *channels, uint8_t num_channels, uint32_t freq=SOC_ADC_SAMPLE_FREQ_THRES_LOW, adc_atten_t attenuation=ADC_ATTEN_DB_12, adc_bitwidth_t bitwidth=static_cast< adc_bitwidth_t >(SOC_ADC_DIGI_MAX_BITWIDTH), float reference_voltage=3.3f, size_t dma_buf_size=256)
 
Channel & GetChannel (uint8_t idx)
 
float ReadChannel (uint8_t idx)
 

Detailed Description

Definition at line 22 of file esp_adc.hpp.

Member Enumeration Documentation

◆ ContinuousInitResult

enum class LibXR::ESP32ADC::ContinuousInitResult : uint8_t
strong

Definition at line 41 of file esp_adc.hpp.

42 {
43 STARTED = 0,
44 UNSUPPORTED,
45 FAILED,
46 };
@ FAILED
操作失败 / Operation failed

Constructor & Destructor Documentation

◆ ESP32ADC()

LibXR::ESP32ADC::ESP32ADC ( adc_unit_t unit,
const adc_channel_t * channels,
uint8_t num_channels,
uint32_t freq = SOC_ADC_SAMPLE_FREQ_THRES_LOW,
adc_atten_t attenuation = ADC_ATTEN_DB_12,
adc_bitwidth_t bitwidth = static_cast<adc_bitwidth_t>(SOC_ADC_DIGI_MAX_BITWIDTH),
float reference_voltage = 3.3f,
size_t dma_buf_size = 256 )

Definition at line 52 of file esp_adc.cpp.

55 : unit_(unit),
56 num_channels_(num_channels),
57 attenuation_(attenuation),
58 bitwidth_(ResolveBitwidth(bitwidth)),
59 reference_voltage_(reference_voltage),
60 max_raw_((1U << static_cast<uint8_t>(bitwidth_)) - 1U)
61{
62 ASSERT(channels != nullptr);
63 ASSERT(num_channels_ > 0U);
64 ASSERT(reference_voltage_ > 0.0f);
65 ASSERT(max_raw_ != 0U);
66 ASSERT(num_channels_ <= SOC_ADC_MAX_CHANNEL_NUM);
67 if ((channels == nullptr) || (num_channels_ == 0U) || (reference_voltage_ <= 0.0f) ||
68 (max_raw_ == 0U) || (num_channels_ > SOC_ADC_MAX_CHANNEL_NUM))
69 {
70 return;
71 }
72
73 channels_ = new Channel[num_channels_];
74 channel_ids_ = new adc_channel_t[num_channels_];
75 channel_ready_ = new bool[num_channels_];
76 latest_values_ = new float[num_channels_];
77 latest_raw_ = new uint16_t[num_channels_];
78
79 for (uint8_t i = 0; i < SOC_ADC_MAX_CHANNEL_NUM; ++i)
80 {
81 channel_idx_map_[i] = INVALID_CHANNEL_IDX;
82 cali_handles_[i] = nullptr;
83 }
84
85 for (uint8_t i = 0; i < num_channels_; ++i)
86 {
87 ASSERT(IsValidChannel(channels[i]));
88 const uint8_t ch = static_cast<uint8_t>(channels[i]);
89 ASSERT(channel_idx_map_[ch] == INVALID_CHANNEL_IDX);
90 if (!IsValidChannel(channels[i]) || (channel_idx_map_[ch] != INVALID_CHANNEL_IDX))
91 {
92 return;
93 }
94
95 channels_[i] = Channel(this, i, ch);
96 channel_ids_[i] = channels[i];
97 channel_idx_map_[ch] = i;
98 channel_ready_[i] = false;
99 latest_values_[i] = 0.0f;
100 latest_raw_[i] = 0U;
101 }
102
103#if defined(CONFIG_IDF_TARGET_ESP32) && CONFIG_IDF_TARGET_ESP32 && \
104 defined(CONFIG_ESP_WIFI_ENABLED) && CONFIG_ESP_WIFI_ENABLED
105 if (unit_ == ADC_UNIT_2)
106 {
107 wifi_mode_t wifi_mode = WIFI_MODE_NULL;
108 const esp_err_t wifi_mode_err = esp_wifi_get_mode(&wifi_mode);
109 const bool wifi_active = (wifi_mode_err == ESP_OK) && (wifi_mode != WIFI_MODE_NULL);
110 ASSERT(!wifi_active);
111 if (wifi_active)
112 {
113 return;
114 }
115 }
116#endif
117
118 (void)InitCalibration();
119
120 const ContinuousInitResult cont_ans = InitContinuous(freq, dma_buf_size);
121 if (cont_ans == ContinuousInitResult::STARTED)
122 {
123 valid_ = true;
124 return;
125 }
126
127 if (cont_ans == ContinuousInitResult::FAILED)
128 {
129 REQUIRE(false);
130 return;
131 }
132
133 const bool oneshot_ok = InitOneshot();
134 REQUIRE(oneshot_ok);
135 if (!oneshot_ok)
136 {
137 return;
138 }
139
140 valid_ = true;
141}

Member Function Documentation

◆ GetChannel()

ESP32ADC::Channel & LibXR::ESP32ADC::GetChannel ( uint8_t idx)

Definition at line 143 of file esp_adc.cpp.

144{
145 ASSERT(idx < num_channels_);
146 return channels_[idx];
147}

◆ ReadChannel()

float LibXR::ESP32ADC::ReadChannel ( uint8_t idx)

Definition at line 149 of file esp_adc.cpp.

150{
151 ASSERT(valid_);
152 ASSERT(idx < num_channels_);
153 if (!valid_ || (idx >= num_channels_))
154 {
155 return 0.f;
156 }
157
158 if (backend_ == Backend::CONTINUOUS_DMA)
159 {
160#if SOC_ADC_DIG_CTRL_SUPPORTED && SOC_ADC_DMA_SUPPORTED
161 if (!channel_ready_[idx])
162 {
163 DrainContinuousFrames(continuous_prime_timeout_ms_);
164 }
165 else
166 {
167 DrainContinuousFrames(0U);
168 }
169#endif
170 return latest_values_[idx];
171 }
172
173 DEV_ASSERT(backend_ == Backend::ONESHOT);
174 DEV_ASSERT(oneshot_inited_ && (oneshot_hal_ != nullptr));
175 if (!oneshot_inited_ || (oneshot_hal_ == nullptr))
176 {
177 return 0.f;
178 }
179
180 const esp_err_t lock_err = adc_lock_try_acquire(unit_);
181
182 if (lock_err != ESP_OK)
183 {
184 return 0.f;
185 }
186
187 int raw = 0;
188 bool converted = false;
189 bool clk_src_enabled = false;
190
191 portENTER_CRITICAL(&rtc_spinlock);
192
193#if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
194 const esp_err_t clk_on =
195 esp_clk_tree_enable_src(static_cast<soc_module_clk_t>(oneshot_hal_->clk_src), true);
196
197 clk_src_enabled = (clk_on == ESP_OK);
198#else
199 clk_src_enabled = true;
200#endif
201
202 if (clk_src_enabled)
203 {
204 ANALOG_CLOCK_ENABLE();
205 adc_oneshot_hal_setup(oneshot_hal_, channel_ids_[idx]);
206#if SOC_ADC_CALIBRATION_V1_SUPPORTED
207 adc_set_hw_calibration_code(unit_, attenuation_);
208#endif
209 converted = adc_oneshot_hal_convert(oneshot_hal_, &raw);
210 ANALOG_CLOCK_DISABLE();
211 }
212
213#if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
214 if (clk_src_enabled)
215 {
216 const esp_err_t clk_off = esp_clk_tree_enable_src(
217 static_cast<soc_module_clk_t>(oneshot_hal_->clk_src), false);
218
219 if (clk_off != ESP_OK)
220 {
221 converted = false;
222 }
223 }
224#endif
225
226 portEXIT_CRITICAL(&rtc_spinlock);
227
228 const esp_err_t unlock_err = adc_lock_release(unit_);
229
230 if (unlock_err != ESP_OK)
231 {
232 return 0.f;
233 }
234
235 if (!converted)
236 {
237 return 0.f;
238 }
239
240 latest_raw_[idx] = static_cast<uint16_t>(raw);
241 latest_values_[idx] = RawToVoltage(idx, latest_raw_[idx]);
242 channel_ready_[idx] = true;
243 return latest_values_[idx];
244}

The documentation for this class was generated from the following files: