adc_common.c 21 KB

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