adc_common.c 19 KB

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