adc_common.c 20 KB

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