1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include "sdkconfig.h"
13 #include "unity.h"
14 #include "esp_log.h"
15 #include "soc/soc_caps.h"
16 #include "esp_private/adc_private.h"
17 #include "esp_adc/adc_cali.h"
18 #include "esp_adc/adc_cali_scheme.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #ifndef MAX
25 #define MAX(a, b)         (((a) > (b)) ? (a) : (b))
26 #endif
27 
28 /*---------------------------------------------------------------
29         ADC Level
30 ---------------------------------------------------------------*/
31 /**
32  * We use weak pulldown, `ADC_TEST_LOW_THRESH` may vary.
33  * If connect to GND, `ADC_TEST_LOW_THRESH` can be smaller
34  */
35 #if CONFIG_IDF_TARGET_ESP32
36 #define ADC_TEST_LOW_VAL         0
37 #define ADC_TEST_LOW_THRESH      10
38 
39 #define ADC_TEST_HIGH_VAL        4095
40 #define ADC_TEST_HIGH_VAL_DMA    4095
41 #define ADC_TEST_HIGH_THRESH     10
42 
43 #elif CONFIG_IDF_TARGET_ESP32S2
44 #define ADC_TEST_LOW_VAL         0
45 #define ADC_TEST_LOW_THRESH      35
46 
47 #define ADC_TEST_HIGH_VAL        8191
48 #define ADC_TEST_HIGH_VAL_DMA    4095
49 #define ADC_TEST_HIGH_THRESH     10
50 
51 #elif CONFIG_IDF_TARGET_ESP32C3
52 #define ADC_TEST_LOW_VAL         0
53 #define ADC_TEST_LOW_THRESH      60     //This is due to ADC2 accuracy is not as good as ADC1, and also we use weak pulldown
54 
55 #define ADC_TEST_HIGH_VAL        4095
56 #define ADC_TEST_HIGH_VAL_DMA    4095
57 #define ADC_TEST_HIGH_THRESH     10
58 
59 #elif CONFIG_IDF_TARGET_ESP32S3
60 #define ADC_TEST_LOW_VAL         0
61 #define ADC_TEST_LOW_THRESH      15
62 
63 #define ADC_TEST_HIGH_VAL        4095
64 #define ADC_TEST_HIGH_VAL_DMA    4095
65 #define ADC_TEST_HIGH_THRESH     0
66 
67 #elif CONFIG_IDF_TARGET_ESP32C2
68 #define ADC_TEST_LOW_VAL         0
69 #define ADC_TEST_LOW_THRESH      15
70 
71 #define ADC_TEST_HIGH_VAL        3400
72 #define ADC_TEST_HIGH_THRESH     200
73 
74 #elif CONFIG_IDF_TARGET_ESP32C6
75 #define ADC_TEST_LOW_VAL         0
76 #define ADC_TEST_LOW_THRESH      15
77 
78 #define ADC_TEST_HIGH_VAL        3350
79 #define ADC_TEST_HIGH_VAL_DMA    4081
80 #define ADC_TEST_HIGH_THRESH     200
81 
82 #elif CONFIG_IDF_TARGET_ESP32H2  // TODO: IDF-6216
83 #define ADC_TEST_LOW_VAL         2144
84 #define ADC_TEST_LOW_THRESH      200
85 
86 #define ADC_TEST_HIGH_VAL        4081
87 #define ADC_TEST_HIGH_VAL_DMA    4081
88 #define ADC_TEST_HIGH_THRESH     200
89 #endif
90 
91 
92 /*---------------------------------------------------------------
93         ADC Attenuation
94 ---------------------------------------------------------------*/
95 #if CONFIG_IDF_TARGET_ESP32C2
96 #define TEST_ATTEN_NUMS                 2
97 extern adc_atten_t g_test_atten[TEST_ATTEN_NUMS];
98 #else
99 #define TEST_ATTEN_NUMS                 4
100 extern adc_atten_t g_test_atten[TEST_ATTEN_NUMS];
101 #endif
102 
103 /*---------------------------------------------------------------
104         ADC Filter
105 ---------------------------------------------------------------*/
106 #if SOC_ADC_DIG_IIR_FILTER_SUPPORTED
107 #define TEST_FILTER_COEFF_NUMS          5
108 extern adc_digi_iir_filter_coeff_t g_test_filter_coeff[TEST_FILTER_COEFF_NUMS];
109 #endif
110 
111 /*---------------------------------------------------------------
112         ADC Calibration
113 ---------------------------------------------------------------*/
114 /**
115  * @brief Initialise ADC Calibration
116  *
117  * @param[in]  unit         ADC unit
118  * @param[in]  channel      ADC channel
119  * @param[in]  atten        ADC attenuation
120  * @param[in]  bitwidth     ADC bit width
121  * @param[out] out_handle   ADC calibration handle
122  *
123  * @return
124  *        - True  Calibration success
125  *        - False Calibration fail
126  */
127 bool test_adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_bitwidth_t bitwidth, adc_cali_handle_t *out_handle);
128 
129 /**
130  * @brief De-initialise ADC Calibration
131  *
132  * @param[in] handle    ADC calibration handle
133  */
134 void test_adc_calibration_deinit(adc_cali_handle_t handle);
135 
136 
137 /*---------------------------------------------------------------
138         ADC GPIO
139 ---------------------------------------------------------------*/
140 /**
141  * @brief Set ADC IO level
142  *
143  * @param[in] unit      ADC unit
144  * @param[in] channel   ADC channel
145  * @param[in] level     IO level. True: high; False: low
146  */
147 void test_adc_set_io_level(adc_unit_t unit, adc_channel_t channel, bool level);
148 
149 /**
150  * @brief Set ADC IO to a middle level
151  *
152  * @note We don't expect this IO to have a fixed level among different chips.
153  *       We just need the IO to be stable at a certain level, which is neither 0 nor overflow.
154  *
155  * @param[in] unit      ADC unit
156  * @param[in] channel   ADC channel
157  */
158 void test_adc_set_io_middle(adc_unit_t unit, adc_channel_t channel);
159 
160 #ifdef __cplusplus
161 }
162 #endif
163