1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef __ADC_LL_H__
16 #define __ADC_LL_H__
17
18 #pragma once
19
20 #include "soc/adc_periph.h"
21 #include "hal/adc_types.h"
22 #include "soc/rtc_io_struct.h"
23 #include <stdbool.h>
24
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif /* __cplusplus */
29
30 typedef enum {
31 ADC_NUM_1 = 0, /*!< SAR ADC 1 */
32 ADC_NUM_2 = 1, /*!< SAR ADC 2 */
33 ADC_NUM_MAX,
34 } adc_ll_num_t;
35
36 typedef enum {
37 ADC_POWER_BY_FSM, /*!< ADC XPD controlled by FSM. Used for polling mode */
38 ADC_POWER_SW_ON, /*!< ADC XPD controlled by SW. power on. Used for DMA mode */
39 ADC_POWER_SW_OFF, /*!< ADC XPD controlled by SW. power off. */
40 ADC_POWER_MAX, /*!< For parameter check. */
41 } adc_ll_power_t;
42
43 typedef enum {
44 ADC_HALL_CTRL_ULP = 0x0,/*!< Hall sensor controlled by ULP */
45 ADC_HALL_CTRL_RTC = 0x1 /*!< Hall sensor controlled by RTC */
46 } adc_ll_hall_controller_t ;
47
48 typedef enum {
49 ADC_CTRL_RTC = 0,
50 ADC_CTRL_ULP = 1,
51 ADC_CTRL_DIG = 2,
52 ADC2_CTRL_PWDET = 3,
53 } adc_hal_controller_t ;
54
55 typedef enum {
56 ADC_RTC_DATA_OK = 0,
57 } adc_ll_rtc_raw_data_t;
58
59 /*---------------------------------------------------------------
60 Digital controller setting
61 ---------------------------------------------------------------*/
62
63 /**
64 * Set adc fsm interval parameter for digital controller. These values are fixed for same platforms.
65 *
66 * @param rst_wait cycles between DIG ADC controller reset ADC sensor and start ADC sensor.
67 * @param start_wait Delay time after open xpd.
68 * @param standby_wait Delay time to close xpd.
69 */
adc_ll_digi_set_fsm_time(uint32_t rst_wait,uint32_t start_wait,uint32_t standby_wait)70 static inline void adc_ll_digi_set_fsm_time(uint32_t rst_wait, uint32_t start_wait, uint32_t standby_wait)
71 {
72 // Internal FSM reset wait time
73 SYSCON.saradc_fsm.rstb_wait = rst_wait;
74 // Internal FSM start wait time
75 SYSCON.saradc_fsm.start_wait = start_wait;
76 // Internal FSM standby wait time
77 SYSCON.saradc_fsm.standby_wait = standby_wait;
78 }
79
80 /**
81 * Set adc sample cycle.
82 *
83 * @note Normally, please use default value.
84 * @param sample_cycle The number of ADC sampling cycles. Range: 1 ~ 7.
85 */
adc_ll_set_sample_cycle(uint32_t sample_cycle)86 static inline void adc_ll_set_sample_cycle(uint32_t sample_cycle)
87 {
88 SYSCON.saradc_fsm.sample_cycle = sample_cycle;
89 }
90
91 /**
92 * ADC module clock division factor setting. ADC clock divided from APB clock.
93 *
94 * @param div Division factor.
95 */
adc_ll_digi_set_clk_div(uint32_t div)96 static inline void adc_ll_digi_set_clk_div(uint32_t div)
97 {
98 /* ADC clock divided from APB clk, e.g. 80 / 2 = 40Mhz, */
99 SYSCON.saradc_ctrl.sar_clk_div = div;
100 }
101
102 /**
103 * Set adc output data format for digital controller.
104 *
105 * @param format Output data format, see ``adc_digi_output_format_t``.
106 */
adc_ll_digi_set_output_format(adc_digi_output_format_t format)107 static inline void adc_ll_digi_set_output_format(adc_digi_output_format_t format)
108 {
109 SYSCON.saradc_ctrl.data_sar_sel = format;
110 }
111
112 /**
113 * Set adc max conversion number for digital controller.
114 * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
115 *
116 * @param meas_num Max conversion number. Range: 0 ~ 255.
117 */
adc_ll_digi_set_convert_limit_num(uint32_t meas_num)118 static inline void adc_ll_digi_set_convert_limit_num(uint32_t meas_num)
119 {
120 SYSCON.saradc_ctrl2.max_meas_num = meas_num;
121 }
122
123 /**
124 * Enable max conversion number detection for digital controller.
125 * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
126 */
adc_ll_digi_convert_limit_enable(void)127 static inline void adc_ll_digi_convert_limit_enable(void)
128 {
129 SYSCON.saradc_ctrl2.meas_num_limit = 1;
130 }
131
132 /**
133 * Disable max conversion number detection for digital controller.
134 * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
135 */
adc_ll_digi_convert_limit_disable(void)136 static inline void adc_ll_digi_convert_limit_disable(void)
137 {
138 SYSCON.saradc_ctrl2.meas_num_limit = 0;
139 }
140
141 /**
142 * Set adc conversion mode for digital controller.
143 *
144 * @note ESP32 only support ADC1 single mode.
145 *
146 * @param mode Conversion mode select, see ``adc_digi_convert_mode_t``.
147 */
adc_ll_digi_set_convert_mode(adc_digi_convert_mode_t mode)148 static inline void adc_ll_digi_set_convert_mode(adc_digi_convert_mode_t mode)
149 {
150 if (mode == ADC_CONV_SINGLE_UNIT_1) {
151 SYSCON.saradc_ctrl.work_mode = 0;
152 SYSCON.saradc_ctrl.sar_sel = 0;
153 } else if (mode == ADC_CONV_SINGLE_UNIT_2) {
154 SYSCON.saradc_ctrl.work_mode = 0;
155 SYSCON.saradc_ctrl.sar_sel = 1;
156 } else if (mode == ADC_CONV_BOTH_UNIT) {
157 SYSCON.saradc_ctrl.work_mode = 1;
158 } else if (mode == ADC_CONV_ALTER_UNIT) {
159 SYSCON.saradc_ctrl.work_mode = 2;
160 }
161 }
162
163 /**
164 * ADC module Digital output data invert or not.
165 *
166 * @prarm adc_n ADC unit.
167 */
adc_ll_digi_output_invert(adc_ll_num_t adc_n,bool inv_en)168 static inline void adc_ll_digi_output_invert(adc_ll_num_t adc_n, bool inv_en)
169 {
170 if (adc_n == ADC_NUM_1) {
171 SYSCON.saradc_ctrl2.sar1_inv = inv_en; // Enable / Disable ADC data invert
172 } else { // adc_n == ADC_NUM_2
173 SYSCON.saradc_ctrl2.sar2_inv = inv_en; // Enable / Disable ADC data invert
174 }
175 }
176
177 /**
178 * Set I2S DMA data source for digital controller.
179 *
180 * @param src i2s data source, see ``adc_i2s_source_t``.
181 */
adc_ll_digi_set_data_source(adc_i2s_source_t src)182 static inline void adc_ll_digi_set_data_source(adc_i2s_source_t src)
183 {
184 /* 1: I2S input data is from SAR ADC (for DMA) 0: I2S input data is from GPIO matrix */
185 SYSCON.saradc_ctrl.data_to_i2s = src;
186 }
187
188 /**
189 * Set pattern table length for digital controller.
190 * The pattern table that defines the conversion rules for each SAR ADC. Each table has 16 items, in which channel selection,
191 * resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
192 * pattern table one by one. For each controller the scan sequence has at most 16 different rules before repeating itself.
193 *
194 * @param adc_n ADC unit.
195 * @param patt_len Items range: 1 ~ 16.
196 */
adc_ll_digi_set_pattern_table_len(adc_ll_num_t adc_n,uint32_t patt_len)197 static inline void adc_ll_digi_set_pattern_table_len(adc_ll_num_t adc_n, uint32_t patt_len)
198 {
199 if (adc_n == ADC_NUM_1) {
200 SYSCON.saradc_ctrl.sar1_patt_len = patt_len - 1;
201 } else { // adc_n == ADC_NUM_2
202 SYSCON.saradc_ctrl.sar2_patt_len = patt_len - 1;
203 }
204 }
205
206 /**
207 * Set pattern table lenth for digital controller.
208 * The pattern table that defines the conversion rules for each SAR ADC. Each table has 16 items, in which channel selection,
209 * resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
210 * pattern table one by one. For each controller the scan sequence has at most 16 different rules before repeating itself.
211 *
212 * @param adc_n ADC unit.
213 * @param pattern_index Items index. Range: 0 ~ 15.
214 * @param pattern Stored conversion rules, see ``adc_digi_pattern_table_t``.
215 */
adc_ll_digi_set_pattern_table(adc_ll_num_t adc_n,uint32_t pattern_index,adc_digi_pattern_table_t pattern)216 static inline void adc_ll_digi_set_pattern_table(adc_ll_num_t adc_n, uint32_t pattern_index, adc_digi_pattern_table_t pattern)
217 {
218 uint32_t tab;
219 uint8_t index = pattern_index / 4;
220 uint8_t offset = (pattern_index % 4) * 8;
221 if (adc_n == ADC_NUM_1) {
222 tab = SYSCON.saradc_sar1_patt_tab[index]; // Read old register value
223 tab &= (~(0xFF000000 >> offset)); // clear old data
224 tab |= ((uint32_t)pattern.val << 24) >> offset; // Fill in the new data
225 SYSCON.saradc_sar1_patt_tab[index] = tab; // Write back
226 } else { // adc_n == ADC_NUM_2
227 tab = SYSCON.saradc_sar2_patt_tab[index]; // Read old register value
228 tab &= (~(0xFF000000 >> offset)); // clear old data
229 tab |= ((uint32_t)pattern.val << 24) >> offset; // Fill in the new data
230 SYSCON.saradc_sar2_patt_tab[index] = tab; // Write back
231 }
232 }
233
234 /**
235 * Reset the pattern table pointer, then take the measurement rule from table header in next measurement.
236 *
237 * @param adc_n ADC unit.
238 */
adc_ll_digi_clear_pattern_table(adc_ll_num_t adc_n)239 static inline void adc_ll_digi_clear_pattern_table(adc_ll_num_t adc_n)
240 {
241 if (adc_n == ADC_NUM_1) {
242 SYSCON.saradc_ctrl.sar1_patt_p_clear = 1;
243 SYSCON.saradc_ctrl.sar1_patt_p_clear = 0;
244 } else { // adc_n == ADC_NUM_2
245 SYSCON.saradc_ctrl.sar2_patt_p_clear = 1;
246 SYSCON.saradc_ctrl.sar2_patt_p_clear = 0;
247 }
248 }
249
250 /*---------------------------------------------------------------
251 PWDET(Power detect) controller setting
252 ---------------------------------------------------------------*/
253 /**
254 * Set adc cct for PWDET controller.
255 *
256 * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
257 * @param cct Range: 0 ~ 7.
258 */
adc_ll_pwdet_set_cct(uint32_t cct)259 static inline void adc_ll_pwdet_set_cct(uint32_t cct)
260 {
261 /* Capacitor tuning of the PA power monitor. cct set to the same value with PHY. */
262 SENS.sar_start_force.sar2_pwdet_cct = cct;
263 }
264
265 /**
266 * Get adc cct for PWDET controller.
267 *
268 * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
269 * @return cct Range: 0 ~ 7.
270 */
adc_ll_pwdet_get_cct(void)271 static inline uint32_t adc_ll_pwdet_get_cct(void)
272 {
273 /* Capacitor tuning of the PA power monitor. cct set to the same value with PHY. */
274 return SENS.sar_start_force.sar2_pwdet_cct;
275 }
276
277 /*---------------------------------------------------------------
278 RTC controller setting
279 ---------------------------------------------------------------*/
280 /**
281 * Set adc output data format for RTC controller.
282 *
283 * @param adc_n ADC unit.
284 * @param bits Output data bits width option, see ``adc_bits_width_t``.
285 */
adc_ll_rtc_set_output_format(adc_ll_num_t adc_n,adc_bits_width_t bits)286 static inline void adc_ll_rtc_set_output_format(adc_ll_num_t adc_n, adc_bits_width_t bits)
287 {
288 if (adc_n == ADC_NUM_1) {
289 SENS.sar_start_force.sar1_bit_width = bits;
290 SENS.sar_read_ctrl.sar1_sample_bit = bits;
291 } else { // adc_n == ADC_NUM_2
292 SENS.sar_start_force.sar2_bit_width = bits;
293 SENS.sar_read_ctrl2.sar2_sample_bit = bits;
294 }
295 }
296
297 /**
298 * Enable adc channel to start convert.
299 *
300 * @note Only one channel can be selected in once measurement.
301 *
302 * @param adc_n ADC unit.
303 * @param channel ADC channel number for each ADCn.
304 */
adc_ll_rtc_enable_channel(adc_ll_num_t adc_n,int channel)305 static inline void adc_ll_rtc_enable_channel(adc_ll_num_t adc_n, int channel)
306 {
307 if (adc_n == ADC_NUM_1) {
308 SENS.sar_meas_start1.sar1_en_pad = (1 << channel); //only one channel is selected.
309 } else { // adc_n == ADC_NUM_2
310 SENS.sar_meas_start2.sar2_en_pad = (1 << channel); //only one channel is selected.
311 }
312 }
313
314 /**
315 * Disable adc channel to start convert.
316 *
317 * @note Only one channel can be selected in once measurement.
318 *
319 * @param adc_n ADC unit.
320 */
adc_ll_rtc_disable_channel(adc_ll_num_t adc_n)321 static inline void adc_ll_rtc_disable_channel(adc_ll_num_t adc_n)
322 {
323 if (adc_n == ADC_NUM_1) {
324 SENS.sar_meas_start1.sar1_en_pad = 0; //only one channel is selected.
325 } else { // adc_n == ADC_NUM_2
326 SENS.sar_meas_start2.sar2_en_pad = 0; //only one channel is selected.
327 }
328 }
329
330 /**
331 * Start conversion once by software for RTC controller.
332 *
333 * @note It may be block to wait conversion idle for ADC1.
334 *
335 * @param adc_n ADC unit.
336 * @param channel ADC channel number for each ADCn.
337 */
adc_ll_rtc_start_convert(adc_ll_num_t adc_n,int channel)338 static inline void adc_ll_rtc_start_convert(adc_ll_num_t adc_n, int channel)
339 {
340 if (adc_n == ADC_NUM_1) {
341 while (SENS.sar_slave_addr1.meas_status != 0);
342 SENS.sar_meas_start1.meas1_start_sar = 0;
343 SENS.sar_meas_start1.meas1_start_sar = 1;
344 } else { // adc_n == ADC_NUM_2
345 SENS.sar_meas_start2.meas2_start_sar = 0; //start force 0
346 SENS.sar_meas_start2.meas2_start_sar = 1; //start force 1
347 }
348 }
349
350 /**
351 * Check the conversion done flag for each ADCn for RTC controller.
352 *
353 * @param adc_n ADC unit.
354 * @return
355 * -true : The conversion process is finish.
356 * -false : The conversion process is not finish.
357 */
adc_ll_rtc_convert_is_done(adc_ll_num_t adc_n)358 static inline bool adc_ll_rtc_convert_is_done(adc_ll_num_t adc_n)
359 {
360 bool ret = true;
361 if (adc_n == ADC_NUM_1) {
362 ret = (bool)SENS.sar_meas_start1.meas1_done_sar;
363 } else { // adc_n == ADC_NUM_2
364 ret = (bool)SENS.sar_meas_start2.meas2_done_sar;
365 }
366 return ret;
367 }
368
369 /**
370 * Get the converted value for each ADCn for RTC controller.
371 *
372 * @param adc_n ADC unit.
373 * @return
374 * - Converted value.
375 */
adc_ll_rtc_get_convert_value(adc_ll_num_t adc_n)376 static inline int adc_ll_rtc_get_convert_value(adc_ll_num_t adc_n)
377 {
378 int ret_val = 0;
379 if (adc_n == ADC_NUM_1) {
380 ret_val = SENS.sar_meas_start1.meas1_data_sar;
381 } else { // adc_n == ADC_NUM_2
382 ret_val = SENS.sar_meas_start2.meas2_data_sar;
383 }
384 return ret_val;
385 }
386
387 /**
388 * ADC module RTC output data invert or not.
389 *
390 * @param adc_n ADC unit.
391 */
adc_ll_rtc_output_invert(adc_ll_num_t adc_n,bool inv_en)392 static inline void adc_ll_rtc_output_invert(adc_ll_num_t adc_n, bool inv_en)
393 {
394 if (adc_n == ADC_NUM_1) {
395 SENS.sar_read_ctrl.sar1_data_inv = inv_en; // Enable / Disable ADC data invert
396 } else { // adc_n == ADC_NUM_2
397 SENS.sar_read_ctrl2.sar2_data_inv = inv_en; // Enable / Disable ADC data invert
398 }
399 }
400
401 /**
402 * Analyze whether the obtained raw data is correct.
403 *
404 * @param adc_n ADC unit.
405 * @param raw_data ADC raw data input (convert value).
406 * @return
407 * - 0: The data is correct to use.
408 */
adc_ll_rtc_analysis_raw_data(adc_ll_num_t adc_n,uint16_t raw_data)409 static inline adc_ll_rtc_raw_data_t adc_ll_rtc_analysis_raw_data(adc_ll_num_t adc_n, uint16_t raw_data)
410 {
411 /* ADC1 don't need check data */
412 return ADC_RTC_DATA_OK;
413 }
414
415 /*---------------------------------------------------------------
416 Common setting
417 ---------------------------------------------------------------*/
418 /**
419 * Set ADC module power management.
420 *
421 * @param manage Set ADC power status.
422 */
adc_ll_set_power_manage(adc_ll_power_t manage)423 static inline void adc_ll_set_power_manage(adc_ll_power_t manage)
424 {
425 /* Bit1 0:Fsm 1: SW mode
426 Bit0 0:SW mode power down 1: SW mode power on */
427 if (manage == ADC_POWER_SW_ON) {
428 SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PU;
429 } else if (manage == ADC_POWER_BY_FSM) {
430 SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_FSM;
431 } else if (manage == ADC_POWER_SW_OFF) {
432 SENS.sar_meas_wait2.force_xpd_sar = SENS_FORCE_XPD_SAR_PD;
433 }
434 }
435
436 /**
437 * Get ADC module power management.
438 *
439 * @return
440 * - ADC power status.
441 */
adc_ll_get_power_manage(void)442 static inline adc_ll_power_t adc_ll_get_power_manage(void)
443 {
444 /* Bit1 0:Fsm 1: SW mode
445 Bit0 0:SW mode power down 1: SW mode power on */
446 adc_ll_power_t manage;
447 if (SENS.sar_meas_wait2.force_xpd_sar == SENS_FORCE_XPD_SAR_PU) {
448 manage = ADC_POWER_SW_ON;
449 } else if (SENS.sar_meas_wait2.force_xpd_sar == SENS_FORCE_XPD_SAR_PD) {
450 manage = ADC_POWER_SW_OFF;
451 } else {
452 manage = ADC_POWER_BY_FSM;
453 }
454 return manage;
455 }
456
457 /**
458 * ADC SAR clock division factor setting. ADC SAR clock divided from `RTC_FAST_CLK`.
459 *
460 * @param div Division factor.
461 */
adc_ll_set_sar_clk_div(adc_ll_num_t adc_n,uint32_t div)462 static inline void adc_ll_set_sar_clk_div(adc_ll_num_t adc_n, uint32_t div)
463 {
464 if (adc_n == ADC_NUM_1) {
465 SENS.sar_read_ctrl.sar1_clk_div = div;
466 } else { // adc_n == ADC_NUM_2
467 SENS.sar_read_ctrl2.sar2_clk_div = div;
468 }
469 }
470
471 /**
472 * Set the attenuation of a particular channel on ADCn.
473 */
adc_ll_set_atten(adc_ll_num_t adc_n,adc_channel_t channel,adc_atten_t atten)474 static inline void adc_ll_set_atten(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten)
475 {
476 if (adc_n == ADC_NUM_1) {
477 SENS.sar_atten1 = ( SENS.sar_atten1 & ~(0x3 << (channel * 2)) ) | ((atten & 0x3) << (channel * 2));
478 } else { // adc_n == ADC_NUM_2
479 SENS.sar_atten2 = ( SENS.sar_atten2 & ~(0x3 << (channel * 2)) ) | ((atten & 0x3) << (channel * 2));
480 }
481 }
482
483 /**
484 * Get the attenuation of a particular channel on ADCn.
485 *
486 * @param adc_n ADC unit.
487 * @param channel ADCn channel number.
488 * @return atten The attenuation option.
489 */
adc_ll_get_atten(adc_ll_num_t adc_n,adc_channel_t channel)490 static inline adc_atten_t adc_ll_get_atten(adc_ll_num_t adc_n, adc_channel_t channel)
491 {
492 if (adc_n == ADC_NUM_1) {
493 return (adc_atten_t)((SENS.sar_atten1 >> (channel * 2)) & 0x3);
494 } else {
495 return (adc_atten_t)((SENS.sar_atten2 >> (channel * 2)) & 0x3);
496 }
497 }
498
499 /**
500 * Set ADC module controller.
501 * There are five SAR ADC controllers:
502 * Two digital controller: Continuous conversion mode (DMA). High performance with multiple channel scan modes;
503 * Two RTC controller: Single conversion modes (Polling). For low power purpose working during deep sleep;
504 * the other is dedicated for Power detect (PWDET / PKDET), Only support ADC2.
505 *
506 * @param adc_n ADC unit.
507 * @param ctrl ADC controller.
508 */
adc_ll_set_controller(adc_ll_num_t adc_n,adc_hal_controller_t ctrl)509 static inline void adc_ll_set_controller(adc_ll_num_t adc_n, adc_hal_controller_t ctrl)
510 {
511 if (adc_n == ADC_NUM_1) {
512 switch ( ctrl ) {
513 case ADC_CTRL_RTC:
514 SENS.sar_read_ctrl.sar1_dig_force = 0; // 1: Select digital control; 0: Select RTC control.
515 SENS.sar_meas_start1.meas1_start_force = 1; // 1: SW control RTC ADC start; 0: ULP control RTC ADC start.
516 SENS.sar_meas_start1.sar1_en_pad_force = 1; // 1: SW control RTC ADC bit map; 0: ULP control RTC ADC bit map;
517 SENS.sar_touch_ctrl1.xpd_hall_force = 1; // 1: SW control HALL power; 0: ULP FSM control HALL power.
518 SENS.sar_touch_ctrl1.hall_phase_force = 1; // 1: SW control HALL phase; 0: ULP FSM control HALL phase.
519 break;
520 case ADC_CTRL_ULP:
521 SENS.sar_read_ctrl.sar1_dig_force = 0; // 1: Select digital control; 0: Select RTC control.
522 SENS.sar_meas_start1.meas1_start_force = 0; // 1: SW control RTC ADC start; 0: ULP control RTC ADC start.
523 SENS.sar_meas_start1.sar1_en_pad_force = 0; // 1: SW control RTC ADC bit map; 0: ULP control RTC ADC bit map;
524 SENS.sar_touch_ctrl1.xpd_hall_force = 0; // 1: SW control HALL power; 0: ULP FSM control HALL power.
525 SENS.sar_touch_ctrl1.hall_phase_force = 0; // 1: SW control HALL phase; 0: ULP FSM control HALL phase.
526 break;
527 case ADC_CTRL_DIG:
528 SENS.sar_read_ctrl.sar1_dig_force = 1; // 1: Select digital control; 0: Select RTC control.
529 SENS.sar_meas_start1.meas1_start_force = 1; // 1: SW control RTC ADC start; 0: ULP control RTC ADC start.
530 SENS.sar_meas_start1.sar1_en_pad_force = 1; // 1: SW control RTC ADC bit map; 0: ULP control RTC ADC bit map;
531 SENS.sar_touch_ctrl1.xpd_hall_force = 1; // 1: SW control HALL power; 0: ULP FSM control HALL power.
532 SENS.sar_touch_ctrl1.hall_phase_force = 1; // 1: SW control HALL phase; 0: ULP FSM control HALL phase.
533 break;
534 default:
535 break;
536 }
537 } else { // adc_n == ADC_NUM_2
538 switch ( ctrl ) {
539 case ADC_CTRL_RTC:
540 SENS.sar_meas_start2.meas2_start_force = 1; // 1: SW control RTC ADC start; 0: ULP control RTC ADC start.
541 SENS.sar_meas_start2.sar2_en_pad_force = 1; // 1: SW control RTC ADC bit map; 0: ULP control RTC ADC bit map;
542 SENS.sar_read_ctrl2.sar2_dig_force = 0; // 1: Select digital control; 0: Select RTC control.
543 SENS.sar_read_ctrl2.sar2_pwdet_force = 0; // 1: Select power detect control; 0: Select RTC control.
544 SYSCON.saradc_ctrl.sar2_mux = 1; // 1: Select digital control; 0: Select power detect control.
545 break;
546 case ADC_CTRL_ULP:
547 SENS.sar_meas_start2.meas2_start_force = 0; // 1: SW control RTC ADC start; 0: ULP control RTC ADC start.
548 SENS.sar_meas_start2.sar2_en_pad_force = 0; // 1: SW control RTC ADC bit map; 0: ULP control RTC ADC bit map;
549 SENS.sar_read_ctrl2.sar2_dig_force = 0; // 1: Select digital control; 0: Select RTC control.
550 SENS.sar_read_ctrl2.sar2_pwdet_force = 0; // 1: Select power detect control; 0: Select RTC control.
551 SYSCON.saradc_ctrl.sar2_mux = 1; // 1: Select digital control; 0: Select power detect control.
552 break;
553 case ADC_CTRL_DIG:
554 SENS.sar_meas_start2.meas2_start_force = 1; // 1: SW control RTC ADC start; 0: ULP control RTC ADC start.
555 SENS.sar_meas_start2.sar2_en_pad_force = 1; // 1: SW control RTC ADC bit map; 0: ULP control RTC ADC bit map;
556 SENS.sar_read_ctrl2.sar2_dig_force = 1; // 1: Select digital control; 0: Select RTC control.
557 SENS.sar_read_ctrl2.sar2_pwdet_force = 0; // 1: Select power detect control; 0: Select RTC control.
558 SYSCON.saradc_ctrl.sar2_mux = 1; // 1: Select digital control; 0: Select power detect control.
559 break;
560 case ADC2_CTRL_PWDET: // currently only used by Wi-Fi
561 SENS.sar_meas_start2.meas2_start_force = 1; // 1: SW control RTC ADC start; 0: ULP control RTC ADC start.
562 SENS.sar_meas_start2.sar2_en_pad_force = 1; // 1: SW control RTC ADC bit map; 0: ULP control RTC ADC bit map;
563 SENS.sar_read_ctrl2.sar2_dig_force = 0; // 1: Select digital control; 0: Select RTC control.
564 SENS.sar_read_ctrl2.sar2_pwdet_force = 1; // 1: Select power detect control; 0: Select RTC control.
565 SYSCON.saradc_ctrl.sar2_mux = 0; // 1: Select digital control; 0: Select power detect control.
566 break;
567 default:
568 break;
569 }
570 }
571 }
572
573 /**
574 * Close ADC AMP module if don't use it for power save.
575 */
adc_ll_amp_disable(void)576 static inline void adc_ll_amp_disable(void)
577 {
578 //channel is set in the convert function
579 SENS.sar_meas_wait2.force_xpd_amp = SENS_FORCE_XPD_AMP_PD;
580 //disable FSM, it's only used by the LNA.
581 SENS.sar_meas_ctrl.amp_rst_fb_fsm = 0;
582 SENS.sar_meas_ctrl.amp_short_ref_fsm = 0;
583 SENS.sar_meas_ctrl.amp_short_ref_gnd_fsm = 0;
584 SENS.sar_meas_wait1.sar_amp_wait1 = 1;
585 SENS.sar_meas_wait1.sar_amp_wait2 = 1;
586 SENS.sar_meas_wait2.sar_amp_wait3 = 1;
587 }
588
589 /*---------------------------------------------------------------
590 Hall sensor setting
591 ---------------------------------------------------------------*/
592
593 /**
594 * Enable hall sensor.
595 */
adc_ll_hall_enable(void)596 static inline void adc_ll_hall_enable(void)
597 {
598 RTCIO.hall_sens.xpd_hall = 1;
599 }
600
601 /**
602 * Disable hall sensor.
603 */
adc_ll_hall_disable(void)604 static inline void adc_ll_hall_disable(void)
605 {
606 RTCIO.hall_sens.xpd_hall = 0;
607 }
608
609 /**
610 * Reverse phase of hall sensor.
611 */
adc_ll_hall_phase_enable(void)612 static inline void adc_ll_hall_phase_enable(void)
613 {
614 RTCIO.hall_sens.hall_phase = 1;
615 }
616
617 /**
618 * Don't reverse phase of hall sensor.
619 */
adc_ll_hall_phase_disable(void)620 static inline void adc_ll_hall_phase_disable(void)
621 {
622 RTCIO.hall_sens.hall_phase = 0;
623 }
624
625 /**
626 * Set hall sensor controller.
627 *
628 * @param hall_ctrl Hall controller.
629 */
adc_ll_set_hall_controller(adc_ll_hall_controller_t hall_ctrl)630 static inline void adc_ll_set_hall_controller(adc_ll_hall_controller_t hall_ctrl)
631 {
632 SENS.sar_touch_ctrl1.xpd_hall_force = hall_ctrl; // 1: SW control HALL power; 0: ULP FSM control HALL power.
633 SENS.sar_touch_ctrl1.hall_phase_force = hall_ctrl; // 1: SW control HALL phase; 0: ULP FSM control HALL phase.
634 }
635
636 /**
637 * Output ADC internal reference voltage to channels, only available for ADC2 on ESP32.
638 *
639 * This function routes the internal reference voltage of ADCn to one of
640 * ADC2's channels. This reference voltage can then be manually measured
641 * for calibration purposes.
642 *
643 * @param[in] adc ADC unit select
644 * @param[in] channel ADC2 channel number
645 * @param[in] en Enable/disable the reference voltage output
646 */
adc_ll_vref_output(adc_ll_num_t adc,adc_channel_t channel,bool en)647 static inline void adc_ll_vref_output(adc_ll_num_t adc, adc_channel_t channel, bool en)
648 {
649 if (adc != ADC_NUM_2) return;
650
651 if (en) {
652 RTCCNTL.bias_conf.dbg_atten = 0; //Check DBG effect outside sleep mode
653 //set dtest (MUX_SEL : 0 -> RTC; 1-> vdd_sar2)
654 RTCCNTL.test_mux.dtest_rtc = 1; //Config test mux to route v_ref to ADC2 Channels
655 //set ent
656 RTCCNTL.test_mux.ent_rtc = 1;
657 //set sar2_en_test
658 SENS.sar_start_force.sar2_en_test = 1;
659 //set sar2 en force
660 SENS.sar_meas_start2.sar2_en_pad_force = 1; //Pad bitmap controlled by SW
661 //set en_pad for channels 7,8,9 (bits 0x380)
662 SENS.sar_meas_start2.sar2_en_pad = 1 << channel;
663 } else {
664 RTCCNTL.test_mux.dtest_rtc = 0; //Config test mux to route v_ref to ADC2 Channels
665 //set ent
666 RTCCNTL.test_mux.ent_rtc = 0;
667 //set sar2_en_test
668 SENS.sar_start_force.sar2_en_test = 0;
669 //set sar2 en force
670 SENS.sar_meas_start2.sar2_en_pad_force = 0; //Pad bitmap controlled by SW
671 //set en_pad for channels 7,8,9 (bits 0x380)
672 SENS.sar_meas_start2.sar2_en_pad = 0;
673 }
674 }
675
676
677 #ifdef __cplusplus
678 }
679 #endif /* __cplusplus */
680 #endif /* __ADC_LL_H__ */