adc_legacy.c 31 KB

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