adc_deprecated.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*----------------------------------------------------------------------------------
  7. This file contains ESP32 and ESP32S2 Depricated ADC APIs and functions
  8. -----------------------------------------------------------------------------------*/
  9. #include "sdkconfig.h"
  10. #include "esp_types.h"
  11. #include "esp_log.h"
  12. #include "esp_intr_alloc.h"
  13. #include "driver/rtc_io.h"
  14. #include "hal/adc_ll.h"
  15. #include "hal/adc_types.h"
  16. #include "hal/adc_hal_conf.h"
  17. #ifdef CONFIG_PM_ENABLE
  18. #include "esp_pm.h"
  19. #endif
  20. #include "adc.h"
  21. #include "esp_private/adc_cali.h"
  22. #include "freertos/FreeRTOS.h"
  23. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  24. #include "deprecated/driver/adc_types_deprecated.h"
  25. static const char *ADC_TAG = "ADC";
  26. #define ADC_CHECK_RET(fun_ret) ({ \
  27. if (fun_ret != ESP_OK) { \
  28. ESP_LOGE(ADC_TAG,"%s:%d\n",__FUNCTION__,__LINE__); \
  29. return ESP_FAIL; \
  30. } \
  31. })
  32. #define ADC_CHECK(a, str, ret_val) ({ \
  33. if (!(a)) { \
  34. ESP_LOGE(ADC_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  35. return (ret_val); \
  36. } \
  37. })
  38. #define ADC_CHANNEL_CHECK(periph, channel) ADC_CHECK(channel < SOC_ADC_CHANNEL_NUM(periph), "ADC"#periph" channel error", ESP_ERR_INVALID_ARG)
  39. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  40. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  41. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  42. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  43. #ifdef CONFIG_PM_ENABLE
  44. esp_pm_lock_handle_t adc_digi_arbiter_lock = NULL;
  45. #endif //CONFIG_PM_ENABLE
  46. #if CONFIG_IDF_TARGET_ESP32
  47. /*---------------------------------------------------------------
  48. ESP32 Depricated ADC APIs and functions
  49. ---------------------------------------------------------------*/
  50. #define ADC_MEAS_NUM_LIM_DEFAULT (1)
  51. #define ADC_MAX_MEAS_NUM_DEFAULT (255)
  52. #define DIG_ADC_OUTPUT_FORMAT_DEFUALT (ADC_DIGI_FORMAT_12BIT)
  53. #define DIG_ADC_ATTEN_DEFUALT (ADC_ATTEN_DB_11)
  54. #define DIG_ADC_BIT_WIDTH_DEFUALT (ADC_WIDTH_BIT_12)
  55. esp_err_t adc_digi_init(void)
  56. {
  57. ADC_ENTER_CRITICAL();
  58. adc_ll_digi_set_fsm_time(ADC_HAL_FSM_RSTB_WAIT_DEFAULT, ADC_HAL_FSM_START_WAIT_DEFAULT,
  59. ADC_HAL_FSM_STANDBY_WAIT_DEFAULT);
  60. adc_ll_set_sample_cycle(ADC_HAL_SAMPLE_CYCLE_DEFAULT);
  61. adc_hal_pwdet_set_cct(ADC_HAL_PWDET_CCT_DEFAULT);
  62. adc_ll_digi_output_invert(ADC_UNIT_1, ADC_HAL_DIGI_DATA_INVERT_DEFAULT(ADC_UNIT_1));
  63. adc_ll_digi_output_invert(ADC_UNIT_2, ADC_HAL_DIGI_DATA_INVERT_DEFAULT(ADC_UNIT_2));
  64. adc_ll_digi_set_clk_div(ADC_HAL_DIGI_SAR_CLK_DIV_DEFAULT);
  65. ADC_EXIT_CRITICAL();
  66. return ESP_OK;
  67. }
  68. esp_err_t adc_digi_deinit(void)
  69. {
  70. adc_power_release();
  71. ADC_ENTER_CRITICAL();
  72. adc_hal_digi_deinit(NULL);
  73. ADC_EXIT_CRITICAL();
  74. return ESP_OK;
  75. }
  76. /**
  77. * Set adc output 16-bit-data format from digital controller.
  78. *
  79. * @param data_sel 1: [15] unit, [14:11] channel, [10:0] data, 11-bit-width at most. Only work under `ADC_LL_DIGI_CONV_BOTH_UNIT` or `ADC_LL_DIGI_CONV_ALTER_UNIT` mode.
  80. * 0: [15:12] channel, [11:0] data, 12-bit-width at most. Only work under `ADC_LL_DIGI_CONV_ONLY_ADC1` or `ADC_LL_DIGI_CONV_ONLY_ADC2` mode
  81. * @note see `adc_ll_digi_pattern_table_t` for more detail of data bit width
  82. */
  83. static inline void adc_ll_digi_set_output_format(bool data_sel)
  84. {
  85. SYSCON.saradc_ctrl.data_sar_sel = data_sel;
  86. }
  87. static inline void adc_ll_digi_prepare_pattern_table(adc_unit_t adc_n, uint32_t pattern_index, adc_digi_pattern_table_t pattern)
  88. {
  89. uint32_t tab;
  90. uint8_t index = pattern_index / 4;
  91. uint8_t offset = (pattern_index % 4) * 8;
  92. if (adc_n == ADC_UNIT_1) {
  93. tab = SYSCON.saradc_sar1_patt_tab[index]; // Read old register value
  94. tab &= (~(0xFF000000 >> offset)); // clear old data
  95. tab |= ((uint32_t)pattern.val << 24) >> offset; // Fill in the new data
  96. SYSCON.saradc_sar1_patt_tab[index] = tab; // Write back
  97. } else { // adc_n == ADC_UNIT_2
  98. tab = SYSCON.saradc_sar2_patt_tab[index]; // Read old register value
  99. tab &= (~(0xFF000000 >> offset)); // clear old data
  100. tab |= ((uint32_t)pattern.val << 24) >> offset; // Fill in the new data
  101. SYSCON.saradc_sar2_patt_tab[index] = tab; // Write back
  102. }
  103. }
  104. void adc_digi_controller_reg_set(const adc_digi_config_t *cfg)
  105. {
  106. /* On ESP32, only support ADC1 */
  107. switch (cfg->conv_mode) {
  108. case ADC_CONV_SINGLE_UNIT_1:
  109. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_ONLY_ADC1);
  110. break;
  111. case ADC_CONV_SINGLE_UNIT_2:
  112. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_ONLY_ADC2);
  113. break;
  114. case ADC_CONV_BOTH_UNIT:
  115. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_BOTH_UNIT);
  116. break;
  117. case ADC_CONV_ALTER_UNIT:
  118. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_ALTER_UNIT);
  119. break;
  120. default:
  121. abort();
  122. }
  123. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  124. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_DIG);
  125. if (cfg->adc1_pattern_len) {
  126. adc_ll_digi_clear_pattern_table(ADC_UNIT_1);
  127. adc_ll_digi_set_pattern_table_len(ADC_UNIT_1, cfg->adc1_pattern_len);
  128. for (uint32_t i = 0; i < cfg->adc1_pattern_len; i++) {
  129. adc_ll_digi_prepare_pattern_table(ADC_UNIT_1, i, cfg->adc1_pattern[i]);
  130. }
  131. }
  132. }
  133. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  134. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_DIG);
  135. if (cfg->adc2_pattern_len) {
  136. adc_ll_digi_clear_pattern_table(ADC_UNIT_2);
  137. adc_ll_digi_set_pattern_table_len(ADC_UNIT_2, cfg->adc2_pattern_len);
  138. for (uint32_t i = 0; i < cfg->adc2_pattern_len; i++) {
  139. adc_ll_digi_prepare_pattern_table(ADC_UNIT_2, i, cfg->adc2_pattern[i]);
  140. }
  141. }
  142. }
  143. adc_ll_digi_set_output_format(cfg->format);
  144. if (cfg->conv_limit_en) {
  145. adc_ll_digi_set_convert_limit_num(cfg->conv_limit_num);
  146. adc_ll_digi_convert_limit_enable();
  147. } else {
  148. adc_ll_digi_convert_limit_disable();
  149. }
  150. adc_ll_digi_set_data_source(ADC_I2S_DATA_SRC_ADC);
  151. }
  152. esp_err_t adc_digi_controller_config(const adc_digi_config_t *config)
  153. {
  154. adc_power_acquire();
  155. ADC_ENTER_CRITICAL();
  156. adc_digi_controller_reg_set(config);
  157. ADC_EXIT_CRITICAL();
  158. return ESP_OK;
  159. }
  160. esp_err_t adc_set_i2s_data_source(adc_i2s_source_t src)
  161. {
  162. ADC_CHECK(src < ADC_I2S_DATA_SRC_MAX, "ADC i2s data source error", ESP_ERR_INVALID_ARG);
  163. ADC_ENTER_CRITICAL();
  164. adc_ll_digi_set_data_source(src);
  165. ADC_EXIT_CRITICAL();
  166. return ESP_OK;
  167. }
  168. extern esp_err_t adc_common_gpio_init(adc_unit_t adc_unit, adc_channel_t channel);
  169. esp_err_t adc_i2s_mode_init(adc_unit_t adc_unit, adc_channel_t channel)
  170. {
  171. if (adc_unit == ADC_UNIT_1) {
  172. ADC_CHECK((SOC_ADC_SUPPORT_DMA_MODE(ADC_UNIT_1)), "ADC1 not support DMA for now.", ESP_ERR_INVALID_ARG);
  173. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  174. } else if (adc_unit == ADC_UNIT_2) {
  175. //ADC2 does not support DMA mode
  176. ADC_CHECK((SOC_ADC_SUPPORT_DMA_MODE(ADC_UNIT_2)), "ADC2 not support DMA for now.", ESP_ERR_INVALID_ARG);
  177. ADC_CHANNEL_CHECK(ADC_UNIT_2, channel);
  178. }
  179. adc_digi_pattern_table_t adc1_pattern[1];
  180. adc_digi_pattern_table_t adc2_pattern[1];
  181. adc_digi_config_t dig_cfg = {
  182. .conv_limit_en = ADC_MEAS_NUM_LIM_DEFAULT,
  183. .conv_limit_num = ADC_MAX_MEAS_NUM_DEFAULT,
  184. .format = DIG_ADC_OUTPUT_FORMAT_DEFUALT,
  185. .conv_mode = ADC_CONV_SINGLE_UNIT_1,
  186. };
  187. if (adc_unit == ADC_UNIT_1) {
  188. adc1_pattern[0].atten = DIG_ADC_ATTEN_DEFUALT;
  189. adc1_pattern[0].bit_width = DIG_ADC_BIT_WIDTH_DEFUALT;
  190. adc1_pattern[0].channel = channel;
  191. dig_cfg.adc1_pattern_len = 1;
  192. dig_cfg.adc1_pattern = adc1_pattern;
  193. } else if (adc_unit == ADC_UNIT_2) {
  194. adc2_pattern[0].atten = DIG_ADC_ATTEN_DEFUALT;
  195. adc2_pattern[0].bit_width = DIG_ADC_BIT_WIDTH_DEFUALT;
  196. adc2_pattern[0].channel = channel;
  197. dig_cfg.adc2_pattern_len = 1;
  198. dig_cfg.adc2_pattern = adc2_pattern;
  199. }
  200. adc_common_gpio_init(adc_unit, channel);
  201. ADC_ENTER_CRITICAL();
  202. adc_ll_digi_set_fsm_time(ADC_HAL_FSM_RSTB_WAIT_DEFAULT, ADC_HAL_FSM_START_WAIT_DEFAULT,
  203. ADC_HAL_FSM_STANDBY_WAIT_DEFAULT);
  204. adc_ll_set_sample_cycle(ADC_HAL_SAMPLE_CYCLE_DEFAULT);
  205. adc_hal_pwdet_set_cct(ADC_HAL_PWDET_CCT_DEFAULT);
  206. adc_ll_digi_output_invert(ADC_UNIT_1, ADC_HAL_DIGI_DATA_INVERT_DEFAULT(ADC_UNIT_1));
  207. adc_ll_digi_output_invert(ADC_UNIT_2, ADC_HAL_DIGI_DATA_INVERT_DEFAULT(ADC_UNIT_2));
  208. adc_ll_digi_set_clk_div(ADC_HAL_DIGI_SAR_CLK_DIV_DEFAULT);
  209. adc_digi_controller_reg_set(&dig_cfg);
  210. ADC_EXIT_CRITICAL();
  211. return ESP_OK;
  212. }
  213. #endif //#if CONFIG_IDF_TARGET_ESP32
  214. #if CONFIG_IDF_TARGET_ESP32S2
  215. /*---------------------------------------------------------------
  216. ESP32S2 Depricated ADC functions and APIs
  217. ---------------------------------------------------------------*/
  218. esp_err_t adc_arbiter_config(adc_unit_t adc_unit, adc_arbiter_t *config)
  219. {
  220. if (adc_unit == ADC_UNIT_1) {
  221. return ESP_ERR_NOT_SUPPORTED;
  222. }
  223. ADC_ENTER_CRITICAL();
  224. adc_hal_arbiter_config(config);
  225. ADC_EXIT_CRITICAL();
  226. return ESP_OK;
  227. }
  228. /**
  229. * Enable interrupt of adc digital controller by bitmask.
  230. *
  231. * @param adc_n ADC unit.
  232. * @param intr Interrupt bitmask.
  233. */
  234. static inline void adc_ll_digi_intr_enable(adc_unit_t adc_n, adc_digi_intr_t intr)
  235. {
  236. if (adc_n == ADC_UNIT_1) {
  237. if (intr & ADC_DIGI_INTR_MASK_MONITOR) {
  238. APB_SARADC.int_ena.adc1_thres = 1;
  239. }
  240. if (intr & ADC_DIGI_INTR_MASK_MEAS_DONE) {
  241. APB_SARADC.int_ena.adc1_done = 1;
  242. }
  243. } else { // adc_n == ADC_UNIT_2
  244. if (intr & ADC_DIGI_INTR_MASK_MONITOR) {
  245. APB_SARADC.int_ena.adc2_thres = 1;
  246. }
  247. if (intr & ADC_DIGI_INTR_MASK_MEAS_DONE) {
  248. APB_SARADC.int_ena.adc2_done = 1;
  249. }
  250. }
  251. }
  252. esp_err_t adc_digi_intr_enable(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  253. {
  254. ADC_ENTER_CRITICAL();
  255. if (adc_unit == ADC_UNIT_1) {
  256. adc_ll_digi_intr_enable(ADC_UNIT_1, intr_mask);
  257. } else if (adc_unit == ADC_UNIT_2) {
  258. adc_ll_digi_intr_enable(ADC_UNIT_2, intr_mask);
  259. }
  260. ADC_EXIT_CRITICAL();
  261. return ESP_OK;
  262. }
  263. /**
  264. * Disable interrupt of adc digital controller by bitmask.
  265. *
  266. * @param adc_n ADC unit.
  267. * @param intr Interrupt bitmask.
  268. */
  269. static inline void adc_ll_digi_intr_disable(adc_unit_t adc_n, adc_digi_intr_t intr)
  270. {
  271. if (adc_n == ADC_UNIT_1) {
  272. if (intr & ADC_DIGI_INTR_MASK_MONITOR) {
  273. APB_SARADC.int_ena.adc1_thres = 0;
  274. }
  275. if (intr & ADC_DIGI_INTR_MASK_MEAS_DONE) {
  276. APB_SARADC.int_ena.adc1_done = 0;
  277. }
  278. } else { // adc_n == ADC_UNIT_2
  279. if (intr & ADC_DIGI_INTR_MASK_MONITOR) {
  280. APB_SARADC.int_ena.adc2_thres = 0;
  281. }
  282. if (intr & ADC_DIGI_INTR_MASK_MEAS_DONE) {
  283. APB_SARADC.int_ena.adc2_done = 0;
  284. }
  285. }
  286. }
  287. esp_err_t adc_digi_intr_disable(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  288. {
  289. ADC_ENTER_CRITICAL();
  290. if (adc_unit == ADC_UNIT_1) {
  291. adc_ll_digi_intr_disable(ADC_UNIT_1, intr_mask);
  292. } else if (adc_unit == ADC_UNIT_2) {
  293. adc_ll_digi_intr_disable(ADC_UNIT_2, intr_mask);
  294. }
  295. ADC_EXIT_CRITICAL();
  296. return ESP_OK;
  297. }
  298. /**
  299. * Clear interrupt of adc digital controller by bitmask.
  300. *
  301. * @param adc_n ADC unit.
  302. * @param intr Interrupt bitmask.
  303. */
  304. static inline void adc_ll_digi_intr_clear(adc_unit_t adc_n, adc_digi_intr_t intr)
  305. {
  306. if (adc_n == ADC_UNIT_1) {
  307. if (intr & ADC_DIGI_INTR_MASK_MONITOR) {
  308. APB_SARADC.int_clr.adc1_thres = 1;
  309. }
  310. if (intr & ADC_DIGI_INTR_MASK_MEAS_DONE) {
  311. APB_SARADC.int_clr.adc1_done = 1;
  312. }
  313. } else { // adc_n == ADC_UNIT_2
  314. if (intr & ADC_DIGI_INTR_MASK_MONITOR) {
  315. APB_SARADC.int_clr.adc2_thres = 1;
  316. }
  317. if (intr & ADC_DIGI_INTR_MASK_MEAS_DONE) {
  318. APB_SARADC.int_clr.adc2_done = 1;
  319. }
  320. }
  321. }
  322. esp_err_t adc_digi_intr_clear(adc_unit_t adc_unit, adc_digi_intr_t intr_mask)
  323. {
  324. ADC_ENTER_CRITICAL();
  325. if (adc_unit == ADC_UNIT_1) {
  326. adc_ll_digi_intr_clear(ADC_UNIT_1, intr_mask);
  327. } else if (adc_unit == ADC_UNIT_2) {
  328. adc_ll_digi_intr_clear(ADC_UNIT_2, intr_mask);
  329. }
  330. ADC_EXIT_CRITICAL();
  331. return ESP_OK;
  332. }
  333. /**
  334. * Get interrupt status mask of adc digital controller.
  335. *
  336. * @param adc_n ADC unit.
  337. * @return
  338. * - intr Interrupt bitmask.
  339. */
  340. static inline uint32_t adc_ll_digi_get_intr_status(adc_unit_t adc_n)
  341. {
  342. uint32_t int_st = APB_SARADC.int_st.val;
  343. uint32_t ret_msk = 0;
  344. if (adc_n == ADC_UNIT_1) {
  345. if (int_st & APB_SARADC_ADC1_DONE_INT_ST_M) {
  346. ret_msk |= ADC_DIGI_INTR_MASK_MEAS_DONE;
  347. }
  348. if (int_st & APB_SARADC_ADC1_THRES_INT_ST) {
  349. ret_msk |= ADC_DIGI_INTR_MASK_MONITOR;
  350. }
  351. } else { // adc_n == ADC_UNIT_2
  352. if (int_st & APB_SARADC_ADC2_DONE_INT_ST_M) {
  353. ret_msk |= ADC_DIGI_INTR_MASK_MEAS_DONE;
  354. }
  355. if (int_st & APB_SARADC_ADC2_THRES_INT_ST_M) {
  356. ret_msk |= ADC_DIGI_INTR_MASK_MONITOR;
  357. }
  358. }
  359. return ret_msk;
  360. }
  361. uint32_t adc_digi_intr_get_status(adc_unit_t adc_unit)
  362. {
  363. uint32_t ret = 0;
  364. ADC_ENTER_CRITICAL();
  365. if (adc_unit == ADC_UNIT_1) {
  366. ret = adc_ll_digi_get_intr_status(ADC_UNIT_1);
  367. } else if (adc_unit == ADC_UNIT_2) {
  368. ret = adc_ll_digi_get_intr_status(ADC_UNIT_2);
  369. }
  370. ADC_EXIT_CRITICAL();
  371. return ret;
  372. }
  373. static uint8_t s_isr_registered = 0;
  374. static intr_handle_t s_adc_isr_handle = NULL;
  375. esp_err_t adc_digi_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags)
  376. {
  377. ADC_CHECK((fn != NULL), "Parameter error", ESP_ERR_INVALID_ARG);
  378. ADC_CHECK(s_isr_registered == 0, "ADC ISR have installed, can not install again", ESP_FAIL);
  379. esp_err_t ret = esp_intr_alloc(ETS_APB_ADC_INTR_SOURCE, intr_alloc_flags, fn, arg, &s_adc_isr_handle);
  380. if (ret == ESP_OK) {
  381. s_isr_registered = 1;
  382. }
  383. return ret;
  384. }
  385. esp_err_t adc_digi_isr_deregister(void)
  386. {
  387. esp_err_t ret = ESP_FAIL;
  388. if (s_isr_registered) {
  389. ret = esp_intr_free(s_adc_isr_handle);
  390. if (ret == ESP_OK) {
  391. s_isr_registered = 0;
  392. }
  393. }
  394. return ret;
  395. }
  396. esp_err_t adc_digi_init(void)
  397. {
  398. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  399. ADC_ENTER_CRITICAL();
  400. adc_ll_digi_set_fsm_time(ADC_HAL_FSM_RSTB_WAIT_DEFAULT, ADC_HAL_FSM_START_WAIT_DEFAULT,
  401. ADC_HAL_FSM_STANDBY_WAIT_DEFAULT);
  402. adc_ll_set_sample_cycle(ADC_HAL_SAMPLE_CYCLE_DEFAULT);
  403. adc_hal_pwdet_set_cct(ADC_HAL_PWDET_CCT_DEFAULT);
  404. adc_ll_digi_output_invert(ADC_UNIT_1, ADC_HAL_DIGI_DATA_INVERT_DEFAULT(ADC_UNIT_1));
  405. adc_ll_digi_output_invert(ADC_UNIT_2, ADC_HAL_DIGI_DATA_INVERT_DEFAULT(ADC_UNIT_2));
  406. adc_ll_digi_set_clk_div(ADC_HAL_DIGI_SAR_CLK_DIV_DEFAULT);
  407. adc_hal_arbiter_config(&config);
  408. ADC_EXIT_CRITICAL();
  409. adc_hal_calibration_init(ADC_UNIT_1);
  410. adc_hal_calibration_init(ADC_UNIT_2);
  411. return ESP_OK;
  412. }
  413. esp_err_t adc_digi_deinit(void)
  414. {
  415. #ifdef CONFIG_PM_ENABLE
  416. if (adc_digi_arbiter_lock) {
  417. esp_pm_lock_delete(adc_digi_arbiter_lock);
  418. adc_digi_arbiter_lock = NULL;
  419. }
  420. #endif
  421. adc_power_release();
  422. ADC_ENTER_CRITICAL();
  423. adc_hal_digi_deinit(NULL);
  424. ADC_EXIT_CRITICAL();
  425. return ESP_OK;
  426. }
  427. /**
  428. * @brief Reset FSM of adc digital controller.
  429. *
  430. * @return
  431. * - ESP_OK Success
  432. */
  433. esp_err_t adc_digi_reset(void)
  434. {
  435. ADC_ENTER_CRITICAL();
  436. adc_ll_digi_reset();
  437. adc_ll_digi_clear_pattern_table(ADC_UNIT_1);
  438. adc_ll_digi_clear_pattern_table(ADC_UNIT_2);
  439. ADC_EXIT_CRITICAL();
  440. return ESP_OK;
  441. }
  442. /**
  443. * Set adc output data format for digital controller.
  444. *
  445. * @param format Output data format.
  446. */
  447. static inline void adc_ll_digi_set_output_format(adc_digi_output_format_t format)
  448. {
  449. APB_SARADC.ctrl.data_sar_sel = format;
  450. }
  451. static inline void adc_ll_digi_prepare_pattern_table(adc_unit_t adc_n, uint32_t pattern_index, adc_digi_pattern_table_t pattern)
  452. {
  453. uint32_t tab;
  454. uint8_t index = pattern_index / 4;
  455. uint8_t offset = (pattern_index % 4) * 8;
  456. if (adc_n == ADC_UNIT_1) {
  457. tab = APB_SARADC.sar1_patt_tab[index]; // Read old register value
  458. tab &= (~(0xFF000000 >> offset)); // clear old data
  459. tab |= ((uint32_t)pattern.val << 24) >> offset; // Fill in the new data
  460. APB_SARADC.sar1_patt_tab[index] = tab; // Write back
  461. } else { // adc_n == ADC_UNIT_2
  462. tab = APB_SARADC.sar2_patt_tab[index]; // Read old register value
  463. tab &= (~(0xFF000000 >> offset)); // clear old data
  464. tab |= ((uint32_t)pattern.val << 24) >> offset; // Fill in the new data
  465. APB_SARADC.sar2_patt_tab[index] = tab; // Write back
  466. }
  467. }
  468. static void adc_digi_controller_reg_set(const adc_digi_config_t *cfg)
  469. {
  470. /* Single channel mode or multi channel mode. */
  471. switch (cfg->conv_mode) {
  472. case ADC_CONV_SINGLE_UNIT_1:
  473. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_ONLY_ADC1);
  474. break;
  475. case ADC_CONV_SINGLE_UNIT_2:
  476. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_ONLY_ADC2);
  477. break;
  478. case ADC_CONV_BOTH_UNIT:
  479. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_BOTH_UNIT);
  480. break;
  481. case ADC_CONV_ALTER_UNIT:
  482. adc_ll_digi_set_convert_mode(ADC_LL_DIGI_CONV_ALTER_UNIT);
  483. break;
  484. default:
  485. abort();
  486. }
  487. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  488. if (cfg->adc1_pattern_len) {
  489. adc_ll_digi_clear_pattern_table(ADC_UNIT_1);
  490. adc_ll_digi_set_pattern_table_len(ADC_UNIT_1, cfg->adc1_pattern_len);
  491. for (uint32_t i = 0; i < cfg->adc1_pattern_len; i++) {
  492. adc_ll_digi_prepare_pattern_table(ADC_UNIT_1, i, cfg->adc1_pattern[i]);
  493. }
  494. }
  495. }
  496. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  497. if (cfg->adc2_pattern_len) {
  498. adc_ll_digi_clear_pattern_table(ADC_UNIT_2);
  499. adc_ll_digi_set_pattern_table_len(ADC_UNIT_2, cfg->adc2_pattern_len);
  500. for (uint32_t i = 0; i < cfg->adc2_pattern_len; i++) {
  501. adc_ll_digi_prepare_pattern_table(ADC_UNIT_2, i, cfg->adc2_pattern[i]);
  502. }
  503. }
  504. }
  505. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  506. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_DIG);
  507. }
  508. if (cfg->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  509. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_ARB);
  510. }
  511. adc_ll_digi_set_output_format(cfg->format);
  512. if (cfg->conv_limit_en) {
  513. adc_ll_digi_set_convert_limit_num(cfg->conv_limit_num);
  514. adc_ll_digi_convert_limit_enable();
  515. } else {
  516. adc_ll_digi_convert_limit_disable();
  517. }
  518. adc_ll_digi_set_trigger_interval(cfg->interval);
  519. adc_ll_digi_controller_clk_div(cfg->dig_clk.div_num, cfg->dig_clk.div_b, cfg->dig_clk.div_a);
  520. adc_ll_digi_clk_sel(cfg->dig_clk.use_apll);
  521. adc_ll_digi_dma_set_eof_num(cfg->dma_eof_num);
  522. }
  523. esp_err_t adc_digi_controller_config(const adc_digi_config_t *config)
  524. {
  525. #ifdef CONFIG_PM_ENABLE
  526. esp_err_t err;
  527. if (adc_digi_arbiter_lock == NULL) {
  528. if (config->dig_clk.use_apll) {
  529. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "adc_dma", &adc_digi_arbiter_lock);
  530. } else {
  531. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc_dma", &adc_digi_arbiter_lock);
  532. }
  533. if (err != ESP_OK) {
  534. adc_digi_arbiter_lock = NULL;
  535. ESP_LOGE(ADC_TAG, "ADC-DMA pm lock error");
  536. return err;
  537. }
  538. }
  539. #endif //CONFIG_PM_ENABLE
  540. if (config->conv_mode & ADC_CONV_SINGLE_UNIT_1) {
  541. for (int i = 0; i < config->adc1_pattern_len; i++) {
  542. adc_cal_offset(ADC_UNIT_1, config->adc1_pattern[i].atten);
  543. }
  544. }
  545. if (config->conv_mode & ADC_CONV_SINGLE_UNIT_2) {
  546. for (int i = 0; i < config->adc2_pattern_len; i++) {
  547. adc_cal_offset(ADC_UNIT_2, config->adc2_pattern[i].atten);
  548. }
  549. }
  550. /* If enable digtal controller, adc xpd should always on. */
  551. adc_power_acquire();
  552. ADC_ENTER_CRITICAL();
  553. adc_digi_controller_reg_set(config);
  554. ADC_EXIT_CRITICAL();
  555. return ESP_OK;
  556. }
  557. #endif // #if CONFIG_IDF_TARGET_ESP32S2
  558. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
  559. /*---------------------------------------------------------------
  560. ESP32S2 Depricated ADC functions and APIs
  561. ---------------------------------------------------------------*/
  562. esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
  563. {
  564. gpio_num_t gpio_num = 0;
  565. //If called with `ADC_UNIT_BOTH (ADC_UNIT_1 | ADC_UNIT_2)`, both if blocks will be run
  566. if (adc_unit == ADC_UNIT_1) {
  567. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  568. gpio_num = ADC_GET_IO_NUM(ADC_UNIT_1, channel);
  569. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  570. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  571. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  572. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  573. }
  574. if (adc_unit == ADC_UNIT_2) {
  575. ADC_CHANNEL_CHECK(ADC_UNIT_2, channel);
  576. gpio_num = ADC_GET_IO_NUM(ADC_UNIT_2, channel);
  577. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  578. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  579. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  580. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  581. }
  582. return ESP_OK;
  583. }
  584. #endif //#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3