adc_common.c 19 KB

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