adc_common.c 20 KB

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