adc_common.c 20 KB

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