adc_continuous.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 "esp_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_size = hdl_config->max_store_buf_size;
  101. adc_ctx->ringbuf_storage = heap_caps_calloc(1, hdl_config->max_store_buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  102. adc_ctx->ringbuf_struct = heap_caps_calloc(1, sizeof(StaticRingbuffer_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  103. if (!adc_ctx->ringbuf_storage || !adc_ctx->ringbuf_struct) {
  104. ret = ESP_ERR_NO_MEM;
  105. goto cleanup;
  106. }
  107. //ringbuffer
  108. adc_ctx->ringbuf_hdl = xRingbufferCreateStatic(hdl_config->max_store_buf_size, RINGBUF_TYPE_BYTEBUF, adc_ctx->ringbuf_storage, adc_ctx->ringbuf_struct);
  109. if (!adc_ctx->ringbuf_hdl) {
  110. ret = ESP_ERR_NO_MEM;
  111. goto cleanup;
  112. }
  113. //malloc internal buffer used by DMA
  114. adc_ctx->rx_dma_buf = heap_caps_calloc(1, hdl_config->conv_frame_size * INTERNAL_BUF_NUM, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
  115. if (!adc_ctx->rx_dma_buf) {
  116. ret = ESP_ERR_NO_MEM;
  117. goto cleanup;
  118. }
  119. //malloc dma descriptor
  120. uint32_t dma_desc_num_per_frame = (hdl_config->conv_frame_size + DMA_DESCRIPTOR_BUFFER_MAX_SIZE_4B_ALIGNED - 1) / DMA_DESCRIPTOR_BUFFER_MAX_SIZE_4B_ALIGNED;
  121. uint32_t dma_desc_max_num = dma_desc_num_per_frame * INTERNAL_BUF_NUM;
  122. adc_ctx->hal.rx_desc = heap_caps_calloc(1, (sizeof(dma_descriptor_t)) * dma_desc_max_num, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA);
  123. if (!adc_ctx->hal.rx_desc) {
  124. ret = ESP_ERR_NO_MEM;
  125. goto cleanup;
  126. }
  127. //malloc pattern table
  128. adc_ctx->hal_digi_ctrlr_cfg.adc_pattern = calloc(1, SOC_ADC_PATT_LEN_MAX * sizeof(adc_digi_pattern_config_t));
  129. if (!adc_ctx->hal_digi_ctrlr_cfg.adc_pattern) {
  130. ret = ESP_ERR_NO_MEM;
  131. goto cleanup;
  132. }
  133. #if CONFIG_PM_ENABLE
  134. ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "adc_dma", &adc_ctx->pm_lock);
  135. if (ret != ESP_OK) {
  136. goto cleanup;
  137. }
  138. #endif //CONFIG_PM_ENABLE
  139. #if SOC_GDMA_SUPPORTED
  140. //alloc rx gdma channel
  141. gdma_channel_alloc_config_t rx_alloc_config = {
  142. .direction = GDMA_CHANNEL_DIRECTION_RX,
  143. };
  144. ret = gdma_new_channel(&rx_alloc_config, &adc_ctx->rx_dma_channel);
  145. if (ret != ESP_OK) {
  146. goto cleanup;
  147. }
  148. gdma_connect(adc_ctx->rx_dma_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_ADC, 0));
  149. gdma_strategy_config_t strategy_config = {
  150. .auto_update_desc = true,
  151. .owner_check = true
  152. };
  153. gdma_apply_strategy(adc_ctx->rx_dma_channel, &strategy_config);
  154. gdma_rx_event_callbacks_t cbs = {
  155. .on_recv_eof = adc_dma_in_suc_eof_callback
  156. };
  157. gdma_register_rx_event_callbacks(adc_ctx->rx_dma_channel, &cbs, adc_ctx);
  158. int dma_chan;
  159. gdma_get_channel_id(adc_ctx->rx_dma_channel, &dma_chan);
  160. #elif CONFIG_IDF_TARGET_ESP32S2
  161. //ADC utilises SPI3 DMA on ESP32S2
  162. bool spi_success = false;
  163. uint32_t dma_chan = 0;
  164. spi_success = spicommon_periph_claim(SPI3_HOST, "adc");
  165. ret = spicommon_dma_chan_alloc(SPI3_HOST, SPI_DMA_CH_AUTO, &dma_chan, &dma_chan);
  166. if (ret == ESP_OK) {
  167. adc_ctx->spi_host = SPI3_HOST;
  168. }
  169. if (!spi_success || (adc_ctx->spi_host != SPI3_HOST)) {
  170. goto cleanup;
  171. }
  172. ret = esp_intr_alloc(spicommon_irqdma_source_for_host(adc_ctx->spi_host), ESP_INTR_FLAG_IRAM, adc_dma_intr_handler,
  173. (void *)adc_ctx, &adc_ctx->dma_intr_hdl);
  174. if (ret != ESP_OK) {
  175. goto cleanup;
  176. }
  177. #elif CONFIG_IDF_TARGET_ESP32
  178. //ADC utilises I2S0 DMA on ESP32
  179. uint32_t dma_chan = 0;
  180. ret = i2s_platform_acquire_occupation(I2S_NUM_0, "adc");
  181. if (ret != ESP_OK) {
  182. ret = ESP_ERR_NOT_FOUND;
  183. goto cleanup;
  184. }
  185. adc_ctx->i2s_host = I2S_NUM_0;
  186. ret = esp_intr_alloc(i2s_periph_signal[adc_ctx->i2s_host].irq, ESP_INTR_FLAG_IRAM, adc_dma_intr_handler,
  187. (void *)adc_ctx, &adc_ctx->dma_intr_hdl);
  188. if (ret != ESP_OK) {
  189. goto cleanup;
  190. }
  191. #endif
  192. adc_hal_dma_config_t config = {
  193. #if SOC_GDMA_SUPPORTED
  194. .dev = (void *)GDMA_LL_GET_HW(0),
  195. #elif CONFIG_IDF_TARGET_ESP32S2
  196. .dev = (void *)SPI_LL_GET_HW(adc_ctx->spi_host),
  197. #elif CONFIG_IDF_TARGET_ESP32
  198. .dev = (void *)I2S_LL_GET_HW(adc_ctx->i2s_host),
  199. #endif
  200. .eof_desc_num = INTERNAL_BUF_NUM,
  201. .eof_step = dma_desc_num_per_frame,
  202. .dma_chan = dma_chan,
  203. .eof_num = hdl_config->conv_frame_size / SOC_ADC_DIGI_DATA_BYTES_PER_CONV
  204. };
  205. adc_hal_dma_ctx_config(&adc_ctx->hal, &config);
  206. adc_ctx->flags.flush_pool = hdl_config->flags.flush_pool;
  207. adc_ctx->fsm = ADC_FSM_INIT;
  208. *ret_handle = adc_ctx;
  209. //enable ADC digital part
  210. periph_module_enable(PERIPH_SARADC_MODULE);
  211. //reset ADC digital part
  212. periph_module_reset(PERIPH_SARADC_MODULE);
  213. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  214. adc_hal_calibration_init(ADC_UNIT_1);
  215. adc_hal_calibration_init(ADC_UNIT_2);
  216. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  217. return ret;
  218. cleanup:
  219. adc_continuous_deinit(adc_ctx);
  220. return ret;
  221. }
  222. #if SOC_GDMA_SUPPORTED
  223. 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)
  224. {
  225. assert(event_data);
  226. adc_continuous_ctx_t *ctx = (adc_continuous_ctx_t *)user_data;
  227. ctx->rx_eof_desc_addr = event_data->rx_eof_desc_addr;
  228. return s_adc_dma_intr(user_data);
  229. }
  230. #else
  231. static IRAM_ATTR void adc_dma_intr_handler(void *arg)
  232. {
  233. adc_continuous_ctx_t *ctx = (adc_continuous_ctx_t *)arg;
  234. bool need_yield = false;
  235. bool conversion_finish = adc_hal_check_event(&ctx->hal, ADC_HAL_DMA_INTR_MASK);
  236. if (conversion_finish) {
  237. adc_hal_digi_clr_intr(&ctx->hal, ADC_HAL_DMA_INTR_MASK);
  238. intptr_t desc_addr = adc_hal_get_desc_addr(&ctx->hal);
  239. ctx->rx_eof_desc_addr = desc_addr;
  240. need_yield = s_adc_dma_intr(ctx);
  241. }
  242. if (need_yield) {
  243. portYIELD_FROM_ISR();
  244. }
  245. }
  246. #endif
  247. static IRAM_ATTR bool s_adc_dma_intr(adc_continuous_ctx_t *adc_digi_ctx)
  248. {
  249. BaseType_t taskAwoken = 0;
  250. bool need_yield = false;
  251. BaseType_t ret;
  252. adc_hal_dma_desc_status_t status = false;
  253. uint8_t *finished_buffer = NULL;
  254. uint32_t finished_size = 0;
  255. while (1) {
  256. status = adc_hal_get_reading_result(&adc_digi_ctx->hal, adc_digi_ctx->rx_eof_desc_addr, &finished_buffer, &finished_size);
  257. if (status != ADC_HAL_DMA_DESC_VALID) {
  258. break;
  259. }
  260. ret = xRingbufferSendFromISR(adc_digi_ctx->ringbuf_hdl, finished_buffer, finished_size, &taskAwoken);
  261. need_yield |= (taskAwoken == pdTRUE);
  262. if (adc_digi_ctx->cbs.on_conv_done) {
  263. adc_continuous_evt_data_t edata = {
  264. .conv_frame_buffer = finished_buffer,
  265. .size = finished_size,
  266. };
  267. if (adc_digi_ctx->cbs.on_conv_done(adc_digi_ctx, &edata, adc_digi_ctx->user_data)) {
  268. need_yield |= true;
  269. }
  270. }
  271. if (ret == pdFALSE) {
  272. if (adc_digi_ctx->flags.flush_pool) {
  273. size_t actual_size = 0;
  274. uint8_t *old_data = xRingbufferReceiveUpToFromISR(adc_digi_ctx->ringbuf_hdl, &actual_size, adc_digi_ctx->ringbuf_size);
  275. /**
  276. * Replace by ringbuffer reset API when this API is ready.
  277. * Now we do mannual reset.
  278. * For old_data == NULL condition (equals to the future ringbuffer reset fail condition), we don't care this time data,
  279. * as this only happens when the ringbuffer size is small, new data will be filled in soon.
  280. */
  281. if (old_data) {
  282. vRingbufferReturnItemFromISR(adc_digi_ctx->ringbuf_hdl, old_data, &taskAwoken);
  283. xRingbufferSendFromISR(adc_digi_ctx->ringbuf_hdl, finished_buffer, finished_size, &taskAwoken);
  284. if (taskAwoken == pdTRUE) {
  285. need_yield |= true;
  286. }
  287. }
  288. }
  289. //ringbuffer overflow happens before
  290. if (adc_digi_ctx->cbs.on_pool_ovf) {
  291. adc_continuous_evt_data_t edata = {};
  292. if (adc_digi_ctx->cbs.on_pool_ovf(adc_digi_ctx, &edata, adc_digi_ctx->user_data)) {
  293. need_yield |= true;
  294. }
  295. }
  296. }
  297. }
  298. return need_yield;
  299. }
  300. esp_err_t adc_continuous_start(adc_continuous_handle_t handle)
  301. {
  302. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  303. 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");
  304. if (handle->pm_lock) {
  305. ESP_RETURN_ON_ERROR(esp_pm_lock_acquire(handle->pm_lock), ADC_TAG, "acquire pm_lock failed");
  306. }
  307. handle->fsm = ADC_FSM_STARTED;
  308. sar_periph_ctrl_adc_continuous_power_acquire();
  309. //reset flags
  310. if (handle->use_adc1) {
  311. adc_lock_acquire(ADC_UNIT_1);
  312. }
  313. if (handle->use_adc2) {
  314. adc_lock_acquire(ADC_UNIT_2);
  315. }
  316. #if SOC_ADC_CALIBRATION_V1_SUPPORTED
  317. if (handle->use_adc1) {
  318. adc_set_hw_calibration_code(ADC_UNIT_1, handle->adc1_atten);
  319. }
  320. if (handle->use_adc2) {
  321. adc_set_hw_calibration_code(ADC_UNIT_2, handle->adc2_atten);
  322. }
  323. #endif //#if SOC_ADC_CALIBRATION_V1_SUPPORTED
  324. #if SOC_ADC_ARBITER_SUPPORTED
  325. if (handle->use_adc2) {
  326. adc_arbiter_t config = ADC_ARBITER_CONFIG_DEFAULT();
  327. adc_hal_arbiter_config(&config);
  328. }
  329. #endif //#if SOC_ADC_ARBITER_SUPPORTED
  330. if (handle->use_adc1) {
  331. adc_hal_set_controller(ADC_UNIT_1, ADC_HAL_CONTINUOUS_READ_MODE);
  332. }
  333. if (handle->use_adc2) {
  334. adc_hal_set_controller(ADC_UNIT_2, ADC_HAL_CONTINUOUS_READ_MODE);
  335. }
  336. adc_hal_digi_init(&handle->hal);
  337. adc_hal_digi_controller_config(&handle->hal, &handle->hal_digi_ctrlr_cfg);
  338. //start conversion
  339. adc_hal_digi_start(&handle->hal, handle->rx_dma_buf);
  340. return ESP_OK;
  341. }
  342. esp_err_t adc_continuous_stop(adc_continuous_handle_t handle)
  343. {
  344. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  345. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_STARTED, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver is already stopped");
  346. handle->fsm = ADC_FSM_INIT;
  347. //disable the in suc eof intrrupt
  348. adc_hal_digi_dis_intr(&handle->hal, ADC_HAL_DMA_INTR_MASK);
  349. //clear the in suc eof interrupt
  350. adc_hal_digi_clr_intr(&handle->hal, ADC_HAL_DMA_INTR_MASK);
  351. //stop ADC
  352. adc_hal_digi_stop(&handle->hal);
  353. adc_hal_digi_deinit(&handle->hal);
  354. if (handle->use_adc2) {
  355. adc_lock_release(ADC_UNIT_2);
  356. }
  357. if (handle->use_adc1) {
  358. adc_lock_release(ADC_UNIT_1);
  359. }
  360. sar_periph_ctrl_adc_continuous_power_release();
  361. //release power manager lock
  362. if (handle->pm_lock) {
  363. ESP_RETURN_ON_ERROR(esp_pm_lock_release(handle->pm_lock), ADC_TAG, "release pm_lock failed");
  364. }
  365. return ESP_OK;
  366. }
  367. 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)
  368. {
  369. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  370. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_STARTED, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver is already stopped");
  371. TickType_t ticks_to_wait;
  372. esp_err_t ret = ESP_OK;
  373. uint8_t *data = NULL;
  374. size_t size = 0;
  375. ticks_to_wait = timeout_ms / portTICK_PERIOD_MS;
  376. if (timeout_ms == ADC_MAX_DELAY) {
  377. ticks_to_wait = portMAX_DELAY;
  378. }
  379. data = xRingbufferReceiveUpTo(handle->ringbuf_hdl, &size, ticks_to_wait, length_max);
  380. if (!data) {
  381. ESP_LOGV(ADC_TAG, "No data, increase timeout");
  382. ret = ESP_ERR_TIMEOUT;
  383. *out_length = 0;
  384. return ret;
  385. }
  386. memcpy(buf, data, size);
  387. vRingbufferReturnItem(handle->ringbuf_hdl, data);
  388. assert((size % 4) == 0);
  389. *out_length = size;
  390. return ret;
  391. }
  392. esp_err_t adc_continuous_deinit(adc_continuous_handle_t handle)
  393. {
  394. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  395. ESP_RETURN_ON_FALSE(handle->fsm == ADC_FSM_INIT, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver is still running");
  396. if (handle->ringbuf_hdl) {
  397. vRingbufferDelete(handle->ringbuf_hdl);
  398. handle->ringbuf_hdl = NULL;
  399. free(handle->ringbuf_storage);
  400. free(handle->ringbuf_struct);
  401. }
  402. if (handle->pm_lock) {
  403. esp_pm_lock_delete(handle->pm_lock);
  404. }
  405. free(handle->rx_dma_buf);
  406. free(handle->hal.rx_desc);
  407. free(handle->hal_digi_ctrlr_cfg.adc_pattern);
  408. #if SOC_GDMA_SUPPORTED
  409. gdma_disconnect(handle->rx_dma_channel);
  410. gdma_del_channel(handle->rx_dma_channel);
  411. #elif CONFIG_IDF_TARGET_ESP32S2
  412. esp_intr_free(handle->dma_intr_hdl);
  413. spicommon_dma_chan_free(handle->spi_host);
  414. spicommon_periph_free(handle->spi_host);
  415. #elif CONFIG_IDF_TARGET_ESP32
  416. esp_intr_free(handle->dma_intr_hdl);
  417. i2s_platform_release_occupation(handle->i2s_host);
  418. #endif
  419. free(handle);
  420. handle = NULL;
  421. periph_module_disable(PERIPH_SARADC_MODULE);
  422. return ESP_OK;
  423. }
  424. /*---------------------------------------------------------------
  425. Digital controller setting
  426. ---------------------------------------------------------------*/
  427. esp_err_t adc_continuous_config(adc_continuous_handle_t handle, const adc_continuous_config_t *config)
  428. {
  429. ESP_RETURN_ON_FALSE(handle, ESP_ERR_INVALID_STATE, ADC_TAG, "The driver isn't initialised");
  430. 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");
  431. //Pattern related check
  432. 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);
  433. for (int i = 0; i < config->pattern_num; i++) {
  434. 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");
  435. }
  436. for (int i = 0; i < config->pattern_num; i++) {
  437. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
  438. //we add this error log to hint users what happened
  439. if (SOC_ADC_DIG_SUPPORTED_UNIT(config->adc_pattern[i].unit) == 0) {
  440. 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");
  441. }
  442. #endif //CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
  443. #if !CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3
  444. /**
  445. * On all continuous mode supported chips, we will always check the unit to see if it's a continuous mode supported unit.
  446. * However, on ESP32C3 and ESP32S3, we will jump this check, if `CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3` is enabled.
  447. */
  448. 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");
  449. #endif //#if !CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3
  450. }
  451. 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");
  452. #if CONFIG_IDF_TARGET_ESP32
  453. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE1, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type1");
  454. #elif CONFIG_IDF_TARGET_ESP32S2
  455. if (config->conv_mode == ADC_CONV_BOTH_UNIT || config->conv_mode == ADC_CONV_ALTER_UNIT) {
  456. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE2, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type2");
  457. } else if (config->conv_mode == ADC_CONV_SINGLE_UNIT_1 || config->conv_mode == ADC_CONV_SINGLE_UNIT_2) {
  458. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE1, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type1");
  459. }
  460. #else
  461. ESP_RETURN_ON_FALSE(config->format == ADC_DIGI_OUTPUT_FORMAT_TYPE2, ESP_ERR_INVALID_ARG, ADC_TAG, "Please use type2");
  462. #endif
  463. uint32_t clk_src_freq_hz = 0;
  464. esp_clk_tree_src_get_freq_hz(ADC_DIGI_CLK_SRC_DEFAULT, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &clk_src_freq_hz);
  465. handle->hal_digi_ctrlr_cfg.adc_pattern_len = config->pattern_num;
  466. handle->hal_digi_ctrlr_cfg.sample_freq_hz = config->sample_freq_hz;
  467. handle->hal_digi_ctrlr_cfg.conv_mode = config->conv_mode;
  468. memcpy(handle->hal_digi_ctrlr_cfg.adc_pattern, config->adc_pattern, config->pattern_num * sizeof(adc_digi_pattern_config_t));
  469. handle->hal_digi_ctrlr_cfg.clk_src = ADC_DIGI_CLK_SRC_DEFAULT;
  470. handle->hal_digi_ctrlr_cfg.clk_src_freq_hz = clk_src_freq_hz;
  471. const int atten_uninitialized = 999;
  472. handle->adc1_atten = atten_uninitialized;
  473. handle->adc2_atten = atten_uninitialized;
  474. handle->use_adc1 = 0;
  475. handle->use_adc2 = 0;
  476. uint32_t adc1_chan_mask = 0;
  477. uint32_t adc2_chan_mask = 0;
  478. for (int i = 0; i < config->pattern_num; i++) {
  479. const adc_digi_pattern_config_t *pat = &config->adc_pattern[i];
  480. if (pat->unit == ADC_UNIT_1) {
  481. handle->use_adc1 = 1;
  482. adc1_chan_mask |= BIT(pat->channel);
  483. if (handle->adc1_atten == atten_uninitialized) {
  484. handle->adc1_atten = pat->atten;
  485. } else if (handle->adc1_atten != pat->atten) {
  486. return ESP_ERR_INVALID_ARG;
  487. }
  488. } else if (pat->unit == ADC_UNIT_2) {
  489. handle->use_adc2 = 1;
  490. adc2_chan_mask |= BIT(pat->channel);
  491. if (handle->adc2_atten == atten_uninitialized) {
  492. handle->adc2_atten = pat->atten;
  493. } else if (handle->adc2_atten != pat->atten) {
  494. return ESP_ERR_INVALID_ARG;
  495. }
  496. }
  497. }
  498. if (handle->use_adc1) {
  499. adc_digi_gpio_init(ADC_UNIT_1, adc1_chan_mask);
  500. }
  501. if (handle->use_adc2) {
  502. adc_digi_gpio_init(ADC_UNIT_2, adc2_chan_mask);
  503. }
  504. return ESP_OK;
  505. }
  506. esp_err_t adc_continuous_register_event_callbacks(adc_continuous_handle_t handle, const adc_continuous_evt_cbs_t *cbs, void *user_data)
  507. {
  508. ESP_RETURN_ON_FALSE(handle && cbs, ESP_ERR_INVALID_ARG, ADC_TAG, "invalid argument");
  509. 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");
  510. #if CONFIG_ADC_CONTINUOUS_ISR_IRAM_SAFE
  511. if (cbs->on_conv_done) {
  512. 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");
  513. }
  514. if (cbs->on_pool_ovf) {
  515. 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");
  516. }
  517. #endif
  518. handle->cbs.on_conv_done = cbs->on_conv_done;
  519. handle->cbs.on_pool_ovf = cbs->on_pool_ovf;
  520. handle->user_data = user_data;
  521. return ESP_OK;
  522. }
  523. esp_err_t adc_continuous_io_to_channel(int io_num, adc_unit_t * const unit_id, adc_channel_t * const channel)
  524. {
  525. return adc_io_to_channel(io_num, unit_id, channel);
  526. }
  527. esp_err_t adc_continuous_channel_to_io(adc_unit_t unit_id, adc_channel_t channel, int * const io_num)
  528. {
  529. return adc_channel_to_io(unit_id, channel, io_num);
  530. }