adc_hal.c 13 KB

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