1 // Copyright 2015-2020 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 #pragma once
15 
16 #include <stdbool.h>
17 #include <stdlib.h>
18 #include "regi2c_ctrl.h"
19 #include "esp_attr.h"
20 
21 #include "soc/adc_periph.h"
22 #include "hal/adc_types.h"
23 #include "soc/apb_saradc_struct.h"
24 #include "soc/apb_saradc_reg.h"
25 #include "soc/rtc_cntl_struct.h"
26 #include "soc/rtc_cntl_reg.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define ADC_LL_CLKM_DIV_NUM_DEFAULT 15
33 #define ADC_LL_CLKM_DIV_B_DEFAULT   1
34 #define ADC_LL_CLKM_DIV_A_DEFAULT   0
35 
36 typedef enum {
37     ADC_NUM_1 = 0,          /*!< SAR ADC 1 */
38     ADC_NUM_2 = 1,          /*!< SAR ADC 2 */
39     ADC_NUM_MAX,
40 } adc_ll_num_t;
41 
42 typedef enum {
43     ADC_POWER_BY_FSM,   /*!< ADC XPD controled by FSM. Used for polling mode */
44     ADC_POWER_SW_ON,    /*!< ADC XPD controled by SW. power on. Used for DMA mode */
45     ADC_POWER_SW_OFF,   /*!< ADC XPD controled by SW. power off. */
46     ADC_POWER_MAX,      /*!< For parameter check. */
47 } adc_ll_power_t;
48 
49 typedef enum {
50     ADC_RTC_DATA_OK = 0,
51     ADC_RTC_CTRL_UNSELECTED = 1,
52     ADC_RTC_CTRL_BREAK = 2,
53     ADC_RTC_DATA_FAIL = -1,
54 } adc_ll_rtc_raw_data_t;
55 
56 //These values should be set according to the HW
57 typedef enum {
58     ADC_LL_INTR_THRES1_LOW  = BIT(26),
59     ADC_LL_INTR_THRES0_LOW  = BIT(27),
60     ADC_LL_INTR_THRES1_HIGH = BIT(28),
61     ADC_LL_INTR_THRES0_HIGH = BIT(29),
62     ADC_LL_INTR_ADC2_DONE   = BIT(30),
63     ADC_LL_INTR_ADC1_DONE   = BIT(31),
64 } adc_ll_intr_t;
65 FLAG_ATTR(adc_ll_intr_t)
66 
67 /**
68  * @brief ADC controller type selection.
69  *
70  * @note For ADC2, use the force option with care. The system power consumption detection will use ADC2.
71  *       If it is forced to switch to another controller, it may cause the system to obtain incorrect values.
72  * @note Normally, there is no need to switch the controller manually.
73  */
74 typedef enum {
75     ADC_CTRL_RTC = 0,   /*!<For ADC1. Select RTC controller. For ADC2. The controller is selected by the arbiter. Arbiter in default mode. */
76     ADC_CTRL_DIG = 2,   /*!<For ADC1. Select DIG controller. For ADC2. The controller is selected by the arbiter. Arbiter in default mode. */
77     ADC2_CTRL_PWDET = 3,/*!<For ADC2. The controller is selected by the arbiter. Arbiter in default mode. */
78     ADC2_CTRL_FORCE_PWDET = 3,  /*!<For ADC2. Arbiter in shield mode. Force select Wi-Fi controller work. */
79     ADC2_CTRL_FORCE_RTC = 4,    /*!<For ADC2. Arbiter in shield mode. Force select RTC controller work. */
80     ADC2_CTRL_FORCE_DIG = 6,    /*!<For ADC2. Arbiter in shield mode. Force select digital controller work. */
81 } adc_ll_controller_t;
82 
83 /*---------------------------------------------------------------
84                     Digital controller setting
85 ---------------------------------------------------------------*/
86 
87 /**
88  * Set adc fsm interval parameter for digital controller. These values are fixed for same platforms.
89  *
90  * @param rst_wait cycles between DIG ADC controller reset ADC sensor and start ADC sensor.
91  * @param start_wait Delay time after open xpd.
92  * @param standby_wait Delay time to close xpd.
93  */
adc_ll_digi_set_fsm_time(uint32_t rst_wait,uint32_t start_wait,uint32_t standby_wait)94 static inline void adc_ll_digi_set_fsm_time(uint32_t rst_wait, uint32_t start_wait, uint32_t standby_wait)
95 {
96     // Internal FSM reset wait time
97     APB_SARADC.fsm_wait.rstb_wait = rst_wait;
98     // Internal FSM start wait time
99     APB_SARADC.fsm_wait.xpd_wait = start_wait;
100     // Internal FSM standby wait time
101     APB_SARADC.fsm_wait.standby_wait = standby_wait;
102 }
103 
104 /**
105  * Set adc sample cycle for digital controller.
106  *
107  * @note Normally, please use default value.
108  * @param sample_cycle Cycles between DIG ADC controller start ADC sensor and beginning to receive data from sensor.
109  *                     Range: 2 ~ 0xFF.
110  */
adc_ll_set_sample_cycle(uint32_t sample_cycle)111 static inline void adc_ll_set_sample_cycle(uint32_t sample_cycle)
112 {
113     /* Should be called before writing I2C registers. */
114     SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_PU);
115     REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_SAMPLE_CYCLE_ADDR, sample_cycle);
116 }
117 
118 /**
119  * Set SAR ADC module clock division factor.
120  * SAR ADC clock divided from digital controller clock.
121  *
122  * @param div Division factor.
123  */
adc_ll_digi_set_clk_div(uint32_t div)124 static inline void adc_ll_digi_set_clk_div(uint32_t div)
125 {
126     /* ADC clock devided from digital controller clock clk */
127     APB_SARADC.ctrl.sar_clk_div = div;
128 }
129 
130 /**
131  * Set adc max conversion number for digital controller.
132  * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
133  *
134  * @param meas_num Max conversion number. Range: 0 ~ 255.
135  */
adc_ll_digi_set_convert_limit_num(uint32_t meas_num)136 static inline void adc_ll_digi_set_convert_limit_num(uint32_t meas_num)
137 {
138     APB_SARADC.ctrl2.max_meas_num = meas_num;
139 }
140 
141 /**
142  * Enable max conversion number detection for digital controller.
143  * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
144  */
adc_ll_digi_convert_limit_enable(void)145 static inline void adc_ll_digi_convert_limit_enable(void)
146 {
147     APB_SARADC.ctrl2.meas_num_limit = 1;
148 }
149 
150 /**
151  * Disable max conversion number detection for digital controller.
152  * If the number of ADC conversion is equal to the maximum, the conversion is stopped.
153  */
adc_ll_digi_convert_limit_disable(void)154 static inline void adc_ll_digi_convert_limit_disable(void)
155 {
156     APB_SARADC.ctrl2.meas_num_limit = 0;
157 }
158 
159 /**
160  * Set pattern table length for digital controller.
161  * The pattern table that defines the conversion rules for each SAR ADC. Each table has 8 items, in which channel selection,
162  * and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
163  * pattern table one by one. For each controller the scan sequence has at most 8 different rules before repeating itself.
164  *
165  * @param adc_n ADC unit.
166  * @param patt_len Items range: 1 ~ 8.
167  */
adc_ll_digi_set_pattern_table_len(adc_ll_num_t adc_n,uint32_t patt_len)168 static inline void adc_ll_digi_set_pattern_table_len(adc_ll_num_t adc_n, uint32_t patt_len)
169 {
170     APB_SARADC.ctrl.sar_patt_len = patt_len - 1;
171 }
172 
173 /**
174  * Set pattern table for digital controller.
175  * The pattern table that defines the conversion rules for each SAR ADC. Each table has 8 items, in which channel selection,
176  * resolution and attenuation are stored. When the conversion is started, the controller reads conversion rules from the
177  * pattern table one by one. For each controller the scan sequence has at most 8 different rules before repeating itself.
178  *
179  * @param adc_n ADC unit.
180  * @param pattern_index Items index. Range: 0 ~ 7.
181  * @param pattern Stored conversion rules.
182  */
adc_ll_digi_set_pattern_table(adc_ll_num_t adc_n,uint32_t pattern_index,adc_digi_pattern_table_t pattern)183 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)
184 {
185     uint32_t tab;
186     uint8_t index = pattern_index / 4;
187     uint8_t offset = (pattern_index % 4) * 6;
188 
189     tab = APB_SARADC.sar_patt_tab[index].sar_patt_tab1;  // Read old register value
190     tab &= (~(0xFC0000 >> offset));                      // Clear old data
191     tab |= ((uint32_t)(pattern.val & 0x3F) << 18) >> offset;       // Fill in the new data
192     APB_SARADC.sar_patt_tab[index].sar_patt_tab1 = tab;  // Write back
193 }
194 
195 /**
196  * Reset the pattern table pointer, then take the measurement rule from table header in next measurement.
197  *
198  * @param adc_n ADC unit.
199  */
adc_ll_digi_clear_pattern_table(adc_ll_num_t adc_n)200 static inline void adc_ll_digi_clear_pattern_table(adc_ll_num_t adc_n)
201 {
202     APB_SARADC.ctrl.sar_patt_p_clear = 1;
203     APB_SARADC.ctrl.sar_patt_p_clear = 0;
204 }
205 
206 /**
207  * Sets the number of cycles required for the conversion to complete and wait for the arbiter to stabilize.
208  *
209  * @note Only ADC2 have arbiter function.
210  * @param cycle range: 0 ~ 4.
211  */
adc_ll_digi_set_arbiter_stable_cycle(uint32_t cycle)212 static inline void adc_ll_digi_set_arbiter_stable_cycle(uint32_t cycle)
213 {
214     APB_SARADC.ctrl.wait_arb_cycle = cycle;
215 }
216 
217 /**
218  * ADC Digital controller output data invert or not.
219  *
220  * @param adc_n ADC unit.
221  * @param inv_en data invert or not.
222  */
adc_ll_digi_output_invert(adc_ll_num_t adc_n,bool inv_en)223 static inline void adc_ll_digi_output_invert(adc_ll_num_t adc_n, bool inv_en)
224 {
225     if (adc_n == ADC_NUM_1) {
226         APB_SARADC.ctrl2.sar1_inv = inv_en;   // Enable / Disable ADC data invert
227     } else { // adc_n == ADC_NUM_2
228         APB_SARADC.ctrl2.sar2_inv = inv_en;   // Enable / Disable ADC data invert
229     }
230 }
231 
232 /**
233  * Set the interval clock cycle for the digital controller to trigger the measurement.
234  * Expression: `trigger_meas_freq` = `controller_clk` / 2 / interval.
235  *
236  * @note The trigger interval should not be smaller than the sampling time of the SAR ADC.
237  * @param cycle The clock cycle (trigger interval) of the measurement. Range: 30 ~ 4095.
238  */
adc_ll_digi_set_trigger_interval(uint32_t cycle)239 static inline void adc_ll_digi_set_trigger_interval(uint32_t cycle)
240 {
241     APB_SARADC.ctrl2.timer_target = cycle;
242 }
243 
244 /**
245  * Enable digital controller timer to trigger the measurement.
246  */
adc_ll_digi_trigger_enable(void)247 static inline void adc_ll_digi_trigger_enable(void)
248 {
249     APB_SARADC.ctrl2.timer_en = 1;
250 }
251 
252 /**
253  * Disable digital controller timer to trigger the measurement.
254  */
adc_ll_digi_trigger_disable(void)255 static inline void adc_ll_digi_trigger_disable(void)
256 {
257     APB_SARADC.ctrl2.timer_en = 0;
258 }
259 
260 /**
261  * Set ADC digital controller clock division factor. The clock divided from `APLL` or `APB` clock.
262  * Expression: controller_clk = APLL/APB * (div_num  + div_b / div_a).
263  *
264  * @param div_num Division factor. Range: 1 ~ 255.
265  * @param div_b Division factor. Range: 1 ~ 63.
266  * @param div_a Division factor. Range: 0 ~ 63.
267  */
adc_ll_digi_controller_clk_div(uint32_t div_num,uint32_t div_b,uint32_t div_a)268 static inline void adc_ll_digi_controller_clk_div(uint32_t div_num, uint32_t div_b, uint32_t div_a)
269 {
270     APB_SARADC.apb_adc_clkm_conf.clkm_div_num = div_num;
271     APB_SARADC.apb_adc_clkm_conf.clkm_div_b = div_b;
272     APB_SARADC.apb_adc_clkm_conf.clkm_div_a = div_a;
273 }
274 
275 /**
276  * Enable clock and select clock source for ADC digital controller.
277  *
278  * @param use_apll true: use APLL clock; false: use APB clock.
279  */
adc_ll_digi_controller_clk_enable(bool use_apll)280 static inline void adc_ll_digi_controller_clk_enable(bool use_apll)
281 {
282     if (use_apll) {
283         APB_SARADC.apb_adc_clkm_conf.clk_sel = 1;   // APLL clock
284     } else {
285         APB_SARADC.apb_adc_clkm_conf.clk_sel = 2;   // APB clock
286     }
287     APB_SARADC.ctrl.sar_clk_gated = 1;
288 }
289 
290 /**
291  * Disable clock for ADC digital controller.
292  */
adc_ll_digi_controller_clk_disable(void)293 static inline void adc_ll_digi_controller_clk_disable(void)
294 {
295     APB_SARADC.ctrl.sar_clk_gated = 0;
296 }
297 
298 /**
299  * Reset adc digital controller filter.
300  *
301  * @param adc_n ADC unit.
302  */
adc_ll_digi_filter_reset(adc_ll_num_t adc_n)303 static inline void adc_ll_digi_filter_reset(adc_ll_num_t adc_n)
304 {
305     APB_SARADC.filter_ctrl0.filter_reset = 1;
306 }
307 
308 /**
309  * Set adc digital controller filter factor.
310  *
311  * @note If the channel info is not supported, the filter function will not be enabled.
312  * @param idx ADC filter unit.
313  * @param filter Filter config. Expression: filter_data = (k-1)/k * last_data + new_data / k. Set values: (2, 4, 8, 16, 64).
314  */
adc_ll_digi_filter_set_factor(adc_digi_filter_idx_t idx,adc_digi_filter_t * filter)315 static inline void adc_ll_digi_filter_set_factor(adc_digi_filter_idx_t idx, adc_digi_filter_t *filter)
316 {
317     if (idx == ADC_DIGI_FILTER_IDX0) {
318         APB_SARADC.filter_ctrl0.filter_channel0 = (filter->adc_unit << 3) | (filter->channel & 0x7);
319         APB_SARADC.filter_ctrl1.filter_factor0 = filter->mode;
320     } else if (idx == ADC_DIGI_FILTER_IDX1) {
321         APB_SARADC.filter_ctrl0.filter_channel1 = (filter->adc_unit << 3) | (filter->channel & 0x7);
322         APB_SARADC.filter_ctrl1.filter_factor1 = filter->mode;
323     }
324 }
325 
326 /**
327  * Get adc digital controller filter factor.
328  *
329  * @param adc_n ADC unit.
330  * @param factor Expression: filter_data = (k-1)/k * last_data + new_data / k. Set values: (2, 4, 8, 16, 64).
331  */
adc_ll_digi_filter_get_factor(adc_digi_filter_idx_t idx,adc_digi_filter_t * filter)332 static inline void adc_ll_digi_filter_get_factor(adc_digi_filter_idx_t idx, adc_digi_filter_t *filter)
333 {
334     if (idx == ADC_DIGI_FILTER_IDX0) {
335         filter->adc_unit = (APB_SARADC.filter_ctrl0.filter_channel0 >> 3) & 0x1;
336         filter->channel = APB_SARADC.filter_ctrl0.filter_channel0 & 0x7;
337         filter->mode = APB_SARADC.filter_ctrl1.filter_factor0;
338     } else if (idx == ADC_DIGI_FILTER_IDX1) {
339         filter->adc_unit = (APB_SARADC.filter_ctrl0.filter_channel1 >> 3) & 0x1;
340         filter->channel = APB_SARADC.filter_ctrl0.filter_channel1 & 0x7;
341         filter->mode = APB_SARADC.filter_ctrl1.filter_factor1;
342     }
343 }
344 
345 /**
346  * Disable adc digital controller filter.
347  * Filtering the ADC data to obtain smooth data at higher sampling rates.
348  *
349  * @note If the channel info is not supported, the filter function will not be enabled.
350  * @param adc_n ADC unit.
351  */
adc_ll_digi_filter_disable(adc_digi_filter_idx_t idx)352 static inline void adc_ll_digi_filter_disable(adc_digi_filter_idx_t idx)
353 {
354     if (idx == ADC_DIGI_FILTER_IDX0) {
355         APB_SARADC.filter_ctrl0.filter_channel0 = 0xF;
356         APB_SARADC.filter_ctrl1.filter_factor0 = 0;
357     } else if (idx == ADC_DIGI_FILTER_IDX1) {
358         APB_SARADC.filter_ctrl0.filter_channel1 = 0xF;
359         APB_SARADC.filter_ctrl1.filter_factor1 = 0;
360     }
361 }
362 
363 /**
364  * Set monitor mode of adc digital controller.
365  *
366  * @note If the channel info is not supported, the monitor function will not be enabled.
367  * @param adc_n ADC unit.
368  * @param is_larger true:  If ADC_OUT >  threshold, Generates monitor interrupt.
369  *                  false: If ADC_OUT <  threshold, Generates monitor interrupt.
370  */
adc_ll_digi_monitor_set_mode(adc_digi_monitor_idx_t idx,adc_digi_monitor_t * cfg)371 static inline void adc_ll_digi_monitor_set_mode(adc_digi_monitor_idx_t idx, adc_digi_monitor_t *cfg)
372 {
373     if (idx == ADC_DIGI_MONITOR_IDX0) {
374         APB_SARADC.thres0_ctrl.thres0_channel = (cfg->adc_unit << 3) | (cfg->channel & 0x7);
375         APB_SARADC.thres0_ctrl.thres0_high = cfg->h_threshold;
376         APB_SARADC.thres0_ctrl.thres0_low = cfg->l_threshold;
377     } else { // ADC_DIGI_MONITOR_IDX1
378         APB_SARADC.thres1_ctrl.thres1_channel = (cfg->adc_unit << 3) | (cfg->channel & 0x7);
379         APB_SARADC.thres1_ctrl.thres1_high = cfg->h_threshold;
380         APB_SARADC.thres1_ctrl.thres1_low = cfg->l_threshold;
381     }
382 }
383 
384 /**
385  * Enable/disable monitor of adc digital controller.
386  *
387  * @note If the channel info is not supported, the monitor function will not be enabled.
388  * @param adc_n ADC unit.
389  */
adc_ll_digi_monitor_disable(adc_digi_monitor_idx_t idx)390 static inline void adc_ll_digi_monitor_disable(adc_digi_monitor_idx_t idx)
391 {
392     if (idx == ADC_DIGI_MONITOR_IDX0) {
393         APB_SARADC.thres0_ctrl.thres0_channel = 0xF;
394     } else { // ADC_DIGI_MONITOR_IDX1
395         APB_SARADC.thres1_ctrl.thres1_channel = 0xF;
396     }
397 }
398 
399 /**
400  * Set DMA eof num of adc digital controller.
401  * If the number of measurements reaches `dma_eof_num`, then `dma_in_suc_eof` signal is generated.
402  *
403  * @param num eof num of DMA.
404  */
adc_ll_digi_dma_set_eof_num(uint32_t num)405 static inline void adc_ll_digi_dma_set_eof_num(uint32_t num)
406 {
407     APB_SARADC.dma_conf.apb_adc_eof_num = num;
408 }
409 
410 /**
411  * Enable output data to DMA from adc digital controller.
412  */
adc_ll_digi_dma_enable(void)413 static inline void adc_ll_digi_dma_enable(void)
414 {
415     APB_SARADC.dma_conf.apb_adc_trans = 1;
416 }
417 
418 /**
419  * Disable output data to DMA from adc digital controller.
420  */
adc_ll_digi_dma_disable(void)421 static inline void adc_ll_digi_dma_disable(void)
422 {
423     APB_SARADC.dma_conf.apb_adc_trans = 0;
424 }
425 
426 /**
427  * Reset adc digital controller.
428  */
adc_ll_digi_reset(void)429 static inline void adc_ll_digi_reset(void)
430 {
431     APB_SARADC.dma_conf.apb_adc_reset_fsm = 1;
432     APB_SARADC.dma_conf.apb_adc_reset_fsm = 0;
433 }
434 
435 /*---------------------------------------------------------------
436                     PWDET(Power detect) controller setting
437 ---------------------------------------------------------------*/
438 /**
439  * Set adc cct for PWDET controller.
440  *
441  * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
442  * @param cct Range: 0 ~ 7.
443  */
adc_ll_pwdet_set_cct(uint32_t cct)444 static inline void adc_ll_pwdet_set_cct(uint32_t cct)
445 {
446     /* Capacitor tuning of the PA power monitor. cct set to the same value with PHY. */
447     RTCCNTL.sensor_ctrl.sar2_pwdet_cct = cct;
448 }
449 
450 /**
451  * Get adc cct for PWDET controller.
452  *
453  * @note Capacitor tuning of the PA power monitor. cct set to the same value with PHY.
454  * @return cct Range: 0 ~ 7.
455  */
adc_ll_pwdet_get_cct(void)456 static inline uint32_t adc_ll_pwdet_get_cct(void)
457 {
458     /* Capacitor tuning of the PA power monitor. cct set to the same value with PHY. */
459     return RTCCNTL.sensor_ctrl.sar2_pwdet_cct;
460 }
461 
462 /**
463  * Analyze whether the obtained raw data is correct.
464  * ADC2 can use arbiter. The arbitration result is stored in the channel information of the returned data.
465  *
466  * @param adc_n ADC unit.
467  * @param raw_data ADC raw data input (convert value).
468  * @return
469  *        -  0: The data is correct to use.
470  *        - -1: The data is invalid.
471  */
adc_ll_analysis_raw_data(adc_ll_num_t adc_n,int raw_data)472 static inline adc_ll_rtc_raw_data_t adc_ll_analysis_raw_data(adc_ll_num_t adc_n, int raw_data)
473 {
474     if (adc_n == ADC_NUM_1) {
475         return ADC_RTC_DATA_OK;
476     }
477 
478     //The raw data API returns value without channel information. Read value directly from the register
479     if (((APB_SARADC.apb_saradc2_data_status.adc2_data >> 13) & 0xF) > 9) {
480         return ADC_RTC_DATA_FAIL;
481     }
482 
483     return ADC_RTC_DATA_OK;
484 }
485 
486 /*---------------------------------------------------------------
487                     Common setting
488 ---------------------------------------------------------------*/
489 /**
490  * Set ADC module power management.
491  *
492  * @param manage Set ADC power status.
493  */
adc_ll_set_power_manage(adc_ll_power_t manage)494 static inline void adc_ll_set_power_manage(adc_ll_power_t manage)
495 {
496     // /* Bit1  0:Fsm  1: SW mode
497     //    Bit0  0:SW mode power down  1: SW mode power on */
498     if (manage == ADC_POWER_SW_ON) {
499         APB_SARADC.ctrl.sar_clk_gated = 1;
500         APB_SARADC.ctrl.xpd_sar_force = 3;
501     } else if (manage == ADC_POWER_BY_FSM) {
502         APB_SARADC.ctrl.sar_clk_gated = 1;
503         APB_SARADC.ctrl.xpd_sar_force = 0;
504     } else if (manage == ADC_POWER_SW_OFF) {
505         APB_SARADC.ctrl.xpd_sar_force = 2;
506         APB_SARADC.ctrl.sar_clk_gated = 0;
507     }
508 }
509 
510 /**
511  * Get ADC module power management.
512  *
513  * @return
514  *      - ADC power status.
515  */
adc_ll_get_power_manage(void)516 static inline adc_ll_power_t adc_ll_get_power_manage(void)
517 {
518     /* Bit1  0:Fsm  1: SW mode
519        Bit0  0:SW mode power down  1: SW mode power on */
520     adc_ll_power_t manage;
521     if (APB_SARADC.ctrl.xpd_sar_force == 3) {
522         manage = ADC_POWER_SW_ON;
523     } else if (APB_SARADC.ctrl.xpd_sar_force == 2) {
524         manage = ADC_POWER_SW_OFF;
525     } else {
526         manage = ADC_POWER_BY_FSM;
527     }
528     return manage;
529 }
530 
531 /**
532  * Set ADC2 module arbiter work mode.
533  * The arbiter is to improve the use efficiency of ADC2. After the control right is robbed by the high priority,
534  * 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.
535  *
536  * @note Only ADC2 support arbiter.
537  * @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.
538  *
539  * @param mode Refer to `adc_arbiter_mode_t`.
540  */
adc_ll_set_arbiter_work_mode(adc_arbiter_mode_t mode)541 static inline void adc_ll_set_arbiter_work_mode(adc_arbiter_mode_t mode)
542 {
543     if (mode == ADC_ARB_MODE_FIX) {
544         APB_SARADC.apb_adc_arb_ctrl.adc_arb_grant_force = 0;
545         APB_SARADC.apb_adc_arb_ctrl.adc_arb_fix_priority = 1;
546     } else if (mode == ADC_ARB_MODE_LOOP) {
547         APB_SARADC.apb_adc_arb_ctrl.adc_arb_grant_force = 0;
548         APB_SARADC.apb_adc_arb_ctrl.adc_arb_fix_priority = 0;
549     } else {
550         APB_SARADC.apb_adc_arb_ctrl.adc_arb_grant_force = 1;    // Shield arbiter.
551     }
552 }
553 
554 /**
555  * Set ADC2 module controller priority in arbiter.
556  * The arbiter is to improve the use efficiency of ADC2. After the control right is robbed by the high priority,
557  * 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.
558  *
559  * @note Only ADC2 support arbiter.
560  * @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.
561  * @note Default priority: Wi-Fi(2) > RTC(1) > Digital(0);
562  *
563  * @param pri_rtc RTC controller priority. Range: 0 ~ 2.
564  * @param pri_dig Digital controller priority. Range: 0 ~ 2.
565  * @param pri_pwdet Wi-Fi controller priority. Range: 0 ~ 2.
566  */
adc_ll_set_arbiter_priority(uint8_t pri_rtc,uint8_t pri_dig,uint8_t pri_pwdet)567 static inline void adc_ll_set_arbiter_priority(uint8_t pri_rtc, uint8_t pri_dig, uint8_t pri_pwdet)
568 {
569     if (pri_rtc != pri_dig && pri_rtc != pri_pwdet && pri_dig != pri_pwdet) {
570         APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_priority = pri_rtc;
571         APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_priority = pri_dig;
572         APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_priority = pri_pwdet;
573     }
574     /* Should select highest priority controller. */
575     if (pri_rtc > pri_dig) {
576         if (pri_rtc > pri_pwdet) {
577             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 0;
578             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 1;
579             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 0;
580         } else {
581             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 0;
582             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 0;
583             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 1;
584         }
585     } else {
586         if (pri_dig > pri_pwdet) {
587             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 1;
588             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 0;
589             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 0;
590         } else {
591             APB_SARADC.apb_adc_arb_ctrl.adc_arb_apb_force = 0;
592             APB_SARADC.apb_adc_arb_ctrl.adc_arb_rtc_force = 0;
593             APB_SARADC.apb_adc_arb_ctrl.adc_arb_wifi_force = 1;
594         }
595     }
596 }
597 
598 /* ADC calibration code. */
599 /**
600  * @brief Set common calibration configuration. Should be shared with other parts (PWDET).
601  */
adc_ll_calibration_init(adc_ll_num_t adc_n)602 static inline void adc_ll_calibration_init(adc_ll_num_t adc_n)
603 {
604     if (adc_n == ADC_NUM_1) {
605         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_DREF_ADDR, 1);
606     } else {
607         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_DREF_ADDR, 1);
608     }
609 }
610 
611 /**
612  * Configure the registers for ADC calibration. You need to call the ``adc_ll_calibration_finish`` interface to resume after calibration.
613  *
614  * @note  Different ADC units and different attenuation options use different calibration data (initial data).
615  *
616  * @param adc_n ADC index number.
617  * @param channel adc channel number.
618  * @param internal_gnd true:  Disconnect from the IO port and use the internal GND as the calibration voltage.
619  *                     false: Use IO external voltage as calibration voltage.
620  */
adc_ll_calibration_prepare(adc_ll_num_t adc_n,adc_channel_t channel,bool internal_gnd)621 static inline void adc_ll_calibration_prepare(adc_ll_num_t adc_n, adc_channel_t channel, bool internal_gnd)
622 {
623     /* Enable/disable internal connect GND (for calibration). */
624     if (adc_n == ADC_NUM_1) {
625         if (internal_gnd) {
626             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 1);
627         } else {
628             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
629         }
630     } else {
631         if (internal_gnd) {
632             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 1);
633         } else {
634             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
635         }
636     }
637 }
638 
639 /**
640  * Resume register status after calibration.
641  *
642  * @param adc_n ADC index number.
643  */
adc_ll_calibration_finish(adc_ll_num_t adc_n)644 static inline void adc_ll_calibration_finish(adc_ll_num_t adc_n)
645 {
646     if (adc_n == ADC_NUM_1) {
647         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_ENCAL_GND_ADDR, 0);
648     } else {
649         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_ENCAL_GND_ADDR, 0);
650     }
651 }
652 
653 /**
654  * Set the calibration result to ADC.
655  *
656  * @note  Different ADC units and different attenuation options use different calibration data (initial data).
657  *
658  * @param adc_n ADC index number.
659  */
adc_ll_set_calibration_param(adc_ll_num_t adc_n,uint32_t param)660 static inline void adc_ll_set_calibration_param(adc_ll_num_t adc_n, uint32_t param)
661 {
662     uint8_t msb = param >> 8;
663     uint8_t lsb = param & 0xFF;
664     if (adc_n == ADC_NUM_1) {
665         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_HIGH_ADDR, msb);
666         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR1_INITIAL_CODE_LOW_ADDR, lsb);
667     } else {
668         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_HIGH_ADDR, msb);
669         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SAR2_INITIAL_CODE_LOW_ADDR, lsb);
670     }
671 }
672 /* Temp code end. */
673 
674 /**
675  *  Output ADCn inter reference voltage to ADC2 channels.
676  *
677  *  This function routes the internal reference voltage of ADCn to one of
678  *  ADC1's channels. This reference voltage can then be manually measured
679  *  for calibration purposes.
680  *
681  *  @param[in]  adc ADC unit select
682  *  @param[in]  channel ADC1 channel number
683  *  @param[in]  en Enable/disable the reference voltage output
684  */
adc_ll_vref_output(adc_ll_num_t adc,adc_channel_t channel,bool en)685 static inline void adc_ll_vref_output(adc_ll_num_t adc, adc_channel_t channel, bool en)
686 {
687     if (en) {
688         REG_SET_FIELD(RTC_CNTL_SENSOR_CTRL_REG, RTC_CNTL_FORCE_XPD_SAR, 3);
689         SET_PERI_REG_MASK(RTC_CNTL_REG, RTC_CNTL_REGULATOR_FORCE_PU);
690 
691         REG_SET_FIELD(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_SEL, 2);
692         SET_PERI_REG_MASK(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_EN);
693         SET_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_GRANT_FORCE);
694         SET_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_APB_FORCE);
695         APB_SARADC.sar_patt_tab[0].sar_patt_tab1 = 0xFFFFFF;
696         APB_SARADC.sar_patt_tab[1].sar_patt_tab1 = 0xFFFFFF;
697         APB_SARADC.onetime_sample.adc1_onetime_sample = 1;
698         APB_SARADC.onetime_sample.onetime_channel = channel;
699         SET_PERI_REG_MASK(RTC_CNTL_ANA_CONF_REG, RTC_CNTL_SAR_I2C_PU);
700         if (adc == ADC_NUM_1) {
701             /* Config test mux to route v_ref to ADC1 Channels */
702             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC1_ENCAL_REF_ADDR, 1);
703             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR, 1);
704             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_TSENS_ADDR, 0);
705             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR, 1);
706         } else {
707             /* Config test mux to route v_ref to ADC2 Channels */
708             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC2_ENCAL_REF_ADDR, 1);
709             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR, 0);
710             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_TSENS_ADDR, 0);
711             REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR, 0);
712         }
713     } else {
714         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC2_ENCAL_REF_ADDR, 0);
715         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC1_ENCAL_REF_ADDR, 0);
716         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_DTEST_RTC_ADDR, 0);
717         REGI2C_WRITE_MASK(I2C_SAR_ADC, ADC_SARADC_ENT_RTC_ADDR, 0);
718         APB_SARADC.onetime_sample.adc1_onetime_sample = 0;
719         APB_SARADC.onetime_sample.onetime_channel = 0xf;
720         REG_SET_FIELD(RTC_CNTL_SENSOR_CTRL_REG, RTC_CNTL_FORCE_XPD_SAR, 0);
721         REG_SET_FIELD(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_SEL, 0);
722         CLEAR_PERI_REG_MASK(APB_SARADC_APB_ADC_CLKM_CONF_REG, APB_SARADC_CLK_EN);
723         CLEAR_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_GRANT_FORCE);
724         CLEAR_PERI_REG_MASK(APB_SARADC_APB_ADC_ARB_CTRL_REG, APB_SARADC_ADC_ARB_APB_FORCE);
725     }
726 }
727 
728 /*---------------------------------------------------------------
729                     Single Read
730 ---------------------------------------------------------------*/
731 /**
732  * Trigger single read
733  *
734  * @param val Usage: set to 1 to start the ADC conversion. The step signal should at least keep 3 ADC digital controller clock cycle,
735  *            otherwise the step signal may not be captured by the ADC digital controller when its frequency is slow.
736  *            This hardware limitation will be removed in future versions.
737  */
adc_ll_onetime_start(bool val)738 static inline void adc_ll_onetime_start(bool val)
739 {
740     APB_SARADC.onetime_sample.onetime_start = val;
741 }
742 
adc_ll_onetime_set_channel(adc_ll_num_t unit,adc_channel_t channel)743 static inline void adc_ll_onetime_set_channel(adc_ll_num_t unit, adc_channel_t channel)
744 {
745     APB_SARADC.onetime_sample.onetime_channel = ((unit << 3) | channel);
746 }
747 
adc_ll_onetime_set_atten(adc_atten_t atten)748 static inline void adc_ll_onetime_set_atten(adc_atten_t atten)
749 {
750     APB_SARADC.onetime_sample.onetime_atten = atten;
751 }
752 
adc_ll_intr_enable(adc_ll_intr_t mask)753 static inline void adc_ll_intr_enable(adc_ll_intr_t mask)
754 {
755     APB_SARADC.int_ena.val |= mask;
756 }
757 
adc_ll_intr_disable(adc_ll_intr_t mask)758 static inline void adc_ll_intr_disable(adc_ll_intr_t mask)
759 {
760     APB_SARADC.int_ena.val &= ~mask;
761 }
762 
adc_ll_intr_clear(adc_ll_intr_t mask)763 static inline void adc_ll_intr_clear(adc_ll_intr_t mask)
764 {
765     APB_SARADC.int_clr.val |= mask;
766 }
767 
adc_ll_intr_get_raw(adc_ll_intr_t mask)768 static inline bool adc_ll_intr_get_raw(adc_ll_intr_t mask)
769 {
770     return (APB_SARADC.int_raw.val & mask);
771 }
772 
adc_ll_intr_get_status(adc_ll_intr_t mask)773 static inline bool adc_ll_intr_get_status(adc_ll_intr_t mask)
774 {
775     return (APB_SARADC.int_st.val & mask);
776 }
777 
adc_ll_onetime_sample_enable(adc_ll_num_t adc_n,bool enable)778 static inline void adc_ll_onetime_sample_enable(adc_ll_num_t adc_n, bool enable)
779 {
780     if (adc_n == ADC_NUM_1) {
781         APB_SARADC.onetime_sample.adc1_onetime_sample = enable;
782     } else {
783         APB_SARADC.onetime_sample.adc2_onetime_sample = enable;
784     }
785 }
786 
adc_ll_adc1_read(void)787 static inline uint32_t adc_ll_adc1_read(void)
788 {
789     //On ESP32C3, valid data width is 12-bit
790     return (APB_SARADC.apb_saradc1_data_status.adc1_data & 0xfff);
791 }
792 
adc_ll_adc2_read(void)793 static inline uint32_t adc_ll_adc2_read(void)
794 {
795     //On ESP32C3, valid data width is 12-bit
796     return (APB_SARADC.apb_saradc2_data_status.adc2_data & 0xfff);
797 }
798 
799 #ifdef __cplusplus
800 }
801 #endif
802