adc_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <esp_types.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9. #include "sdkconfig.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/semphr.h"
  12. #include "freertos/timers.h"
  13. #include "esp_log.h"
  14. #include "esp_pm.h"
  15. #include "soc/rtc.h"
  16. #include "driver/rtc_io.h"
  17. #include "sys/lock.h"
  18. #include "driver/gpio.h"
  19. #include "driver/adc.h"
  20. #include "adc1_private.h"
  21. #include "hal/adc_types.h"
  22. #include "hal/adc_hal.h"
  23. #include "hal/adc_hal_conf.h"
  24. #if SOC_DAC_SUPPORTED
  25. #include "driver/dac.h"
  26. #include "hal/dac_hal.h"
  27. #endif
  28. #if CONFIG_IDF_TARGET_ESP32S3
  29. #include "esp_efuse_rtc_calib.h"
  30. #endif
  31. #define ADC_CHECK_RET(fun_ret) ({ \
  32. if (fun_ret != ESP_OK) { \
  33. ESP_LOGE(ADC_TAG,"%s:%d\n",__FUNCTION__,__LINE__); \
  34. return ESP_FAIL; \
  35. } \
  36. })
  37. static const char *ADC_TAG = "ADC";
  38. #define ADC_CHECK(a, str, ret_val) ({ \
  39. if (!(a)) { \
  40. ESP_LOGE(ADC_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  41. return (ret_val); \
  42. } \
  43. })
  44. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  45. #define ADC_CHANNEL_CHECK(periph, channel) ADC_CHECK(channel < SOC_ADC_CHANNEL_NUM(periph), "ADC"#periph" channel error", ESP_ERR_INVALID_ARG)
  46. //////////////////////// Locks ///////////////////////////////////////////
  47. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  48. #define RTC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  49. #define RTC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  50. #define DIGI_ENTER_CRITICAL()
  51. #define DIGI_EXIT_CRITICAL()
  52. #define ADC_POWER_ENTER() RTC_ENTER_CRITICAL()
  53. #define ADC_POWER_EXIT() RTC_EXIT_CRITICAL()
  54. #define DIGI_CONTROLLER_ENTER() DIGI_ENTER_CRITICAL()
  55. #define DIGI_CONTROLLER_EXIT() DIGI_EXIT_CRITICAL()
  56. #define SARADC1_ENTER() RTC_ENTER_CRITICAL()
  57. #define SARADC1_EXIT() RTC_EXIT_CRITICAL()
  58. #define SARADC2_ENTER() RTC_ENTER_CRITICAL()
  59. #define SARADC2_EXIT() RTC_EXIT_CRITICAL()
  60. //n stands for ADC unit: 1 for ADC1 and 2 for ADC2. Currently both unit touches the same registers
  61. #define VREF_ENTER(n) RTC_ENTER_CRITICAL()
  62. #define VREF_EXIT(n) RTC_EXIT_CRITICAL()
  63. #define FSM_ENTER() RTC_ENTER_CRITICAL()
  64. #define FSM_EXIT() RTC_EXIT_CRITICAL()
  65. //TODO: IDF-3610
  66. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  67. //prevent ADC1 being used by I2S dma and other tasks at the same time.
  68. static _lock_t adc1_dma_lock;
  69. #define SARADC1_ACQUIRE() _lock_acquire( &adc1_dma_lock )
  70. #define SARADC1_RELEASE() _lock_release( &adc1_dma_lock )
  71. #endif
  72. /*
  73. In ADC2, there're two locks used for different cases:
  74. 1. lock shared with app and Wi-Fi:
  75. ESP32:
  76. When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed.
  77. ESP32S2:
  78. The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.
  79. 2. lock shared between tasks:
  80. when several tasks sharing the ADC2, we want to guarantee
  81. all the requests will be handled.
  82. Since conversions are short (about 31us), app returns the lock very soon,
  83. we use a spinlock to stand there waiting to do conversions one by one.
  84. adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
  85. */
  86. #ifdef CONFIG_IDF_TARGET_ESP32
  87. //prevent ADC2 being used by wifi and other tasks at the same time.
  88. static _lock_t adc2_wifi_lock;
  89. /** For ESP32S2 the ADC2 The right to use ADC2 is controlled by the arbiter, and there is no need to set a lock. */
  90. #define SARADC2_ACQUIRE() _lock_acquire( &adc2_wifi_lock )
  91. #define SARADC2_RELEASE() _lock_release( &adc2_wifi_lock )
  92. #define SARADC2_TRY_ACQUIRE() _lock_try_acquire( &adc2_wifi_lock )
  93. #define SARADC2_LOCK_CHECK() ((uint32_t *)adc2_wifi_lock != NULL)
  94. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  95. #define SARADC2_ACQUIRE()
  96. #define SARADC2_RELEASE()
  97. #define SARADC2_TRY_ACQUIRE() (0) //WIFI controller and rtc controller have independent parameter configuration.
  98. #define SARADC2_LOCK_CHECK() (true)
  99. #endif // CONFIG_IDF_TARGET_*
  100. #if CONFIG_IDF_TARGET_ESP32S2
  101. #ifdef CONFIG_PM_ENABLE
  102. static esp_pm_lock_handle_t s_adc2_arbiter_lock;
  103. #endif //CONFIG_PM_ENABLE
  104. #endif // !CONFIG_IDF_TARGET_ESP32
  105. /*---------------------------------------------------------------
  106. ADC Common
  107. ---------------------------------------------------------------*/
  108. // ADC Power
  109. // This gets incremented when adc_power_acquire() is called, and decremented when
  110. // adc_power_release() is called. ADC is powered down when the value reaches zero.
  111. // Should be modified within critical section (ADC_ENTER/EXIT_CRITICAL).
  112. static int s_adc_power_on_cnt;
  113. static void adc_power_on_internal(void)
  114. {
  115. /* Set the power always on to increase precision. */
  116. adc_hal_set_power_manage(ADC_POWER_SW_ON);
  117. }
  118. void adc_power_acquire(void)
  119. {
  120. ADC_POWER_ENTER();
  121. s_adc_power_on_cnt++;
  122. if (s_adc_power_on_cnt == 1) {
  123. adc_power_on_internal();
  124. }
  125. ADC_POWER_EXIT();
  126. }
  127. void adc_power_on(void)
  128. {
  129. ADC_POWER_ENTER();
  130. adc_power_on_internal();
  131. ADC_POWER_EXIT();
  132. }
  133. static void adc_power_off_internal(void)
  134. {
  135. #if CONFIG_IDF_TARGET_ESP32
  136. adc_hal_set_power_manage(ADC_POWER_SW_OFF);
  137. #else
  138. adc_hal_set_power_manage(ADC_POWER_BY_FSM);
  139. #endif
  140. }
  141. void adc_power_release(void)
  142. {
  143. ADC_POWER_ENTER();
  144. s_adc_power_on_cnt--;
  145. /* Sanity check */
  146. if (s_adc_power_on_cnt < 0) {
  147. ADC_POWER_EXIT();
  148. ESP_LOGE(ADC_TAG, "%s called, but s_adc_power_on_cnt == 0", __func__);
  149. abort();
  150. } else if (s_adc_power_on_cnt == 0) {
  151. adc_power_off_internal();
  152. }
  153. ADC_POWER_EXIT();
  154. }
  155. void adc_power_off(void)
  156. {
  157. ADC_POWER_ENTER();
  158. adc_power_off_internal();
  159. ADC_POWER_EXIT();
  160. }
  161. esp_err_t adc1_pad_get_io_num(adc1_channel_t channel, gpio_num_t *gpio_num)
  162. {
  163. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  164. int io = ADC_GET_IO_NUM(ADC_UNIT_1, channel);
  165. if (io < 0) {
  166. return ESP_ERR_INVALID_ARG;
  167. } else {
  168. *gpio_num = (gpio_num_t)io;
  169. }
  170. return ESP_OK;
  171. }
  172. esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num)
  173. {
  174. ADC_CHANNEL_CHECK(ADC_UNIT_2, channel);
  175. int io = ADC_GET_IO_NUM(ADC_UNIT_2, channel);
  176. if (io < 0) {
  177. return ESP_ERR_INVALID_ARG;
  178. } else {
  179. *gpio_num = (gpio_num_t)io;
  180. }
  181. return ESP_OK;
  182. }
  183. //------------------------------------------------------------RTC Single Read----------------------------------------------//
  184. #if SOC_ADC_RTC_CTRL_SUPPORTED
  185. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  186. static uint32_t get_calibration_offset(adc_unit_t adc_n, adc_channel_t chan)
  187. {
  188. adc_atten_t atten = adc_ll_get_atten(adc_n, chan);
  189. extern uint32_t adc_get_calibration_offset(adc_unit_t adc_n, adc_channel_t channel, adc_atten_t atten);
  190. return adc_get_calibration_offset(adc_n, chan, atten);
  191. }
  192. #endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
  193. esp_err_t adc_set_clk_div(uint8_t clk_div)
  194. {
  195. DIGI_CONTROLLER_ENTER();
  196. adc_ll_digi_set_clk_div(clk_div);
  197. DIGI_CONTROLLER_EXIT();
  198. return ESP_OK;
  199. }
  200. static void adc_rtc_chan_init(adc_unit_t adc_unit)
  201. {
  202. if (adc_unit == ADC_UNIT_1) {
  203. /* Workaround: Disable the synchronization operation function of ADC1 and DAC.
  204. If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage. */
  205. #if SOC_DAC_SUPPORTED
  206. dac_hal_rtc_sync_by_adc(false);
  207. #endif
  208. adc_hal_rtc_output_invert(ADC_UNIT_1, ADC_HAL_DATA_INVERT_DEFAULT(ADC_UNIT_1));
  209. adc_ll_set_sar_clk_div(ADC_UNIT_1, ADC_HAL_SAR_CLK_DIV_DEFAULT(ADC_UNIT_1));
  210. #ifdef CONFIG_IDF_TARGET_ESP32
  211. adc_ll_hall_disable(); //Disable other peripherals.
  212. adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
  213. #endif
  214. }
  215. if (adc_unit == ADC_UNIT_2) {
  216. adc_hal_pwdet_set_cct(ADC_HAL_PWDET_CCT_DEFAULT);
  217. adc_hal_rtc_output_invert(ADC_UNIT_2, ADC_HAL_DATA_INVERT_DEFAULT(ADC_UNIT_2));
  218. adc_ll_set_sar_clk_div(ADC_UNIT_2, ADC_HAL_SAR_CLK_DIV_DEFAULT(ADC_UNIT_2));
  219. }
  220. }
  221. /**
  222. * This function is NOT an API.
  223. * Now some to-be-deprecated APIs are using this function, so don't make it static for now.
  224. * Will make this static on v5.0
  225. */
  226. esp_err_t adc_common_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
  227. {
  228. gpio_num_t gpio_num = 0;
  229. //If called with `ADC_UNIT_BOTH (ADC_UNIT_1 | ADC_UNIT_2)`, both if blocks will be run
  230. if (adc_unit == ADC_UNIT_1) {
  231. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  232. gpio_num = ADC_GET_IO_NUM(ADC_UNIT_1, channel);
  233. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  234. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  235. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  236. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  237. }
  238. if (adc_unit == ADC_UNIT_2) {
  239. ADC_CHANNEL_CHECK(ADC_UNIT_2, channel);
  240. gpio_num = ADC_GET_IO_NUM(ADC_UNIT_2, channel);
  241. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  242. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  243. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  244. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  245. }
  246. return ESP_OK;
  247. }
  248. esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en)
  249. {
  250. if (adc_unit == ADC_UNIT_1) {
  251. SARADC1_ENTER();
  252. adc_hal_rtc_output_invert(ADC_UNIT_1, inv_en);
  253. SARADC1_EXIT();
  254. }
  255. if (adc_unit == ADC_UNIT_2) {
  256. SARADC2_ENTER();
  257. adc_hal_rtc_output_invert(ADC_UNIT_2, inv_en);
  258. SARADC2_EXIT();
  259. }
  260. return ESP_OK;
  261. }
  262. esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t width_bit)
  263. {
  264. #if CONFIG_IDF_TARGET_ESP32
  265. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "WIDTH ERR: ESP32 support 9 ~ 12 bit width", ESP_ERR_INVALID_ARG);
  266. #else
  267. ADC_CHECK(width_bit == ADC_WIDTH_MAX - 1, "WIDTH ERR: see `adc_bits_width_t` for supported bit width", ESP_ERR_INVALID_ARG);
  268. #endif
  269. if (adc_unit == ADC_UNIT_1) {
  270. SARADC1_ENTER();
  271. adc_hal_rtc_set_output_format(ADC_UNIT_1, width_bit);
  272. SARADC1_EXIT();
  273. }
  274. if (adc_unit == ADC_UNIT_2) {
  275. SARADC2_ENTER();
  276. adc_hal_rtc_set_output_format(ADC_UNIT_2, width_bit);
  277. SARADC2_EXIT();
  278. }
  279. return ESP_OK;
  280. }
  281. /**
  282. * @brief Reset RTC controller FSM.
  283. *
  284. * @return
  285. * - ESP_OK Success
  286. */
  287. #if !CONFIG_IDF_TARGET_ESP32
  288. esp_err_t adc_rtc_reset(void)
  289. {
  290. FSM_ENTER();
  291. adc_ll_rtc_reset();
  292. FSM_EXIT();
  293. return ESP_OK;
  294. }
  295. #endif
  296. /*-------------------------------------------------------------------------------------
  297. * ADC1
  298. *------------------------------------------------------------------------------------*/
  299. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  300. {
  301. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  302. ADC_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  303. adc_common_gpio_init(ADC_UNIT_1, channel);
  304. SARADC1_ENTER();
  305. adc_rtc_chan_init(ADC_UNIT_1);
  306. adc_hal_set_atten(ADC_UNIT_1, channel, atten);
  307. SARADC1_EXIT();
  308. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  309. adc_hal_calibration_init(ADC_UNIT_1);
  310. #endif
  311. return ESP_OK;
  312. }
  313. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  314. {
  315. #if CONFIG_IDF_TARGET_ESP32
  316. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "WIDTH ERR: ESP32 support 9 ~ 12 bit width", ESP_ERR_INVALID_ARG);
  317. #else
  318. ADC_CHECK(width_bit == ADC_WIDTH_MAX - 1, "WIDTH ERR: see `adc_bits_width_t` for supported bit width", ESP_ERR_INVALID_ARG);
  319. #endif
  320. SARADC1_ENTER();
  321. adc_hal_rtc_set_output_format(ADC_UNIT_1, width_bit);
  322. SARADC1_EXIT();
  323. return ESP_OK;
  324. }
  325. esp_err_t adc1_dma_mode_acquire(void)
  326. {
  327. /* Use locks to avoid digtal and RTC controller conflicts.
  328. for adc1, block until acquire the lock. */
  329. SARADC1_ACQUIRE();
  330. ESP_LOGD( ADC_TAG, "dma mode takes adc1 lock." );
  331. adc_power_acquire();
  332. SARADC1_ENTER();
  333. /* switch SARADC into DIG channel */
  334. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_DIG);
  335. SARADC1_EXIT();
  336. return ESP_OK;
  337. }
  338. esp_err_t adc1_rtc_mode_acquire(void)
  339. {
  340. /* Use locks to avoid digtal and RTC controller conflicts.
  341. for adc1, block until acquire the lock. */
  342. SARADC1_ACQUIRE();
  343. adc_power_acquire();
  344. SARADC1_ENTER();
  345. /* switch SARADC into RTC channel. */
  346. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_RTC);
  347. SARADC1_EXIT();
  348. return ESP_OK;
  349. }
  350. esp_err_t adc1_lock_release(void)
  351. {
  352. ADC_CHECK((uint32_t *)adc1_dma_lock != NULL, "adc1 lock release called before acquire", ESP_ERR_INVALID_STATE );
  353. /* Use locks to avoid digtal and RTC controller conflicts. for adc1, block until acquire the lock. */
  354. adc_power_release();
  355. SARADC1_RELEASE();
  356. return ESP_OK;
  357. }
  358. int adc1_get_raw(adc1_channel_t channel)
  359. {
  360. int adc_value;
  361. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  362. adc1_rtc_mode_acquire();
  363. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  364. // Get calibration value before going into critical section
  365. uint32_t cal_val = get_calibration_offset(ADC_UNIT_1, channel);
  366. adc_hal_set_calibration_param(ADC_UNIT_1, cal_val);
  367. #endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
  368. SARADC1_ENTER();
  369. #ifdef CONFIG_IDF_TARGET_ESP32
  370. adc_ll_hall_disable(); //Disable other peripherals.
  371. adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
  372. #endif
  373. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_RTC); //Set controller
  374. adc_hal_convert(ADC_UNIT_1, channel, &adc_value); //Start conversion, For ADC1, the data always valid.
  375. #if !CONFIG_IDF_TARGET_ESP32
  376. adc_ll_rtc_reset(); //Reset FSM of rtc controller
  377. #endif
  378. SARADC1_EXIT();
  379. adc1_lock_release();
  380. return adc_value;
  381. }
  382. int adc1_get_voltage(adc1_channel_t channel) //Deprecated. Use adc1_get_raw() instead
  383. {
  384. return adc1_get_raw(channel);
  385. }
  386. #if SOC_ULP_SUPPORTED
  387. void adc1_ulp_enable(void)
  388. {
  389. adc_power_acquire();
  390. SARADC1_ENTER();
  391. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_ULP);
  392. /* since most users do not need LNA and HALL with uLP, we disable them here
  393. open them in the uLP if needed. */
  394. #ifdef CONFIG_IDF_TARGET_ESP32
  395. /* disable other peripherals. */
  396. adc_ll_hall_disable();
  397. adc_ll_amp_disable();
  398. #endif
  399. SARADC1_EXIT();
  400. }
  401. #endif
  402. /*---------------------------------------------------------------
  403. ADC2
  404. ---------------------------------------------------------------*/
  405. /** For ESP32S2 the ADC2 The right to use ADC2 is controlled by the arbiter, and there is no need to set a lock.*/
  406. esp_err_t adc2_wifi_acquire(void)
  407. {
  408. /* Wi-Fi module will use adc2. Use locks to avoid conflicts. */
  409. SARADC2_ACQUIRE();
  410. ESP_LOGD( ADC_TAG, "Wi-Fi takes adc2 lock." );
  411. return ESP_OK;
  412. }
  413. esp_err_t adc2_wifi_release(void)
  414. {
  415. ADC_CHECK(SARADC2_LOCK_CHECK(), "wifi release called before acquire", ESP_ERR_INVALID_STATE );
  416. SARADC2_RELEASE();
  417. ESP_LOGD( ADC_TAG, "Wi-Fi returns adc2 lock." );
  418. return ESP_OK;
  419. }
  420. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  421. {
  422. ADC_CHANNEL_CHECK(ADC_UNIT_2, channel);
  423. ADC_CHECK(atten <= ADC_ATTEN_11db, "ADC2 Atten Err", ESP_ERR_INVALID_ARG);
  424. adc_common_gpio_init(ADC_UNIT_2, channel);
  425. if ( SARADC2_TRY_ACQUIRE() == -1 ) {
  426. //try the lock, return if failed (wifi using).
  427. return ESP_ERR_TIMEOUT;
  428. }
  429. //avoid collision with other tasks
  430. SARADC2_ENTER();
  431. adc_rtc_chan_init(ADC_UNIT_2);
  432. adc_hal_set_atten(ADC_UNIT_2, channel, atten);
  433. SARADC2_EXIT();
  434. SARADC2_RELEASE();
  435. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  436. adc_hal_calibration_init(ADC_UNIT_2);
  437. #endif
  438. return ESP_OK;
  439. }
  440. static inline void adc2_init(void)
  441. {
  442. #if CONFIG_IDF_TARGET_ESP32S2
  443. #ifdef CONFIG_PM_ENABLE
  444. /* Lock APB clock. */
  445. if (s_adc2_arbiter_lock == NULL) {
  446. esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc2", &s_adc2_arbiter_lock);
  447. }
  448. #endif //CONFIG_PM_ENABLE
  449. #endif //CONFIG_IDF_TARGET_ESP32S2
  450. }
  451. static inline void adc2_dac_disable( adc2_channel_t channel)
  452. {
  453. #if SOC_DAC_SUPPORTED
  454. #ifdef CONFIG_IDF_TARGET_ESP32
  455. if ( channel == ADC2_CHANNEL_8 ) { // the same as DAC channel 1
  456. dac_output_disable(DAC_CHANNEL_1);
  457. } else if ( channel == ADC2_CHANNEL_9 ) {
  458. dac_output_disable(DAC_CHANNEL_2);
  459. }
  460. #else
  461. if ( channel == ADC2_CHANNEL_6 ) { // the same as DAC channel 1
  462. dac_output_disable(DAC_CHANNEL_1);
  463. } else if ( channel == ADC2_CHANNEL_7 ) {
  464. dac_output_disable(DAC_CHANNEL_2);
  465. }
  466. #endif
  467. #endif // SOC_DAC_SUPPORTED
  468. }
  469. /**
  470. * @note For ESP32S2:
  471. * The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
  472. * Or, the RTC controller will fail when get raw data.
  473. * This issue does not occur on digital controllers (DMA mode), and the hardware guarantees that there will be no errors.
  474. */
  475. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out)
  476. {
  477. esp_err_t ret = ESP_OK;
  478. int adc_value = 0;
  479. ADC_CHECK(raw_out != NULL, "ADC out value err", ESP_ERR_INVALID_ARG);
  480. ADC_CHECK(channel < ADC2_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  481. #if CONFIG_IDF_TARGET_ESP32
  482. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "WIDTH ERR: ESP32 support 9 ~ 12 bit width", ESP_ERR_INVALID_ARG);
  483. #else
  484. ADC_CHECK(width_bit == ADC_WIDTH_MAX - 1, "WIDTH ERR: see `adc_bits_width_t` for supported bit width", ESP_ERR_INVALID_ARG);
  485. #endif
  486. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  487. // Get calibration value before going into critical section
  488. uint32_t cal_val = get_calibration_offset(ADC_UNIT_2, channel);
  489. adc_hal_set_calibration_param(ADC_UNIT_2, cal_val);
  490. #endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
  491. if ( SARADC2_TRY_ACQUIRE() == -1 ) {
  492. //try the lock, return if failed (wifi using).
  493. return ESP_ERR_TIMEOUT;
  494. }
  495. adc_power_acquire(); //in critical section with whole rtc module
  496. //avoid collision with other tasks
  497. adc2_init(); // in critical section with whole rtc module. because the PWDET use the same registers, place it here.
  498. SARADC2_ENTER();
  499. #if SOC_ADC_ARBITER_SUPPORTED
  500. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  501. adc_hal_arbiter_config(&config);
  502. #endif
  503. #ifdef CONFIG_ADC_DISABLE_DAC
  504. adc2_dac_disable(channel); //disable other peripherals
  505. #endif
  506. adc_hal_rtc_set_output_format(ADC_UNIT_2, width_bit);
  507. #if CONFIG_IDF_TARGET_ESP32
  508. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_RTC);// set controller
  509. #else
  510. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_ARB);// set controller
  511. #endif
  512. #if CONFIG_IDF_TARGET_ESP32S2
  513. #ifdef CONFIG_PM_ENABLE
  514. if (s_adc2_arbiter_lock) {
  515. esp_pm_lock_acquire(s_adc2_arbiter_lock);
  516. }
  517. #endif //CONFIG_PM_ENABLE
  518. #endif //CONFIG_IDF_TARGET_ESP32
  519. ret = adc_hal_convert(ADC_UNIT_2, channel, &adc_value);
  520. if (ret != ESP_OK) {
  521. adc_value = -1;
  522. }
  523. #if CONFIG_IDF_TARGET_ESP32S2
  524. #ifdef CONFIG_PM_ENABLE
  525. /* Release APB clock. */
  526. if (s_adc2_arbiter_lock) {
  527. esp_pm_lock_release(s_adc2_arbiter_lock);
  528. }
  529. #endif //CONFIG_PM_ENABLE
  530. #endif //CONFIG_IDF_TARGET_ESP32
  531. SARADC2_EXIT();
  532. adc_power_release();
  533. SARADC2_RELEASE();
  534. *raw_out = adc_value;
  535. return ret;
  536. }
  537. esp_err_t adc2_vref_to_gpio(gpio_num_t gpio)
  538. {
  539. return adc_vref_to_gpio(ADC_UNIT_2, gpio);
  540. }
  541. esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
  542. {
  543. #ifdef CONFIG_IDF_TARGET_ESP32
  544. if (adc_unit == ADC_UNIT_1) {
  545. return ESP_ERR_INVALID_ARG;
  546. }
  547. #endif
  548. adc2_channel_t ch = ADC2_CHANNEL_MAX;
  549. /* Check if the GPIO supported. */
  550. for (int i = 0; i < ADC2_CHANNEL_MAX; i++) {
  551. if (gpio == ADC_GET_IO_NUM(ADC_UNIT_2, i)) {
  552. ch = i;
  553. break;
  554. }
  555. }
  556. if (ch == ADC2_CHANNEL_MAX) {
  557. return ESP_ERR_INVALID_ARG;
  558. }
  559. adc_power_acquire();
  560. if (adc_unit == ADC_UNIT_1) {
  561. VREF_ENTER(1);
  562. adc_hal_vref_output(ADC_UNIT_1, ch, true);
  563. VREF_EXIT(1);
  564. } else if (adc_unit == ADC_UNIT_2) {
  565. VREF_ENTER(2);
  566. adc_hal_vref_output(ADC_UNIT_2, ch, true);
  567. VREF_EXIT(2);
  568. }
  569. //Configure RTC gpio, Only ADC2's channels IO are supported to output reference voltage.
  570. adc_common_gpio_init(ADC_UNIT_2, ch);
  571. return ESP_OK;
  572. }
  573. #endif //SOC_ADC_RTC_CTRL_SUPPORTED