adc_common.c 20 KB

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