adc_common.c 20 KB

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