adc_common.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "sdkconfig.h"
  11. #include "driver/gpio.h"
  12. #include "hal/adc_types.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #if CONFIG_IDF_TARGET_ESP32
  17. /**** `adc1_channel_t` will be deprecated functions, combine into `adc_channel_t` ********/
  18. typedef enum {
  19. ADC1_CHANNEL_0 = 0, /*!< ADC1 channel 0 is GPIO36 */
  20. ADC1_CHANNEL_1, /*!< ADC1 channel 1 is GPIO37 */
  21. ADC1_CHANNEL_2, /*!< ADC1 channel 2 is GPIO38 */
  22. ADC1_CHANNEL_3, /*!< ADC1 channel 3 is GPIO39 */
  23. ADC1_CHANNEL_4, /*!< ADC1 channel 4 is GPIO32 */
  24. ADC1_CHANNEL_5, /*!< ADC1 channel 5 is GPIO33 */
  25. ADC1_CHANNEL_6, /*!< ADC1 channel 6 is GPIO34 */
  26. ADC1_CHANNEL_7, /*!< ADC1 channel 7 is GPIO35 */
  27. ADC1_CHANNEL_MAX,
  28. } adc1_channel_t;
  29. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 // TODO ESP32-S3 channels are wrong IDF-1776
  30. /**** `adc1_channel_t` will be deprecated functions, combine into `adc_channel_t` ********/
  31. typedef enum {
  32. ADC1_CHANNEL_0 = 0, /*!< ADC1 channel 0 is GPIO1 */
  33. ADC1_CHANNEL_1, /*!< ADC1 channel 1 is GPIO2 */
  34. ADC1_CHANNEL_2, /*!< ADC1 channel 2 is GPIO3 */
  35. ADC1_CHANNEL_3, /*!< ADC1 channel 3 is GPIO4 */
  36. ADC1_CHANNEL_4, /*!< ADC1 channel 4 is GPIO5 */
  37. ADC1_CHANNEL_5, /*!< ADC1 channel 5 is GPIO6 */
  38. ADC1_CHANNEL_6, /*!< ADC1 channel 6 is GPIO7 */
  39. ADC1_CHANNEL_7, /*!< ADC1 channel 7 is GPIO8 */
  40. ADC1_CHANNEL_8, /*!< ADC1 channel 8 is GPIO9 */
  41. ADC1_CHANNEL_9, /*!< ADC1 channel 9 is GPIO10 */
  42. ADC1_CHANNEL_MAX,
  43. } adc1_channel_t;
  44. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  45. /**** `adc1_channel_t` will be deprecated functions, combine into `adc_channel_t` ********/
  46. typedef enum {
  47. ADC1_CHANNEL_0 = 0, /*!< ADC1 channel 0 is GPIO0 */
  48. ADC1_CHANNEL_1, /*!< ADC1 channel 1 is GPIO1 */
  49. ADC1_CHANNEL_2, /*!< ADC1 channel 2 is GPIO2 */
  50. ADC1_CHANNEL_3, /*!< ADC1 channel 3 is GPIO3 */
  51. ADC1_CHANNEL_4, /*!< ADC1 channel 4 is GPIO4 */
  52. ADC1_CHANNEL_MAX,
  53. } adc1_channel_t;
  54. #endif // CONFIG_IDF_TARGET_*
  55. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 // TODO ESP32-S3 channels are wrong IDF-1776
  56. /**** `adc2_channel_t` will be deprecated functions, combine into `adc_channel_t` ********/
  57. typedef enum {
  58. ADC2_CHANNEL_0 = 0, /*!< ADC2 channel 0 is GPIO4 (ESP32), GPIO11 (ESP32-S2) */
  59. ADC2_CHANNEL_1, /*!< ADC2 channel 1 is GPIO0 (ESP32), GPIO12 (ESP32-S2) */
  60. ADC2_CHANNEL_2, /*!< ADC2 channel 2 is GPIO2 (ESP32), GPIO13 (ESP32-S2) */
  61. ADC2_CHANNEL_3, /*!< ADC2 channel 3 is GPIO15 (ESP32), GPIO14 (ESP32-S2) */
  62. ADC2_CHANNEL_4, /*!< ADC2 channel 4 is GPIO13 (ESP32), GPIO15 (ESP32-S2) */
  63. ADC2_CHANNEL_5, /*!< ADC2 channel 5 is GPIO12 (ESP32), GPIO16 (ESP32-S2) */
  64. ADC2_CHANNEL_6, /*!< ADC2 channel 6 is GPIO14 (ESP32), GPIO17 (ESP32-S2) */
  65. ADC2_CHANNEL_7, /*!< ADC2 channel 7 is GPIO27 (ESP32), GPIO18 (ESP32-S2) */
  66. ADC2_CHANNEL_8, /*!< ADC2 channel 8 is GPIO25 (ESP32), GPIO19 (ESP32-S2) */
  67. ADC2_CHANNEL_9, /*!< ADC2 channel 9 is GPIO26 (ESP32), GPIO20 (ESP32-S2) */
  68. ADC2_CHANNEL_MAX,
  69. } adc2_channel_t;
  70. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  71. /**** `adc2_channel_t` will be deprecated functions, combine into `adc_channel_t` ********/
  72. typedef enum {
  73. ADC2_CHANNEL_0 = 0, /*!< ADC2 channel 0 is GPIO5 */
  74. ADC2_CHANNEL_MAX,
  75. } adc2_channel_t;
  76. #endif
  77. /**
  78. * @brief ADC rtc controller attenuation option.
  79. *
  80. * @note This definitions are only for being back-compatible
  81. */
  82. #define ADC_ATTEN_0db ADC_ATTEN_DB_0
  83. #define ADC_ATTEN_2_5db ADC_ATTEN_DB_2_5
  84. #define ADC_ATTEN_6db ADC_ATTEN_DB_6
  85. #define ADC_ATTEN_11db ADC_ATTEN_DB_11
  86. /**
  87. * The default (max) bit width of the ADC of current version. You can also get the maximum bitwidth
  88. * by `SOC_ADC_MAX_BITWIDTH` defined in soc_caps.h.
  89. */
  90. #define ADC_WIDTH_BIT_DEFAULT (ADC_WIDTH_MAX-1)
  91. //this definitions are only for being back-compatible
  92. #define ADC_WIDTH_9Bit ADC_WIDTH_BIT_9
  93. #define ADC_WIDTH_10Bit ADC_WIDTH_BIT_10
  94. #define ADC_WIDTH_11Bit ADC_WIDTH_BIT_11
  95. #define ADC_WIDTH_12Bit ADC_WIDTH_BIT_12
  96. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  97. /**
  98. * @brief Digital ADC DMA read max timeout value, it may make the ``adc_digi_read_bytes`` block forever if the OS supports
  99. */
  100. #define ADC_MAX_DELAY UINT32_MAX
  101. #endif
  102. /**
  103. * @brief ADC digital controller encode option.
  104. *
  105. * @deprecated The ESP32-S2 doesn't use I2S DMA. Call ``adc_digi_output_format_t`` instead.
  106. */
  107. typedef enum {
  108. ADC_ENCODE_12BIT, /*!< ADC to DMA data format, , [15:12]-channel [11:0]-12 bits ADC data */
  109. ADC_ENCODE_11BIT, /*!< ADC to DMA data format, [15]-unit, [14:11]-channel [10:0]-11 bits ADC data */
  110. ADC_ENCODE_MAX,
  111. } adc_i2s_encode_t;
  112. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  113. //This feature is currently supported on ESP32C3, will be supported on other chips soon
  114. /**
  115. * @brief Digital ADC DMA configuration
  116. */
  117. typedef struct adc_digi_init_config_s {
  118. uint32_t max_store_buf_size; ///< Max length of the converted data that driver can store before they are processed.
  119. uint32_t conv_num_each_intr; ///< Bytes of data that can be converted in 1 interrupt.
  120. uint32_t adc1_chan_mask; ///< Channel list of ADC1 to be initialized.
  121. uint32_t adc2_chan_mask; ///< Channel list of ADC2 to be initialized.
  122. } adc_digi_init_config_t;
  123. #endif
  124. /*---------------------------------------------------------------
  125. Common setting
  126. ---------------------------------------------------------------*/
  127. /**
  128. * @brief Enable ADC power
  129. * @deprecated Use adc_power_acquire and adc_power_release instead.
  130. */
  131. void adc_power_on(void) __attribute__((deprecated));
  132. /**
  133. * @brief Power off SAR ADC
  134. * @deprecated Use adc_power_acquire and adc_power_release instead.
  135. * This function will force power down for ADC.
  136. * This function is deprecated because forcing power ADC power off may
  137. * disrupt operation of other components which may be using the ADC.
  138. */
  139. void adc_power_off(void) __attribute__((deprecated));
  140. /**
  141. * @brief Increment the usage counter for ADC module.
  142. * ADC will stay powered on while the counter is greater than 0.
  143. * Call adc_power_release when done using the ADC.
  144. */
  145. void adc_power_acquire(void);
  146. /**
  147. * @brief Decrement the usage counter for ADC module.
  148. * ADC will stay powered on while the counter is greater than 0.
  149. * Call this function when done using the ADC.
  150. */
  151. void adc_power_release(void);
  152. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
  153. /**
  154. * @brief Initialize ADC pad
  155. * @param adc_unit ADC unit index
  156. * @param channel ADC channel index
  157. * @return
  158. * - ESP_OK success
  159. * - ESP_ERR_INVALID_ARG Parameter error
  160. */
  161. esp_err_t adc_gpio_init(adc_unit_t adc_unit, adc_channel_t channel);
  162. #endif //#if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
  163. /*---------------------------------------------------------------
  164. ADC Single Read Setting
  165. ---------------------------------------------------------------*/
  166. /**
  167. * @brief Get the GPIO number of a specific ADC1 channel.
  168. *
  169. * @param channel Channel to get the GPIO number
  170. * @param gpio_num output buffer to hold the GPIO number
  171. *
  172. * @return
  173. * - ESP_OK if success
  174. * - ESP_ERR_INVALID_ARG if channel not valid
  175. */
  176. esp_err_t adc1_pad_get_io_num(adc1_channel_t channel, gpio_num_t *gpio_num);
  177. /**
  178. * @brief Set the attenuation of a particular channel on ADC1, and configure its associated GPIO pin mux.
  179. *
  180. * The default ADC voltage is for attenuation 0 dB and listed in the table below.
  181. * By setting higher attenuation it is possible to read higher voltages.
  182. *
  183. * Due to ADC characteristics, most accurate results are obtained within the "suggested range"
  184. * shown in the following table.
  185. *
  186. * +----------+-------------+-----------------+
  187. * | | attenuation | suggested range |
  188. * | SoC | (dB) | (mV) |
  189. * +==========+=============+=================+
  190. * | | 0 | 100 ~ 950 |
  191. * | +-------------+-----------------+
  192. * | | 2.5 | 100 ~ 1250 |
  193. * | ESP32 +-------------+-----------------+
  194. * | | 6 | 150 ~ 1750 |
  195. * | +-------------+-----------------+
  196. * | | 11 | 150 ~ 2450 |
  197. * +----------+-------------+-----------------+
  198. * | | 0 | 0 ~ 750 |
  199. * | +-------------+-----------------+
  200. * | | 2.5 | 0 ~ 1050 |
  201. * | ESP32-S2 +-------------+-----------------+
  202. * | | 6 | 0 ~ 1300 |
  203. * | +-------------+-----------------+
  204. * | | 11 | 0 ~ 2500 |
  205. * +----------+-------------+-----------------+
  206. *
  207. * For maximum accuracy, use the ADC calibration APIs and measure voltages within these recommended ranges.
  208. *
  209. * @note For any given channel, this function must be called before the first time ``adc1_get_raw()`` is called for that channel.
  210. *
  211. * @note This function can be called multiple times to configure multiple
  212. * ADC channels simultaneously. You may call ``adc1_get_raw()`` only after configuring a channel.
  213. *
  214. * @param channel ADC1 channel to configure
  215. * @param atten Attenuation level
  216. *
  217. * @return
  218. * - ESP_OK success
  219. * - ESP_ERR_INVALID_ARG Parameter error
  220. */
  221. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten);
  222. /**
  223. * @brief Configure ADC1 capture width, meanwhile enable output invert for ADC1.
  224. * The configuration is for all channels of ADC1
  225. * @param width_bit Bit capture width for ADC1
  226. *
  227. * @return
  228. * - ESP_OK success
  229. * - ESP_ERR_INVALID_ARG Parameter error
  230. */
  231. esp_err_t adc1_config_width(adc_bits_width_t width_bit);
  232. /**
  233. * @brief Take an ADC1 reading from a single channel.
  234. * @note ESP32:
  235. * When the power switch of SARADC1, SARADC2, HALL sensor and AMP sensor is turned on,
  236. * the input of GPIO36 and GPIO39 will be pulled down for about 80ns.
  237. * When enabling power for any of these peripherals, ignore input from GPIO36 and GPIO39.
  238. * Please refer to section 3.11 of 'ECO_and_Workarounds_for_Bugs_in_ESP32' for the description of this issue.
  239. * As a workaround, call adc_power_acquire() in the app. This will result in higher power consumption (by ~1mA),
  240. * but will remove the glitches on GPIO36 and GPIO39.
  241. *
  242. * @note Call ``adc1_config_width()`` before the first time this
  243. * function is called.
  244. *
  245. * @note For any given channel, adc1_config_channel_atten(channel)
  246. * must be called before the first time this function is called. Configuring
  247. * a new channel does not prevent a previously configured channel from being read.
  248. *
  249. * @param channel ADC1 channel to read
  250. *
  251. * @return
  252. * - -1: Parameter error
  253. * - Other: ADC1 channel reading.
  254. */
  255. int adc1_get_raw(adc1_channel_t channel);
  256. #if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
  257. /**
  258. * @brief Set ADC data invert
  259. * @param adc_unit ADC unit index
  260. * @param inv_en whether enable data invert
  261. * @return
  262. * - ESP_OK success
  263. * - ESP_ERR_INVALID_ARG Parameter error
  264. */
  265. esp_err_t adc_set_data_inv(adc_unit_t adc_unit, bool inv_en);
  266. /**
  267. * @brief Set ADC source clock
  268. * @param clk_div ADC clock divider, ADC clock is divided from APB clock
  269. * @return
  270. * - ESP_OK success
  271. */
  272. esp_err_t adc_set_clk_div(uint8_t clk_div);
  273. /**
  274. * @brief Configure ADC capture width.
  275. *
  276. * @param adc_unit ADC unit index
  277. * @param width_bit Bit capture width for ADC unit.
  278. *
  279. * @return
  280. * - ESP_OK success
  281. * - ESP_ERR_INVALID_ARG Parameter error
  282. */
  283. esp_err_t adc_set_data_width(adc_unit_t adc_unit, adc_bits_width_t width_bit);
  284. /**
  285. * @brief Configure ADC1 to be usable by the ULP
  286. *
  287. * This function reconfigures ADC1 to be controlled by the ULP.
  288. * Effect of this function can be reverted using ``adc1_get_raw()`` function.
  289. *
  290. * Note that adc1_config_channel_atten, ``adc1_config_width()`` functions need
  291. * to be called to configure ADC1 channels, before ADC1 is used by the ULP.
  292. */
  293. void adc1_ulp_enable(void);
  294. #endif //#if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
  295. /**
  296. * @brief Get the GPIO number of a specific ADC2 channel.
  297. *
  298. * @param channel Channel to get the GPIO number
  299. *
  300. * @param gpio_num output buffer to hold the GPIO number
  301. *
  302. * @return
  303. * - ESP_OK if success
  304. * - ESP_ERR_INVALID_ARG if channel not valid
  305. */
  306. esp_err_t adc2_pad_get_io_num(adc2_channel_t channel, gpio_num_t *gpio_num);
  307. /**
  308. * @brief Configure the ADC2 channel, including setting attenuation.
  309. *
  310. * The default ADC voltage is for attenuation 0 dB and listed in the table below.
  311. * By setting higher attenuation it is possible to read higher voltages.
  312. *
  313. * Due to ADC characteristics, most accurate results are obtained within the "suggested range"
  314. * shown in the following table.
  315. *
  316. * +----------+-------------+-----------------+
  317. * | | attenuation | suggested range |
  318. * | SoC | (dB) | (mV) |
  319. * +==========+=============+=================+
  320. * | | 0 | 100 ~ 950 |
  321. * | +-------------+-----------------+
  322. * | | 2.5 | 100 ~ 1250 |
  323. * | ESP32 +-------------+-----------------+
  324. * | | 6 | 150 ~ 1750 |
  325. * | +-------------+-----------------+
  326. * | | 11 | 150 ~ 2450 |
  327. * +----------+-------------+-----------------+
  328. * | | 0 | 0 ~ 750 |
  329. * | +-------------+-----------------+
  330. * | | 2.5 | 0 ~ 1050 |
  331. * | ESP32-S2 +-------------+-----------------+
  332. * | | 6 | 0 ~ 1300 |
  333. * | +-------------+-----------------+
  334. * | | 11 | 0 ~ 2500 |
  335. * +----------+-------------+-----------------+
  336. *
  337. * For maximum accuracy, use the ADC calibration APIs and measure voltages within these recommended ranges.
  338. *
  339. * @note This function also configures the input GPIO pin mux to
  340. * connect it to the ADC2 channel. It must be called before calling
  341. * ``adc2_get_raw()`` for this channel.
  342. *
  343. * @note For any given channel, this function must be called before the first time ``adc2_get_raw()`` is called for that channel.
  344. *
  345. * @param channel ADC2 channel to configure
  346. * @param atten Attenuation level
  347. *
  348. * @return
  349. * - ESP_OK success
  350. * - ESP_ERR_INVALID_ARG Parameter error
  351. */
  352. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten);
  353. /**
  354. * @brief Take an ADC2 reading on a single channel
  355. *
  356. * @note ESP32:
  357. * When the power switch of SARADC1, SARADC2, HALL sensor and AMP sensor is turned on,
  358. * the input of GPIO36 and GPIO39 will be pulled down for about 80ns.
  359. * When enabling power for any of these peripherals, ignore input from GPIO36 and GPIO39.
  360. * Please refer to section 3.11 of 'ECO_and_Workarounds_for_Bugs_in_ESP32' for the description of this issue.
  361. * As a workaround, call adc_power_acquire() in the app. This will result in higher power consumption (by ~1mA),
  362. * but will remove the glitches on GPIO36 and GPIO39.
  363. *
  364. *
  365. * @note ESP32:
  366. * For a given channel, ``adc2_config_channel_atten()``
  367. * must be called before the first time this function is called. If Wi-Fi is started via ``esp_wifi_start()``, this
  368. * function will always fail with ``ESP_ERR_TIMEOUT``.
  369. *
  370. * @note ESP32-S2:
  371. * ADC2 support hardware arbiter. The arbiter is to improve the use efficiency of ADC2. After the control right is robbed by the high priority,
  372. * the low priority controller will read the invalid ADC2 data. Default priority: Wi-Fi > RTC > Digital;
  373. *
  374. * @param channel ADC2 channel to read
  375. * @param width_bit Bit capture width for ADC2
  376. * @param raw_out the variable to hold the output data.
  377. *
  378. * @return
  379. * - ESP_OK if success
  380. * - ESP_ERR_TIMEOUT ADC2 is being used by other controller and the request timed out.
  381. * - ESP_ERR_INVALID_STATE The controller status is invalid. Please try again.
  382. */
  383. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out);
  384. /**
  385. * @brief Output ADC1 or ADC2's reference voltage to ``adc2_channe_t``'s IO.
  386. *
  387. * This function routes the internal reference voltage of ADCn to one of
  388. * ADC2's channels. This reference voltage can then be manually measured
  389. * for calibration purposes.
  390. *
  391. * @note ESP32 only supports output of ADC2's internal reference voltage.
  392. * @param[in] adc_unit ADC unit index
  393. * @param[in] gpio GPIO number (Only ADC2's channels IO are supported)
  394. *
  395. * @return
  396. * - ESP_OK: v_ref successfully routed to selected GPIO
  397. * - ESP_ERR_INVALID_ARG: Unsupported GPIO
  398. */
  399. esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio);
  400. /**
  401. * @brief Output ADC2 reference voltage to ``adc2_channe_t``'s IO.
  402. *
  403. * This function routes the internal reference voltage of ADCn to one of
  404. * ADC2's channels. This reference voltage can then be manually measured
  405. * for calibration purposes.
  406. *
  407. * @deprecated Use ``adc_vref_to_gpio`` instead.
  408. *
  409. * @param[in] gpio GPIO number (ADC2's channels are supported)
  410. *
  411. * @return
  412. * - ESP_OK: v_ref successfully routed to selected GPIO
  413. * - ESP_ERR_INVALID_ARG: Unsupported GPIO
  414. */
  415. esp_err_t adc2_vref_to_gpio(gpio_num_t gpio) __attribute__((deprecated));
  416. /*---------------------------------------------------------------
  417. Digital controller setting
  418. ---------------------------------------------------------------*/
  419. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  420. //These APIs are only supported on ESP32 and ESP32-S2. On ESP32-C3 and later chips, please use ``adc_digi_initialize`` and ``adc_digi_deinitialize``
  421. /**
  422. * @brief ADC digital controller initialization.
  423. * @return
  424. * - ESP_OK Success
  425. */
  426. esp_err_t adc_digi_init(void);
  427. /**
  428. * @brief ADC digital controller deinitialization.
  429. * @return
  430. * - ESP_OK Success
  431. */
  432. esp_err_t adc_digi_deinit(void);
  433. #endif
  434. /**
  435. * @brief Setting the digital controller.
  436. *
  437. * @param config Pointer to digital controller paramter. Refer to ``adc_digi_config_t``.
  438. *
  439. * @return
  440. * - ESP_ERR_INVALID_STATE Driver state is invalid.
  441. * - ESP_ERR_INVALID_ARG If the combination of arguments is invalid.
  442. * - ESP_OK On success
  443. */
  444. esp_err_t adc_digi_controller_config(const adc_digi_config_t *config);
  445. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  446. //This feature is currently supported on ESP32C3, will be supported on other chips soon
  447. /*---------------------------------------------------------------
  448. DMA setting
  449. ---------------------------------------------------------------*/
  450. /**
  451. * @brief Initialize the Digital ADC.
  452. *
  453. * @param init_config Pointer to Digital ADC initilization config. Refer to ``adc_digi_init_config_t``.
  454. *
  455. * @return
  456. * - ESP_ERR_INVALID_ARG If the combination of arguments is invalid.
  457. * - ESP_ERR_NOT_FOUND No free interrupt found with the specified flags
  458. * - ESP_ERR_NO_MEM If out of memory
  459. * - ESP_OK On success
  460. */
  461. esp_err_t adc_digi_initialize(const adc_digi_init_config_t *init_config);
  462. /**
  463. * @brief Start the Digital ADC and DMA peripherals. After this, the hardware starts working.
  464. *
  465. * @return
  466. * - ESP_ERR_INVALID_STATE Driver state is invalid.
  467. * - ESP_OK On success
  468. */
  469. esp_err_t adc_digi_start(void);
  470. /**
  471. * @brief Stop the Digital ADC and DMA peripherals. After this, the hardware stops working.
  472. *
  473. * @return
  474. * - ESP_ERR_INVALID_STATE Driver state is invalid.
  475. * - ESP_OK On success
  476. */
  477. esp_err_t adc_digi_stop(void);
  478. /**
  479. * @brief Read bytes from Digital ADC through DMA.
  480. *
  481. * @param[out] buf Buffer to read from ADC.
  482. * @param[in] length_max Expected length of data read from the ADC.
  483. * @param[out] out_length Real length of data read from the ADC via this API.
  484. * @param[in] timeout_ms Time to wait for data via this API, in millisecond.
  485. *
  486. * @return
  487. * - ESP_ERR_INVALID_STATE Driver state is invalid. Usually it means the ADC sampling rate is faster than the task processing rate.
  488. * - ESP_ERR_TIMEOUT Operation timed out
  489. * - ESP_OK On success
  490. */
  491. esp_err_t adc_digi_read_bytes(uint8_t *buf, uint32_t length_max, uint32_t *out_length, uint32_t timeout_ms);
  492. /**
  493. * @brief Deinitialize the Digital ADC.
  494. *
  495. * @return
  496. * - ESP_ERR_INVALID_STATE Driver state is invalid.
  497. * - ESP_OK On success
  498. */
  499. esp_err_t adc_digi_deinitialize(void);
  500. #endif //#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  501. #ifdef __cplusplus
  502. }
  503. #endif