adc_continuous.c 23 KB

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