adc_deprecated.c 21 KB

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