adc_common.c 22 KB

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