adc_continuous.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 "esp_heap_caps.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 "esp_private/adc_private.h"
  22. #include "esp_private/adc_share_hw_ctrl.h"
  23. #include "esp_private/sar_periph_ctrl.h"
  24. #include "clk_tree.h"
  25. #include "driver/gpio.h"
  26. #include "esp_adc/adc_continuous.h"
  27. #include "hal/adc_types.h"
  28. #include "hal/adc_hal.h"
  29. #include "hal/dma_types.h"
  30. #include "esp_memory_utils.h"
  31. #include "adc_continuous_internal.h"
  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 "esp_private/spi_common_internal.h"
  38. #elif CONFIG_IDF_TARGET_ESP32
  39. #include "hal/i2s_types.h"
  40. #include "driver/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_continuous";
  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. #define INTERNAL_BUF_NUM 5
  50. /*---------------------------------------------------------------
  51. ADC Continuous Read Mode (via DMA)
  52. ---------------------------------------------------------------*/
  53. //Function to address transaction
  54. static bool s_adc_dma_intr(adc_continuous_ctx_t *adc_digi_ctx);
  55. #if SOC_GDMA_SUPPORTED
  56. static bool adc_dma_in_suc_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data);
  57. #else
  58. static void adc_dma_intr_handler(void *arg);
  59. #endif
  60. static int8_t adc_digi_get_io_num(adc_unit_t adc_unit, uint8_t adc_channel)
  61. {
  62. assert(adc_unit <= SOC_ADC_PERIPH_NUM);
  63. uint8_t adc_n = (adc_unit == ADC_UNIT_1) ? 0 : 1;
  64. return adc_channel_io_map[adc_n][adc_channel];
  65. }
  66. static esp_err_t adc_digi_gpio_init(adc_unit_t adc_unit, uint16_t channel_mask)
  67. {
  68. esp_err_t ret = ESP_OK;
  69. uint64_t gpio_mask = 0;
  70. uint32_t n = 0;
  71. int8_t io = 0;
  72. while (channel_mask) {
  73. if (channel_mask & 0x1) {
  74. io = adc_digi_get_io_num(adc_unit, n);
  75. if (io < 0) {
  76. return ESP_ERR_INVALID_ARG;
  77. }
  78. gpio_mask |= BIT64(io);
  79. }
  80. channel_mask = channel_mask >> 1;
  81. n++;
  82. }
  83. gpio_config_t cfg = {
  84. .pin_bit_mask = gpio_mask,
  85. .mode = GPIO_MODE_DISABLE,
  86. };
  87. ret = gpio_config(&cfg);
  88. return ret;
  89. }
  90. esp_err_t adc_continuous_new_handle(const adc_continuous_handle_cfg_t *hdl_config, adc_continuous_handle_t *ret_handle)
  91. {
  92. esp_err_t ret = ESP_OK;
  93. ESP_RETURN_ON_FALSE((hdl_config->conv_frame_size % SOC_ADC_DIGI_DATA_BYTES_PER_CONV == 0), ESP_ERR_INVALID_ARG, ADC_TAG, "conv_frame_size should be in multiples of `SOC_ADC_DIGI_DATA_BYTES_PER_CONV`");
  94. adc_continuous_ctx_t *adc_ctx = heap_caps_calloc(1, sizeof(adc_continuous_ctx_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  95. if (adc_ctx == NULL) {
  96. ret = ESP_ERR_NO_MEM;
  97. goto cleanup;
  98. }
  99. //ringbuffer storage/struct buffer
  100. adc_ctx->ringbuf_storage = heap_caps_calloc(1, hdl_config->max_store_buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  101. adc_ctx->ringbuf_struct = heap_caps_calloc(1, sizeof(StaticRingbuffer_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  102. if (!adc_ctx->ringbuf_storage || !adc_ctx->ringbuf_struct) {
  103. ret = ESP_ERR_NO_MEM;
  104. goto cleanup;
  105. }
  106. //ringbuffer
  107. adc_ctx->ringbuf_hdl = xRingbufferCreateStatic(hdl_config->max_store_buf_size, RINGBUF_TYPE_BYTEBUF, adc_ctx->ringbuf_storage, adc_ctx->ringbuf_struct);
  108. if (!adc_ctx->ringbuf_hdl) {
  109. ret = ESP_ERR_NO_MEM;
  110. goto cleanup;
  111. }
  112. //malloc internal buffer used by DMA
  113. adc_ctx->rx_dma_buf = heap_caps_calloc(1, hdl_config->conv_frame_size * INTERNAL_BUF_NUM, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
  114. if (!adc_ctx->rx_dma_buf) {
  115. ret = ESP_ERR_NO_MEM;
  116. goto cleanup;
  117. }
  118. //malloc dma descriptor
  119. adc_ctx->hal.rx_desc = heap_caps_calloc(1, (sizeof(dma_descriptor_t)) * INTERNAL_BUF_NUM, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
  120. if (!adc_ctx->hal.rx_desc) {
  121. ret = ESP_ERR_NO_MEM;
  122. goto cleanup;
  123. }
  124. //malloc pattern table
  125. adc_ctx->hal_digi_ctrlr_cfg.adc_pattern = calloc(1, SOC_ADC_PATT_LEN_MAX * sizeof(adc_digi_pattern_config_t));
  126. if (!adc_ctx->hal_digi_ctrlr_cfg.adc_pattern) {
  127. ret = ESP_ERR_NO_MEM;
  128. goto cleanup;
  129. }
  130. #if CONFIG_PM_ENABLE
  131. ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc_dma", &adc_ctx->pm_lock);
  132. if (ret != ESP_OK) {
  133. goto cleanup;
  134. }
  135. #endif //CONFIG_PM_ENABLE
  136. #if SOC_GDMA_SUPPORTED
  137. //alloc rx gdma channel
  138. gdma_channel_alloc_config_t rx_alloc_config = {
  139. .direction = GDMA_CHANNEL_DIRECTION_RX,
  140. };
  141. ret = gdma_new_channel(&rx_alloc_config, &adc_ctx->rx_dma_channel);
  142. if (ret != ESP_OK) {
  143. goto cleanup;
  144. }
  145. gdma_connect(adc_ctx->rx_dma_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_ADC, 0));
  146. gdma_strategy_config_t strategy_config = {
  147. .auto_update_desc = true,
  148. .owner_check = true
  149. };
  150. gdma_apply_strategy(adc_ctx->rx_dma_channel, &strategy_config);
  151. gdma_rx_event_callbacks_t cbs = {
  152. .on_recv_eof = adc_dma_in_suc_eof_callback
  153. };
  154. gdma_register_rx_event_callbacks(adc_ctx->rx_dma_channel, &cbs, adc_ctx);
  155. int dma_chan;
  156. gdma_get_channel_id(adc_ctx->rx_dma_channel, &dma_chan);
  157. #elif CONFIG_IDF_TARGET_ESP32S2
  158. //ADC utilises SPI3 DMA on ESP32S2
  159. bool spi_success = false;
  160. uint32_t dma_chan = 0;
  161. spi_success = spicommon_periph_claim(SPI3_HOST, "adc");
  162. ret = spicommon_dma_chan_alloc(SPI3_HOST, SPI_DMA_CH_AUTO, &dma_chan, &dma_chan);
  163. if (ret == ESP_OK) {
  164. adc_ctx->spi_host = SPI3_HOST;
  165. }
  166. if (!spi_success || (adc_ctx->spi_host != SPI3_HOST)) {
  167. goto cleanup;
  168. }
  169. ret = esp_intr_alloc(spicommon_irqdma_source_for_host(adc_ctx->spi_host), ESP_INTR_FLAG_IRAM, adc_dma_intr_handler,
  170. (void *)adc_ctx, &adc_ctx->dma_intr_hdl);
  171. if (ret != ESP_OK) {
  172. goto cleanup;
  173. }
  174. #elif CONFIG_IDF_TARGET_ESP32
  175. //ADC utilises I2S0 DMA on ESP32
  176. uint32_t dma_chan = 0;
  177. ret = i2s_platform_acquire_occupation(I2S_NUM_0, "adc");
  178. if (ret != ESP_OK) {
  179. ret = ESP_ERR_NOT_FOUND;
  180. goto cleanup;
  181. }
  182. adc_ctx->i2s_host = I2S_NUM_0;
  183. ret = esp_intr_alloc(i2s_periph_signal[adc_ctx->i2s_host].irq, ESP_INTR_FLAG_IRAM, adc_dma_intr_handler,
  184. (void *)adc_ctx, &adc_ctx->dma_intr_hdl);
  185. if (ret != ESP_OK) {
  186. goto cleanup;
  187. }
  188. #endif
  189. adc_hal_dma_config_t config = {
  190. #if SOC_GDMA_SUPPORTED
  191. .dev = (void *)GDMA_LL_GET_HW(0),
  192. #elif CONFIG_IDF_TARGET_ESP32S2
  193. .dev = (void *)SPI_LL_GET_HW(adc_ctx->spi_host),
  194. #elif CONFIG_IDF_TARGET_ESP32
  195. .dev = (void *)I2S_LL_GET_HW(adc_ctx->i2s_host),
  196. #endif
  197. .desc_max_num = INTERNAL_BUF_NUM,
  198. .dma_chan = dma_chan,
  199. .eof_num = hdl_config->conv_frame_size / SOC_ADC_DIGI_DATA_BYTES_PER_CONV
  200. };
  201. adc_hal_dma_ctx_config(&adc_ctx->hal, &config);
  202. adc_ctx->fsm = ADC_FSM_INIT;
  203. *ret_handle = adc_ctx;
  204. //enable ADC digital part
  205. periph_module_enable(PERIPH_SARADC_MODULE);
  206. //reset ADC digital part
  207. periph_module_reset(PERIPH_SARADC_MODULE);
  208. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  209. adc_hal_calibration_init(ADC_UNIT_1);
  210. adc_hal_calibration_init(ADC_UNIT_2);
  211. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  212. return ret;
  213. cleanup:
  214. adc_continuous_deinit(adc_ctx);
  215. return ret;
  216. }
  217. #if SOC_GDMA_SUPPORTED
  218. 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)
  219. {
  220. assert(event_data);
  221. adc_continuous_ctx_t *ctx = (adc_continuous_ctx_t *)user_data;
  222. ctx->rx_eof_desc_addr = event_data->rx_eof_desc_addr;
  223. return s_adc_dma_intr(user_data);
  224. }
  225. #else
  226. static IRAM_ATTR void adc_dma_intr_handler(void *arg)
  227. {
  228. adc_continuous_ctx_t *ctx = (adc_continuous_ctx_t *)arg;
  229. bool need_yield = false;
  230. bool conversion_finish = adc_hal_check_event(&ctx->hal, ADC_HAL_DMA_INTR_MASK);
  231. if (conversion_finish) {
  232. adc_hal_digi_clr_intr(&ctx->hal, ADC_HAL_DMA_INTR_MASK);
  233. intptr_t desc_addr = adc_hal_get_desc_addr(&ctx->hal);
  234. ctx->rx_eof_desc_addr = desc_addr;
  235. need_yield = s_adc_dma_intr(ctx);
  236. }
  237. if (need_yield) {
  238. portYIELD_FROM_ISR();
  239. }
  240. }
  241. #endif
  242. static IRAM_ATTR bool s_adc_dma_intr(adc_continuous_ctx_t *adc_digi_ctx)
  243. {
  244. portBASE_TYPE taskAwoken = 0;
  245. bool need_yield = false;
  246. BaseType_t ret;
  247. adc_hal_dma_desc_status_t status = false;
  248. dma_descriptor_t *current_desc = NULL;
  249. while (1) {
  250. status = adc_hal_get_reading_result(&adc_digi_ctx->hal, adc_digi_ctx->rx_eof_desc_addr, &current_desc);
  251. if (status != ADC_HAL_DMA_DESC_VALID) {
  252. break;
  253. }
  254. ret = xRingbufferSendFromISR(adc_digi_ctx->ringbuf_hdl, current_desc->buffer, current_desc->dw0.length, &taskAwoken);
  255. need_yield |= (taskAwoken == pdTRUE);
  256. if (adc_digi_ctx->cbs.on_conv_done) {
  257. adc_continuous_evt_data_t edata = {
  258. .conv_frame_buffer = current_desc->buffer,
  259. .size = current_desc->dw0.length,
  260. };
  261. if (adc_digi_ctx->cbs.on_conv_done(adc_digi_ctx, &edata, adc_digi_ctx->user_data)) {
  262. need_yield |= true;
  263. }
  264. }
  265. if (ret == pdFALSE) {
  266. //ringbuffer overflow
  267. if (adc_digi_ctx->cbs.on_pool_ovf) {
  268. adc_continuous_evt_data_t edata = {};
  269. if (adc_digi_ctx->cbs.on_pool_ovf(adc_digi_ctx, &edata, adc_digi_ctx->user_data)) {
  270. need_yield |= true;
  271. }
  272. }
  273. }
  274. }
  275. if (status == ADC_HAL_DMA_DESC_NULL) {
  276. //start next turns of dma operation
  277. adc_hal_digi_start(&adc_digi_ctx->hal, adc_digi_ctx->rx_dma_buf);
  278. }
  279. return need_yield;
  280. }
  281. esp_err_t adc_continuous_start(adc_continuous_handle_t handle)
  282. {
  283. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  284. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_INIT, ESP_ERR_INVALID_STATE, ADC_TAG, "ADC continuous mode isn't in the init state, it's started already");
  285. if (handle->pm_lock) {
  286. ESP_RETURN_ON_ERROR(esp_pm_lock_acquire(handle->pm_lock), ADC_TAG, "acquire pm_lock failed");
  287. }
  288. handle->fsm = ADC_FSM_STARTED;
  289. sar_periph_ctrl_adc_continuous_power_acquire();
  290. //reset flags
  291. if (handle->use_adc1) {
  292. adc_lock_acquire(ADC_UNIT_1);
  293. }
  294. if (handle->use_adc2) {
  295. adc_lock_acquire(ADC_UNIT_2);
  296. }
  297. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  298. if (handle->use_adc1) {
  299. adc_set_hw_calibration_code(ADC_UNIT_1, handle->adc1_atten);
  300. }
  301. if (handle->use_adc2) {
  302. adc_set_hw_calibration_code(ADC_UNIT_2, handle->adc2_atten);
  303. }
  304. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  305. #if SOC_ADC_ARBITER_SUPPORTED
  306. if (handle->use_adc2) {
  307. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  308. adc_hal_arbiter_config(&config);
  309. }
  310. #endif //#if SOC_ADC_ARBITER_SUPPORTED
  311. if (handle->use_adc1) {
  312. adc_hal_set_controller(ADC_UNIT_1, ADC_HAL_CONTINUOUS_READ_MODE);
  313. }
  314. if (handle->use_adc2) {
  315. adc_hal_set_controller(ADC_UNIT_2, ADC_HAL_CONTINUOUS_READ_MODE);
  316. }
  317. adc_hal_digi_init(&handle->hal);
  318. adc_hal_digi_controller_config(&handle->hal, &handle->hal_digi_ctrlr_cfg);
  319. //start conversion
  320. adc_hal_digi_start(&handle->hal, handle->rx_dma_buf);
  321. return ESP_OK;
  322. }
  323. esp_err_t adc_continuous_stop(adc_continuous_handle_t handle)
  324. {
  325. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  326. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_STARTED, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver is already stopped");
  327. handle->fsm = ADC_FSM_INIT;
  328. //disable the in suc eof intrrupt
  329. adc_hal_digi_dis_intr(&handle->hal, ADC_HAL_DMA_INTR_MASK);
  330. //clear the in suc eof interrupt
  331. adc_hal_digi_clr_intr(&handle->hal, ADC_HAL_DMA_INTR_MASK);
  332. //stop ADC
  333. adc_hal_digi_stop(&handle->hal);
  334. adc_hal_digi_deinit(&handle->hal);
  335. if (handle->use_adc2) {
  336. adc_lock_release(ADC_UNIT_2);
  337. }
  338. if (handle->use_adc1) {
  339. adc_lock_release(ADC_UNIT_1);
  340. }
  341. sar_periph_ctrl_adc_continuous_power_release();
  342. //release power manager lock
  343. if (handle->pm_lock) {
  344. ESP_RETURN_ON_ERROR(esp_pm_lock_release(handle->pm_lock), ADC_TAG, "release pm_lock failed");
  345. }
  346. return ESP_OK;
  347. }
  348. esp_err_t adc_continuous_read(adc_continuous_handle_t handle, uint8_t *buf, uint32_t length_max, uint32_t *out_length, uint32_t timeout_ms)
  349. {
  350. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  351. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_STARTED, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver is already stopped");
  352. TickType_t ticks_to_wait;
  353. esp_err_t ret = ESP_OK;
  354. uint8_t *data = NULL;
  355. size_t size = 0;
  356. ticks_to_wait = timeout_ms / portTICK_PERIOD_MS;
  357. if (timeout_ms == ADC_MAX_DELAY) {
  358. ticks_to_wait = portMAX_DELAY;
  359. }
  360. data = xRingbufferReceiveUpTo(handle->ringbuf_hdl, &size, ticks_to_wait, length_max);
  361. if (!data) {
  362. ESP_LOGV(ADC_TAG, "No data, increase timeout");
  363. ret = ESP_ERR_TIMEOUT;
  364. *out_length = 0;
  365. return ret;
  366. }
  367. memcpy(buf, data, size);
  368. vRingbufferReturnItem(handle->ringbuf_hdl, data);
  369. assert((size % 4) == 0);
  370. *out_length = size;
  371. return ret;
  372. }
  373. esp_err_t adc_continuous_deinit(adc_continuous_handle_t handle)
  374. {
  375. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  376. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_INIT, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver is still running");
  377. if (handle->ringbuf_hdl) {
  378. vRingbufferDelete(handle->ringbuf_hdl);
  379. handle->ringbuf_hdl = NULL;
  380. free(handle->ringbuf_storage);
  381. free(handle->ringbuf_struct);
  382. }
  383. if (handle->pm_lock) {
  384. esp_pm_lock_delete(handle->pm_lock);
  385. }
  386. free(handle->rx_dma_buf);
  387. free(handle->hal.rx_desc);
  388. free(handle->hal_digi_ctrlr_cfg.adc_pattern);
  389. #if SOC_GDMA_SUPPORTED
  390. gdma_disconnect(handle->rx_dma_channel);
  391. gdma_del_channel(handle->rx_dma_channel);
  392. #elif CONFIG_IDF_TARGET_ESP32S2
  393. esp_intr_free(handle->dma_intr_hdl);
  394. spicommon_dma_chan_free(handle->spi_host);
  395. spicommon_periph_free(handle->spi_host);
  396. #elif CONFIG_IDF_TARGET_ESP32
  397. esp_intr_free(handle->dma_intr_hdl);
  398. i2s_platform_release_occupation(handle->i2s_host);
  399. #endif
  400. free(handle);
  401. handle = NULL;
  402. periph_module_disable(PERIPH_SARADC_MODULE);
  403. return ESP_OK;
  404. }
  405. /*---------------------------------------------------------------
  406. Digital controller setting
  407. ---------------------------------------------------------------*/
  408. esp_err_t adc_continuous_config(adc_continuous_handle_t handle, const adc_continuous_config_t *config)
  409. {
  410. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  411. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_INIT, ESP_ERR_INVALID_STATE, ADC_TAG, "ADC continuous mode isn't in the init state, it's started already");
  412. //Pattern related check
  413. 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);
  414. for (int i = 0; i < config->pattern_num; i++) {
  415. 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");
  416. }
  417. for (int i = 0; i < config->pattern_num; i++) {
  418. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
  419. //we add this error log to hint users what happened
  420. if (SOC_ADC_DIG_SUPPORTED_UNIT(config->adc_pattern[i].unit) == 0) {
  421. ESP_LOGE(ADC_TAG, "ADC2 continuous mode is no longer supported, please use ADC1. Search for errata on espressif website for more details. You can enable CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3 to force use ADC2");
  422. }
  423. #endif //CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
  424. #if !CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3
  425. /**
  426. * On all continuous mode supported chips, we will always check the unit to see if it's a continuous mode supported unit.
  427. * However, on ESP32C3 and ESP32S3, we will jump this check, if `CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3` is enabled.
  428. */
  429. ESP_RETURN_ON_FALSE(SOC_ADC_DIG_SUPPORTED_UNIT(config->adc_pattern[i].unit), ESP_ERR_INVALID_ARG, ADC_TAG, "Only support using ADC1 DMA mode");
  430. #endif //#if !CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3
  431. }
  432. 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");
  433. #if CONFIG_IDF_TARGET_ESP32
  434. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE1, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type1");
  435. #elif CONFIG_IDF_TARGET_ESP32S2
  436. if (config->conv_mode == ADC_CONV_BOTH_UNIT || config->conv_mode == ADC_CONV_ALTER_UNIT) {
  437. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE2, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type2");
  438. } else if (config->conv_mode == ADC_CONV_SINGLE_UNIT_1 || config->conv_mode == ADC_CONV_SINGLE_UNIT_2) {
  439. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE1, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type1");
  440. }
  441. #else
  442. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE2, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type2");
  443. #endif
  444. uint32_t clk_src_freq_hz = 0;
  445. clk_tree_src_get_freq_hz(ADC_DIGI_CLK_SRC_DEFAULT, CLK_TREE_SRC_FREQ_PRECISION_CACHED, &clk_src_freq_hz);
  446. handle->hal_digi_ctrlr_cfg.adc_pattern_len = config->pattern_num;
  447. handle->hal_digi_ctrlr_cfg.sample_freq_hz = config->sample_freq_hz;
  448. handle->hal_digi_ctrlr_cfg.conv_mode = config->conv_mode;
  449. memcpy(handle->hal_digi_ctrlr_cfg.adc_pattern, config->adc_pattern, config->pattern_num * sizeof(adc_digi_pattern_config_t));
  450. handle->hal_digi_ctrlr_cfg.clk_src = ADC_DIGI_CLK_SRC_DEFAULT;
  451. handle->hal_digi_ctrlr_cfg.clk_src_freq_hz = clk_src_freq_hz;
  452. const int atten_uninitialized = 999;
  453. handle->adc1_atten = atten_uninitialized;
  454. handle->adc2_atten = atten_uninitialized;
  455. handle->use_adc1 = 0;
  456. handle->use_adc2 = 0;
  457. uint32_t adc1_chan_mask = 0;
  458. uint32_t adc2_chan_mask = 0;
  459. for (int i = 0; i < config->pattern_num; i++) {
  460. const adc_digi_pattern_config_t *pat = &config->adc_pattern[i];
  461. if (pat->unit == ADC_UNIT_1) {
  462. handle->use_adc1 = 1;
  463. adc1_chan_mask |= BIT(pat->channel);
  464. if (handle->adc1_atten == atten_uninitialized) {
  465. handle->adc1_atten = pat->atten;
  466. } else if (handle->adc1_atten != pat->atten) {
  467. return ESP_ERR_INVALID_ARG;
  468. }
  469. } else if (pat->unit == ADC_UNIT_2) {
  470. handle->use_adc2 = 1;
  471. adc2_chan_mask |= BIT(pat->channel);
  472. if (handle->adc2_atten == atten_uninitialized) {
  473. handle->adc2_atten = pat->atten;
  474. } else if (handle->adc2_atten != pat->atten) {
  475. return ESP_ERR_INVALID_ARG;
  476. }
  477. }
  478. }
  479. if (handle->use_adc1) {
  480. adc_digi_gpio_init(ADC_UNIT_1, adc1_chan_mask);
  481. }
  482. if (handle->use_adc2) {
  483. adc_digi_gpio_init(ADC_UNIT_2, adc2_chan_mask);
  484. }
  485. return ESP_OK;
  486. }
  487. esp_err_t adc_continuous_register_event_callbacks(adc_continuous_handle_t handle, const adc_continuous_evt_cbs_t *cbs, void *user_data)
  488. {
  489. ESP_RETURN_ON_FALSE(handle && cbs, ESP_ERR_INVALID_ARG, ADC_TAG, "invalid argument");
  490. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_INIT, ESP_ERR_INVALID_STATE, ADC_TAG, "ADC continuous mode isn't in the init state, it's started already");
  491. #if CONFIG_ADC_CONTINUOUS_ISR_IRAM_SAFE
  492. if (cbs->on_conv_done) {
  493. ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_conv_done), ESP_ERR_INVALID_ARG, ADC_TAG, "on_conv_done callback not in IRAM");
  494. }
  495. if (cbs->on_pool_ovf) {
  496. ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_pool_ovf), ESP_ERR_INVALID_ARG, ADC_TAG, "on_pool_ovf callback not in IRAM");
  497. }
  498. #endif
  499. handle->cbs.on_conv_done = cbs->on_conv_done;
  500. handle->cbs.on_pool_ovf = cbs->on_pool_ovf;
  501. handle->user_data = user_data;
  502. return ESP_OK;
  503. }
  504. esp_err_t adc_continuous_io_to_channel(int io_num, adc_unit_t *unit_id, adc_channel_t *channel)
  505. {
  506. return adc_io_to_channel(io_num, unit_id, channel);
  507. }
  508. esp_err_t adc_continuous_channel_to_io(adc_unit_t unit_id, adc_channel_t channel, int *io_num)
  509. {
  510. return adc_channel_to_io(unit_id, channel, io_num);
  511. }