1 /*
2  * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdint.h>
10 #include <stdbool.h>
11 #include "sdkconfig.h"
12 #include "esp_pm.h"
13 #include "freertos/FreeRTOS.h"
14 #include "freertos/ringbuf.h"
15 #include "hal/adc_types.h"
16 #include "hal/adc_hal.h"
17 //For DMA
18 #if SOC_GDMA_SUPPORTED
19 #include "esp_private/gdma.h"
20 #elif CONFIG_IDF_TARGET_ESP32S2
21 #include "hal/spi_types.h"
22 #elif CONFIG_IDF_TARGET_ESP32
23 #include "driver/i2s_types.h"
24 #endif
25 
26 #include "esp_adc/adc_filter.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 typedef enum {
33     ADC_FSM_INIT,
34     ADC_FSM_STARTED,
35 } adc_fsm_t;
36 
37 /*---------------------------------------------------------------
38             Driver Context
39 ---------------------------------------------------------------*/
40 typedef struct adc_iir_filter_t adc_iir_filter_t;
41 typedef struct adc_continuous_ctx_t adc_continuous_ctx_t;
42 
43 /**
44  * @brief ADC iir filter context
45  */
46 struct adc_iir_filter_t {
47     adc_digi_iir_filter_t filter_id;                            // Filter ID
48     adc_continuous_iir_filter_config_t  cfg;                    //filter configuration
49     adc_continuous_ctx_t                *continuous_ctx;        //ADC continuous driver context
50 };
51 
52 /**
53  * @brief ADC continuous driver context
54  */
55 struct adc_continuous_ctx_t {
56     uint8_t                         *rx_dma_buf;                //dma buffer
57     adc_hal_dma_ctx_t               hal;                        //hal context
58 #if SOC_GDMA_SUPPORTED
59     gdma_channel_handle_t           rx_dma_channel;             //dma rx channel handle
60 #elif CONFIG_IDF_TARGET_ESP32S2
61     spi_host_device_t               spi_host;                   //ADC uses this SPI DMA
62 #elif CONFIG_IDF_TARGET_ESP32
63     i2s_port_t                      i2s_host;                   //ADC uses this I2S DMA
64 #endif
65     intr_handle_t                   dma_intr_hdl;               //DMA Interrupt handler
66     RingbufHandle_t                 ringbuf_hdl;                //RX ringbuffer handler
67     void*                           ringbuf_storage;            //Ringbuffer storage buffer
68     void*                           ringbuf_struct;             //Ringbuffer structure buffer
69     intptr_t                        rx_eof_desc_addr;           //eof descriptor address of RX channel
70     adc_fsm_t                       fsm;                        //ADC continuous mode driver internal states
71     bool                            use_adc1;                   //1: ADC unit1 will be used; 0: ADC unit1 won't be used.
72     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.
73     adc_atten_t                     adc1_atten;                 //Attenuation for ADC1. On this chip each ADC can only support one attenuation.
74     adc_atten_t                     adc2_atten;                 //Attenuation for ADC2. On this chip each ADC can only support one attenuation.
75     adc_hal_digi_ctrlr_cfg_t        hal_digi_ctrlr_cfg;         //Hal digital controller configuration
76     adc_continuous_evt_cbs_t        cbs;                        //Callbacks
77     void                            *user_data;                 //User context
78     esp_pm_lock_handle_t            pm_lock;                    //For power management
79 #if SOC_ADC_DIG_IIR_FILTER_SUPPORTED
80     adc_iir_filter_t                *iir_filter[SOC_ADC_DIGI_IIR_FILTER_NUM];  //ADC IIR filter context
81 #endif
82 };
83 
84 
85 #ifdef __cplusplus
86 }
87 #endif
88