adc_legacy.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 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 "sdkconfig.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/semphr.h"
  12. #include "freertos/timers.h"
  13. #include "esp_log.h"
  14. #include "esp_check.h"
  15. #include "esp_pm.h"
  16. #include "soc/rtc.h"
  17. #include "driver/rtc_io.h"
  18. #include "sys/lock.h"
  19. #include "driver/gpio.h"
  20. #include "esp_private/adc_share_hw_ctrl.h"
  21. #include "adc1_private.h"
  22. #include "hal/adc_types.h"
  23. #include "hal/adc_hal.h"
  24. #include "hal/adc_hal_common.h"
  25. #include "hal/adc_hal_conf.h"
  26. #include "esp_private/periph_ctrl.h"
  27. #include "driver/adc_types_legacy.h"
  28. #if SOC_DAC_SUPPORTED
  29. #include "driver/dac.h"
  30. #include "hal/dac_hal.h"
  31. #endif
  32. #if CONFIG_IDF_TARGET_ESP32S3
  33. #include "esp_efuse_rtc_calib.h"
  34. #endif
  35. static const char *ADC_TAG = "ADC";
  36. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  37. //////////////////////// Locks ///////////////////////////////////////////
  38. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  39. #define RTC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  40. #define RTC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  41. #define DIGI_ENTER_CRITICAL()
  42. #define DIGI_EXIT_CRITICAL()
  43. #define ADC_POWER_ENTER() RTC_ENTER_CRITICAL()
  44. #define ADC_POWER_EXIT() RTC_EXIT_CRITICAL()
  45. #define DIGI_CONTROLLER_ENTER() DIGI_ENTER_CRITICAL()
  46. #define DIGI_CONTROLLER_EXIT() DIGI_EXIT_CRITICAL()
  47. #define SARADC1_ENTER() RTC_ENTER_CRITICAL()
  48. #define SARADC1_EXIT() RTC_EXIT_CRITICAL()
  49. #define SARADC2_ENTER() RTC_ENTER_CRITICAL()
  50. #define SARADC2_EXIT() RTC_EXIT_CRITICAL()
  51. //n stands for ADC unit: 1 for ADC1 and 2 for ADC2. Currently both unit touches the same registers
  52. #define VREF_ENTER(n) RTC_ENTER_CRITICAL()
  53. #define VREF_EXIT(n) RTC_EXIT_CRITICAL()
  54. #define FSM_ENTER() RTC_ENTER_CRITICAL()
  55. #define FSM_EXIT() RTC_EXIT_CRITICAL()
  56. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  57. //prevent ADC1 being used by I2S dma and other tasks at the same time.
  58. static _lock_t adc1_dma_lock;
  59. #define SARADC1_ACQUIRE() _lock_acquire( &adc1_dma_lock )
  60. #define SARADC1_RELEASE() _lock_release( &adc1_dma_lock )
  61. #endif
  62. /*
  63. In ADC2, there're two locks used for different cases:
  64. 1. lock shared with app and Wi-Fi:
  65. ESP32:
  66. When Wi-Fi using the ADC2, we assume it will never stop, so app checks the lock and returns immediately if failed.
  67. ESP32S2:
  68. The controller's control over the ADC is determined by the arbiter. There is no need to control by lock.
  69. 2. lock shared between tasks:
  70. when several tasks sharing the ADC2, we want to guarantee
  71. all the requests will be handled.
  72. Since conversions are short (about 31us), app returns the lock very soon,
  73. we use a spinlock to stand there waiting to do conversions one by one.
  74. adc2_spinlock should be acquired first, then call `adc_lock_release(ADC_UNIT_2)` or rtc_spinlock.
  75. */
  76. #if CONFIG_IDF_TARGET_ESP32S2
  77. #ifdef CONFIG_PM_ENABLE
  78. static esp_pm_lock_handle_t s_adc2_arbiter_lock;
  79. #endif //CONFIG_PM_ENABLE
  80. #endif // !CONFIG_IDF_TARGET_ESP32
  81. static esp_err_t adc_hal_convert(adc_unit_t adc_n, int channel, int *out_raw);
  82. /*---------------------------------------------------------------
  83. ADC Common
  84. ---------------------------------------------------------------*/
  85. esp_err_t adc1_pad_get_io_num(adc1_channel_t channel, gpio_num_t *gpio_num)
  86. {
  87. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_1), ESP_ERR_INVALID_ARG, ADC_TAG, "invalid channel");
  88. int io = ADC_GET_IO_NUM(ADC_UNIT_1, channel);
  89. if (io < 0) {
  90. return ESP_ERR_INVALID_ARG;
  91. } else {
  92. *gpio_num = (gpio_num_t)io;
  93. }
  94. return ESP_OK;
  95. }
  96. #if (SOC_ADC_PERIPH_NUM >= 2)
  97. esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num)
  98. {
  99. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_2), ESP_ERR_INVALID_ARG, ADC_TAG, "invalid channel");
  100. int io = ADC_GET_IO_NUM(ADC_UNIT_2, channel);
  101. if (io < 0) {
  102. return ESP_ERR_INVALID_ARG;
  103. } else {
  104. *gpio_num = (gpio_num_t)io;
  105. }
  106. return ESP_OK;
  107. }
  108. #endif
  109. //------------------------------------------------------------RTC Single Read----------------------------------------------//
  110. #if SOC_ADC_RTC_CTRL_SUPPORTED
  111. esp_err_t adc_set_clk_div(uint8_t clk_div)
  112. {
  113. DIGI_CONTROLLER_ENTER();
  114. adc_ll_digi_set_clk_div(clk_div);
  115. DIGI_CONTROLLER_EXIT();
  116. return ESP_OK;
  117. }
  118. static void adc_rtc_chan_init(adc_unit_t adc_unit)
  119. {
  120. if (adc_unit == ADC_UNIT_1) {
  121. /* Workaround: Disable the synchronization operation function of ADC1 and DAC.
  122. If enabled(default), ADC RTC controller sampling will cause the DAC channel output voltage. */
  123. #if SOC_DAC_SUPPORTED
  124. dac_hal_rtc_sync_by_adc(false);
  125. #endif
  126. adc_oneshot_ll_output_invert(ADC_UNIT_1, ADC_HAL_DATA_INVERT_DEFAULT(ADC_UNIT_1));
  127. adc_ll_set_sar_clk_div(ADC_UNIT_1, ADC_HAL_SAR_CLK_DIV_DEFAULT(ADC_UNIT_1));
  128. #ifdef CONFIG_IDF_TARGET_ESP32
  129. adc_ll_hall_disable(); //Disable other peripherals.
  130. adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
  131. #endif
  132. }
  133. if (adc_unit == ADC_UNIT_2) {
  134. adc_hal_pwdet_set_cct(ADC_HAL_PWDET_CCT_DEFAULT);
  135. adc_oneshot_ll_output_invert(ADC_UNIT_2, ADC_HAL_DATA_INVERT_DEFAULT(ADC_UNIT_2));
  136. adc_ll_set_sar_clk_div(ADC_UNIT_2, ADC_HAL_SAR_CLK_DIV_DEFAULT(ADC_UNIT_2));
  137. }
  138. }
  139. esp_err_t adc_common_gpio_init(adc_unit_t adc_unit, adc_channel_t channel)
  140. {
  141. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(adc_unit), ESP_ERR_INVALID_ARG, ADC_TAG, "invalid channel");
  142. gpio_num_t gpio_num = 0;
  143. //If called with `ADC_UNIT_BOTH (ADC_UNIT_1 | ADC_UNIT_2)`, both if blocks will be run
  144. if (adc_unit == ADC_UNIT_1) {
  145. gpio_num = ADC_GET_IO_NUM(ADC_UNIT_1, channel);
  146. } else if (adc_unit == ADC_UNIT_2) {
  147. gpio_num = ADC_GET_IO_NUM(ADC_UNIT_2, channel);
  148. } else {
  149. return ESP_ERR_INVALID_ARG;
  150. }
  151. ESP_RETURN_ON_ERROR(rtc_gpio_init(gpio_num), ADC_TAG, "rtc_gpio_init fail");
  152. ESP_RETURN_ON_ERROR(rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_DISABLED), ADC_TAG, "rtc_gpio_set_direction fail");
  153. ESP_RETURN_ON_ERROR(rtc_gpio_pulldown_dis(gpio_num), ADC_TAG, "rtc_gpio_pulldown_dis fail");
  154. ESP_RETURN_ON_ERROR(rtc_gpio_pullup_dis(gpio_num), ADC_TAG, "rtc_gpio_pullup_dis fail");
  155. return ESP_OK;
  156. }
  157. esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en)
  158. {
  159. if (adc_unit == ADC_UNIT_1) {
  160. SARADC1_ENTER();
  161. adc_oneshot_ll_output_invert(ADC_UNIT_1, inv_en);
  162. SARADC1_EXIT();
  163. }
  164. if (adc_unit == ADC_UNIT_2) {
  165. SARADC2_ENTER();
  166. adc_oneshot_ll_output_invert(ADC_UNIT_2, inv_en);
  167. SARADC2_EXIT();
  168. }
  169. return ESP_OK;
  170. }
  171. esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t width_bit)
  172. {
  173. ESP_RETURN_ON_FALSE(width_bit < ADC_WIDTH_MAX, ESP_ERR_INVALID_ARG, ADC_TAG, "unsupported bit width");
  174. adc_bitwidth_t bitwidth = 0;
  175. #if CONFIG_IDF_TARGET_ESP32
  176. if ((uint32_t)width_bit == (uint32_t)ADC_BITWIDTH_DEFAULT) {
  177. bitwidth = SOC_ADC_RTC_MAX_BITWIDTH;
  178. } else {
  179. switch(width_bit) {
  180. case ADC_WIDTH_BIT_9:
  181. bitwidth = ADC_BITWIDTH_9;
  182. break;
  183. case ADC_WIDTH_BIT_10:
  184. bitwidth = ADC_BITWIDTH_10;
  185. break;
  186. case ADC_WIDTH_BIT_11:
  187. bitwidth = ADC_BITWIDTH_11;
  188. break;
  189. case ADC_WIDTH_BIT_12:
  190. bitwidth = ADC_BITWIDTH_12;
  191. break;
  192. default:
  193. return ESP_ERR_INVALID_ARG;
  194. }
  195. }
  196. #elif CONFIG_IDF_TARGET_ESP32S2
  197. bitwidth = ADC_BITWIDTH_13;
  198. #else //esp32s3
  199. bitwidth = ADC_BITWIDTH_12;
  200. #endif
  201. if (adc_unit == ADC_UNIT_1) {
  202. SARADC1_ENTER();
  203. adc_oneshot_ll_set_output_bits(ADC_UNIT_1, bitwidth);
  204. SARADC1_EXIT();
  205. }
  206. if (adc_unit == ADC_UNIT_2) {
  207. SARADC2_ENTER();
  208. adc_oneshot_ll_set_output_bits(ADC_UNIT_2, bitwidth);
  209. SARADC2_EXIT();
  210. }
  211. return ESP_OK;
  212. }
  213. /**
  214. * @brief Reset RTC controller FSM.
  215. *
  216. * @return
  217. * - ESP_OK Success
  218. */
  219. #if !CONFIG_IDF_TARGET_ESP32
  220. esp_err_t adc_rtc_reset(void)
  221. {
  222. FSM_ENTER();
  223. adc_ll_rtc_reset();
  224. FSM_EXIT();
  225. return ESP_OK;
  226. }
  227. #endif
  228. /*-------------------------------------------------------------------------------------
  229. * ADC1
  230. *------------------------------------------------------------------------------------*/
  231. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  232. {
  233. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_1), ESP_ERR_INVALID_ARG, ADC_TAG, "invalid channel");
  234. ESP_RETURN_ON_FALSE(atten < SOC_ADC_ATTEN_NUM, ESP_ERR_INVALID_ARG, ADC_TAG, "ADC Atten Err");
  235. adc_common_gpio_init(ADC_UNIT_1, channel);
  236. SARADC1_ENTER();
  237. adc_rtc_chan_init(ADC_UNIT_1);
  238. adc_oneshot_ll_set_atten(ADC_UNIT_1, channel, atten);
  239. SARADC1_EXIT();
  240. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  241. adc_hal_calibration_init(ADC_UNIT_1);
  242. #endif
  243. return ESP_OK;
  244. }
  245. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  246. {
  247. ESP_RETURN_ON_FALSE(width_bit < ADC_WIDTH_MAX, ESP_ERR_INVALID_ARG, ADC_TAG, "unsupported bit width");
  248. adc_bitwidth_t bitwidth = 0;
  249. #if CONFIG_IDF_TARGET_ESP32
  250. if ((uint32_t)width_bit == (uint32_t)ADC_BITWIDTH_DEFAULT) {
  251. bitwidth = SOC_ADC_RTC_MAX_BITWIDTH;
  252. } else {
  253. switch(width_bit) {
  254. case ADC_WIDTH_BIT_9:
  255. bitwidth = ADC_BITWIDTH_9;
  256. break;
  257. case ADC_WIDTH_BIT_10:
  258. bitwidth = ADC_BITWIDTH_10;
  259. break;
  260. case ADC_WIDTH_BIT_11:
  261. bitwidth = ADC_BITWIDTH_11;
  262. break;
  263. case ADC_WIDTH_BIT_12:
  264. bitwidth = ADC_BITWIDTH_12;
  265. break;
  266. default:
  267. return ESP_ERR_INVALID_ARG;
  268. }
  269. }
  270. #elif CONFIG_IDF_TARGET_ESP32S2
  271. bitwidth = ADC_BITWIDTH_13;
  272. #else //esp32s3
  273. bitwidth = ADC_BITWIDTH_12;
  274. #endif
  275. SARADC1_ENTER();
  276. adc_oneshot_ll_set_output_bits(ADC_UNIT_1, bitwidth);
  277. SARADC1_EXIT();
  278. return ESP_OK;
  279. }
  280. esp_err_t adc1_dma_mode_acquire(void)
  281. {
  282. /* Use locks to avoid digtal and RTC controller conflicts.
  283. for adc1, block until acquire the lock. */
  284. SARADC1_ACQUIRE();
  285. ESP_LOGD( ADC_TAG, "dma mode takes adc1 lock." );
  286. adc_power_acquire();
  287. SARADC1_ENTER();
  288. /* switch SARADC into DIG channel */
  289. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_DIG);
  290. SARADC1_EXIT();
  291. return ESP_OK;
  292. }
  293. esp_err_t adc1_rtc_mode_acquire(void)
  294. {
  295. /* Use locks to avoid digtal and RTC controller conflicts.
  296. for adc1, block until acquire the lock. */
  297. SARADC1_ACQUIRE();
  298. adc_power_acquire();
  299. SARADC1_ENTER();
  300. /* switch SARADC into RTC channel. */
  301. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_RTC);
  302. SARADC1_EXIT();
  303. return ESP_OK;
  304. }
  305. esp_err_t adc1_lock_release(void)
  306. {
  307. ESP_RETURN_ON_FALSE((uint32_t *)adc1_dma_lock != NULL, ESP_ERR_INVALID_STATE, ADC_TAG, "adc1 lock release called before acquire");
  308. /* Use locks to avoid digtal and RTC controller conflicts. for adc1, block until acquire the lock. */
  309. adc_power_release();
  310. SARADC1_RELEASE();
  311. return ESP_OK;
  312. }
  313. int adc1_get_raw(adc1_channel_t channel)
  314. {
  315. int adc_value;
  316. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_1), ESP_ERR_INVALID_ARG, ADC_TAG, "invalid channel");
  317. adc1_rtc_mode_acquire();
  318. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  319. adc_atten_t atten = adc_ll_get_atten(ADC_UNIT_1, channel);
  320. adc_set_hw_calibration_code(ADC_UNIT_1, atten);
  321. #endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
  322. SARADC1_ENTER();
  323. #ifdef CONFIG_IDF_TARGET_ESP32
  324. adc_ll_hall_disable(); //Disable other peripherals.
  325. adc_ll_amp_disable(); //Currently the LNA is not open, close it by default.
  326. #endif
  327. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_RTC); //Set controller
  328. adc_oneshot_ll_set_channel(ADC_UNIT_1, channel);
  329. adc_hal_convert(ADC_UNIT_1, channel, &adc_value); //Start conversion, For ADC1, the data always valid.
  330. #if !CONFIG_IDF_TARGET_ESP32
  331. adc_ll_rtc_reset(); //Reset FSM of rtc controller
  332. #endif
  333. SARADC1_EXIT();
  334. adc1_lock_release();
  335. return adc_value;
  336. }
  337. int adc1_get_voltage(adc1_channel_t channel) //Deprecated. Use adc1_get_raw() instead
  338. {
  339. return adc1_get_raw(channel);
  340. }
  341. #if SOC_ULP_SUPPORTED
  342. void adc1_ulp_enable(void)
  343. {
  344. adc_power_acquire();
  345. SARADC1_ENTER();
  346. adc_ll_set_controller(ADC_UNIT_1, ADC_LL_CTRL_ULP);
  347. /* since most users do not need LNA and HALL with uLP, we disable them here
  348. open them in the uLP if needed. */
  349. #ifdef CONFIG_IDF_TARGET_ESP32
  350. /* disable other peripherals. */
  351. adc_ll_hall_disable();
  352. adc_ll_amp_disable();
  353. #endif
  354. SARADC1_EXIT();
  355. }
  356. #endif
  357. #if (SOC_ADC_PERIPH_NUM >= 2)
  358. /*---------------------------------------------------------------
  359. ADC2
  360. ---------------------------------------------------------------*/
  361. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  362. {
  363. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_2), ESP_ERR_INVALID_ARG, ADC_TAG, "invalid channel");
  364. ESP_RETURN_ON_FALSE(atten <= SOC_ADC_ATTEN_NUM, ESP_ERR_INVALID_ARG, ADC_TAG, "ADC2 Atten Err");
  365. adc_common_gpio_init(ADC_UNIT_2, channel);
  366. #if CONFIG_IDF_TARGET_ESP32
  367. /** For ESP32S2 and S3, the right to use ADC2 is controlled by the arbiter, and there is no need to set a lock.*/
  368. if (adc_lock_try_acquire(ADC_UNIT_2) != ESP_OK) {
  369. //try the lock, return if failed (wifi using).
  370. return ESP_ERR_TIMEOUT;
  371. }
  372. #endif
  373. //avoid collision with other tasks
  374. SARADC2_ENTER();
  375. adc_rtc_chan_init(ADC_UNIT_2);
  376. adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
  377. SARADC2_EXIT();
  378. #if CONFIG_IDF_TARGET_ESP32
  379. adc_lock_release(ADC_UNIT_2);
  380. #endif
  381. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  382. adc_hal_calibration_init(ADC_UNIT_2);
  383. #endif
  384. return ESP_OK;
  385. }
  386. static inline void adc2_init(void)
  387. {
  388. #if CONFIG_IDF_TARGET_ESP32S2
  389. #ifdef CONFIG_PM_ENABLE
  390. /* Lock APB clock. */
  391. if (s_adc2_arbiter_lock == NULL) {
  392. esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc2", &s_adc2_arbiter_lock);
  393. }
  394. #endif //CONFIG_PM_ENABLE
  395. #endif //CONFIG_IDF_TARGET_ESP32S2
  396. }
  397. static inline void adc2_dac_disable( adc2_channel_t channel)
  398. {
  399. #if SOC_DAC_SUPPORTED
  400. #ifdef CONFIG_IDF_TARGET_ESP32
  401. if ( channel == ADC2_CHANNEL_8 ) { // the same as DAC channel 1
  402. dac_output_disable(DAC_CHANNEL_1);
  403. } else if ( channel == ADC2_CHANNEL_9 ) {
  404. dac_output_disable(DAC_CHANNEL_2);
  405. }
  406. #else
  407. if ( channel == ADC2_CHANNEL_6 ) { // the same as DAC channel 1
  408. dac_output_disable(DAC_CHANNEL_1);
  409. } else if ( channel == ADC2_CHANNEL_7 ) {
  410. dac_output_disable(DAC_CHANNEL_2);
  411. }
  412. #endif
  413. #endif // SOC_DAC_SUPPORTED
  414. }
  415. /**
  416. * @note For ESP32S2:
  417. * The arbiter's working clock is APB_CLK. When the APB_CLK clock drops below 8 MHz, the arbiter must be in shield mode.
  418. * Or, the RTC controller will fail when get raw data.
  419. * This issue does not occur on digital controllers (DMA mode), and the hardware guarantees that there will be no errors.
  420. */
  421. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out)
  422. {
  423. esp_err_t ret = ESP_OK;
  424. int adc_value = 0;
  425. adc_bitwidth_t bitwidth = 0;
  426. ESP_RETURN_ON_FALSE(raw_out != NULL, ESP_ERR_INVALID_ARG, ADC_TAG, "ADC out value err");
  427. ESP_RETURN_ON_FALSE(channel < ADC2_CHANNEL_MAX, ESP_ERR_INVALID_ARG, ADC_TAG, "ADC Channel Err");
  428. ESP_RETURN_ON_FALSE(width_bit < ADC_WIDTH_MAX, ESP_ERR_INVALID_ARG, ADC_TAG, "unsupported bit width");
  429. #if CONFIG_IDF_TARGET_ESP32
  430. if ((uint32_t)width_bit == (uint32_t)ADC_BITWIDTH_DEFAULT) {
  431. bitwidth = SOC_ADC_RTC_MAX_BITWIDTH;
  432. } else {
  433. switch(width_bit) {
  434. case ADC_WIDTH_BIT_9:
  435. bitwidth = ADC_BITWIDTH_9;
  436. break;
  437. case ADC_WIDTH_BIT_10:
  438. bitwidth = ADC_BITWIDTH_10;
  439. break;
  440. case ADC_WIDTH_BIT_11:
  441. bitwidth = ADC_BITWIDTH_11;
  442. break;
  443. case ADC_WIDTH_BIT_12:
  444. bitwidth = ADC_BITWIDTH_12;
  445. break;
  446. default:
  447. return ESP_ERR_INVALID_ARG;
  448. }
  449. }
  450. #elif CONFIG_IDF_TARGET_ESP32S2
  451. bitwidth = ADC_BITWIDTH_13;
  452. #else //esp32s3
  453. bitwidth = ADC_BITWIDTH_12;
  454. #endif
  455. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  456. adc_atten_t atten = adc_ll_get_atten(ADC_UNIT_2, channel);
  457. adc_set_hw_calibration_code(ADC_UNIT_2, atten);
  458. #endif //SOC_ADC_CALIBRATION_V1_SUPPORTED
  459. #if CONFIG_IDF_TARGET_ESP32
  460. /** For ESP32S2 and S3, the right to use ADC2 is controlled by the arbiter, and there is no need to set a lock.*/
  461. if (adc_lock_try_acquire(ADC_UNIT_2) != ESP_OK) {
  462. //try the lock, return if failed (wifi using).
  463. return ESP_ERR_TIMEOUT;
  464. }
  465. #endif
  466. adc_power_acquire(); //in critical section with whole rtc module
  467. //avoid collision with other tasks
  468. adc2_init(); // in critical section with whole rtc module. because the PWDET use the same registers, place it here.
  469. SARADC2_ENTER();
  470. #if SOC_ADC_ARBITER_SUPPORTED
  471. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  472. adc_hal_arbiter_config(&config);
  473. #endif
  474. #ifdef CONFIG_ADC_DISABLE_DAC
  475. adc2_dac_disable(channel); //disable other peripherals
  476. #endif
  477. adc_oneshot_ll_set_output_bits(ADC_UNIT_2, bitwidth);
  478. #if CONFIG_IDF_TARGET_ESP32
  479. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_RTC);// set controller
  480. #else
  481. adc_ll_set_controller(ADC_UNIT_2, ADC_LL_CTRL_ARB);// set controller
  482. #endif
  483. #if CONFIG_IDF_TARGET_ESP32S2
  484. #ifdef CONFIG_PM_ENABLE
  485. if (s_adc2_arbiter_lock) {
  486. esp_pm_lock_acquire(s_adc2_arbiter_lock);
  487. }
  488. #endif //CONFIG_PM_ENABLE
  489. #endif //CONFIG_IDF_TARGET_ESP32
  490. adc_oneshot_ll_set_channel(ADC_UNIT_2, channel);
  491. ret = adc_hal_convert(ADC_UNIT_2, channel, &adc_value);
  492. if (ret != ESP_OK) {
  493. adc_value = -1;
  494. }
  495. #if CONFIG_IDF_TARGET_ESP32S2
  496. #ifdef CONFIG_PM_ENABLE
  497. /* Release APB clock. */
  498. if (s_adc2_arbiter_lock) {
  499. esp_pm_lock_release(s_adc2_arbiter_lock);
  500. }
  501. #endif //CONFIG_PM_ENABLE
  502. #endif //CONFIG_IDF_TARGET_ESP32
  503. SARADC2_EXIT();
  504. adc_power_release();
  505. #if CONFIG_IDF_TARGET_ESP32
  506. adc_lock_release(ADC_UNIT_2);
  507. #endif
  508. *raw_out = adc_value;
  509. return ret;
  510. }
  511. esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
  512. {
  513. #ifdef CONFIG_IDF_TARGET_ESP32
  514. if (adc_unit == ADC_UNIT_1) {
  515. return ESP_ERR_INVALID_ARG;
  516. }
  517. #endif
  518. adc2_channel_t ch = ADC2_CHANNEL_MAX;
  519. /* Check if the GPIO supported. */
  520. for (int i = 0; i < ADC2_CHANNEL_MAX; i++) {
  521. if (gpio == ADC_GET_IO_NUM(ADC_UNIT_2, i)) {
  522. ch = i;
  523. break;
  524. }
  525. }
  526. if (ch == ADC2_CHANNEL_MAX) {
  527. return ESP_ERR_INVALID_ARG;
  528. }
  529. adc_power_acquire();
  530. if (adc_unit == ADC_UNIT_1) {
  531. VREF_ENTER(1);
  532. adc_hal_vref_output(ADC_UNIT_1, ch, true);
  533. VREF_EXIT(1);
  534. } else if (adc_unit == ADC_UNIT_2) {
  535. VREF_ENTER(2);
  536. adc_hal_vref_output(ADC_UNIT_2, ch, true);
  537. VREF_EXIT(2);
  538. }
  539. //Configure RTC gpio, Only ADC2's channels IO are supported to output reference voltage.
  540. adc_common_gpio_init(ADC_UNIT_2, ch);
  541. return ESP_OK;
  542. }
  543. #endif //SOC_ADC_RTC_CTRL_SUPPORTED
  544. #endif //#if (SOC_ADC_PERIPH_NUM >= 2)
  545. #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  546. /*---------------------------------------------------------------
  547. Legacy ADC Single Read Mode
  548. when RTC controller isn't supported
  549. ---------------------------------------------------------------*/
  550. #include "esp_check.h"
  551. portMUX_TYPE adc_reg_lock = portMUX_INITIALIZER_UNLOCKED;
  552. #define ADC_REG_LOCK_ENTER() portENTER_CRITICAL(&adc_reg_lock)
  553. #define ADC_REG_LOCK_EXIT() portEXIT_CRITICAL(&adc_reg_lock)
  554. static adc_atten_t s_atten1_single[ADC1_CHANNEL_MAX]; //Array saving attenuate of each channel of ADC1, used by single read API
  555. #if (SOC_ADC_PERIPH_NUM >= 2)
  556. static adc_atten_t s_atten2_single[ADC2_CHANNEL_MAX]; //Array saving attenuate of each channel of ADC2, used by single read API
  557. #endif
  558. static int8_t adc_digi_get_io_num(adc_unit_t adc_unit, uint8_t adc_channel)
  559. {
  560. assert(adc_unit <= SOC_ADC_PERIPH_NUM);
  561. uint8_t adc_n = (adc_unit == ADC_UNIT_1) ? 0 : 1;
  562. return adc_channel_io_map[adc_n][adc_channel];
  563. }
  564. static esp_err_t adc_digi_gpio_init(adc_unit_t adc_unit, uint16_t channel_mask)
  565. {
  566. esp_err_t ret = ESP_OK;
  567. uint64_t gpio_mask = 0;
  568. uint32_t n = 0;
  569. int8_t io = 0;
  570. while (channel_mask) {
  571. if (channel_mask & 0x1) {
  572. io = adc_digi_get_io_num(adc_unit, n);
  573. if (io < 0) {
  574. return ESP_ERR_INVALID_ARG;
  575. }
  576. gpio_mask |= BIT64(io);
  577. }
  578. channel_mask = channel_mask >> 1;
  579. n++;
  580. }
  581. gpio_config_t cfg = {
  582. .pin_bit_mask = gpio_mask,
  583. .mode = GPIO_MODE_DISABLE,
  584. };
  585. ret = gpio_config(&cfg);
  586. return ret;
  587. }
  588. #if CONFIG_IDF_TARGET_ESP32C3
  589. esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
  590. {
  591. esp_err_t ret;
  592. uint32_t channel = ADC2_CHANNEL_MAX;
  593. if (adc_unit == ADC_UNIT_2) {
  594. for (int i = 0; i < ADC2_CHANNEL_MAX; i++) {
  595. if (gpio == ADC_GET_IO_NUM(ADC_UNIT_2, i)) {
  596. channel = i;
  597. break;
  598. }
  599. }
  600. if (channel == ADC2_CHANNEL_MAX) {
  601. return ESP_ERR_INVALID_ARG;
  602. }
  603. }
  604. adc_power_acquire();
  605. if (adc_unit == ADC_UNIT_1) {
  606. RTC_ENTER_CRITICAL();
  607. adc_hal_vref_output(ADC_UNIT_1, channel, true);
  608. RTC_EXIT_CRITICAL();
  609. } else { //ADC_UNIT_2
  610. RTC_ENTER_CRITICAL();
  611. adc_hal_vref_output(ADC_UNIT_2, channel, true);
  612. RTC_EXIT_CRITICAL();
  613. }
  614. ret = adc_digi_gpio_init(ADC_UNIT_2, BIT(channel));
  615. return ret;
  616. }
  617. #endif
  618. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  619. {
  620. //On ESP32C3, the data width is always 12-bits.
  621. if (width_bit != ADC_WIDTH_BIT_12) {
  622. return ESP_ERR_INVALID_ARG;
  623. }
  624. return ESP_OK;
  625. }
  626. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  627. {
  628. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_1), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC1 channel error");
  629. ESP_RETURN_ON_FALSE((atten < SOC_ADC_ATTEN_NUM), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC Atten Err");
  630. esp_err_t ret = ESP_OK;
  631. s_atten1_single[channel] = atten;
  632. ret = adc_digi_gpio_init(ADC_UNIT_1, BIT(channel));
  633. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  634. adc_hal_calibration_init(ADC_UNIT_1);
  635. #endif
  636. return ret;
  637. }
  638. int adc1_get_raw(adc1_channel_t channel)
  639. {
  640. int raw_out = 0;
  641. if (adc_lock_try_acquire(ADC_UNIT_1) != ESP_OK) {
  642. return ESP_ERR_TIMEOUT;
  643. }
  644. periph_module_enable(PERIPH_SARADC_MODULE);
  645. adc_power_acquire();
  646. adc_ll_digi_clk_sel(0);
  647. adc_atten_t atten = s_atten1_single[channel];
  648. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  649. adc_set_hw_calibration_code(ADC_UNIT_1, atten);
  650. #endif
  651. ADC_REG_LOCK_ENTER();
  652. adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
  653. adc_hal_convert(ADC_UNIT_1, channel, &raw_out);
  654. ADC_REG_LOCK_EXIT();
  655. adc_power_release();
  656. periph_module_disable(PERIPH_SARADC_MODULE);
  657. adc_lock_release(ADC_UNIT_1);
  658. return raw_out;
  659. }
  660. #if (SOC_ADC_PERIPH_NUM >= 2)
  661. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  662. {
  663. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_2), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC2 channel error");
  664. ESP_RETURN_ON_FALSE((atten <= ADC_ATTEN_DB_11), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC2 Atten Err");
  665. esp_err_t ret = ESP_OK;
  666. s_atten2_single[channel] = atten;
  667. ret = adc_digi_gpio_init(ADC_UNIT_2, BIT(channel));
  668. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  669. adc_hal_calibration_init(ADC_UNIT_2);
  670. #endif
  671. return ret;
  672. }
  673. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out)
  674. {
  675. //On ESP32C3, the data width is always 12-bits.
  676. if (width_bit != ADC_WIDTH_BIT_12) {
  677. return ESP_ERR_INVALID_ARG;
  678. }
  679. esp_err_t ret = ESP_OK;
  680. if (adc_lock_try_acquire(ADC_UNIT_2) != ESP_OK) {
  681. return ESP_ERR_TIMEOUT;
  682. }
  683. periph_module_enable(PERIPH_SARADC_MODULE);
  684. adc_power_acquire();
  685. adc_ll_digi_clk_sel(0);
  686. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  687. adc_hal_arbiter_config(&config);
  688. adc_atten_t atten = s_atten2_single[channel];
  689. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  690. adc_set_hw_calibration_code(ADC_UNIT_2, atten);
  691. #endif
  692. ADC_REG_LOCK_ENTER();
  693. adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
  694. ret = adc_hal_convert(ADC_UNIT_2, channel, raw_out);
  695. ADC_REG_LOCK_EXIT();
  696. adc_power_release();
  697. periph_module_disable(PERIPH_SARADC_MODULE);
  698. adc_lock_release(ADC_UNIT_2);
  699. return ret;
  700. }
  701. #endif //#if (SOC_ADC_PERIPH_NUM >= 2)
  702. #endif //#if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  703. static void adc_hal_onetime_start(adc_unit_t adc_n)
  704. {
  705. #if SOC_ADC_DIG_CTRL_SUPPORTED && !SOC_ADC_RTC_CTRL_SUPPORTED
  706. (void)adc_n;
  707. /**
  708. * There is a hardware limitation. If the APB clock frequency is high, the step of this reg signal: ``onetime_start`` may not be captured by the
  709. * ADC digital controller (when its clock frequency is too slow). A rough estimate for this step should be at least 3 ADC digital controller
  710. * clock cycle.
  711. *
  712. * This limitation will be removed in hardware future versions.
  713. *
  714. */
  715. uint32_t digi_clk = APB_CLK_FREQ / (ADC_LL_CLKM_DIV_NUM_DEFAULT + ADC_LL_CLKM_DIV_A_DEFAULT / ADC_LL_CLKM_DIV_B_DEFAULT + 1);
  716. //Convert frequency to time (us). Since decimals are removed by this division operation. Add 1 here in case of the fact that delay is not enough.
  717. uint32_t delay = (1000 * 1000) / digi_clk + 1;
  718. //3 ADC digital controller clock cycle
  719. delay = delay * 3;
  720. //This coefficient (8) is got from test. When digi_clk is not smaller than ``APB_CLK_FREQ/8``, no delay is needed.
  721. if (digi_clk >= APB_CLK_FREQ/8) {
  722. delay = 0;
  723. }
  724. adc_oneshot_ll_start(false);
  725. esp_rom_delay_us(delay);
  726. adc_oneshot_ll_start(true);
  727. //No need to delay here. Becuase if the start signal is not seen, there won't be a done intr.
  728. #else
  729. adc_oneshot_ll_start(adc_n);
  730. #endif
  731. }
  732. static esp_err_t adc_hal_convert(adc_unit_t adc_n, int channel, int *out_raw)
  733. {
  734. uint32_t event = (adc_n == ADC_UNIT_1) ? ADC_LL_EVENT_ADC1_ONESHOT_DONE : ADC_LL_EVENT_ADC2_ONESHOT_DONE;
  735. adc_oneshot_ll_clear_event(event);
  736. adc_oneshot_ll_disable_all_unit();
  737. adc_oneshot_ll_enable(adc_n);
  738. adc_oneshot_ll_set_channel(adc_n, channel);
  739. adc_hal_onetime_start(adc_n);
  740. while (adc_oneshot_ll_get_event(event) != true) {
  741. ;
  742. }
  743. *out_raw = adc_oneshot_ll_get_raw_result(adc_n);
  744. if (adc_oneshot_ll_raw_check_valid(adc_n, *out_raw) == false) {
  745. return ESP_ERR_INVALID_STATE;
  746. }
  747. //HW workaround: when enabling periph clock, this should be false
  748. adc_oneshot_ll_disable_all_unit();
  749. return ESP_OK;
  750. }
  751. /**
  752. * @brief This function will be called during start up, to check that adc_oneshot driver is not running along with the legacy adc oneshot driver
  753. */
  754. __attribute__((constructor))
  755. static void check_adc_oneshot_driver_conflict(void)
  756. {
  757. // This function was declared as weak here. adc_oneshot driver has one implementation.
  758. // So if adc_oneshot driver is not linked in, then `adc_oneshot_new_unit` should be NULL at runtime.
  759. extern __attribute__((weak)) esp_err_t adc_oneshot_new_unit(const void *init_config, void **ret_unit);
  760. if ((void *)adc_oneshot_new_unit != NULL) {
  761. ESP_EARLY_LOGE(ADC_TAG, "CONFLICT! driver_ng is not allowed to be used with the legacy driver");
  762. abort();
  763. }
  764. ESP_EARLY_LOGW(ADC_TAG, "legacy driver is deprecated, please migrate to `esp_adc/adc_oneshot.h`");
  765. }
  766. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  767. /*---------------------------------------------------------------
  768. ADC Hardware Calibration
  769. ---------------------------------------------------------------*/
  770. static __attribute__((constructor)) void adc_hw_calibration(void)
  771. {
  772. //Calculate all ICode
  773. for (int i = 0; i < SOC_ADC_PERIPH_NUM; i++) {
  774. adc_hal_calibration_init(i);
  775. for (int j = 0; j < SOC_ADC_ATTEN_NUM; j++) {
  776. /**
  777. * This may get wrong when attenuations are NOT consecutive on some chips,
  778. * update this when bringing up the calibration on that chip
  779. */
  780. adc_calc_hw_calibration_code(i, j);
  781. }
  782. }
  783. }
  784. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED