adc_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <esp_types.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/xtensa_api.h"
  19. #include "freertos/semphr.h"
  20. #include "freertos/timers.h"
  21. #include "esp_log.h"
  22. #include "esp_pm.h"
  23. #include "soc/rtc.h"
  24. #include "driver/rtc_io.h"
  25. #include "driver/dac.h"
  26. #include "sys/lock.h"
  27. #include "driver/gpio.h"
  28. #include "driver/adc.h"
  29. #include "adc1_private.h"
  30. #include "hal/adc_types.h"
  31. #include "hal/adc_hal.h"
  32. #include "hal/dac_hal.h"
  33. #define ADC_CHECK_RET(fun_ret) ({ \
  34. if (fun_ret != ESP_OK) { \
  35. ESP_LOGE(ADC_TAG,"%s:%d\n",__FUNCTION__,__LINE__); \
  36. return ESP_FAIL; \
  37. } \
  38. })
  39. static const char *ADC_TAG = "ADC";
  40. #define ADC_CHECK(a, str, ret_val) ({ \
  41. if (!(a)) { \
  42. ESP_LOGE(ADC_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  43. return (ret_val); \
  44. } \
  45. })
  46. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  47. #define ADC_CHANNEL_CHECK(periph, channel) ADC_CHECK(channel < SOC_ADC_CHANNEL_NUM(periph), "ADC"#periph" channel error", ESP_ERR_INVALID_ARG)
  48. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  49. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  50. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  51. /*
  52. In ADC2, there're two locks used for different cases:
  53. 1. lock shared with app and Wi-Fi:
  54. ESP32:
  55. When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed.
  56. ESP32S2:
  57. The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.
  58. 2. lock shared between tasks:
  59. when several tasks sharing the ADC2, we want to guarantee
  60. all the requests will be handled.
  61. Since conversions are short (about 31us), app returns the lock very soon,
  62. we use a spinlock to stand there waiting to do conversions one by one.
  63. adc2_spinlock should be acquired first, then adc2_wifi_lock or rtc_spinlock.
  64. */
  65. #ifdef CONFIG_IDF_TARGET_ESP32
  66. //prevent ADC2 being used by wifi and other tasks at the same time.
  67. static _lock_t adc2_wifi_lock;
  68. /** For ESP32S2 the ADC2 The right to use ADC2 is controlled by the arbiter, and there is no need to set a lock. */
  69. #define ADC2_WIFI_LOCK_ACQUIRE() _lock_acquire( &adc2_wifi_lock )
  70. #define ADC2_WIFI_LOCK_RELEASE() _lock_release( &adc2_wifi_lock )
  71. #define ADC2_WIFI_LOCK_TRY_ACQUIRE() _lock_try_acquire( &adc2_wifi_lock )
  72. #define ADC2_WIFI_LOCK_CHECK() ((uint32_t *)adc2_wifi_lock != NULL)
  73. #elif defined CONFIG_IDF_TARGET_ESP32S2
  74. #define ADC2_WIFI_LOCK_ACQUIRE()
  75. #define ADC2_WIFI_LOCK_RELEASE()
  76. #define ADC2_WIFI_LOCK_TRY_ACQUIRE() (0) //WIFI controller and rtc controller have independent parameter configuration.
  77. #define ADC2_WIFI_LOCK_CHECK() (true)
  78. #endif
  79. //prevent ADC2 being used by tasks (regardless of WIFI)
  80. static portMUX_TYPE adc2_spinlock = portMUX_INITIALIZER_UNLOCKED;
  81. #define ADC2_ENTER_CRITICAL() portENTER_CRITICAL( &adc2_spinlock )
  82. #define ADC2_EXIT_CRITICAL() portEXIT_CRITICAL( &adc2_spinlock )
  83. //prevent ADC1 being used by I2S dma and other tasks at the same time.
  84. static _lock_t adc1_dma_lock;
  85. #define ADC1_DMA_LOCK_ACQUIRE() _lock_acquire( &adc1_dma_lock )
  86. #define ADC1_DMA_LOCK_RELEASE() _lock_release( &adc1_dma_lock )
  87. #ifdef CONFIG_IDF_TARGET_ESP32S2
  88. #ifdef CONFIG_PM_ENABLE
  89. static esp_pm_lock_handle_t s_adc2_arbiter_lock;
  90. #endif //CONFIG_PM_ENABLE
  91. #endif //CONFIG_IDF_TARGET_ESP32S2
  92. /*---------------------------------------------------------------
  93. ADC Common
  94. ---------------------------------------------------------------*/
  95. void adc_power_always_on(void)
  96. {
  97. ADC_ENTER_CRITICAL();
  98. adc_hal_set_power_manage(ADC_POWER_SW_ON);
  99. ADC_EXIT_CRITICAL();
  100. }
  101. void adc_power_on(void)
  102. {
  103. ADC_ENTER_CRITICAL();
  104. /* The power FSM controlled mode saves more power, while the ADC noise may get increased. */
  105. #ifndef CONFIG_ADC_FORCE_XPD_FSM
  106. /* Set the power always on to increase precision. */
  107. adc_hal_set_power_manage(ADC_POWER_SW_ON);
  108. #else
  109. /* Use the FSM to turn off the power while not used to save power. */
  110. if (adc_hal_get_power_manage() != ADC_POWER_BY_FSM) {
  111. adc_hal_set_power_manage(ADC_POWER_SW_ON);
  112. }
  113. #endif
  114. ADC_EXIT_CRITICAL();
  115. }
  116. void adc_power_off(void)
  117. {
  118. ADC_ENTER_CRITICAL();
  119. adc_hal_set_power_manage(ADC_POWER_SW_OFF);
  120. ADC_EXIT_CRITICAL();
  121. }
  122. esp_err_t adc_set_clk_div(uint8_t clk_div)
  123. {
  124. ADC_ENTER_CRITICAL();
  125. adc_hal_digi_set_clk_div(clk_div);
  126. ADC_EXIT_CRITICAL();
  127. return ESP_OK;
  128. }
  129. static void adc_rtc_chan_init(adc_unit_t adc_unit)
  130. {
  131. if (adc_unit & ADC_UNIT_1) {
  132. /* Workaround: Disable the synchronization operation function of ADC1 and DAC.
  133. If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage. */
  134. dac_hal_rtc_sync_by_adc(false);
  135. adc_hal_rtc_output_invert(ADC_NUM_1, SOC_ADC1_DATA_INVERT_DEFAULT);
  136. adc_hal_set_sar_clk_div(ADC_NUM_1, SOC_ADC_SAR_CLK_DIV_DEFAULT(ADC_NUM_1));
  137. #ifdef CONFIG_IDF_TARGET_ESP32
  138. adc_hal_hall_disable(); //Disable other peripherals.
  139. adc_hal_amp_disable(); //Currently the LNA is not open, close it by default.
  140. #endif
  141. }
  142. if (adc_unit & ADC_UNIT_2) {
  143. adc_hal_pwdet_set_cct(SOC_ADC_PWDET_CCT_DEFAULT);
  144. adc_hal_rtc_output_invert(ADC_NUM_2, SOC_ADC2_DATA_INVERT_DEFAULT);
  145. adc_hal_set_sar_clk_div(ADC_NUM_2, SOC_ADC_SAR_CLK_DIV_DEFAULT(ADC_NUM_2));
  146. }
  147. }
  148. esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
  149. {
  150. gpio_num_t gpio_num = 0;
  151. if (adc_unit & ADC_UNIT_1) {
  152. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  153. gpio_num = ADC_GET_IO_NUM(ADC_NUM_1, channel);
  154. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  155. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  156. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  157. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  158. }
  159. if (adc_unit & ADC_UNIT_2) {
  160. ADC_CHANNEL_CHECK(ADC_NUM_2, channel);
  161. gpio_num = ADC_GET_IO_NUM(ADC_NUM_2, channel);
  162. ADC_CHECK_RET(rtc_gpio_init(gpio_num));
  163. ADC_CHECK_RET(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED));
  164. ADC_CHECK_RET(rtc_gpio_pulldown_dis(gpio_num));
  165. ADC_CHECK_RET(rtc_gpio_pullup_dis(gpio_num));
  166. }
  167. return ESP_OK;
  168. }
  169. esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en)
  170. {
  171. ADC_ENTER_CRITICAL();
  172. if (adc_unit & ADC_UNIT_1) {
  173. adc_hal_rtc_output_invert(ADC_NUM_1, inv_en);
  174. }
  175. if (adc_unit & ADC_UNIT_2) {
  176. adc_hal_rtc_output_invert(ADC_NUM_1, inv_en);
  177. }
  178. ADC_EXIT_CRITICAL();
  179. return ESP_OK;
  180. }
  181. esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t bits)
  182. {
  183. #ifdef CONFIG_IDF_TARGET_ESP32
  184. ADC_CHECK(bits < ADC_WIDTH_MAX, "WIDTH ERR: ESP32 support 9 ~ 12 bit width", ESP_ERR_INVALID_ARG);
  185. #elif defined CONFIG_IDF_TARGET_ESP32S2
  186. ADC_CHECK(bits == ADC_WIDTH_BIT_13, "WIDTH ERR: ESP32S2 support 13 bit width", ESP_ERR_INVALID_ARG);
  187. #endif
  188. ADC_ENTER_CRITICAL();
  189. if (adc_unit & ADC_UNIT_1) {
  190. adc_hal_rtc_set_output_format(ADC_NUM_1, bits);
  191. }
  192. if (adc_unit & ADC_UNIT_2) {
  193. adc_hal_rtc_set_output_format(ADC_NUM_2, bits);
  194. }
  195. ADC_EXIT_CRITICAL();
  196. return ESP_OK;
  197. }
  198. /**
  199. * @brief Reset RTC controller FSM.
  200. *
  201. * @return
  202. * - ESP_OK Success
  203. */
  204. #ifdef CONFIG_IDF_TARGET_ESP32S2
  205. esp_err_t adc_rtc_reset(void)
  206. {
  207. ADC_ENTER_CRITICAL();
  208. adc_hal_rtc_reset();
  209. ADC_EXIT_CRITICAL();
  210. return ESP_OK;
  211. }
  212. static inline void adc_set_init_code(adc_ll_num_t adc_n, adc_channel_t channel)
  213. {
  214. adc_atten_t atten = adc_hal_get_atten(adc_n, channel);
  215. uint32_t cal_val = adc_hal_calibration(adc_n, channel, atten, true, false);
  216. adc_hal_set_calibration_param(adc_n, cal_val);
  217. ESP_LOGD(ADC_TAG, "Set cal adc %d\n", cal_val);
  218. }
  219. #endif
  220. /*-------------------------------------------------------------------------------------
  221. * ADC1
  222. *------------------------------------------------------------------------------------*/
  223. esp_err_t adc1_pad_get_io_num(adc1_channel_t channel, gpio_num_t *gpio_num)
  224. {
  225. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  226. int io = ADC_GET_IO_NUM(ADC_NUM_1, channel);
  227. if (io < 0) {
  228. return ESP_ERR_INVALID_ARG;
  229. } else {
  230. *gpio_num = (gpio_num_t)io;
  231. }
  232. return ESP_OK;
  233. }
  234. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  235. {
  236. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  237. ADC_CHECK(atten < ADC_ATTEN_MAX, "ADC Atten Err", ESP_ERR_INVALID_ARG);
  238. adc_gpio_init(ADC_UNIT_1, channel);
  239. ADC_ENTER_CRITICAL();
  240. adc_rtc_chan_init(ADC_UNIT_1);
  241. adc_hal_set_atten(ADC_NUM_1, channel, atten);
  242. ADC_EXIT_CRITICAL();
  243. return ESP_OK;
  244. }
  245. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  246. {
  247. #ifdef CONFIG_IDF_TARGET_ESP32
  248. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "WIDTH ERR: ESP32 support 9 ~ 12 bit width", ESP_ERR_INVALID_ARG);
  249. #elif defined CONFIG_IDF_TARGET_ESP32S2
  250. ADC_CHECK(width_bit == ADC_WIDTH_BIT_13, "WIDTH ERR: ESP32S2 support 13 bit width", ESP_ERR_INVALID_ARG);
  251. #endif
  252. ADC_ENTER_CRITICAL();
  253. adc_hal_rtc_set_output_format(ADC_NUM_1, width_bit);
  254. ADC_EXIT_CRITICAL();
  255. return ESP_OK;
  256. }
  257. esp_err_t adc1_dma_mode_acquire(void)
  258. {
  259. /* Use locks to avoid digtal and RTC controller conflicts.
  260. for adc1, block until acquire the lock. */
  261. ADC1_DMA_LOCK_ACQUIRE();
  262. ESP_LOGD( ADC_TAG, "dma mode takes adc1 lock." );
  263. ADC_ENTER_CRITICAL();
  264. adc_hal_set_power_manage(ADC_POWER_SW_ON);
  265. /* switch SARADC into DIG channel */
  266. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_DIG);
  267. ADC_EXIT_CRITICAL();
  268. return ESP_OK;
  269. }
  270. esp_err_t adc1_rtc_mode_acquire(void)
  271. {
  272. /* Use locks to avoid digtal and RTC controller conflicts.
  273. for adc1, block until acquire the lock. */
  274. ADC1_DMA_LOCK_ACQUIRE();
  275. ADC_ENTER_CRITICAL();
  276. /* switch SARADC into RTC channel. */
  277. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_RTC);
  278. ADC_EXIT_CRITICAL();
  279. return ESP_OK;
  280. }
  281. esp_err_t adc1_lock_release(void)
  282. {
  283. ADC_CHECK((uint32_t *)adc1_dma_lock != NULL, "adc1 lock release called before acquire", ESP_ERR_INVALID_STATE );
  284. /* Use locks to avoid digtal and RTC controller conflicts. for adc1, block until acquire the lock. */
  285. ADC1_DMA_LOCK_RELEASE();
  286. return ESP_OK;
  287. }
  288. int adc1_get_raw(adc1_channel_t channel)
  289. {
  290. int adc_value;
  291. ADC_CHANNEL_CHECK(ADC_NUM_1, channel);
  292. adc1_rtc_mode_acquire();
  293. adc_power_on();
  294. ADC_ENTER_CRITICAL();
  295. #ifdef CONFIG_IDF_TARGET_ESP32S2
  296. adc_set_init_code(ADC_NUM_1, channel); // calibration for adc
  297. #endif
  298. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_RTC); //Set controller
  299. adc_hal_convert(ADC_NUM_1, channel, &adc_value); //Start conversion, For ADC1, the data always valid.
  300. ADC_EXIT_CRITICAL();
  301. #ifdef CONFIG_IDF_TARGET_ESP32S2
  302. adc_hal_rtc_reset(); //Reset FSM of rtc controller
  303. #endif
  304. adc1_lock_release();
  305. return adc_value;
  306. }
  307. int adc1_get_voltage(adc1_channel_t channel) //Deprecated. Use adc1_get_raw() instead
  308. {
  309. return adc1_get_raw(channel);
  310. }
  311. void adc1_ulp_enable(void)
  312. {
  313. adc_power_on();
  314. ADC_ENTER_CRITICAL();
  315. adc_hal_set_controller(ADC_NUM_1, ADC_CTRL_ULP);
  316. /* since most users do not need LNA and HALL with uLP, we disable them here
  317. open them in the uLP if needed. */
  318. #ifdef CONFIG_IDF_TARGET_ESP32
  319. /* disable other peripherals. */
  320. adc_hal_hall_disable();
  321. adc_hal_amp_disable();
  322. #endif
  323. ADC_EXIT_CRITICAL();
  324. }
  325. /*---------------------------------------------------------------
  326. ADC2
  327. ---------------------------------------------------------------*/
  328. esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num)
  329. {
  330. ADC_CHANNEL_CHECK(ADC_NUM_2, channel);
  331. int io = ADC_GET_IO_NUM(ADC_NUM_2, channel);
  332. if (io < 0) {
  333. return ESP_ERR_INVALID_ARG;
  334. } else {
  335. *gpio_num = (gpio_num_t)io;
  336. }
  337. return ESP_OK;
  338. }
  339. /** For ESP32S2 the ADC2 The right to use ADC2 is controlled by the arbiter, and there is no need to set a lock.*/
  340. esp_err_t adc2_wifi_acquire(void)
  341. {
  342. /* Wi-Fi module will use adc2. Use locks to avoid conflicts. */
  343. ADC2_WIFI_LOCK_ACQUIRE();
  344. ESP_LOGD( ADC_TAG, "Wi-Fi takes adc2 lock." );
  345. return ESP_OK;
  346. }
  347. esp_err_t adc2_wifi_release(void)
  348. {
  349. ADC_CHECK(ADC2_WIFI_LOCK_CHECK(), "wifi release called before acquire", ESP_ERR_INVALID_STATE );
  350. ADC2_WIFI_LOCK_RELEASE();
  351. ESP_LOGD( ADC_TAG, "Wi-Fi returns adc2 lock." );
  352. return ESP_OK;
  353. }
  354. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  355. {
  356. ADC_CHANNEL_CHECK(ADC_NUM_2, channel);
  357. ADC_CHECK(atten <= ADC_ATTEN_11db, "ADC2 Atten Err", ESP_ERR_INVALID_ARG);
  358. adc_gpio_init(ADC_UNIT_2, channel);
  359. ADC2_ENTER_CRITICAL();
  360. //avoid collision with other tasks
  361. if ( ADC2_WIFI_LOCK_TRY_ACQUIRE() == -1 ) {
  362. //try the lock, return if failed (wifi using).
  363. ADC2_EXIT_CRITICAL();
  364. return ESP_ERR_TIMEOUT;
  365. }
  366. adc_rtc_chan_init(ADC_UNIT_2);
  367. adc_hal_set_atten(ADC_NUM_2, channel, atten);
  368. ADC2_WIFI_LOCK_RELEASE();
  369. ADC2_EXIT_CRITICAL();
  370. return ESP_OK;
  371. }
  372. static inline void adc2_config_width(adc_bits_width_t width_bit)
  373. {
  374. #ifdef CONFIG_IDF_TARGET_ESP32S2
  375. #ifdef CONFIG_PM_ENABLE
  376. /* Lock APB clock. */
  377. if (s_adc2_arbiter_lock == NULL) {
  378. esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc2", &s_adc2_arbiter_lock);
  379. }
  380. #endif //CONFIG_PM_ENABLE
  381. #endif //CONFIG_IDF_TARGET_ESP32S2
  382. ADC_ENTER_CRITICAL();
  383. adc_hal_rtc_set_output_format(ADC_NUM_2, width_bit);
  384. ADC_EXIT_CRITICAL();
  385. }
  386. static inline void adc2_dac_disable( adc2_channel_t channel)
  387. {
  388. #ifdef CONFIG_IDF_TARGET_ESP32
  389. if ( channel == ADC2_CHANNEL_8 ) { // the same as DAC channel 1
  390. dac_output_disable(DAC_CHANNEL_1);
  391. } else if ( channel == ADC2_CHANNEL_9 ) {
  392. dac_output_disable(DAC_CHANNEL_2);
  393. }
  394. #elif defined CONFIG_IDF_TARGET_ESP32S2
  395. if ( channel == ADC2_CHANNEL_6 ) { // the same as DAC channel 1
  396. dac_output_disable(DAC_CHANNEL_1);
  397. } else if ( channel == ADC2_CHANNEL_7 ) {
  398. dac_output_disable(DAC_CHANNEL_2);
  399. }
  400. #endif
  401. }
  402. /**
  403. * @note For ESP32S2:
  404. * The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
  405. * Or, the RTC controller will fail when get raw data.
  406. * This issue does not occur on digital controllers (DMA mode), and the hardware guarantees that there will be no errors.
  407. */
  408. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out)
  409. {
  410. int adc_value = 0;
  411. ADC_CHECK(raw_out != NULL, "ADC out value err", ESP_ERR_INVALID_ARG);
  412. ADC_CHECK(channel < ADC2_CHANNEL_MAX, "ADC Channel Err", ESP_ERR_INVALID_ARG);
  413. #ifdef CONFIG_IDF_TARGET_ESP32
  414. ADC_CHECK(width_bit < ADC_WIDTH_MAX, "WIDTH ERR: ESP32 support 9 ~ 12 bit width", ESP_ERR_INVALID_ARG);
  415. #elif defined CONFIG_IDF_TARGET_ESP32S2
  416. ADC_CHECK(width_bit == ADC_WIDTH_BIT_13, "WIDTH ERR: ESP32S2 support 13 bit width", ESP_ERR_INVALID_ARG);
  417. #endif
  418. adc_power_on(); //in critical section with whole rtc module
  419. ADC2_ENTER_CRITICAL(); //avoid collision with other tasks
  420. if ( ADC2_WIFI_LOCK_TRY_ACQUIRE() == -1 ) { //try the lock, return if failed (wifi using).
  421. ADC2_EXIT_CRITICAL();
  422. return ESP_ERR_TIMEOUT;
  423. }
  424. #ifdef CONFIG_ADC_DISABLE_DAC
  425. adc2_dac_disable(channel); //disable other peripherals
  426. #endif
  427. adc2_config_width(width_bit); // in critical section with whole rtc module. because the PWDET use the same registers, place it here.
  428. #ifdef CONFIG_IDF_TARGET_ESP32S2
  429. adc_set_init_code(ADC_NUM_2, channel); // calibration for adc
  430. #endif
  431. adc_hal_set_controller(ADC_NUM_2, ADC_CTRL_RTC);// set controller
  432. #ifdef CONFIG_IDF_TARGET_ESP32S2
  433. #ifdef CONFIG_PM_ENABLE
  434. if (s_adc2_arbiter_lock) {
  435. esp_pm_lock_acquire(s_adc2_arbiter_lock);
  436. }
  437. #endif //CONFIG_PM_ENABLE
  438. #endif //CONFIG_IDF_TARGET_ESP32
  439. if (adc_hal_convert(ADC_NUM_2, channel, &adc_value)) {
  440. adc_value = -1;
  441. }
  442. #ifdef CONFIG_IDF_TARGET_ESP32S2
  443. #ifdef CONFIG_PM_ENABLE
  444. /* Release APB clock. */
  445. if (s_adc2_arbiter_lock) {
  446. esp_pm_lock_release(s_adc2_arbiter_lock);
  447. }
  448. #endif //CONFIG_PM_ENABLE
  449. #endif //CONFIG_IDF_TARGET_ESP32
  450. ADC2_WIFI_LOCK_RELEASE();
  451. ADC2_EXIT_CRITICAL();
  452. #ifdef CONFIG_IDF_TARGET_ESP32S2
  453. adc_rtc_reset();
  454. #endif
  455. if (adc_value < 0) {
  456. ESP_LOGD( ADC_TAG, "ADC2 ARB: Return data is invalid." );
  457. return ESP_ERR_INVALID_STATE;
  458. }
  459. *raw_out = adc_value;
  460. return ESP_OK;
  461. }