adc_hal.c 13 KB

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