1 // Copyright 2019-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 
15 #include "soc/soc_caps.h"
16 #include "hal/adc_hal.h"
17 #include "hal/adc_hal_conf.h"
18 #include "sdkconfig.h"
19 #include <sys/param.h>
20 
21 
22 #if CONFIG_IDF_TARGET_ESP32C3
23 #include "soc/gdma_channel.h"
24 #include "soc/soc.h"
25 #include "esp_rom_sys.h"
26 
27 typedef enum {
28     ADC_EVENT_ADC1_DONE = BIT(0),
29     ADC_EVENT_ADC2_DONE = BIT(1),
30 } adc_hal_event_t;
31 
32 #endif
33 
adc_hal_init(void)34 void adc_hal_init(void)
35 {
36     // Set internal FSM wait time, fixed value.
37     adc_ll_digi_set_fsm_time(SOC_ADC_FSM_RSTB_WAIT_DEFAULT, SOC_ADC_FSM_START_WAIT_DEFAULT,
38                              SOC_ADC_FSM_STANDBY_WAIT_DEFAULT);
39     adc_ll_set_sample_cycle(ADC_FSM_SAMPLE_CYCLE_DEFAULT);
40     adc_hal_pwdet_set_cct(SOC_ADC_PWDET_CCT_DEFAULT);
41     adc_ll_digi_output_invert(ADC_NUM_1, SOC_ADC_DIGI_DATA_INVERT_DEFAULT(ADC_NUM_1));
42     adc_ll_digi_output_invert(ADC_NUM_2, SOC_ADC_DIGI_DATA_INVERT_DEFAULT(ADC_NUM_2));
43     adc_ll_digi_set_clk_div(SOC_ADC_DIGI_SAR_CLK_DIV_DEFAULT);
44 }
45 
46 /*---------------------------------------------------------------
47                     ADC calibration setting
48 ---------------------------------------------------------------*/
49 #if SOC_ADC_HW_CALIBRATION_V1
50 // ESP32-S2 and C3 support HW offset calibration.
51 
adc_hal_calibration_init(adc_ll_num_t adc_n)52 void adc_hal_calibration_init(adc_ll_num_t adc_n)
53 {
54     adc_ll_calibration_init(adc_n);
55 }
56 
57 static uint32_t s_previous_init_code[SOC_ADC_PERIPH_NUM] = {-1, -1};
58 
adc_hal_set_calibration_param(adc_ll_num_t adc_n,uint32_t param)59 void adc_hal_set_calibration_param(adc_ll_num_t adc_n, uint32_t param)
60 {
61     if (param != s_previous_init_code[adc_n]) {
62         adc_ll_set_calibration_param(adc_n, param);
63         s_previous_init_code[adc_n] = param;
64     }
65 }
66 
67 #if CONFIG_IDF_TARGET_ESP32S2
cal_setup(adc_ll_num_t adc_n,adc_channel_t channel,adc_atten_t atten,bool internal_gnd)68 static void cal_setup(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten, bool internal_gnd)
69 {
70     adc_hal_set_controller(adc_n, ADC_CTRL_RTC);    //Set controller
71 
72     /* Enable/disable internal connect GND (for calibration). */
73     if (internal_gnd) {
74         adc_ll_rtc_disable_channel(adc_n);
75         adc_ll_set_atten(adc_n, 0, atten);  // Note: when disable all channel, HW auto select channel0 atten param.
76     } else {
77         adc_ll_rtc_enable_channel(adc_n, channel);
78         adc_ll_set_atten(adc_n, channel, atten);
79     }
80 }
81 
read_cal_channel(adc_ll_num_t adc_n,int channel)82 static uint32_t read_cal_channel(adc_ll_num_t adc_n, int channel)
83 {
84     adc_ll_rtc_start_convert(adc_n, channel);
85     while (adc_ll_rtc_convert_is_done(adc_n) != true);
86     return (uint32_t)adc_ll_rtc_get_convert_value(adc_n);
87 }
88 
89 #elif CONFIG_IDF_TARGET_ESP32C3
cal_setup(adc_ll_num_t adc_n,adc_channel_t channel,adc_atten_t atten,bool internal_gnd)90 static void cal_setup(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten, bool internal_gnd)
91 {
92     adc_ll_onetime_sample_enable(ADC_NUM_1, false);
93     adc_ll_onetime_sample_enable(ADC_NUM_2, false);
94     /* Enable/disable internal connect GND (for calibration). */
95     if (internal_gnd) {
96         const int esp32c3_invalid_chan = (adc_n == ADC_NUM_1)? 0xF: 0x1;
97         adc_ll_onetime_set_channel(adc_n, esp32c3_invalid_chan);
98     } else {
99         adc_ll_onetime_set_channel(adc_n, channel);
100     }
101     adc_ll_onetime_set_atten(atten);
102     adc_ll_onetime_sample_enable(adc_n, true);
103 }
104 
read_cal_channel(adc_ll_num_t adc_n,int channel)105 static uint32_t read_cal_channel(adc_ll_num_t adc_n, int channel)
106 {
107     adc_ll_intr_clear(ADC_LL_INTR_ADC1_DONE | ADC_LL_INTR_ADC2_DONE);
108     adc_ll_onetime_start(false);
109     esp_rom_delay_us(5);
110     adc_ll_onetime_start(true);
111 
112     while(!adc_ll_intr_get_raw(ADC_LL_INTR_ADC1_DONE | ADC_LL_INTR_ADC2_DONE));
113 
114     uint32_t read_val = -1;
115     if (adc_n == ADC_NUM_1) {
116         read_val = adc_ll_adc1_read();
117     } else if (adc_n == ADC_NUM_2) {
118         read_val = adc_ll_adc2_read();
119         if (adc_ll_analysis_raw_data(adc_n, read_val)) {
120             return -1;
121         }
122     }
123     return read_val;
124 }
125 #endif //CONFIG_IDF_TARGET_*
126 
127 #define ADC_HAL_CAL_TIMES        (10)
128 #define ADC_HAL_CAL_OFFSET_RANGE (4096)
129 
adc_hal_self_calibration(adc_ll_num_t adc_n,adc_channel_t channel,adc_atten_t atten,bool internal_gnd)130 uint32_t adc_hal_self_calibration(adc_ll_num_t adc_n, adc_channel_t channel, adc_atten_t atten, bool internal_gnd)
131 {
132     if (adc_n == ADC_NUM_2) {
133         adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
134         adc_hal_arbiter_config(&config);
135     }
136 
137     cal_setup(adc_n, channel, atten, internal_gnd);
138 
139     adc_ll_calibration_prepare(adc_n, channel, internal_gnd);
140 
141     uint32_t code_list[ADC_HAL_CAL_TIMES] = {0};
142     uint32_t code_sum = 0;
143     uint32_t code_h = 0;
144     uint32_t code_l = 0;
145     uint32_t chk_code = 0;
146 
147     for (uint8_t rpt = 0 ; rpt < ADC_HAL_CAL_TIMES ; rpt ++) {
148         code_h = ADC_HAL_CAL_OFFSET_RANGE;
149         code_l = 0;
150         chk_code = (code_h + code_l) / 2;
151         adc_ll_set_calibration_param(adc_n, chk_code);
152         uint32_t self_cal = read_cal_channel(adc_n, channel);
153         while (code_h - code_l > 1) {
154             if (self_cal == 0) {
155                 code_h = chk_code;
156             } else {
157                 code_l = chk_code;
158             }
159             chk_code = (code_h + code_l) / 2;
160             adc_ll_set_calibration_param(adc_n, chk_code);
161             self_cal = read_cal_channel(adc_n, channel);
162             if ((code_h - code_l == 1)) {
163                 chk_code += 1;
164                 adc_ll_set_calibration_param(adc_n, chk_code);
165                 self_cal = read_cal_channel(adc_n, channel);
166             }
167         }
168         code_list[rpt] = chk_code;
169         code_sum += chk_code;
170     }
171 
172     code_l = code_list[0];
173     code_h = code_list[0];
174     for (uint8_t i = 0 ; i < ADC_HAL_CAL_TIMES ; i++) {
175         code_l = MIN(code_l, code_list[i]);
176         code_h = MAX(code_h, code_list[i]);
177     }
178 
179     chk_code = code_h + code_l;
180     uint32_t ret = ((code_sum - chk_code) % (ADC_HAL_CAL_TIMES - 2) < 4)
181            ? (code_sum - chk_code) / (ADC_HAL_CAL_TIMES - 2)
182            : (code_sum - chk_code) / (ADC_HAL_CAL_TIMES - 2) + 1;
183 
184     adc_ll_calibration_finish(adc_n);
185     return ret;
186 }
187 #endif //SOC_ADC_HW_CALIBRATION_V1
188 
189 #if CONFIG_IDF_TARGET_ESP32C3
190 //This feature is currently supported on ESP32C3, will be supported on other chips soon
191 /*---------------------------------------------------------------
192                     DMA setting
193 ---------------------------------------------------------------*/
adc_hal_context_config(adc_hal_context_t * hal,const adc_hal_config_t * config)194 void adc_hal_context_config(adc_hal_context_t *hal, const adc_hal_config_t *config)
195 {
196     hal->dev = &GDMA;
197     hal->desc_dummy_head.next = hal->rx_desc;
198     hal->desc_max_num = config->desc_max_num;
199     hal->dma_chan = config->dma_chan;
200     hal->eof_num = config->eof_num;
201 }
202 
adc_hal_digi_init(adc_hal_context_t * hal)203 void adc_hal_digi_init(adc_hal_context_t *hal)
204 {
205     gdma_ll_clear_interrupt_status(hal->dev, hal->dma_chan, UINT32_MAX);
206     gdma_ll_enable_interrupt(hal->dev, hal->dma_chan, GDMA_LL_EVENT_RX_SUC_EOF, true);
207     adc_ll_digi_dma_set_eof_num(hal->eof_num);
208     adc_ll_onetime_sample_enable(ADC_NUM_1, false);
209     adc_ll_onetime_sample_enable(ADC_NUM_2, false);
210 }
211 
adc_hal_fifo_reset(adc_hal_context_t * hal)212 void adc_hal_fifo_reset(adc_hal_context_t *hal)
213 {
214     adc_ll_digi_reset();
215     gdma_ll_rx_reset_channel(hal->dev, hal->dma_chan);
216 }
217 
adc_hal_digi_dma_link_descriptors(dma_descriptor_t * desc,uint8_t * data_buf,uint32_t size,uint32_t num)218 static void adc_hal_digi_dma_link_descriptors(dma_descriptor_t *desc, uint8_t *data_buf, uint32_t size, uint32_t num)
219 {
220     assert(((uint32_t)data_buf % 4) == 0);
221     assert((size % 4) == 0);
222     uint32_t n = 0;
223 
224     while (num--) {
225         desc[n].dw0.size = size;
226         desc[n].dw0.suc_eof = 0;
227         desc[n].dw0.owner = 1;
228         desc[n].buffer = data_buf;
229         desc[n].next = &desc[n+1];
230         data_buf += size;
231         n++;
232     }
233     desc[n-1].next = NULL;
234 }
235 
adc_hal_digi_rxdma_start(adc_hal_context_t * hal,uint8_t * data_buf)236 void adc_hal_digi_rxdma_start(adc_hal_context_t *hal, uint8_t *data_buf)
237 {
238     //reset the current descriptor address
239     hal->cur_desc_ptr = &hal->desc_dummy_head;
240     adc_hal_digi_dma_link_descriptors(hal->rx_desc, data_buf, hal->eof_num * ADC_HAL_DATA_LEN_PER_CONV, hal->desc_max_num);
241     gdma_ll_rx_set_desc_addr(hal->dev, hal->dma_chan, (uint32_t)hal->rx_desc);
242     gdma_ll_rx_start(hal->dev, hal->dma_chan);
243 }
244 
adc_hal_digi_start(adc_hal_context_t * hal)245 void adc_hal_digi_start(adc_hal_context_t *hal)
246 {
247     //the ADC data will be sent to the DMA
248     adc_ll_digi_dma_enable();
249     //enable sar adc timer
250     adc_ll_digi_trigger_enable();
251 }
252 
adc_hal_get_reading_result(adc_hal_context_t * hal,const intptr_t eof_desc_addr,dma_descriptor_t ** cur_desc)253 adc_hal_dma_desc_status_t adc_hal_get_reading_result(adc_hal_context_t *hal, const intptr_t eof_desc_addr, dma_descriptor_t **cur_desc)
254 {
255     assert(hal->cur_desc_ptr);
256     if (!hal->cur_desc_ptr->next) {
257         return ADC_HAL_DMA_DESC_NULL;
258     }
259     if ((intptr_t)hal->cur_desc_ptr == eof_desc_addr) {
260         return ADC_HAL_DMA_DESC_WAITING;
261     }
262 
263     hal->cur_desc_ptr = hal->cur_desc_ptr->next;
264     *cur_desc = hal->cur_desc_ptr;
265 
266     return ADC_HAL_DMA_DESC_VALID;
267 }
268 
adc_hal_digi_rxdma_stop(adc_hal_context_t * hal)269 void adc_hal_digi_rxdma_stop(adc_hal_context_t *hal)
270 {
271     gdma_ll_rx_stop(hal->dev, hal->dma_chan);
272 }
273 
adc_hal_digi_clr_intr(adc_hal_context_t * hal,uint32_t mask)274 void adc_hal_digi_clr_intr(adc_hal_context_t *hal, uint32_t mask)
275 {
276     gdma_ll_clear_interrupt_status(hal->dev, hal->dma_chan, mask);
277 }
278 
adc_hal_digi_dis_intr(adc_hal_context_t * hal,uint32_t mask)279 void adc_hal_digi_dis_intr(adc_hal_context_t *hal, uint32_t mask)
280 {
281     gdma_ll_enable_interrupt(hal->dev, hal->dma_chan, mask, false);
282 }
283 
adc_hal_digi_stop(adc_hal_context_t * hal)284 void adc_hal_digi_stop(adc_hal_context_t *hal)
285 {
286     //Set to 0: the ADC data won't be sent to the DMA
287     adc_ll_digi_dma_disable();
288     //disable sar adc timer
289     adc_ll_digi_trigger_disable();
290 }
291 
292 /*---------------------------------------------------------------
293                     Single Read
294 ---------------------------------------------------------------*/
295 
296 //--------------------INTR-------------------------------//
get_event_intr(adc_hal_event_t event)297 static adc_ll_intr_t get_event_intr(adc_hal_event_t event)
298 {
299     adc_ll_intr_t intr_mask = 0;
300     if (event & ADC_EVENT_ADC1_DONE) {
301         intr_mask |= ADC_LL_INTR_ADC1_DONE;
302     }
303     if (event & ADC_EVENT_ADC2_DONE) {
304         intr_mask |= ADC_LL_INTR_ADC2_DONE;
305     }
306     return intr_mask;
307 }
308 
adc_hal_intr_clear(adc_hal_event_t event)309 static void adc_hal_intr_clear(adc_hal_event_t event)
310 {
311     adc_ll_intr_clear(get_event_intr(event));
312 }
313 
adc_hal_intr_get_raw(adc_hal_event_t event)314 static bool adc_hal_intr_get_raw(adc_hal_event_t event)
315 {
316     return adc_ll_intr_get_raw(get_event_intr(event));
317 }
318 
319 //--------------------Single Read-------------------------------//
adc_hal_onetime_start(void)320 static void adc_hal_onetime_start(void)
321 {
322     /**
323      * 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
324      * ADC digital controller (when its clock frequency is too slow). A rough estimate for this step should be at least 3 ADC digital controller
325      * clock cycle.
326      *
327      * This limitation will be removed in hardware future versions.
328      *
329      */
330     uint32_t digi_clk = APB_CLK_FREQ / (ADC_LL_CLKM_DIV_NUM_DEFAULT + ADC_LL_CLKM_DIV_A_DEFAULT / ADC_LL_CLKM_DIV_B_DEFAULT + 1);
331     //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.
332     uint32_t delay = (1000 * 1000) / digi_clk + 1;
333     //3 ADC digital controller clock cycle
334     delay = delay * 3;
335     //This coefficient (8) is got from test. When digi_clk is not smaller than ``APB_CLK_FREQ/8``, no delay is needed.
336     if (digi_clk >= APB_CLK_FREQ/8) {
337         delay = 0;
338     }
339 
340     adc_ll_onetime_start(false);
341     esp_rom_delay_us(delay);
342     adc_ll_onetime_start(true);
343     //No need to delay here. Becuase if the start signal is not seen, there won't be a done intr.
344 }
345 
adc_hal_single_read(adc_ll_num_t adc_n,int * out_raw)346 static esp_err_t adc_hal_single_read(adc_ll_num_t adc_n, int *out_raw)
347 {
348     if (adc_n == ADC_NUM_1) {
349         *out_raw = adc_ll_adc1_read();
350     } else if (adc_n == ADC_NUM_2) {
351         *out_raw = adc_ll_adc2_read();
352         if (adc_ll_analysis_raw_data(adc_n, *out_raw)) {
353             return ESP_ERR_INVALID_STATE;
354         }
355     }
356     return ESP_OK;
357 }
358 
adc_hal_convert(adc_ll_num_t adc_n,int channel,int * out_raw)359 esp_err_t adc_hal_convert(adc_ll_num_t adc_n, int channel, int *out_raw)
360 {
361     esp_err_t ret;
362     adc_hal_event_t event;
363 
364     if (adc_n == ADC_NUM_1) {
365         event = ADC_EVENT_ADC1_DONE;
366     } else {
367         event = ADC_EVENT_ADC2_DONE;
368     }
369 
370     adc_hal_intr_clear(event);
371     adc_ll_onetime_sample_enable(ADC_NUM_1, false);
372     adc_ll_onetime_sample_enable(ADC_NUM_2, false);
373     adc_ll_onetime_sample_enable(adc_n, true);
374     adc_ll_onetime_set_channel(adc_n, channel);
375 
376     //Trigger single read.
377     adc_hal_onetime_start();
378     while (!adc_hal_intr_get_raw(event));
379     ret = adc_hal_single_read(adc_n, out_raw);
380     //HW workaround: when enabling periph clock, this should be false
381     adc_ll_onetime_sample_enable(adc_n, false);
382 
383     return ret;
384 }
385 #else // !CONFIG_IDF_TARGET_ESP32C3
adc_hal_convert(adc_ll_num_t adc_n,int channel,int * out_raw)386 esp_err_t adc_hal_convert(adc_ll_num_t adc_n, int channel, int *out_raw)
387 {
388     adc_ll_rtc_enable_channel(adc_n, channel);
389     adc_ll_rtc_start_convert(adc_n, channel);
390     while (adc_ll_rtc_convert_is_done(adc_n) != true);
391     *out_raw = adc_ll_rtc_get_convert_value(adc_n);
392 
393     if ((int)adc_ll_rtc_analysis_raw_data(adc_n, (uint16_t)(*out_raw))) {
394         return ESP_ERR_INVALID_STATE;
395     }
396 
397     return ESP_OK;
398 }
399 #endif  //#if !CONFIG_IDF_TARGET_ESP32C3
400