1 /*
2 * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <sys/param.h>
8 #include "sdkconfig.h"
9 #include "soc/soc_caps.h"
10 #include "hal/adc_oneshot_hal.h"
11 #include "hal/adc_hal_common.h"
12 #include "hal/adc_ll.h"
13 #include "hal/assert.h"
14 #include "hal/log.h"
15
16 #if SOC_DAC_SUPPORTED
17 #include "hal/dac_ll.h"
18 #endif
19
20 #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
21 /**
22 * For chips without RTC controller, Digital controller is used to trigger an ADC single read.
23 */
24 #include "esp_rom_sys.h"
25 #endif
26
27
28 #if CONFIG_ADC_DISABLE_DAC_OUTPUT
29 // To disable DAC, workarounds, see this function body to know more
30 static void s_disable_dac(adc_oneshot_hal_ctx_t *hal, adc_channel_t channel);
31 #endif
32
33
adc_oneshot_hal_init(adc_oneshot_hal_ctx_t * hal,const adc_oneshot_hal_cfg_t * config)34 void adc_oneshot_hal_init(adc_oneshot_hal_ctx_t *hal, const adc_oneshot_hal_cfg_t *config)
35 {
36 hal->unit = config->unit;
37 hal->work_mode = config->work_mode;
38 hal->clk_src = config->clk_src;
39 hal->clk_src_freq_hz = config->clk_src_freq_hz;
40 }
41
adc_oneshot_hal_channel_config(adc_oneshot_hal_ctx_t * hal,const adc_oneshot_hal_chan_cfg_t * config,adc_channel_t chan)42 void adc_oneshot_hal_channel_config(adc_oneshot_hal_ctx_t *hal, const adc_oneshot_hal_chan_cfg_t *config, adc_channel_t chan)
43 {
44 hal->chan_configs[chan].atten = config->atten;
45 hal->chan_configs[chan].bitwidth = config->bitwidth;
46 }
47
adc_oneshot_hal_setup(adc_oneshot_hal_ctx_t * hal,adc_channel_t chan)48 void adc_oneshot_hal_setup(adc_oneshot_hal_ctx_t *hal, adc_channel_t chan)
49 {
50 adc_unit_t unit = hal->unit;
51
52 #ifdef CONFIG_IDF_TARGET_ESP32
53 adc_ll_hall_disable(); //Disable other peripherals.
54 adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
55 #endif
56
57 #if CONFIG_ADC_DISABLE_DAC_OUTPUT
58 s_disable_dac(hal, chan);
59 #endif
60
61 #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
62 adc_ll_digi_clk_sel(hal->clk_src);
63 #else
64 adc_ll_set_sar_clk_div(unit, ADC_LL_SAR_CLK_DIV_DEFAULT(unit));
65 if (unit == ADC_UNIT_2) {
66 adc_ll_pwdet_set_cct(ADC_LL_PWDET_CCT_DEFAULT);
67 }
68 #endif
69
70 adc_oneshot_ll_output_invert(unit, ADC_LL_DATA_INVERT_DEFAULT(unit));
71 adc_oneshot_ll_set_atten(unit, chan, hal->chan_configs[chan].atten);
72 adc_oneshot_ll_set_output_bits(unit, hal->chan_configs[chan].bitwidth);
73 adc_oneshot_ll_set_channel(unit, chan);
74 adc_hal_set_controller(unit, hal->work_mode);
75
76 #if SOC_ADC_ARBITER_SUPPORTED
77 adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
78 adc_hal_arbiter_config(&config);
79 #endif //#if SOC_ADC_ARBITER_SUPPORTED
80 }
81
adc_hal_onetime_start(adc_unit_t unit,uint32_t clk_src_freq_hz)82 static void adc_hal_onetime_start(adc_unit_t unit, uint32_t clk_src_freq_hz)
83 {
84 #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
85 (void)unit;
86 uint32_t delay = 0;
87 /**
88 * There is a hardware limitation. If the APB clock frequency is high, the step of this reg signal: ``onetime_start`` may not be captured by the
89 * ADC digital controller (when its clock frequency is too slow). A rough estimate for this step should be at least 3 ADC digital controller
90 * clock cycle.
91 */
92 uint32_t digi_clk = clk_src_freq_hz / (ADC_LL_CLKM_DIV_NUM_DEFAULT + ADC_LL_CLKM_DIV_A_DEFAULT / ADC_LL_CLKM_DIV_B_DEFAULT + 1);
93 //Convert frequency to time (us). Since decimals are removed by this division operation. Add 1 here in case of the fact that delay is not enough.
94 delay = (1000 * 1000) / digi_clk + 1;
95 //3 ADC digital controller clock cycle
96 delay = delay * 3;
97 HAL_EARLY_LOGD("adc_hal", "clk_src_freq_hz: %d, digi_clk: %d, delay: %d", clk_src_freq_hz, digi_clk, delay);
98
99 //This coefficient (8) is got from test, and verified from DT. When digi_clk is not smaller than ``APB_CLK_FREQ/8``, no delay is needed.
100 if (digi_clk >= APB_CLK_FREQ/8) {
101 delay = 0;
102 }
103
104 HAL_EARLY_LOGD("adc_hal", "delay: %d", delay);
105 adc_oneshot_ll_start(false);
106 esp_rom_delay_us(delay);
107 adc_oneshot_ll_start(true);
108
109 //No need to delay here. Becuase if the start signal is not seen, there won't be a done intr.
110 #else
111 adc_oneshot_ll_start(unit);
112 #endif
113 }
114
adc_oneshot_hal_convert(adc_oneshot_hal_ctx_t * hal,int * out_raw)115 bool adc_oneshot_hal_convert(adc_oneshot_hal_ctx_t *hal, int *out_raw)
116 {
117 bool valid = true;
118 uint32_t event = 0;
119 if (hal->unit == ADC_UNIT_1) {
120 event = ADC_LL_EVENT_ADC1_ONESHOT_DONE;
121 } else {
122 event = ADC_LL_EVENT_ADC2_ONESHOT_DONE;
123 }
124
125 adc_oneshot_ll_clear_event(event);
126 adc_oneshot_ll_disable_all_unit();
127 adc_oneshot_ll_enable(hal->unit);
128
129 adc_hal_onetime_start(hal->unit, hal->clk_src_freq_hz);
130 while (!adc_oneshot_ll_get_event(event)) {
131 ;
132 }
133 *out_raw = adc_oneshot_ll_get_raw_result(hal->unit);
134 #if (SOC_ADC_PERIPH_NUM == 2)
135 if (hal->unit == ADC_UNIT_2) {
136 valid = adc_oneshot_ll_raw_check_valid(ADC_UNIT_2, *out_raw);
137 if (!valid) {
138 *out_raw = -1;
139 }
140 }
141 #endif
142
143 adc_oneshot_ll_disable_all_unit();
144 return valid;
145 }
146
147 /*---------------------------------------------------------------
148 Workarounds
149 ---------------------------------------------------------------*/
150 #if CONFIG_ADC_DISABLE_DAC_OUTPUT
s_disable_dac(adc_oneshot_hal_ctx_t * hal,adc_channel_t channel)151 static void s_disable_dac(adc_oneshot_hal_ctx_t *hal, adc_channel_t channel)
152 {
153 /**
154 * Workaround: Disable the synchronization operation function of ADC1 and DAC.
155 * If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage.
156 */
157 if (hal->unit == ADC_UNIT_1) {
158 dac_ll_rtc_sync_by_adc(false);
159 }
160
161 #if CONFIG_IDF_TARGET_ESP32
162 if (hal->unit == ADC_UNIT_2) {
163 if (channel == ADC_CHANNEL_8) {
164 dac_ll_power_down(DAC_CHAN_0); // the same as DAC channel 0
165 }
166 if (channel == ADC_CHANNEL_9) {
167 dac_ll_power_down(DAC_CHAN_1);
168 }
169 }
170 #elif CONFIG_IDF_TARGET_ESP32S2
171 if (hal->unit == ADC_UNIT_2) {
172 if (channel == ADC_CHANNEL_6) {
173 dac_ll_power_down(DAC_CHAN_0); // the same as DAC channel 0
174 }
175 if (channel == ADC_CHANNEL_7) {
176 dac_ll_power_down(DAC_CHAN_1);
177 }
178 }
179 #else
180 //Nothing needed (DAC is only supported on ESP32 and ESP32S2), add this if future chips needs
181 #endif
182 }
183 #endif
184