adc_single.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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_atten_t atten);
  190. return adc_get_calibration_offset(adc_n, 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_oneshot_ll_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_oneshot_ll_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_oneshot_ll_output_invert(ADC_UNIT_1, inv_en);
  253. SARADC1_EXIT();
  254. }
  255. if (adc_unit == ADC_UNIT_2) {
  256. SARADC2_ENTER();
  257. adc_oneshot_ll_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. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "unsupported bit width", ESP_ERR_INVALID_ARG);
  265. adc_bitwidth_t bitwidth = 0;
  266. #if CONFIG_IDF_TARGET_ESP32
  267. switch(width_bit) {
  268. case ADC_WIDTH_BIT_9:
  269. bitwidth = ADC_BITWIDTH_9;
  270. break;
  271. case ADC_WIDTH_BIT_10:
  272. bitwidth = ADC_BITWIDTH_10;
  273. break;
  274. case ADC_WIDTH_BIT_11:
  275. bitwidth = ADC_BITWIDTH_11;
  276. break;
  277. case ADC_WIDTH_BIT_12:
  278. bitwidth = ADC_BITWIDTH_12;
  279. break;
  280. default:
  281. abort();
  282. }
  283. #elif CONFIG_IDF_TARGET_ESP32S2
  284. bitwidth = ADC_BITWIDTH_13;
  285. #else //esp32s3
  286. bitwidth = ADC_BITWIDTH_12;
  287. #endif
  288. if (adc_unit == ADC_UNIT_1) {
  289. SARADC1_ENTER();
  290. adc_oneshot_ll_set_output_bits(ADC_UNIT_1, bitwidth);
  291. SARADC1_EXIT();
  292. }
  293. if (adc_unit == ADC_UNIT_2) {
  294. SARADC2_ENTER();
  295. adc_oneshot_ll_set_output_bits(ADC_UNIT_2, bitwidth);
  296. SARADC2_EXIT();
  297. }
  298. return ESP_OK;
  299. }
  300. /**
  301. * @brief Reset RTC controller FSM.
  302. *
  303. * @return
  304. * - ESP_OK Success
  305. */
  306. #if !CONFIG_IDF_TARGET_ESP32
  307. esp_err_t adc_rtc_reset(void)
  308. {
  309. FSM_ENTER();
  310. adc_ll_rtc_reset();
  311. FSM_EXIT();
  312. return ESP_OK;
  313. }
  314. #endif
  315. /*-------------------------------------------------------------------------------------
  316. * ADC1
  317. *------------------------------------------------------------------------------------*/
  318. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  319. {
  320. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  321. ADC_CHECK(atten < SOC_ADC_ATTEN_NUM, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  322. adc_common_gpio_init(ADC_UNIT_1, channel);
  323. SARADC1_ENTER();
  324. adc_rtc_chan_init(ADC_UNIT_1);
  325. adc_oneshot_ll_set_atten(ADC_UNIT_1, channel, atten);
  326. SARADC1_EXIT();
  327. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  328. adc_hal_calibration_init(ADC_UNIT_1);
  329. #endif
  330. return ESP_OK;
  331. }
  332. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  333. {
  334. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "unsupported bit width", ESP_ERR_INVALID_ARG);
  335. adc_bitwidth_t bitwidth = 0;
  336. #if CONFIG_IDF_TARGET_ESP32
  337. switch(width_bit) {
  338. case ADC_WIDTH_BIT_9:
  339. bitwidth = ADC_BITWIDTH_9;
  340. break;
  341. case ADC_WIDTH_BIT_10:
  342. bitwidth = ADC_BITWIDTH_10;
  343. break;
  344. case ADC_WIDTH_BIT_11:
  345. bitwidth = ADC_BITWIDTH_11;
  346. break;
  347. case ADC_WIDTH_BIT_12:
  348. bitwidth = ADC_BITWIDTH_12;
  349. break;
  350. default:
  351. abort();
  352. }
  353. #elif CONFIG_IDF_TARGET_ESP32S2
  354. bitwidth = ADC_BITWIDTH_13;
  355. #else //esp32s3
  356. bitwidth = ADC_BITWIDTH_12;
  357. #endif
  358. SARADC1_ENTER();
  359. adc_oneshot_ll_set_output_bits(ADC_UNIT_1, bitwidth);
  360. SARADC1_EXIT();
  361. return ESP_OK;
  362. }
  363. esp_err_t adc1_dma_mode_acquire(void)
  364. {
  365. /* Use locks to avoid digtal and RTC controller conflicts.
  366. for adc1, block until acquire the lock. */
  367. SARADC1_ACQUIRE();
  368. ESP_LOGD( ADC_TAG, "dma mode takes adc1 lock." );
  369. adc_power_acquire();
  370. SARADC1_ENTER();
  371. /* switch SARADC into DIG channel */
  372. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_DIG);
  373. SARADC1_EXIT();
  374. return ESP_OK;
  375. }
  376. esp_err_t adc1_rtc_mode_acquire(void)
  377. {
  378. /* Use locks to avoid digtal and RTC controller conflicts.
  379. for adc1, block until acquire the lock. */
  380. SARADC1_ACQUIRE();
  381. adc_power_acquire();
  382. SARADC1_ENTER();
  383. /* switch SARADC into RTC channel. */
  384. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_RTC);
  385. SARADC1_EXIT();
  386. return ESP_OK;
  387. }
  388. esp_err_t adc1_lock_release(void)
  389. {
  390. ADC_CHECK((uint32_t *)adc1_dma_lock != NULL, "adc1 lock release called before acquire", ESP_ERR_INVALID_STATE );
  391. /* Use locks to avoid digtal and RTC controller conflicts. for adc1, block until acquire the lock. */
  392. adc_power_release();
  393. SARADC1_RELEASE();
  394. return ESP_OK;
  395. }
  396. int adc1_get_raw(adc1_channel_t channel)
  397. {
  398. int adc_value;
  399. ADC_CHANNEL_CHECK(ADC_UNIT_1, channel);
  400. adc1_rtc_mode_acquire();
  401. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  402. // Get calibration value before going into critical section
  403. uint32_t cal_val = get_calibration_offset(ADC_UNIT_1, channel);
  404. adc_hal_set_calibration_param(ADC_UNIT_1, cal_val);
  405. #endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
  406. SARADC1_ENTER();
  407. #ifdef CONFIG_IDF_TARGET_ESP32
  408. adc_ll_hall_disable(); //Disable other peripherals.
  409. adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
  410. #endif
  411. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_RTC); //Set controller
  412. adc_oneshot_ll_set_channel(ADC_UNIT_1, channel);
  413. adc_hal_convert(ADC_UNIT_1, channel, &adc_value); //Start conversion, For ADC1, the data always valid.
  414. #if !CONFIG_IDF_TARGET_ESP32
  415. adc_ll_rtc_reset(); //Reset FSM of rtc controller
  416. #endif
  417. SARADC1_EXIT();
  418. adc1_lock_release();
  419. return adc_value;
  420. }
  421. int adc1_get_voltage(adc1_channel_t channel) //Deprecated. Use adc1_get_raw() instead
  422. {
  423. return adc1_get_raw(channel);
  424. }
  425. #if SOC_ULP_SUPPORTED
  426. void adc1_ulp_enable(void)
  427. {
  428. adc_power_acquire();
  429. SARADC1_ENTER();
  430. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_ULP);
  431. /* since most users do not need LNA and HALL with uLP, we disable them here
  432. open them in the uLP if needed. */
  433. #ifdef CONFIG_IDF_TARGET_ESP32
  434. /* disable other peripherals. */
  435. adc_ll_hall_disable();
  436. adc_ll_amp_disable();
  437. #endif
  438. SARADC1_EXIT();
  439. }
  440. #endif
  441. /*---------------------------------------------------------------
  442. ADC2
  443. ---------------------------------------------------------------*/
  444. /** For ESP32S2 the ADC2 The right to use ADC2 is controlled by the arbiter, and there is no need to set a lock.*/
  445. esp_err_t adc2_wifi_acquire(void)
  446. {
  447. /* Wi-Fi module will use adc2. Use locks to avoid conflicts. */
  448. SARADC2_ACQUIRE();
  449. ESP_LOGD( ADC_TAG, "Wi-Fi takes adc2 lock." );
  450. return ESP_OK;
  451. }
  452. esp_err_t adc2_wifi_release(void)
  453. {
  454. ADC_CHECK(SARADC2_LOCK_CHECK(), "wifi release called before acquire", ESP_ERR_INVALID_STATE );
  455. SARADC2_RELEASE();
  456. ESP_LOGD( ADC_TAG, "Wi-Fi returns adc2 lock." );
  457. return ESP_OK;
  458. }
  459. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  460. {
  461. ADC_CHANNEL_CHECK(ADC_UNIT_2, channel);
  462. ADC_CHECK(atten <= SOC_ADC_ATTEN_NUM, "ADC2 Atten Err", ESP_ERR_INVALID_ARG);
  463. adc_common_gpio_init(ADC_UNIT_2, channel);
  464. if ( SARADC2_TRY_ACQUIRE() == -1 ) {
  465. //try the lock, return if failed (wifi using).
  466. return ESP_ERR_TIMEOUT;
  467. }
  468. //avoid collision with other tasks
  469. SARADC2_ENTER();
  470. adc_rtc_chan_init(ADC_UNIT_2);
  471. adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
  472. SARADC2_EXIT();
  473. SARADC2_RELEASE();
  474. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  475. adc_hal_calibration_init(ADC_UNIT_2);
  476. #endif
  477. return ESP_OK;
  478. }
  479. static inline void adc2_init(void)
  480. {
  481. #if CONFIG_IDF_TARGET_ESP32S2
  482. #ifdef CONFIG_PM_ENABLE
  483. /* Lock APB clock. */
  484. if (s_adc2_arbiter_lock == NULL) {
  485. esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc2", &s_adc2_arbiter_lock);
  486. }
  487. #endif //CONFIG_PM_ENABLE
  488. #endif //CONFIG_IDF_TARGET_ESP32S2
  489. }
  490. static inline void adc2_dac_disable( adc2_channel_t channel)
  491. {
  492. #if SOC_DAC_SUPPORTED
  493. #ifdef CONFIG_IDF_TARGET_ESP32
  494. if ( channel == ADC2_CHANNEL_8 ) { // the same as DAC channel 1
  495. dac_output_disable(DAC_CHANNEL_1);
  496. } else if ( channel == ADC2_CHANNEL_9 ) {
  497. dac_output_disable(DAC_CHANNEL_2);
  498. }
  499. #else
  500. if ( channel == ADC2_CHANNEL_6 ) { // the same as DAC channel 1
  501. dac_output_disable(DAC_CHANNEL_1);
  502. } else if ( channel == ADC2_CHANNEL_7 ) {
  503. dac_output_disable(DAC_CHANNEL_2);
  504. }
  505. #endif
  506. #endif // SOC_DAC_SUPPORTED
  507. }
  508. /**
  509. * @note For ESP32S2:
  510. * The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
  511. * Or, the RTC controller will fail when get raw data.
  512. * This issue does not occur on digital controllers (DMA mode), and the hardware guarantees that there will be no errors.
  513. */
  514. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out)
  515. {
  516. esp_err_t ret = ESP_OK;
  517. int adc_value = 0;
  518. adc_bitwidth_t bitwidth = 0;
  519. ADC_CHECK(raw_out != NULL, "ADC out value err", ESP_ERR_INVALID_ARG);
  520. ADC_CHECK(channel < ADC2_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  521. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "unsupported bit width", ESP_ERR_INVALID_ARG);
  522. #if CONFIG_IDF_TARGET_ESP32
  523. switch(width_bit) {
  524. case ADC_WIDTH_BIT_9:
  525. bitwidth = ADC_BITWIDTH_9;
  526. break;
  527. case ADC_WIDTH_BIT_10:
  528. bitwidth = ADC_BITWIDTH_10;
  529. break;
  530. case ADC_WIDTH_BIT_11:
  531. bitwidth = ADC_BITWIDTH_11;
  532. break;
  533. case ADC_WIDTH_BIT_12:
  534. bitwidth = ADC_BITWIDTH_12;
  535. break;
  536. default:
  537. abort();
  538. }
  539. #elif CONFIG_IDF_TARGET_ESP32S2
  540. bitwidth = ADC_BITWIDTH_13;
  541. #else //esp32s3
  542. bitwidth = ADC_BITWIDTH_12;
  543. #endif
  544. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  545. // Get calibration value before going into critical section
  546. uint32_t cal_val = get_calibration_offset(ADC_UNIT_2, channel);
  547. adc_hal_set_calibration_param(ADC_UNIT_2, cal_val);
  548. #endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
  549. if ( SARADC2_TRY_ACQUIRE() == -1 ) {
  550. //try the lock, return if failed (wifi using).
  551. return ESP_ERR_TIMEOUT;
  552. }
  553. adc_power_acquire(); //in critical section with whole rtc module
  554. //avoid collision with other tasks
  555. adc2_init(); // in critical section with whole rtc module. because the PWDET use the same registers, place it here.
  556. SARADC2_ENTER();
  557. #if SOC_ADC_ARBITER_SUPPORTED
  558. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  559. adc_hal_arbiter_config(&config);
  560. #endif
  561. #ifdef CONFIG_ADC_DISABLE_DAC
  562. adc2_dac_disable(channel); //disable other peripherals
  563. #endif
  564. adc_oneshot_ll_set_output_bits(ADC_UNIT_2, bitwidth);
  565. #if CONFIG_IDF_TARGET_ESP32
  566. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_RTC);// set controller
  567. #else
  568. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_ARB);// set controller
  569. #endif
  570. #if CONFIG_IDF_TARGET_ESP32S2
  571. #ifdef CONFIG_PM_ENABLE
  572. if (s_adc2_arbiter_lock) {
  573. esp_pm_lock_acquire(s_adc2_arbiter_lock);
  574. }
  575. #endif //CONFIG_PM_ENABLE
  576. #endif //CONFIG_IDF_TARGET_ESP32
  577. adc_oneshot_ll_set_channel(ADC_UNIT_2, channel);
  578. ret = adc_hal_convert(ADC_UNIT_2, channel, &adc_value);
  579. if (ret != ESP_OK) {
  580. adc_value = -1;
  581. }
  582. #if CONFIG_IDF_TARGET_ESP32S2
  583. #ifdef CONFIG_PM_ENABLE
  584. /* Release APB clock. */
  585. if (s_adc2_arbiter_lock) {
  586. esp_pm_lock_release(s_adc2_arbiter_lock);
  587. }
  588. #endif //CONFIG_PM_ENABLE
  589. #endif //CONFIG_IDF_TARGET_ESP32
  590. SARADC2_EXIT();
  591. adc_power_release();
  592. SARADC2_RELEASE();
  593. *raw_out = adc_value;
  594. return ret;
  595. }
  596. esp_err_t adc2_vref_to_gpio(gpio_num_t gpio)
  597. {
  598. return adc_vref_to_gpio(ADC_UNIT_2, gpio);
  599. }
  600. esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
  601. {
  602. #ifdef CONFIG_IDF_TARGET_ESP32
  603. if (adc_unit == ADC_UNIT_1) {
  604. return ESP_ERR_INVALID_ARG;
  605. }
  606. #endif
  607. adc2_channel_t ch = ADC2_CHANNEL_MAX;
  608. /* Check if the GPIO supported. */
  609. for (int i = 0; i < ADC2_CHANNEL_MAX; i++) {
  610. if (gpio == ADC_GET_IO_NUM(ADC_UNIT_2, i)) {
  611. ch = i;
  612. break;
  613. }
  614. }
  615. if (ch == ADC2_CHANNEL_MAX) {
  616. return ESP_ERR_INVALID_ARG;
  617. }
  618. adc_power_acquire();
  619. if (adc_unit == ADC_UNIT_1) {
  620. VREF_ENTER(1);
  621. adc_hal_vref_output(ADC_UNIT_1, ch, true);
  622. VREF_EXIT(1);
  623. } else if (adc_unit == ADC_UNIT_2) {
  624. VREF_ENTER(2);
  625. adc_hal_vref_output(ADC_UNIT_2, ch, true);
  626. VREF_EXIT(2);
  627. }
  628. //Configure RTC gpio, Only ADC2's channels IO are supported to output reference voltage.
  629. adc_common_gpio_init(ADC_UNIT_2, ch);
  630. return ESP_OK;
  631. }
  632. #endif //SOC_ADC_RTC_CTRL_SUPPORTED