adc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. /*
  2. * SPDX-FileCopyrightText: 2016-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 <string.h>
  10. #include "sdkconfig.h"
  11. #include "esp_intr_alloc.h"
  12. #include "esp_log.h"
  13. #include "esp_pm.h"
  14. #include "esp_check.h"
  15. #include "sys/lock.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/semphr.h"
  18. #include "freertos/timers.h"
  19. #include "freertos/ringbuf.h"
  20. #include "esp_private/periph_ctrl.h"
  21. #include "driver/gpio.h"
  22. #include "driver/adc.h"
  23. #include "hal/adc_types.h"
  24. #include "hal/adc_hal.h"
  25. #include "hal/dma_types.h"
  26. //For calibration
  27. #if CONFIG_IDF_TARGET_ESP32S2
  28. #include "esp_efuse_rtc_table.h"
  29. #elif SOC_ADC_CALIBRATION_V1_SUPPORTED
  30. #include "esp_efuse_rtc_calib.h"
  31. #endif
  32. //For DMA
  33. #if SOC_GDMA_SUPPORTED
  34. #include "esp_private/gdma.h"
  35. #elif CONFIG_IDF_TARGET_ESP32S2
  36. #include "hal/spi_types.h"
  37. #include "driver/spi_common_internal.h"
  38. #elif CONFIG_IDF_TARGET_ESP32
  39. #include "driver/i2s.h"
  40. #include "hal/i2s_types.h"
  41. #include "soc/i2s_periph.h"
  42. #include "esp_private/i2s_platform.h"
  43. #endif
  44. static const char *ADC_TAG = "ADC";
  45. #define ADC_GET_IO_NUM(periph, channel) (adc_channel_io_map[periph][channel])
  46. extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate position after the rtc module is finished.
  47. #define ADC_ENTER_CRITICAL() portENTER_CRITICAL(&rtc_spinlock)
  48. #define ADC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
  49. /**
  50. * 1. sar_adc1_lock: this mutex lock is to protect the SARADC1 module.
  51. * 2. sar_adc2_lock: this mutex lock is to protect the SARADC2 module.
  52. * 3. adc_reg_lock: this spin lock is to protect the shared registers used by ADC1 / ADC2 single read mode.
  53. */
  54. static _lock_t sar_adc1_lock;
  55. #define SAR_ADC1_LOCK_ACQUIRE() _lock_acquire(&sar_adc1_lock)
  56. #define SAR_ADC1_LOCK_RELEASE() _lock_release(&sar_adc1_lock)
  57. static _lock_t sar_adc2_lock;
  58. #define SAR_ADC2_LOCK_ACQUIRE() _lock_acquire(&sar_adc2_lock)
  59. #define SAR_ADC2_LOCK_RELEASE() _lock_release(&sar_adc2_lock)
  60. portMUX_TYPE adc_reg_lock = portMUX_INITIALIZER_UNLOCKED;
  61. #define ADC_REG_LOCK_ENTER() portENTER_CRITICAL(&adc_reg_lock)
  62. #define ADC_REG_LOCK_EXIT() portEXIT_CRITICAL(&adc_reg_lock)
  63. #define INTERNAL_BUF_NUM 5
  64. /*---------------------------------------------------------------
  65. Digital Controller Context
  66. ---------------------------------------------------------------*/
  67. typedef struct adc_digi_context_t {
  68. uint8_t *rx_dma_buf; //dma buffer
  69. adc_hal_dma_ctx_t hal; //hal context
  70. #if SOC_GDMA_SUPPORTED
  71. gdma_channel_handle_t rx_dma_channel; //dma rx channel handle
  72. #elif CONFIG_IDF_TARGET_ESP32S2
  73. spi_host_device_t spi_host; //ADC uses this SPI DMA
  74. intr_handle_t intr_hdl; //Interrupt handler
  75. #elif CONFIG_IDF_TARGET_ESP32
  76. i2s_port_t i2s_host; //ADC uses this I2S DMA
  77. intr_handle_t intr_hdl; //Interrupt handler
  78. #endif
  79. RingbufHandle_t ringbuf_hdl; //RX ringbuffer handler
  80. intptr_t rx_eof_desc_addr; //eof descriptor address of RX channel
  81. bool ringbuf_overflow_flag; //1: ringbuffer overflow
  82. bool driver_start_flag; //1: driver is started; 0: driver is stoped
  83. bool use_adc1; //1: ADC unit1 will be used; 0: ADC unit1 won't be used.
  84. bool use_adc2; //1: ADC unit2 will be used; 0: ADC unit2 won't be used. This determines whether to acquire sar_adc2_mutex lock or not.
  85. adc_atten_t adc1_atten; //Attenuation for ADC1. On this chip each ADC can only support one attenuation.
  86. adc_atten_t adc2_atten; //Attenuation for ADC2. On this chip each ADC can only support one attenuation.
  87. adc_hal_digi_ctrlr_cfg_t hal_digi_ctrlr_cfg; //Hal digital controller configuration
  88. esp_pm_lock_handle_t pm_lock; //For power management
  89. } adc_digi_context_t;
  90. static adc_digi_context_t *s_adc_digi_ctx = NULL;
  91. #ifdef CONFIG_PM_ENABLE
  92. //Only for deprecated API
  93. extern esp_pm_lock_handle_t adc_digi_arbiter_lock;
  94. #endif //CONFIG_PM_ENABLE
  95. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  96. uint32_t adc_get_calibration_offset(adc_unit_t adc_n, adc_atten_t atten);
  97. #endif
  98. /*---------------------------------------------------------------
  99. ADC Continuous Read Mode (via DMA)
  100. ---------------------------------------------------------------*/
  101. //Function to address transaction
  102. static bool s_adc_dma_intr(adc_digi_context_t *adc_digi_ctx);
  103. #if SOC_GDMA_SUPPORTED
  104. static bool adc_dma_in_suc_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data);
  105. #else
  106. static void adc_dma_intr_handler(void *arg);
  107. #endif
  108. static int8_t adc_digi_get_io_num(adc_unit_t adc_unit, uint8_t adc_channel)
  109. {
  110. assert(adc_unit <= SOC_ADC_PERIPH_NUM);
  111. uint8_t adc_n = (adc_unit == ADC_UNIT_1) ? 0 : 1;
  112. return adc_channel_io_map[adc_n][adc_channel];
  113. }
  114. static esp_err_t adc_digi_gpio_init(adc_unit_t adc_unit, uint16_t channel_mask)
  115. {
  116. esp_err_t ret = ESP_OK;
  117. uint64_t gpio_mask = 0;
  118. uint32_t n = 0;
  119. int8_t io = 0;
  120. while (channel_mask) {
  121. if (channel_mask & 0x1) {
  122. io = adc_digi_get_io_num(adc_unit, n);
  123. if (io < 0) {
  124. return ESP_ERR_INVALID_ARG;
  125. }
  126. gpio_mask |= BIT64(io);
  127. }
  128. channel_mask = channel_mask >> 1;
  129. n++;
  130. }
  131. gpio_config_t cfg = {
  132. .pin_bit_mask = gpio_mask,
  133. .mode = GPIO_MODE_DISABLE,
  134. };
  135. ret = gpio_config(&cfg);
  136. return ret;
  137. }
  138. esp_err_t adc_digi_initialize(const adc_digi_init_config_t *init_config)
  139. {
  140. esp_err_t ret = ESP_OK;
  141. s_adc_digi_ctx = calloc(1, sizeof(adc_digi_context_t));
  142. if (s_adc_digi_ctx == NULL) {
  143. ret = ESP_ERR_NO_MEM;
  144. goto cleanup;
  145. }
  146. //ringbuffer
  147. s_adc_digi_ctx->ringbuf_hdl = xRingbufferCreate(init_config->max_store_buf_size, RINGBUF_TYPE_BYTEBUF);
  148. if (!s_adc_digi_ctx->ringbuf_hdl) {
  149. ret = ESP_ERR_NO_MEM;
  150. goto cleanup;
  151. }
  152. //malloc internal buffer used by DMA
  153. s_adc_digi_ctx->rx_dma_buf = heap_caps_calloc(1, init_config->conv_num_each_intr * INTERNAL_BUF_NUM, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
  154. if (!s_adc_digi_ctx->rx_dma_buf) {
  155. ret = ESP_ERR_NO_MEM;
  156. goto cleanup;
  157. }
  158. //malloc dma descriptor
  159. s_adc_digi_ctx->hal.rx_desc = heap_caps_calloc(1, (sizeof(dma_descriptor_t)) * INTERNAL_BUF_NUM, MALLOC_CAP_DMA);
  160. if (!s_adc_digi_ctx->hal.rx_desc) {
  161. ret = ESP_ERR_NO_MEM;
  162. goto cleanup;
  163. }
  164. //malloc pattern table
  165. s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern = calloc(1, SOC_ADC_PATT_LEN_MAX * sizeof(adc_digi_pattern_config_t));
  166. if (!s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern) {
  167. ret = ESP_ERR_NO_MEM;
  168. goto cleanup;
  169. }
  170. #if CONFIG_PM_ENABLE
  171. ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc_dma", &s_adc_digi_ctx->pm_lock);
  172. if (ret != ESP_OK) {
  173. goto cleanup;
  174. }
  175. #endif //CONFIG_PM_ENABLE
  176. //init gpio pins
  177. if (init_config->adc1_chan_mask) {
  178. ret = adc_digi_gpio_init(ADC_UNIT_1, init_config->adc1_chan_mask);
  179. if (ret != ESP_OK) {
  180. goto cleanup;
  181. }
  182. }
  183. if (init_config->adc2_chan_mask) {
  184. ret = adc_digi_gpio_init(ADC_UNIT_2, init_config->adc2_chan_mask);
  185. if (ret != ESP_OK) {
  186. goto cleanup;
  187. }
  188. }
  189. #if SOC_GDMA_SUPPORTED
  190. //alloc rx gdma channel
  191. gdma_channel_alloc_config_t rx_alloc_config = {
  192. .direction = GDMA_CHANNEL_DIRECTION_RX,
  193. };
  194. ret = gdma_new_channel(&rx_alloc_config, &s_adc_digi_ctx->rx_dma_channel);
  195. if (ret != ESP_OK) {
  196. goto cleanup;
  197. }
  198. gdma_connect(s_adc_digi_ctx->rx_dma_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_ADC, 0));
  199. gdma_strategy_config_t strategy_config = {
  200. .auto_update_desc = true,
  201. .owner_check = true
  202. };
  203. gdma_apply_strategy(s_adc_digi_ctx->rx_dma_channel, &strategy_config);
  204. gdma_rx_event_callbacks_t cbs = {
  205. .on_recv_eof = adc_dma_in_suc_eof_callback
  206. };
  207. gdma_register_rx_event_callbacks(s_adc_digi_ctx->rx_dma_channel, &cbs, s_adc_digi_ctx);
  208. int dma_chan;
  209. gdma_get_channel_id(s_adc_digi_ctx->rx_dma_channel, &dma_chan);
  210. #elif CONFIG_IDF_TARGET_ESP32S2
  211. //ADC utilises SPI3 DMA on ESP32S2
  212. bool spi_success = false;
  213. uint32_t dma_chan = 0;
  214. spi_success = spicommon_periph_claim(SPI3_HOST, "adc");
  215. ret = spicommon_dma_chan_alloc(SPI3_HOST, SPI_DMA_CH_AUTO, &dma_chan, &dma_chan);
  216. if (ret == ESP_OK) {
  217. s_adc_digi_ctx->spi_host = SPI3_HOST;
  218. }
  219. if (!spi_success || (s_adc_digi_ctx->spi_host != SPI3_HOST)) {
  220. goto cleanup;
  221. }
  222. ret = esp_intr_alloc(spicommon_irqdma_source_for_host(s_adc_digi_ctx->spi_host), 0, adc_dma_intr_handler,
  223. (void *)s_adc_digi_ctx, &s_adc_digi_ctx->intr_hdl);
  224. if (ret != ESP_OK) {
  225. goto cleanup;
  226. }
  227. #elif CONFIG_IDF_TARGET_ESP32
  228. //ADC utilises I2S0 DMA on ESP32
  229. uint32_t dma_chan = 0;
  230. ret = i2s_priv_register_object(&s_adc_digi_ctx, I2S_NUM_0);
  231. if (ret != ESP_OK) {
  232. goto cleanup;
  233. }
  234. s_adc_digi_ctx->i2s_host = I2S_NUM_0;
  235. ret = esp_intr_alloc(i2s_periph_signal[s_adc_digi_ctx->i2s_host].irq, 0, adc_dma_intr_handler,
  236. (void *)s_adc_digi_ctx, &s_adc_digi_ctx->intr_hdl);
  237. if (ret != ESP_OK) {
  238. goto cleanup;
  239. }
  240. #endif
  241. adc_hal_dma_config_t config = {
  242. #if SOC_GDMA_SUPPORTED
  243. .dev = (void *)GDMA_LL_GET_HW(0),
  244. #elif CONFIG_IDF_TARGET_ESP32S2
  245. .dev = (void *)SPI_LL_GET_HW(s_adc_digi_ctx->spi_host),
  246. #elif CONFIG_IDF_TARGET_ESP32
  247. .dev = (void *)I2S_LL_GET_HW(s_adc_digi_ctx->i2s_host),
  248. #endif
  249. .desc_max_num = INTERNAL_BUF_NUM,
  250. .dma_chan = dma_chan,
  251. .eof_num = init_config->conv_num_each_intr / ADC_HAL_DATA_LEN_PER_CONV
  252. };
  253. adc_hal_dma_ctx_config(&s_adc_digi_ctx->hal, &config);
  254. //enable ADC digital part
  255. periph_module_enable(PERIPH_SARADC_MODULE);
  256. //reset ADC digital part
  257. periph_module_reset(PERIPH_SARADC_MODULE);
  258. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  259. adc_hal_calibration_init(ADC_UNIT_1);
  260. adc_hal_calibration_init(ADC_UNIT_2);
  261. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  262. return ret;
  263. cleanup:
  264. adc_digi_deinitialize();
  265. return ret;
  266. }
  267. #if SOC_GDMA_SUPPORTED
  268. static IRAM_ATTR bool adc_dma_in_suc_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data)
  269. {
  270. assert(event_data);
  271. s_adc_digi_ctx->rx_eof_desc_addr = event_data->rx_eof_desc_addr;
  272. return s_adc_dma_intr(user_data);
  273. }
  274. #else
  275. static IRAM_ATTR void adc_dma_intr_handler(void *arg)
  276. {
  277. adc_digi_context_t *ctx = (adc_digi_context_t *)arg;
  278. bool need_yield = false;
  279. bool conversion_finish = adc_hal_check_event(&ctx->hal, ADC_HAL_DMA_INTR_MASK);
  280. if (conversion_finish) {
  281. adc_hal_digi_clr_intr(&s_adc_digi_ctx->hal, ADC_HAL_DMA_INTR_MASK);
  282. intptr_t desc_addr = adc_hal_get_desc_addr(&ctx->hal);
  283. ctx->rx_eof_desc_addr = desc_addr;
  284. need_yield = s_adc_dma_intr(ctx);
  285. }
  286. if (need_yield) {
  287. portYIELD_FROM_ISR();
  288. }
  289. }
  290. #endif
  291. static IRAM_ATTR bool s_adc_dma_intr(adc_digi_context_t *adc_digi_ctx)
  292. {
  293. portBASE_TYPE taskAwoken = 0;
  294. BaseType_t ret;
  295. adc_hal_dma_desc_status_t status = false;
  296. dma_descriptor_t *current_desc = NULL;
  297. while (1) {
  298. status = adc_hal_get_reading_result(&adc_digi_ctx->hal, adc_digi_ctx->rx_eof_desc_addr, &current_desc);
  299. if (status != ADC_HAL_DMA_DESC_VALID) {
  300. break;
  301. }
  302. ret = xRingbufferSendFromISR(adc_digi_ctx->ringbuf_hdl, current_desc->buffer, current_desc->dw0.length, &taskAwoken);
  303. if (ret == pdFALSE) {
  304. //ringbuffer overflow
  305. adc_digi_ctx->ringbuf_overflow_flag = 1;
  306. }
  307. }
  308. if (status == ADC_HAL_DMA_DESC_NULL) {
  309. //start next turns of dma operation
  310. adc_hal_digi_start(&adc_digi_ctx->hal, adc_digi_ctx->rx_dma_buf);
  311. }
  312. return (taskAwoken == pdTRUE);
  313. }
  314. esp_err_t adc_digi_start(void)
  315. {
  316. if (s_adc_digi_ctx) {
  317. if (s_adc_digi_ctx->driver_start_flag != 0) {
  318. ESP_LOGE(ADC_TAG, "The driver is already started");
  319. return ESP_ERR_INVALID_STATE;
  320. }
  321. adc_power_acquire();
  322. //reset flags
  323. s_adc_digi_ctx->ringbuf_overflow_flag = 0;
  324. s_adc_digi_ctx->driver_start_flag = 1;
  325. if (s_adc_digi_ctx->use_adc1) {
  326. SAR_ADC1_LOCK_ACQUIRE();
  327. }
  328. if (s_adc_digi_ctx->use_adc2) {
  329. SAR_ADC2_LOCK_ACQUIRE();
  330. }
  331. #if CONFIG_PM_ENABLE
  332. // Lock APB frequency while ADC driver is in use
  333. esp_pm_lock_acquire(s_adc_digi_ctx->pm_lock);
  334. #endif
  335. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  336. if (s_adc_digi_ctx->use_adc1) {
  337. uint32_t cal_val = adc_get_calibration_offset(ADC_UNIT_1, s_adc_digi_ctx->adc1_atten);
  338. adc_hal_set_calibration_param(ADC_UNIT_1, cal_val);
  339. }
  340. if (s_adc_digi_ctx->use_adc2) {
  341. uint32_t cal_val = adc_get_calibration_offset(ADC_UNIT_2, s_adc_digi_ctx->adc2_atten);
  342. adc_hal_set_calibration_param(ADC_UNIT_2, cal_val);
  343. }
  344. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  345. #if SOC_ADC_ARBITER_SUPPORTED
  346. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  347. adc_hal_arbiter_config(&config);
  348. #endif //#if SOC_ADC_ARBITER_SUPPORTED
  349. adc_hal_set_controller(ADC_UNIT_1, ADC_HAL_CONTINUOUS_READ_MODE);
  350. adc_hal_set_controller(ADC_UNIT_2, ADC_HAL_CONTINUOUS_READ_MODE);
  351. adc_hal_digi_init(&s_adc_digi_ctx->hal);
  352. adc_hal_digi_controller_config(&s_adc_digi_ctx->hal, &s_adc_digi_ctx->hal_digi_ctrlr_cfg);
  353. //start conversion
  354. adc_hal_digi_start(&s_adc_digi_ctx->hal, s_adc_digi_ctx->rx_dma_buf);
  355. }
  356. #if CONFIG_IDF_TARGET_ESP32S2
  357. //For being compatible with the deprecated behaviour
  358. else {
  359. ESP_LOGE(ADC_TAG, "API used without driver initialization before. The following behaviour is deprecated!!");
  360. #ifdef CONFIG_PM_ENABLE
  361. ESP_RETURN_ON_FALSE((adc_digi_arbiter_lock), ESP_FAIL, ADC_TAG, "Should start after call `adc_digi_controller_config`");
  362. esp_pm_lock_acquire(adc_digi_arbiter_lock);
  363. #endif
  364. ADC_ENTER_CRITICAL();
  365. adc_ll_digi_dma_enable();
  366. adc_ll_digi_trigger_enable();
  367. ADC_EXIT_CRITICAL();
  368. }
  369. #endif //#if CONFIG_IDF_TARGET_ESP32S2
  370. return ESP_OK;
  371. }
  372. esp_err_t adc_digi_stop(void)
  373. {
  374. if (s_adc_digi_ctx) {
  375. if (s_adc_digi_ctx->driver_start_flag != 1) {
  376. ESP_LOGE(ADC_TAG, "The driver is already stopped");
  377. return ESP_ERR_INVALID_STATE;
  378. }
  379. s_adc_digi_ctx->driver_start_flag = 0;
  380. //disable the in suc eof intrrupt
  381. adc_hal_digi_dis_intr(&s_adc_digi_ctx->hal, ADC_HAL_DMA_INTR_MASK);
  382. //clear the in suc eof interrupt
  383. adc_hal_digi_clr_intr(&s_adc_digi_ctx->hal, ADC_HAL_DMA_INTR_MASK);
  384. //stop ADC
  385. adc_hal_digi_stop(&s_adc_digi_ctx->hal);
  386. adc_hal_digi_deinit(&s_adc_digi_ctx->hal);
  387. #if CONFIG_PM_ENABLE
  388. if (s_adc_digi_ctx->pm_lock) {
  389. esp_pm_lock_release(s_adc_digi_ctx->pm_lock);
  390. }
  391. #endif //CONFIG_PM_ENABLE
  392. if (s_adc_digi_ctx->use_adc1) {
  393. SAR_ADC1_LOCK_RELEASE();
  394. }
  395. if (s_adc_digi_ctx->use_adc2) {
  396. SAR_ADC2_LOCK_RELEASE();
  397. }
  398. adc_power_release();
  399. }
  400. #if CONFIG_IDF_TARGET_ESP32S2
  401. else {
  402. //For being compatible with the deprecated behaviour
  403. ESP_LOGE(ADC_TAG, "API used without driver initialization before. The following behaviour is deprecated!!");
  404. #ifdef CONFIG_PM_ENABLE
  405. if (adc_digi_arbiter_lock) {
  406. esp_pm_lock_release(adc_digi_arbiter_lock);
  407. }
  408. #endif
  409. ADC_ENTER_CRITICAL();
  410. adc_ll_digi_trigger_disable();
  411. adc_ll_digi_dma_disable();
  412. ADC_EXIT_CRITICAL();
  413. }
  414. #endif //#if CONFIG_IDF_TARGET_ESP32S2
  415. return ESP_OK;
  416. }
  417. esp_err_t adc_digi_read_bytes(uint8_t *buf, uint32_t length_max, uint32_t *out_length, uint32_t timeout_ms)
  418. {
  419. TickType_t ticks_to_wait;
  420. esp_err_t ret = ESP_OK;
  421. uint8_t *data = NULL;
  422. size_t size = 0;
  423. ticks_to_wait = timeout_ms / portTICK_PERIOD_MS;
  424. if (timeout_ms == ADC_MAX_DELAY) {
  425. ticks_to_wait = portMAX_DELAY;
  426. }
  427. data = xRingbufferReceiveUpTo(s_adc_digi_ctx->ringbuf_hdl, &size, ticks_to_wait, length_max);
  428. if (!data) {
  429. ESP_LOGV(ADC_TAG, "No data, increase timeout or reduce conv_num_each_intr");
  430. ret = ESP_ERR_TIMEOUT;
  431. *out_length = 0;
  432. return ret;
  433. }
  434. memcpy(buf, data, size);
  435. vRingbufferReturnItem(s_adc_digi_ctx->ringbuf_hdl, data);
  436. assert((size % 4) == 0);
  437. *out_length = size;
  438. if (s_adc_digi_ctx->ringbuf_overflow_flag) {
  439. ret = ESP_ERR_INVALID_STATE;
  440. }
  441. return ret;
  442. }
  443. esp_err_t adc_digi_deinitialize(void)
  444. {
  445. if (!s_adc_digi_ctx) {
  446. return ESP_ERR_INVALID_STATE;
  447. }
  448. if (s_adc_digi_ctx->driver_start_flag != 0) {
  449. ESP_LOGE(ADC_TAG, "The driver is not stopped");
  450. return ESP_ERR_INVALID_STATE;
  451. }
  452. if (s_adc_digi_ctx->ringbuf_hdl) {
  453. vRingbufferDelete(s_adc_digi_ctx->ringbuf_hdl);
  454. s_adc_digi_ctx->ringbuf_hdl = NULL;
  455. }
  456. #if CONFIG_PM_ENABLE
  457. if (s_adc_digi_ctx->pm_lock) {
  458. esp_pm_lock_delete(s_adc_digi_ctx->pm_lock);
  459. }
  460. #endif //CONFIG_PM_ENABLE
  461. free(s_adc_digi_ctx->rx_dma_buf);
  462. free(s_adc_digi_ctx->hal.rx_desc);
  463. free(s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern);
  464. #if SOC_GDMA_SUPPORTED
  465. gdma_disconnect(s_adc_digi_ctx->rx_dma_channel);
  466. gdma_del_channel(s_adc_digi_ctx->rx_dma_channel);
  467. #elif CONFIG_IDF_TARGET_ESP32S2
  468. esp_intr_free(s_adc_digi_ctx->intr_hdl);
  469. spicommon_dma_chan_free(s_adc_digi_ctx->spi_host);
  470. spicommon_periph_free(s_adc_digi_ctx->spi_host);
  471. #elif CONFIG_IDF_TARGET_ESP32
  472. esp_intr_free(s_adc_digi_ctx->intr_hdl);
  473. i2s_priv_deregister_object(s_adc_digi_ctx->i2s_host);
  474. #endif
  475. free(s_adc_digi_ctx);
  476. s_adc_digi_ctx = NULL;
  477. periph_module_disable(PERIPH_SARADC_MODULE);
  478. return ESP_OK;
  479. }
  480. /*---------------------------------------------------------------
  481. Digital controller setting
  482. ---------------------------------------------------------------*/
  483. esp_err_t adc_digi_controller_configure(const adc_digi_configuration_t *config)
  484. {
  485. if (!s_adc_digi_ctx) {
  486. return ESP_ERR_INVALID_STATE;
  487. }
  488. //Pattern related check
  489. ESP_RETURN_ON_FALSE(config->pattern_num <= SOC_ADC_PATT_LEN_MAX, ESP_ERR_INVALID_ARG, ADC_TAG, "Max pattern num is %d", SOC_ADC_PATT_LEN_MAX);
  490. #if CONFIG_IDF_TARGET_ESP32
  491. for (int i = 0; i < config->pattern_num; i++) {
  492. ESP_RETURN_ON_FALSE((config->adc_pattern[i].bit_width >= SOC_ADC_DIGI_MIN_BITWIDTH && config->adc_pattern->bit_width <= SOC_ADC_DIGI_MAX_BITWIDTH), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC bitwidth not supported");
  493. ESP_RETURN_ON_FALSE(config->adc_pattern[i].unit == 0, ESP_ERR_INVALID_ARG, ADC_TAG, "Only support using ADC1 DMA mode");
  494. }
  495. #else
  496. for (int i = 0; i < config->pattern_num; i++) {
  497. ESP_RETURN_ON_FALSE((config->adc_pattern[i].bit_width == SOC_ADC_DIGI_MAX_BITWIDTH), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC bitwidth not supported");
  498. }
  499. #endif
  500. ESP_RETURN_ON_FALSE(config->sample_freq_hz <= SOC_ADC_SAMPLE_FREQ_THRES_HIGH && config->sample_freq_hz >= SOC_ADC_SAMPLE_FREQ_THRES_LOW, ESP_ERR_INVALID_ARG, ADC_TAG, "ADC sampling frequency out of range");
  501. #if CONFIG_IDF_TARGET_ESP32
  502. ESP_RETURN_ON_FALSE(config->conv_limit_en == 1, ESP_ERR_INVALID_ARG, ADC_TAG, "`conv_limit_en` should be set to 1");
  503. #endif
  504. #if CONFIG_IDF_TARGET_ESP32
  505. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE1, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type1");
  506. #elif CONFIG_IDF_TARGET_ESP32S2
  507. if (config->conv_mode == ADC_CONV_BOTH_UNIT || config->conv_mode == ADC_CONV_ALTER_UNIT) {
  508. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE2, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type2");
  509. } else if (config->conv_mode == ADC_CONV_SINGLE_UNIT_1 || config->conv_mode == ADC_CONV_SINGLE_UNIT_2) {
  510. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE1, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type1");
  511. }
  512. #else
  513. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE2, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type2");
  514. #endif
  515. s_adc_digi_ctx->hal_digi_ctrlr_cfg.conv_limit_en = config->conv_limit_en;
  516. s_adc_digi_ctx->hal_digi_ctrlr_cfg.conv_limit_num = config->conv_limit_num;
  517. s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern_len = config->pattern_num;
  518. s_adc_digi_ctx->hal_digi_ctrlr_cfg.sample_freq_hz = config->sample_freq_hz;
  519. s_adc_digi_ctx->hal_digi_ctrlr_cfg.conv_mode = config->conv_mode;
  520. memcpy(s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern, config->adc_pattern, config->pattern_num * sizeof(adc_digi_pattern_config_t));
  521. const int atten_uninitialized = 999;
  522. s_adc_digi_ctx->adc1_atten = atten_uninitialized;
  523. s_adc_digi_ctx->adc2_atten = atten_uninitialized;
  524. s_adc_digi_ctx->use_adc1 = 0;
  525. s_adc_digi_ctx->use_adc2 = 0;
  526. for (int i = 0; i < config->pattern_num; i++) {
  527. const adc_digi_pattern_config_t *pat = &config->adc_pattern[i];
  528. if (pat->unit == ADC_UNIT_1) {
  529. s_adc_digi_ctx->use_adc1 = 1;
  530. if (s_adc_digi_ctx->adc1_atten == atten_uninitialized) {
  531. s_adc_digi_ctx->adc1_atten = pat->atten;
  532. } else if (s_adc_digi_ctx->adc1_atten != pat->atten) {
  533. return ESP_ERR_INVALID_ARG;
  534. }
  535. } else if (pat->unit == ADC_UNIT_2) {
  536. //See whether ADC2 will be used or not. If yes, the ``sar_adc2_mutex`` should be acquired in the continuous read driver
  537. s_adc_digi_ctx->use_adc2 = 1;
  538. if (s_adc_digi_ctx->adc2_atten == atten_uninitialized) {
  539. s_adc_digi_ctx->adc2_atten = pat->atten;
  540. } else if (s_adc_digi_ctx->adc2_atten != pat->atten) {
  541. return ESP_ERR_INVALID_ARG;
  542. }
  543. }
  544. }
  545. return ESP_OK;
  546. }
  547. #if CONFIG_IDF_TARGET_ESP32C3
  548. /*---------------------------------------------------------------
  549. ADC Single Read Mode
  550. ---------------------------------------------------------------*/
  551. static adc_atten_t s_atten1_single[ADC1_CHANNEL_MAX]; //Array saving attenuate of each channel of ADC1, used by single read API
  552. static adc_atten_t s_atten2_single[ADC2_CHANNEL_MAX]; //Array saving attenuate of each channel of ADC2, used by single read API
  553. esp_err_t adc_vref_to_gpio(adc_unit_t adc_unit, gpio_num_t gpio)
  554. {
  555. esp_err_t ret;
  556. uint32_t channel = ADC2_CHANNEL_MAX;
  557. if (adc_unit == ADC_UNIT_2) {
  558. for (int i = 0; i < ADC2_CHANNEL_MAX; i++) {
  559. if (gpio == ADC_GET_IO_NUM(ADC_UNIT_2, i)) {
  560. channel = i;
  561. break;
  562. }
  563. }
  564. if (channel == ADC2_CHANNEL_MAX) {
  565. return ESP_ERR_INVALID_ARG;
  566. }
  567. }
  568. adc_power_acquire();
  569. if (adc_unit == ADC_UNIT_1) {
  570. ADC_ENTER_CRITICAL();
  571. adc_hal_vref_output(ADC_UNIT_1, channel, true);
  572. ADC_EXIT_CRITICAL();
  573. } else { //ADC_UNIT_2
  574. ADC_ENTER_CRITICAL();
  575. adc_hal_vref_output(ADC_UNIT_2, channel, true);
  576. ADC_EXIT_CRITICAL();
  577. }
  578. ret = adc_digi_gpio_init(ADC_UNIT_2, BIT(channel));
  579. return ret;
  580. }
  581. esp_err_t adc1_config_width(adc_bits_width_t width_bit)
  582. {
  583. //On ESP32C3, the data width is always 12-bits.
  584. if (width_bit != ADC_WIDTH_BIT_12) {
  585. return ESP_ERR_INVALID_ARG;
  586. }
  587. return ESP_OK;
  588. }
  589. esp_err_t adc1_config_channel_atten(adc1_channel_t channel, adc_atten_t atten)
  590. {
  591. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_1), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC1 channel error");
  592. ESP_RETURN_ON_FALSE((atten < SOC_ADC_ATTEN_NUM), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC Atten Err");
  593. esp_err_t ret = ESP_OK;
  594. s_atten1_single[channel] = atten;
  595. ret = adc_digi_gpio_init(ADC_UNIT_1, BIT(channel));
  596. adc_hal_calibration_init(ADC_UNIT_1);
  597. return ret;
  598. }
  599. int adc1_get_raw(adc1_channel_t channel)
  600. {
  601. int raw_out = 0;
  602. periph_module_enable(PERIPH_SARADC_MODULE);
  603. adc_power_acquire();
  604. SAR_ADC1_LOCK_ACQUIRE();
  605. adc_atten_t atten = s_atten1_single[channel];
  606. uint32_t cal_val = adc_get_calibration_offset(ADC_UNIT_1, atten);
  607. adc_hal_set_calibration_param(ADC_UNIT_1, cal_val);
  608. ADC_REG_LOCK_ENTER();
  609. adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
  610. adc_hal_convert(ADC_UNIT_1, channel, &raw_out);
  611. ADC_REG_LOCK_EXIT();
  612. SAR_ADC1_LOCK_RELEASE();
  613. adc_power_release();
  614. periph_module_disable(PERIPH_SARADC_MODULE);
  615. return raw_out;
  616. }
  617. esp_err_t adc2_config_channel_atten(adc2_channel_t channel, adc_atten_t atten)
  618. {
  619. ESP_RETURN_ON_FALSE(channel < SOC_ADC_CHANNEL_NUM(ADC_UNIT_2), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC2 channel error");
  620. ESP_RETURN_ON_FALSE((atten <= ADC_ATTEN_11db), ESP_ERR_INVALID_ARG, ADC_TAG, "ADC2 Atten Err");
  621. esp_err_t ret = ESP_OK;
  622. s_atten2_single[channel] = atten;
  623. ret = adc_digi_gpio_init(ADC_UNIT_2, BIT(channel));
  624. adc_hal_calibration_init(ADC_UNIT_2);
  625. return ret;
  626. }
  627. esp_err_t adc2_get_raw(adc2_channel_t channel, adc_bits_width_t width_bit, int *raw_out)
  628. {
  629. //On ESP32C3, the data width is always 12-bits.
  630. if (width_bit != ADC_WIDTH_BIT_12) {
  631. return ESP_ERR_INVALID_ARG;
  632. }
  633. esp_err_t ret = ESP_OK;
  634. periph_module_enable(PERIPH_SARADC_MODULE);
  635. adc_power_acquire();
  636. SAR_ADC2_LOCK_ACQUIRE();
  637. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  638. adc_hal_arbiter_config(&config);
  639. adc_atten_t atten = s_atten2_single[channel];
  640. uint32_t cal_val = adc_get_calibration_offset(ADC_UNIT_2, atten);
  641. adc_hal_set_calibration_param(ADC_UNIT_2, cal_val);
  642. ADC_REG_LOCK_ENTER();
  643. adc_oneshot_ll_set_atten(ADC_UNIT_2, channel, atten);
  644. ret = adc_hal_convert(ADC_UNIT_2, channel, raw_out);
  645. ADC_REG_LOCK_EXIT();
  646. SAR_ADC2_LOCK_RELEASE();
  647. adc_power_release();
  648. periph_module_disable(PERIPH_SARADC_MODULE);
  649. return ret;
  650. }
  651. /*************************************/
  652. /* Digital controller filter setting */
  653. /*************************************/
  654. esp_err_t adc_digi_filter_reset(adc_digi_filter_idx_t idx)
  655. {
  656. ADC_ENTER_CRITICAL();
  657. adc_hal_digi_filter_reset(idx);
  658. ADC_EXIT_CRITICAL();
  659. return ESP_OK;
  660. }
  661. esp_err_t adc_digi_filter_set_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  662. {
  663. ADC_ENTER_CRITICAL();
  664. adc_hal_digi_filter_set_factor(idx, config);
  665. ADC_EXIT_CRITICAL();
  666. return ESP_OK;
  667. }
  668. esp_err_t adc_digi_filter_get_config(adc_digi_filter_idx_t idx, adc_digi_filter_t *config)
  669. {
  670. ADC_ENTER_CRITICAL();
  671. adc_hal_digi_filter_get_factor(idx, config);
  672. ADC_EXIT_CRITICAL();
  673. return ESP_OK;
  674. }
  675. esp_err_t adc_digi_filter_enable(adc_digi_filter_idx_t idx, bool enable)
  676. {
  677. ADC_ENTER_CRITICAL();
  678. adc_hal_digi_filter_enable(idx, enable);
  679. ADC_EXIT_CRITICAL();
  680. return ESP_OK;
  681. }
  682. /**************************************/
  683. /* Digital controller monitor setting */
  684. /**************************************/
  685. esp_err_t adc_digi_monitor_set_config(adc_digi_monitor_idx_t idx, adc_digi_monitor_t *config)
  686. {
  687. ADC_ENTER_CRITICAL();
  688. adc_hal_digi_monitor_config(idx, config);
  689. ADC_EXIT_CRITICAL();
  690. return ESP_OK;
  691. }
  692. esp_err_t adc_digi_monitor_enable(adc_digi_monitor_idx_t idx, bool enable)
  693. {
  694. ADC_ENTER_CRITICAL();
  695. adc_hal_digi_monitor_enable(idx, enable);
  696. ADC_EXIT_CRITICAL();
  697. return ESP_OK;
  698. }
  699. #endif //#if CONFIG_IDF_TARGET_ESP32C3
  700. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  701. /*---------------------------------------------------------------
  702. Hardware Calibration Setting
  703. ---------------------------------------------------------------*/
  704. #if CONFIG_IDF_TARGET_ESP32S2
  705. #define esp_efuse_rtc_calib_get_ver() esp_efuse_rtc_table_read_calib_version()
  706. static inline uint32_t esp_efuse_rtc_calib_get_init_code(int version, uint32_t adc_unit, int atten)
  707. {
  708. int tag = esp_efuse_rtc_table_get_tag(version, adc_unit + 1, atten, RTCCALIB_V2_PARAM_VINIT);
  709. return esp_efuse_rtc_table_get_parsed_efuse_value(tag, false);
  710. }
  711. #endif
  712. static uint16_t s_adc_cali_param[SOC_ADC_PERIPH_NUM][SOC_ADC_ATTEN_NUM] = {};
  713. //NOTE: according to calibration version, different types of lock may be taken during the process:
  714. // 1. Semaphore when reading efuse
  715. // 2. Lock (Spinlock, or Mutex) if we actually do ADC calibration in the future
  716. //This function shoudn't be called inside critical section or ISR
  717. uint32_t adc_get_calibration_offset(adc_unit_t adc_n, adc_atten_t atten)
  718. {
  719. if (s_adc_cali_param[adc_n][atten]) {
  720. ESP_LOGV(ADC_TAG, "Use calibrated val ADC%d atten=%d: %04X", adc_n, atten, s_adc_cali_param[adc_n][atten]);
  721. return (uint32_t)s_adc_cali_param[adc_n][atten];
  722. }
  723. // check if we can fetch the values from eFuse.
  724. int version = esp_efuse_rtc_calib_get_ver();
  725. uint32_t init_code = 0;
  726. if (version == ESP_EFUSE_ADC_CALIB_VER) {
  727. init_code = esp_efuse_rtc_calib_get_init_code(version, adc_n, atten);
  728. } else {
  729. ESP_LOGD(ADC_TAG, "Calibration eFuse is not configured, use self-calibration for ICode");
  730. adc_power_acquire();
  731. ADC_ENTER_CRITICAL();
  732. const bool internal_gnd = true;
  733. init_code = adc_hal_self_calibration(adc_n, atten, internal_gnd);
  734. ADC_EXIT_CRITICAL();
  735. adc_power_release();
  736. }
  737. s_adc_cali_param[adc_n][atten] = init_code;
  738. ESP_LOGV(ADC_TAG, "Calib(V%d) ADC%d atten=%d: %04X", version, adc_n, atten, init_code);
  739. return init_code;
  740. }
  741. // Internal function to calibrate PWDET for WiFi
  742. esp_err_t adc_cal_offset(adc_unit_t adc_n, adc_atten_t atten)
  743. {
  744. adc_hal_calibration_init(adc_n);
  745. uint32_t cal_val = adc_get_calibration_offset(adc_n, atten);
  746. ADC_ENTER_CRITICAL();
  747. adc_hal_set_calibration_param(adc_n, cal_val);
  748. ADC_EXIT_CRITICAL();
  749. return ESP_OK;
  750. }
  751. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  752. /*---------------------------------------------------------------
  753. Deprecated API
  754. ---------------------------------------------------------------*/
  755. #if CONFIG_IDF_TARGET_ESP32C3
  756. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  757. #include "deprecated/driver/adc_deprecated.h"
  758. #include "deprecated/driver/adc_types_deprecated.h"
  759. esp_err_t adc_digi_controller_config(const adc_digi_config_t *config)
  760. {
  761. if (!s_adc_digi_ctx) {
  762. return ESP_ERR_INVALID_STATE;
  763. }
  764. ESP_RETURN_ON_FALSE((config->sample_freq_hz <= SOC_ADC_SAMPLE_FREQ_THRES_HIGH && config->sample_freq_hz >= SOC_ADC_SAMPLE_FREQ_THRES_LOW), ESP_ERR_INVALID_ARG, ADC_TAG, "DC sampling frequency out of range");
  765. s_adc_digi_ctx->hal_digi_ctrlr_cfg.conv_limit_en = config->conv_limit_en;
  766. s_adc_digi_ctx->hal_digi_ctrlr_cfg.conv_limit_num = config->conv_limit_num;
  767. s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern_len = config->adc_pattern_len;
  768. s_adc_digi_ctx->hal_digi_ctrlr_cfg.sample_freq_hz = config->sample_freq_hz;
  769. for (int i = 0; i < config->adc_pattern_len; i++) {
  770. s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern[i].atten = config->adc_pattern[i].atten;
  771. s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern[i].channel = config->adc_pattern[i].channel;
  772. s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern[i].unit = config->adc_pattern[i].unit;
  773. }
  774. const int atten_uninitialized = 999;
  775. s_adc_digi_ctx->adc1_atten = atten_uninitialized;
  776. s_adc_digi_ctx->adc2_atten = atten_uninitialized;
  777. s_adc_digi_ctx->use_adc1 = 0;
  778. s_adc_digi_ctx->use_adc2 = 0;
  779. for (int i = 0; i < config->adc_pattern_len; i++) {
  780. const adc_digi_pattern_config_t *pat = &s_adc_digi_ctx->hal_digi_ctrlr_cfg.adc_pattern[i];
  781. if (pat->unit == ADC_UNIT_1) {
  782. s_adc_digi_ctx->use_adc1 = 1;
  783. if (s_adc_digi_ctx->adc1_atten == atten_uninitialized) {
  784. s_adc_digi_ctx->adc1_atten = pat->atten;
  785. } else if (s_adc_digi_ctx->adc1_atten != pat->atten) {
  786. return ESP_ERR_INVALID_ARG;
  787. }
  788. } else if (pat->unit == ADC_UNIT_2) {
  789. //See whether ADC2 will be used or not. If yes, the ``sar_adc2_mutex`` should be acquired in the continuous read driver
  790. s_adc_digi_ctx->use_adc2 = 1;
  791. if (s_adc_digi_ctx->adc2_atten == atten_uninitialized) {
  792. s_adc_digi_ctx->adc2_atten = pat->atten;
  793. } else if (s_adc_digi_ctx->adc2_atten != pat->atten) {
  794. return ESP_ERR_INVALID_ARG;
  795. }
  796. }
  797. }
  798. return ESP_OK;
  799. }
  800. #endif //#if CONFIG_IDF_TARGET_ESP32C3