1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #pragma once
7 
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include "esp_attr.h"
11 
12 #include "soc/adc_periph.h"
13 #include "soc/apb_saradc_struct.h"
14 #include "soc/apb_saradc_reg.h"
15 #include "soc/rtc_cntl_struct.h"
16 #include "soc/rtc_cntl_reg.h"
17 #include "soc/clk_tree_defs.h"
18 #include "hal/misc.h"
19 #include "hal/assert.h"
20 #include "hal/adc_types.h"
21 #include "hal/adc_types_private.h"
22 #include "hal/regi2c_ctrl.h"
23 
24 #include "soc/regi2c_saradc.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define ADC_LL_EVENT_ADC1_ONESHOT_DONE    BIT(31)
31 #define ADC_LL_EVENT_ADC2_ONESHOT_DONE    BIT(30)
32 #define ADC_LL_EVENT_THRES0_HIGH          BIT(29)
33 #define ADC_LL_EVENT_THRES1_HIGH          BIT(28)
34 #define ADC_LL_EVENT_THRES0_LOW           BIT(27)
35 #define ADC_LL_EVENT_THRES1_LOW           BIT(26)
36 
37 /*---------------------------------------------------------------
38                     Oneshot
39 ---------------------------------------------------------------*/
40 #define ADC_LL_DATA_INVERT_DEFAULT(PERIPH_NUM)         (0)
41 #define ADC_LL_DELAY_CYCLE_AFTER_DONE_SIGNAL           (0)
42 
43 /*---------------------------------------------------------------
44                     DMA
45 ---------------------------------------------------------------*/
46 #define ADC_LL_DIGI_DATA_INVERT_DEFAULT(PERIPH_NUM)    (0)
47 #define ADC_LL_FSM_RSTB_WAIT_DEFAULT                   (8)
48 #define ADC_LL_FSM_START_WAIT_DEFAULT                  (5)
49 #define ADC_LL_FSM_STANDBY_WAIT_DEFAULT                (100)
50 #define ADC_LL_SAMPLE_CYCLE_DEFAULT                    (2)
51 #define ADC_LL_DIGI_SAR_CLK_DIV_DEFAULT                (1)
52 
53 #define ADC_LL_CLKM_DIV_NUM_DEFAULT       15
54 #define ADC_LL_CLKM_DIV_B_DEFAULT         1
55 #define ADC_LL_CLKM_DIV_A_DEFAULT         0
56 #define ADC_LL_DEFAULT_CONV_LIMIT_EN      0
57 #define ADC_LL_DEFAULT_CONV_LIMIT_NUM     10
58 
59 /**
60  * Workaround: on ESP32C3, the internal hardware counter that counts ADC samples will not be automatically cleared,
61  * and there is no dedicated register to manually clear it. (see section 3.2 of the errata document).
62  * Therefore, traverse from 0 to the value configured last time, so as to clear the ADC sample counter.
63  */
64 #define ADC_LL_WORKAROUND_CLEAR_EOF_COUNTER            (1)
65 
66 /*---------------------------------------------------------------
67                     PWDET (Power Detect)
68 ---------------------------------------------------------------*/
69 #define ADC_LL_PWDET_CCT_DEFAULT                       (4)
70 
71 typedef enum {
72     ADC_LL_POWER_BY_FSM,   /*!< ADC XPD controlled by FSM. Used for polling mode */
73     ADC_LL_POWER_SW_ON,    /*!< ADC XPD controlled by SW. power on. Used for DMA mode */
74     ADC_LL_POWER_SW_OFF,   /*!< ADC XPD controlled by SW. power off. */
75 } adc_ll_power_t;
76 
77 typedef enum {
78     ADC_LL_CTRL_DIG = 0,    ///< For ADC1. Select DIG controller.
79     ADC_LL_CTRL_ARB = 1,    ///< For ADC2. The controller is selected by the arbiter.
80 } adc_ll_controller_t;
81 
82 /**
83  * @brief ADC digital controller (DMA mode) work mode.
84  *
85  * @note  The conversion mode affects the sampling frequency:
86  *        ESP32C3 only support ONLY_ADC1 mode
87  *        SINGLE_UNIT_1: When the measurement is triggered, only ADC1 is sampled once.
88  */
89 typedef enum {
90     ADC_LL_DIGI_CONV_ONLY_ADC1 = 0,     // Only use ADC1 for conversion
91 } adc_ll_digi_convert_mode_t;
92 
93 typedef struct  {
94     union {
95         struct {
96             uint8_t atten:      2;
97             uint8_t channel:    3;
98             uint8_t unit:       1;
99             uint8_t reserved:   2;
100         };
101         uint8_t val;
102     };
103 } __attribute__((packed)) adc_ll_digi_pattern_table_t;
104 
105 /*---------------------------------------------------------------
106                     Digital controller setting
107 ---------------------------------------------------------------*/
108 
109 /**
110  * Set adc fsm interval parameter for digital controller. These values are fixed for same platforms.
111  *
112  * @param rst_wait cycles between DIG ADC controller reset ADC sensor and start ADC sensor.
113  * @param start_wait Delay time after open xpd.
114  * @param standby_wait Delay time to close xpd.
115  */
adc_ll_digi_set_fsm_time(uint32_t rst_wait,uint32_t start_wait,uint32_t standby_wait)116 static inline void adc_ll_digi_set_fsm_time(uint32_t rst_wait, uint32_t start_wait, uint32_t standby_wait)
117 {
118     // Internal FSM reset wait time
119     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.fsm_wait, rstb_wait, rst_wait);
120     // Internal FSM start wait time
121     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.fsm_wait, xpd_wait, start_wait);
122     // Internal FSM standby wait time
123     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.fsm_wait, standby_wait, standby_wait);
124 }
125 
126 /**
127  * Set adc sample cycle for digital controller.
128  *
129  * @note Normally, please use default value.
130  * @param sample_cycle Cycles between DIG ADC controller start ADC sensor and beginning to receive data from sensor.
131  *                     Range: 2 ~ 0xFF.
132  */
adc_ll_set_sample_cycle(uint32_t sample_cycle)133 static inline void adc_ll_set_sample_cycle(uint32_t sample_cycle)
134 {
135     /* Should be called before writing I2C registers. */
136     SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_PU);
137     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_SAMPLE_CYCLE_ADDR, sample_cycle);
138 }
139 
140 /**
141  * Set SAR ADC module clock division factor.
142  * SAR ADC clock divided from digital controller clock.
143  *
144  * @param div Division factor.
145  */
adc_ll_digi_set_clk_div(uint32_t div)146 static inline void adc_ll_digi_set_clk_div(uint32_t div)
147 {
148     /* ADC clock divided from digital controller clock clk */
149     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.ctrl, sar_clk_div, div);
150 }
151 
152 /**
153  * Set adc max conversion number for digital controller.
154  * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
155  *
156  * @param meas_num Max conversion number. Range: 0 ~ 255.
157  */
adc_ll_digi_set_convert_limit_num(uint32_t meas_num)158 static inline void adc_ll_digi_set_convert_limit_num(uint32_t meas_num)
159 {
160     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.ctrl2, max_meas_num, meas_num);
161 }
162 
163 /**
164  * Enable max conversion number detection for digital controller.
165  * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
166  *
167  * @param enable  true: enable; false: disable
168  */
adc_ll_digi_convert_limit_enable(bool enable)169 static inline void adc_ll_digi_convert_limit_enable(bool enable)
170 {
171     APB_SARADC.ctrl2.meas_num_limit = enable;
172 }
173 
174 /**
175  * Set adc conversion mode for digital controller.
176  *
177  * @note ESP32C3 only support ADC1 single mode.
178  *
179  * @param mode Conversion mode select.
180  */
adc_ll_digi_set_convert_mode(adc_ll_digi_convert_mode_t mode)181 static inline void adc_ll_digi_set_convert_mode(adc_ll_digi_convert_mode_t mode)
182 {
183     //ESP32C3 only supports ADC_LL_DIGI_CONV_ONLY_ADC1 mode
184 }
185 
186 /**
187  * Set pattern table length for digital controller.
188  * The pattern table that defines the conversion rules for each SAR ADC. Each table has 8 items, in which channel selection,
189  * and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
190  * pattern table one by one. For each controller the scan sequence has at most 8 different rules before repeating itself.
191  *
192  * @param adc_n ADC unit.
193  * @param patt_len Items range: 1 ~ 8.
194  */
adc_ll_digi_set_pattern_table_len(adc_unit_t adc_n,uint32_t patt_len)195 static inline void adc_ll_digi_set_pattern_table_len(adc_unit_t adc_n, uint32_t patt_len)
196 {
197     APB_SARADC.ctrl.sar_patt_len = patt_len - 1;
198 }
199 
200 /**
201  * Set pattern table for digital controller.
202  * The pattern table that defines the conversion rules for each SAR ADC. Each table has 8 items, in which channel selection,
203  * resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
204  * pattern table one by one. For each controller the scan sequence has at most 8 different rules before repeating itself.
205  *
206  * @param adc_n ADC unit.
207  * @param pattern_index Items index. Range: 0 ~ 7.
208  * @param pattern Stored conversion rules.
209  */
adc_ll_digi_set_pattern_table(adc_unit_t adc_n,uint32_t pattern_index,adc_digi_pattern_config_t table)210 static inline void adc_ll_digi_set_pattern_table(adc_unit_t adc_n, uint32_t pattern_index, adc_digi_pattern_config_t table)
211 {
212     uint32_t tab;
213     uint8_t index = pattern_index / 4;
214     uint8_t offset = (pattern_index % 4) * 6;
215     adc_ll_digi_pattern_table_t pattern = {0};
216 
217     pattern.val = (table.atten & 0x3) | ((table.channel & 0x7) << 2) | ((table.unit & 0x1) << 5);
218     tab = APB_SARADC.sar_patt_tab[index].sar_patt_tab1;         // Read old register value
219     tab &= (~(0xFC0000 >> offset));                             // Clear old data
220     tab |= ((uint32_t)(pattern.val & 0x3F) << 18) >> offset;    // Fill in the new data
221     APB_SARADC.sar_patt_tab[index].sar_patt_tab1 = tab;         // Write back
222 }
223 
224 /**
225  * Reset the pattern table pointer, then take the measurement rule from table header in next measurement.
226  *
227  * @param adc_n ADC unit.
228  */
adc_ll_digi_clear_pattern_table(adc_unit_t adc_n)229 static inline void adc_ll_digi_clear_pattern_table(adc_unit_t adc_n)
230 {
231     APB_SARADC.ctrl.sar_patt_p_clear = 1;
232     APB_SARADC.ctrl.sar_patt_p_clear = 0;
233 }
234 
235 /**
236  * Sets the number of cycles required for the conversion to complete and wait for the arbiter to stabilize.
237  *
238  * @note Only ADC2 have arbiter function.
239  * @param cycle range: 0 ~ 4.
240  */
adc_ll_digi_set_arbiter_stable_cycle(uint32_t cycle)241 static inline void adc_ll_digi_set_arbiter_stable_cycle(uint32_t cycle)
242 {
243     APB_SARADC.ctrl.wait_arb_cycle = cycle;
244 }
245 
246 /**
247  * ADC Digital controller output data invert or not.
248  *
249  * @param adc_n ADC unit.
250  * @param inv_en data invert or not.
251  */
adc_ll_digi_output_invert(adc_unit_t adc_n,bool inv_en)252 static inline void adc_ll_digi_output_invert(adc_unit_t adc_n, bool inv_en)
253 {
254     if (adc_n == ADC_UNIT_1) {
255         APB_SARADC.ctrl2.sar1_inv = inv_en;   // Enable / Disable ADC data invert
256     } else { // adc_n == ADC_UNIT_2
257         APB_SARADC.ctrl2.sar2_inv = inv_en;   // Enable / Disable ADC data invert
258     }
259 }
260 
261 /**
262  * Set the interval clock cycle for the digital controller to trigger the measurement.
263  * Expression: `trigger_meas_freq` = `controller_clk` / 2 / interval.
264  *
265  * @note The trigger interval should not be smaller than the sampling time of the SAR ADC.
266  * @param cycle The clock cycle (trigger interval) of the measurement. Range: 30 ~ 4095.
267  */
adc_ll_digi_set_trigger_interval(uint32_t cycle)268 static inline void adc_ll_digi_set_trigger_interval(uint32_t cycle)
269 {
270     APB_SARADC.ctrl2.timer_target = cycle;
271 }
272 
273 /**
274  * Enable digital controller timer to trigger the measurement.
275  */
adc_ll_digi_trigger_enable(void)276 static inline void adc_ll_digi_trigger_enable(void)
277 {
278     APB_SARADC.ctrl2.timer_en = 1;
279 }
280 
281 /**
282  * Disable digital controller timer to trigger the measurement.
283  */
adc_ll_digi_trigger_disable(void)284 static inline void adc_ll_digi_trigger_disable(void)
285 {
286     APB_SARADC.ctrl2.timer_en = 0;
287 }
288 
289 /**
290  * Set ADC digital controller clock division factor. The clock divided from `APLL` or `APB` clock.
291  * Expression: controller_clk = (APLL or APB) / (div_num + div_a / div_b + 1).
292  *
293  * @param div_num Division factor. Range: 0 ~ 255.
294  * @param div_b Division factor. Range: 1 ~ 63.
295  * @param div_a Division factor. Range: 0 ~ 63.
296  */
adc_ll_digi_controller_clk_div(uint32_t div_num,uint32_t div_b,uint32_t div_a)297 static inline void adc_ll_digi_controller_clk_div(uint32_t div_num, uint32_t div_b, uint32_t div_a)
298 {
299     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.apb_adc_clkm_conf, clkm_div_num, div_num);
300     APB_SARADC.apb_adc_clkm_conf.clkm_div_b = div_b;
301     APB_SARADC.apb_adc_clkm_conf.clkm_div_a = div_a;
302 }
303 
304 /**
305  * Enable clock and select clock source for ADC digital controller.
306  *
307  * @param clk_src clock source for ADC digital controller.
308  */
adc_ll_digi_clk_sel(adc_continuous_clk_src_t clk_src)309 static inline void adc_ll_digi_clk_sel(adc_continuous_clk_src_t clk_src)
310 {
311     // Only support APB clock, should always set to 1 or 2
312     APB_SARADC.apb_adc_clkm_conf.clk_sel = 2;
313     APB_SARADC.ctrl.sar_clk_gated = 1;
314 }
315 
316 /**
317  * Disable clock for ADC digital controller.
318  */
adc_ll_digi_controller_clk_disable(void)319 static inline void adc_ll_digi_controller_clk_disable(void)
320 {
321     APB_SARADC.ctrl.sar_clk_gated = 0;
322 }
323 
324 /**
325  * Reset adc digital controller filter.
326  *
327  * @param idx   Filter index
328  * @param adc_n ADC unit.
329  */
adc_ll_digi_filter_reset(adc_digi_iir_filter_t idx,adc_unit_t adc_n)330 static inline void adc_ll_digi_filter_reset(adc_digi_iir_filter_t idx, adc_unit_t adc_n)
331 {
332     (void)adc_n;
333     APB_SARADC.filter_ctrl0.filter_reset = 1;
334     APB_SARADC.filter_ctrl0.filter_reset = 0;
335 }
336 
337 /**
338  * Set adc digital controller filter coeff.
339  *
340  * @param idx      filter index
341  * @param adc_n    adc unit
342  * @param channel  adc channel
343  * @param coeff    filter coeff
344  */
adc_ll_digi_filter_set_factor(adc_digi_iir_filter_t idx,adc_unit_t adc_n,adc_channel_t channel,adc_digi_iir_filter_coeff_t coeff)345 static inline void adc_ll_digi_filter_set_factor(adc_digi_iir_filter_t idx, adc_unit_t adc_n, adc_channel_t channel, adc_digi_iir_filter_coeff_t coeff)
346 {
347     uint32_t factor_reg_val = 0;
348     switch (coeff) {
349         case ADC_DIGI_IIR_FILTER_COEFF_2:
350             factor_reg_val = 1;
351             break;
352         case ADC_DIGI_IIR_FILTER_COEFF_4:
353             factor_reg_val = 2;
354             break;
355         case ADC_DIGI_IIR_FILTER_COEFF_8:
356             factor_reg_val = 3;
357             break;
358         case ADC_DIGI_IIR_FILTER_COEFF_16:
359             factor_reg_val = 4;
360             break;
361         case ADC_DIGI_IIR_FILTER_COEFF_64:
362             factor_reg_val = 6;
363             break;
364         default:
365             HAL_ASSERT(false);
366     }
367 
368     if (idx == ADC_DIGI_IIR_FILTER_0) {
369         APB_SARADC.filter_ctrl0.filter_channel0 = ((adc_n + 1) << 3) | (channel & 0x7);
370         APB_SARADC.filter_ctrl1.filter_factor0 = factor_reg_val;
371     } else if (idx == ADC_DIGI_IIR_FILTER_1) {
372         APB_SARADC.filter_ctrl0.filter_channel1 = ((adc_n + 1) << 3) | (channel & 0x7);
373         APB_SARADC.filter_ctrl1.filter_factor1 = factor_reg_val;
374     }
375 }
376 
377 /**
378  * Enable adc digital controller filter.
379  * Filtering the ADC data to obtain smooth data at higher sampling rates.
380  *
381  * @param idx      filter index
382  * @param adc_n    ADC unit
383  * @param enable   Enable / Disable
384  */
adc_ll_digi_filter_enable(adc_digi_iir_filter_t idx,adc_unit_t adc_n,bool enable)385 static inline void adc_ll_digi_filter_enable(adc_digi_iir_filter_t idx, adc_unit_t adc_n, bool enable)
386 {
387     (void)adc_n;
388     if (!enable) {
389         if (idx == ADC_DIGI_IIR_FILTER_0) {
390             APB_SARADC.filter_ctrl0.filter_channel0 = 0xF;
391             APB_SARADC.filter_ctrl1.filter_factor0 = 0;
392         } else if (idx == ADC_DIGI_IIR_FILTER_1) {
393             APB_SARADC.filter_ctrl0.filter_channel1 = 0xF;
394             APB_SARADC.filter_ctrl1.filter_factor1 = 0;
395         }
396     }
397     //nothing to do to enable, after adc_ll_digi_filter_set_factor, it's enabled.
398 }
399 
400 /**
401  * Set monitor mode of adc digital controller.
402  *
403  * @note If the channel info is not supported, the monitor function will not be enabled.
404  * @param adc_n ADC unit.
405  * @param is_larger true:  If ADC_OUT >  threshold, Generates monitor interrupt.
406  *                  false: If ADC_OUT <  threshold, Generates monitor interrupt.
407  */
adc_ll_digi_monitor_set_mode(adc_digi_monitor_idx_t idx,adc_digi_monitor_t * cfg)408 static inline void adc_ll_digi_monitor_set_mode(adc_digi_monitor_idx_t idx, adc_digi_monitor_t *cfg)
409 {
410     if (idx == ADC_DIGI_MONITOR_IDX0) {
411         APB_SARADC.thres0_ctrl.thres0_channel = (cfg->adc_unit << 3) | (cfg->channel & 0x7);
412         APB_SARADC.thres0_ctrl.thres0_high = cfg->h_threshold;
413         APB_SARADC.thres0_ctrl.thres0_low = cfg->l_threshold;
414     } else { // ADC_DIGI_MONITOR_IDX1
415         APB_SARADC.thres1_ctrl.thres1_channel = (cfg->adc_unit << 3) | (cfg->channel & 0x7);
416         APB_SARADC.thres1_ctrl.thres1_high = cfg->h_threshold;
417         APB_SARADC.thres1_ctrl.thres1_low = cfg->l_threshold;
418     }
419 }
420 
421 /**
422  * Enable/disable monitor of adc digital controller.
423  *
424  * @note If the channel info is not supported, the monitor function will not be enabled.
425  * @param adc_n ADC unit.
426  */
adc_ll_digi_monitor_disable(adc_digi_monitor_idx_t idx)427 static inline void adc_ll_digi_monitor_disable(adc_digi_monitor_idx_t idx)
428 {
429     if (idx == ADC_DIGI_MONITOR_IDX0) {
430         APB_SARADC.thres0_ctrl.thres0_channel = 0xF;
431     } else { // ADC_DIGI_MONITOR_IDX1
432         APB_SARADC.thres1_ctrl.thres1_channel = 0xF;
433     }
434 }
435 
436 /**
437  * Set DMA eof num of adc digital controller.
438  * If the number of measurements reaches `dma_eof_num`, then `dma_in_suc_eof` signal is generated.
439  *
440  * @param num eof num of DMA.
441  */
adc_ll_digi_dma_set_eof_num(uint32_t num)442 static inline void adc_ll_digi_dma_set_eof_num(uint32_t num)
443 {
444     HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.dma_conf, apb_adc_eof_num, num);
445 }
446 
447 /**
448  * Clear ADC sample counter of adc digital controller.
449  */
adc_ll_digi_dma_clr_eof(void)450 static inline void adc_ll_digi_dma_clr_eof(void)
451 {
452     uint32_t eof_num = HAL_FORCE_READ_U32_REG_FIELD(APB_SARADC.dma_conf, apb_adc_eof_num);
453     for (int i = 0; i <= eof_num; i++)
454     {
455         HAL_FORCE_MODIFY_U32_REG_FIELD(APB_SARADC.dma_conf, apb_adc_eof_num, i);
456     }
457 }
458 
459 /**
460  * Enable output data to DMA from adc digital controller.
461  */
adc_ll_digi_dma_enable(void)462 static inline void adc_ll_digi_dma_enable(void)
463 {
464     APB_SARADC.dma_conf.apb_adc_trans = 1;
465 }
466 
467 /**
468  * Disable output data to DMA from adc digital controller.
469  */
adc_ll_digi_dma_disable(void)470 static inline void adc_ll_digi_dma_disable(void)
471 {
472     APB_SARADC.dma_conf.apb_adc_trans = 0;
473 }
474 
475 /**
476  * Reset adc digital controller.
477  */
adc_ll_digi_reset(void)478 static inline void adc_ll_digi_reset(void)
479 {
480     APB_SARADC.dma_conf.apb_adc_reset_fsm = 1;
481     APB_SARADC.dma_conf.apb_adc_reset_fsm = 0;
482 }
483 
484 /*---------------------------------------------------------------
485                     PWDET(Power detect) controller setting
486 ---------------------------------------------------------------*/
487 /**
488  * Set adc cct for PWDET controller.
489  *
490  * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
491  * @param cct Range: 0 ~ 7.
492  */
adc_ll_pwdet_set_cct(uint32_t cct)493 static inline void adc_ll_pwdet_set_cct(uint32_t cct)
494 {
495     /* Capacitor tuning of the PA power monitor. cct set to the same value with PHY. */
496     RTCCNTL.sensor_ctrl.sar2_pwdet_cct = cct;
497 }
498 
499 /**
500  * Get adc cct for PWDET controller.
501  *
502  * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
503  * @return cct Range: 0 ~ 7.
504  */
adc_ll_pwdet_get_cct(void)505 static inline uint32_t adc_ll_pwdet_get_cct(void)
506 {
507     /* Capacitor tuning of the PA power monitor. cct set to the same value with PHY. */
508     return RTCCNTL.sensor_ctrl.sar2_pwdet_cct;
509 }
510 
511 /*---------------------------------------------------------------
512                     Common setting
513 ---------------------------------------------------------------*/
514 /**
515  * Set ADC module power management.
516  *
517  * @param manage Set ADC power status.
518  */
519 __attribute__((always_inline))
adc_ll_digi_set_power_manage(adc_ll_power_t manage)520 static inline void adc_ll_digi_set_power_manage(adc_ll_power_t manage)
521 {
522     /* Bit1  0:Fsm  1: SW mode
523        Bit0  0:SW mode power down  1: SW mode power on */
524     if (manage == ADC_LL_POWER_SW_ON) {
525         APB_SARADC.ctrl.sar_clk_gated = 1;
526         APB_SARADC.ctrl.xpd_sar_force = 3;
527     } else if (manage == ADC_LL_POWER_BY_FSM) {
528         APB_SARADC.ctrl.sar_clk_gated = 1;
529         APB_SARADC.ctrl.xpd_sar_force = 0;
530     } else if (manage == ADC_LL_POWER_SW_OFF) {
531         APB_SARADC.ctrl.sar_clk_gated = 0;
532         APB_SARADC.ctrl.xpd_sar_force = 2;
533     }
534 }
535 
536 __attribute__((always_inline))
adc_ll_set_controller(adc_unit_t adc_n,adc_ll_controller_t ctrl)537 static inline void adc_ll_set_controller(adc_unit_t adc_n, adc_ll_controller_t ctrl)
538 {
539     //Not used on ESP32C3
540 }
541 
542 /**
543  * Set ADC2 module arbiter work mode.
544  * The arbiter is to improve the use efficiency of ADC2. After the control right is robbed by the high priority,
545  * the low priority controller will read the invalid ADC data, and the validity of the data can be judged by the flag bit in the data.
546  *
547  * @note Only ADC2 support arbiter.
548  * @note The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
549  *
550  * @param mode Refer to `adc_arbiter_mode_t`.
551  */
552 __attribute__((always_inline))
adc_ll_set_arbiter_work_mode(adc_arbiter_mode_t mode)553 static inline void adc_ll_set_arbiter_work_mode(adc_arbiter_mode_t mode)
554 {
555     if (mode == ADC_ARB_MODE_FIX) {
556         APB_SARADC.apb_adc_arb_ctrl.adc_arb_grant_force = 0;
557         APB_SARADC.apb_adc_arb_ctrl.adc_arb_fix_priority = 1;
558     } else if (mode == ADC_ARB_MODE_LOOP) {
559         APB_SARADC.apb_adc_arb_ctrl.adc_arb_grant_force = 0;
560         APB_SARADC.apb_adc_arb_ctrl.adc_arb_fix_priority = 0;
561     } else {
562         APB_SARADC.apb_adc_arb_ctrl.adc_arb_grant_force = 1;    // Shield arbiter.
563     }
564 }
565 
566 /**
567  * Set ADC2 module controller priority in arbiter.
568  * The arbiter is to improve the use efficiency of ADC2. After the control right is robbed by the high priority,
569  * the low priority controller will read the invalid ADC data, and the validity of the data can be judged by the flag bit in the data.
570  *
571  * @note Only ADC2 support arbiter.
572  * @note The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
573  * @note Default priority: Wi-Fi(2) > RTC(1) > Digital(0);
574  *
575  * @param pri_rtc RTC controller priority. Range: 0 ~ 2.
576  * @param pri_dig Digital controller priority. Range: 0 ~ 2.
577  * @param pri_pwdet Wi-Fi controller priority. Range: 0 ~ 2.
578  */
579 __attribute__((always_inline))
adc_ll_set_arbiter_priority(uint8_t pri_rtc,uint8_t pri_dig,uint8_t pri_pwdet)580 static inline void adc_ll_set_arbiter_priority(uint8_t pri_rtc, uint8_t pri_dig, uint8_t pri_pwdet)
581 {
582     if (pri_rtc != pri_dig && pri_rtc != pri_pwdet && pri_dig != pri_pwdet) {
583         APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_priority = pri_rtc;
584         APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_priority = pri_dig;
585         APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_priority = pri_pwdet;
586     }
587     /* Should select highest priority controller. */
588     if (pri_rtc > pri_dig) {
589         if (pri_rtc > pri_pwdet) {
590             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 0;
591             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 1;
592             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 0;
593         } else {
594             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 0;
595             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 0;
596             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 1;
597         }
598     } else {
599         if (pri_dig > pri_pwdet) {
600             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 1;
601             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 0;
602             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 0;
603         } else {
604             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 0;
605             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 0;
606             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 1;
607         }
608     }
609 }
610 
611 /* ADC calibration code. */
612 /**
613  * @brief Set common calibration configuration. Should be shared with other parts (PWDET).
614  */
615 __attribute__((always_inline))
adc_ll_calibration_init(adc_unit_t adc_n)616 static inline void adc_ll_calibration_init(adc_unit_t adc_n)
617 {
618     if (adc_n == ADC_UNIT_1) {
619         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_DREF_ADDR, 1);
620     } else {
621         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_DREF_ADDR, 1);
622     }
623 }
624 
625 /**
626  * Configure the registers for ADC calibration. You need to call the ``adc_ll_calibration_finish`` interface to resume after calibration.
627  *
628  * @note  Different ADC units and different attenuation options use different calibration data (initial data).
629  *
630  * @param adc_n ADC index number.
631  * @param internal_gnd true:  Disconnect from the IO port and use the internal GND as the calibration voltage.
632  *                     false: Use IO external voltage as calibration voltage.
633  */
adc_ll_calibration_prepare(adc_unit_t adc_n,bool internal_gnd)634 static inline void adc_ll_calibration_prepare(adc_unit_t adc_n, bool internal_gnd)
635 {
636     /* Enable/disable internal connect GND (for calibration). */
637     if (adc_n == ADC_UNIT_1) {
638         if (internal_gnd) {
639             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 1);
640         } else {
641             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
642         }
643     } else {
644         if (internal_gnd) {
645             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 1);
646         } else {
647             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
648         }
649     }
650 }
651 
652 /**
653  * Resume register status after calibration.
654  *
655  * @param adc_n ADC index number.
656  */
adc_ll_calibration_finish(adc_unit_t adc_n)657 static inline void adc_ll_calibration_finish(adc_unit_t adc_n)
658 {
659     if (adc_n == ADC_UNIT_1) {
660         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
661     } else {
662         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
663     }
664 }
665 
666 /**
667  * Set the calibration result to ADC.
668  *
669  * @note  Different ADC units and different attenuation options use different calibration data (initial data).
670  *
671  * @param adc_n ADC index number.
672  */
673 __attribute__((always_inline))
adc_ll_set_calibration_param(adc_unit_t adc_n,uint32_t param)674 static inline void adc_ll_set_calibration_param(adc_unit_t adc_n, uint32_t param)
675 {
676     uint8_t msb = param >> 8;
677     uint8_t lsb = param & 0xFF;
678     if (adc_n == ADC_UNIT_1) {
679         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, msb);
680         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, lsb);
681     } else {
682         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_HIGH_ADDR, msb);
683         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_LOW_ADDR, lsb);
684     }
685 }
686 /* Temp code end. */
687 
688 /**
689  *  Output ADCn inter reference voltage to ADC2 channels.
690  *
691  *  This function routes the internal reference voltage of ADCn to one of
692  *  ADC1's channels. This reference voltage can then be manually measured
693  *  for calibration purposes.
694  *
695  *  @param[in]  adc ADC unit select
696  *  @param[in]  channel ADC1 channel number
697  *  @param[in]  en Enable/disable the reference voltage output
698  */
adc_ll_vref_output(adc_unit_t adc,adc_channel_t channel,bool en)699 static inline void adc_ll_vref_output(adc_unit_t adc, adc_channel_t channel, bool en)
700 {
701     if (en) {
702         REG_SET_FIELD(RTC_CNTL_SENSOR_CTRL_REG, RTC_CNTL_FORCE_XPD_SAR, 3);
703         SET_PERI_REG_MASK(RTC_CNTL_REG, RTC_CNTL_REGULATOR_FORCE_PU);
704 
705         REG_SET_FIELD(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_SEL, 2);
706         SET_PERI_REG_MASK(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_EN);
707         SET_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_GRANT_FORCE);
708         SET_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_APB_FORCE);
709         APB_SARADC.sar_patt_tab[0].sar_patt_tab1 = 0xFFFFFF;
710         APB_SARADC.sar_patt_tab[1].sar_patt_tab1 = 0xFFFFFF;
711         APB_SARADC.onetime_sample.adc1_onetime_sample = 1;
712         APB_SARADC.onetime_sample.onetime_channel = channel;
713         SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_PU);
714         if (adc == ADC_UNIT_1) {
715             /* Config test mux to route v_ref to ADC1 Channels */
716             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC1_ENCAL_REF_ADDR, 1);
717             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR, 1);
718             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_TSENS_ADDR, 0);
719             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR, 1);
720         } else {
721             /* Config test mux to route v_ref to ADC2 Channels */
722             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC2_ENCAL_REF_ADDR, 1);
723             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR, 0);
724             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_TSENS_ADDR, 0);
725             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR, 0);
726         }
727     } else {
728         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC2_ENCAL_REF_ADDR, 0);
729         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC1_ENCAL_REF_ADDR, 0);
730         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR, 0);
731         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR, 0);
732         APB_SARADC.onetime_sample.adc1_onetime_sample = 0;
733         APB_SARADC.onetime_sample.onetime_channel = 0xf;
734         REG_SET_FIELD(RTC_CNTL_SENSOR_CTRL_REG, RTC_CNTL_FORCE_XPD_SAR, 0);
735         REG_SET_FIELD(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_SEL, 0);
736         CLEAR_PERI_REG_MASK(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_EN);
737         CLEAR_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_GRANT_FORCE);
738         CLEAR_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_APB_FORCE);
739     }
740 }
741 
742 /*---------------------------------------------------------------
743                     Oneshot Read
744 ---------------------------------------------------------------*/
745 /**
746  * Set adc output data format for oneshot mode
747  *
748  * @note ESP32C3 Oneshot mode only supports 12bit.
749  * @param adc_n ADC unit.
750  * @param bits  Output data bits width option.
751  */
adc_oneshot_ll_set_output_bits(adc_unit_t adc_n,adc_bitwidth_t bits)752 static inline void adc_oneshot_ll_set_output_bits(adc_unit_t adc_n, adc_bitwidth_t bits)
753 {
754     //ESP32C3 only supports 12bit, leave here for compatibility
755     HAL_ASSERT(bits == ADC_BITWIDTH_12 || bits == ADC_BITWIDTH_DEFAULT);
756 }
757 
758 /**
759  * Enable adc channel to start convert.
760  *
761  * @note Only one channel can be selected for measurement.
762  *
763  * @param adc_n   ADC unit.
764  * @param channel ADC channel number for each ADCn.
765  */
adc_oneshot_ll_set_channel(adc_unit_t adc_n,adc_channel_t channel)766 static inline void adc_oneshot_ll_set_channel(adc_unit_t adc_n, adc_channel_t channel)
767 {
768     APB_SARADC.onetime_sample.onetime_channel = ((adc_n << 3) | channel);
769 }
770 
771 /**
772  * Disable adc channel to start convert.
773  *
774  * @note Only one channel can be selected in once measurement.
775  *
776  * @param adc_n ADC unit.
777  */
adc_oneshot_ll_disable_channel(adc_unit_t adc_n)778 static inline void adc_oneshot_ll_disable_channel(adc_unit_t adc_n)
779 {
780     if (adc_n == ADC_UNIT_1) {
781         APB_SARADC.onetime_sample.onetime_channel = ((adc_n << 3) | 0xF);
782     } else { // adc_n == ADC_UNIT_2
783         APB_SARADC.onetime_sample.onetime_channel = ((adc_n << 3) | 0x1);
784     }
785 }
786 
787 /**
788  * Start oneshot conversion by software
789  *
790  * @param val Usage: set to 1 to start the ADC conversion. The step signal should at least keep 3 ADC digital controller clock cycle,
791  *            otherwise the step signal may not be captured by the ADC digital controller when its frequency is slow.
792  *            This hardware limitation will be removed in future versions.
793  */
adc_oneshot_ll_start(bool val)794 static inline void adc_oneshot_ll_start(bool val)
795 {
796     APB_SARADC.onetime_sample.onetime_start = val;
797 }
798 
799 /**
800  * Clear the event for each ADCn for Oneshot mode
801  *
802  * @param event ADC event
803  */
adc_oneshot_ll_clear_event(uint32_t event_mask)804 static inline void adc_oneshot_ll_clear_event(uint32_t event_mask)
805 {
806     APB_SARADC.int_clr.val |= event_mask;
807 }
808 
809 /**
810  * Check the event for each ADCn for Oneshot mode
811  *
812  * @param event ADC event
813  *
814  * @return
815  *      -true  : The conversion process is finish.
816  *      -false : The conversion process is not finish.
817  */
adc_oneshot_ll_get_event(uint32_t event_mask)818 static inline bool adc_oneshot_ll_get_event(uint32_t event_mask)
819 {
820     return (APB_SARADC.int_raw.val & event_mask);
821 }
822 
823 /**
824  * Get the converted value for each ADCn for RTC controller.
825  *
826  * @param adc_n ADC unit.
827  * @return
828  *      - Converted value.
829  */
adc_oneshot_ll_get_raw_result(adc_unit_t adc_n)830 static inline uint32_t adc_oneshot_ll_get_raw_result(adc_unit_t adc_n)
831 {
832     uint32_t ret_val = 0;
833     if (adc_n == ADC_UNIT_1) {
834         ret_val = APB_SARADC.apb_saradc1_data_status.adc1_data & 0xfff;
835     } else { // adc_n == ADC_UNIT_2
836         ret_val = APB_SARADC.apb_saradc2_data_status.adc2_data & 0xfff;
837     }
838     return ret_val;
839 }
840 
841 /**
842  * Analyze whether the obtained raw data is correct.
843  * ADC2 can use arbiter. The arbitration result is stored in the channel information of the returned data.
844  *
845  * @param adc_n    ADC unit.
846  * @param raw_data ADC raw data input (convert value).
847  * @return
848  *        - 1: The data is correct to use.
849  *        - 0: The data is invalid.
850  */
adc_oneshot_ll_raw_check_valid(adc_unit_t adc_n,uint32_t raw_data)851 static inline bool adc_oneshot_ll_raw_check_valid(adc_unit_t adc_n, uint32_t raw_data)
852 {
853     if (adc_n == ADC_UNIT_1) {
854         return true;
855     }
856 
857     //The raw data API returns value without channel information. Read value directly from the register
858     if (((APB_SARADC.apb_saradc2_data_status.adc2_data >> 13) & 0xF) > 9) {
859         return false;
860     }
861 
862     return true;
863 }
864 
865 /**
866  * ADC module RTC output data invert or not.
867  *
868  * @param adc_n ADC unit.
869  * @param inv_en data invert or not.
870  */
adc_oneshot_ll_output_invert(adc_unit_t adc_n,bool inv_en)871 static inline void adc_oneshot_ll_output_invert(adc_unit_t adc_n, bool inv_en)
872 {
873     (void)adc_n;
874     (void)inv_en;
875     //For compatibility
876 }
877 
878 /**
879  * Enable oneshot conversion trigger
880  *
881  * @param adc_n  ADC unit
882  */
adc_oneshot_ll_enable(adc_unit_t adc_n)883 static inline void adc_oneshot_ll_enable(adc_unit_t adc_n)
884 {
885     if (adc_n == ADC_UNIT_1) {
886         APB_SARADC.onetime_sample.adc1_onetime_sample = 1;
887     } else {
888         APB_SARADC.onetime_sample.adc2_onetime_sample = 1;
889     }
890 }
891 
892 /**
893  * Disable oneshot conversion trigger for all the ADC units
894  */
adc_oneshot_ll_disable_all_unit(void)895 static inline void adc_oneshot_ll_disable_all_unit(void)
896 {
897     APB_SARADC.onetime_sample.adc1_onetime_sample = 0;
898     APB_SARADC.onetime_sample.adc2_onetime_sample = 0;
899 }
900 
901 /**
902  * Set attenuation
903  *
904  * @note Attenuation is for all channels
905  *
906  * @param adc_n   ADC unit
907  * @param channel ADC channel
908  * @param atten   ADC attenuation
909  */
adc_oneshot_ll_set_atten(adc_unit_t adc_n,adc_channel_t channel,adc_atten_t atten)910 static inline void adc_oneshot_ll_set_atten(adc_unit_t adc_n, adc_channel_t channel, adc_atten_t atten)
911 {
912     (void)adc_n;
913     (void)channel;
914     // Attenuation is for all channels, unit and channel are for compatibility
915     APB_SARADC.onetime_sample.onetime_atten = atten;
916 }
917 
918 /**
919  * Get the attenuation of a particular channel on ADCn.
920  *
921  * @param adc_n ADC unit.
922  * @param channel ADCn channel number.
923  * @return atten The attenuation option.
924  */
925 __attribute__((always_inline))
adc_ll_get_atten(adc_unit_t adc_n,adc_channel_t channel)926 static inline adc_atten_t adc_ll_get_atten(adc_unit_t adc_n, adc_channel_t channel)
927 {
928     (void)adc_n;
929     (void)channel;
930     return APB_SARADC.onetime_sample.onetime_atten;
931 }
932 
933 #ifdef __cplusplus
934 }
935 #endif
936