adc_continuous.c 23 KB

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