Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
#include "esp_adc/adc_oneshot.h"
#include "driver/gpio.h"
#include "esp_cpu.h"
#include "esp_log.h"
#include "sd_test_io.h"
TAG
#define ADC_ATTEN_DB
#define GPIO_INPUT_PIN_SEL
get_cycles_until_pin_level(int, int, int)
check_sd_card_pins(pin_configuration_t *, const int)
Files
loading (1/4)...
SourceVuESP-IDF Framework and Examplessdmmc samplecomponents/sd_card/sd_test_io.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <stdio.h> #include <inttypes.h> #include <unistd.h> #include "esp_adc/adc_oneshot.h" #include "driver/gpio.h" #include "esp_cpu.h" #include "esp_log.h" #include "sd_test_io.h"8 includes const static char *TAG = "SD_TEST"; #define ADC_ATTEN_DB ADC_ATTEN_DB_12 #define GPIO_INPUT_PIN_SEL(pin) (1ULL<<pin) #if CONFIG_EXAMPLE_ENABLE_ADC_FEATURE static bool adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_cali_handle_t *out_handle) { adc_cali_handle_t handle = NULL; esp_err_t ret = ESP_FAIL; bool calibrated = false; #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED if (!calibrated) { adc_cali_curve_fitting_config_t cali_config = { .unit_id = unit, .chan = channel, .atten = atten, .bitwidth = ADC_BITWIDTH_DEFAULT, }{...}; ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle); if (ret == ESP_OK) { calibrated = true; }{...} }{...} /* ... */#endif //ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED #if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED if (!calibrated) { adc_cali_line_fitting_config_t cali_config = { .unit_id = unit, .atten = atten, .bitwidth = ADC_BITWIDTH_DEFAULT, }{...}; ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle); if (ret == ESP_OK) { calibrated = true; }{...} }{...} /* ... */#endif //ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED *out_handle = handle; if (ret != ESP_OK) { if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) { ESP_LOGW(TAG, "eFuse not burnt, skip software calibration"); }{...} else { ESP_LOGE(TAG, "Invalid arg or no memory"); }{...} }{...} return calibrated; }{...} static void example_adc_calibration_deinit(adc_cali_handle_t handle) { #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle)); /* ... */ #elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle)); #endif }{...} static float get_pin_voltage(int i, adc_oneshot_unit_handle_t adc_handle, bool do_calibration, adc_cali_handle_t adc_cali_handle) { int voltage = 0; int val; adc_oneshot_chan_cfg_t config = { .bitwidth = ADC_BITWIDTH_DEFAULT, .atten = ADC_ATTEN_DB, }{...}; ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, i, &config)); ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, i, &val)); if (do_calibration) { ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_cali_handle, val, &voltage)); }{...} return (float)voltage/1000; }{...} /* ... */#endif //CONFIG_EXAMPLE_ENABLE_ADC_FEATURE static uint32_t get_cycles_until_pin_level(int i, int level, int timeout) { uint32_t start = esp_cpu_get_cycle_count(); while(gpio_get_level(i) == !level && esp_cpu_get_cycle_count() - start < timeout) { ; }{...} uint32_t end = esp_cpu_get_cycle_count(); return end - start; }{ ... } void check_sd_card_pins(pin_configuration_t *config, const int pin_count) { ESP_LOGI(TAG, "Testing SD pin connections and pullup strength"); gpio_config_t io_conf = {}; for (int i = 0; i < pin_count; ++i) { io_conf.intr_type = GPIO_INTR_DISABLE; io_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD; io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL(config->pins[i]); io_conf.pull_down_en = 0; io_conf.pull_up_en = 0; gpio_config(&io_conf); }{...} printf("\n**** PIN recovery time ****\n\n"); for (int i = 0; i < pin_count; ++i) { gpio_set_direction(config->pins[i], GPIO_MODE_INPUT_OUTPUT_OD); gpio_set_level(config->pins[i], 0); usleep(100); gpio_set_level(config->pins[i], 1); uint32_t cycles = get_cycles_until_pin_level(config->pins[i], 1, 10000); printf("PIN %2d %3s %"PRIu32" cycles\n", config->pins[i], config->names[i], cycles); }{...} printf("\n**** PIN recovery time with weak pullup ****\n\n"); for (int i = 0; i < pin_count; ++i) { gpio_set_direction(config->pins[i], GPIO_MODE_INPUT_OUTPUT_OD); gpio_pullup_en(config->pins[i]); gpio_set_level(config->pins[i], 0); usleep(100); gpio_set_level(config->pins[i], 1); uint32_t cycles = get_cycles_until_pin_level(config->pins[i], 1, 10000); printf("PIN %2d %3s %"PRIu32" cycles\n", config->pins[i], config->names[i], cycles); gpio_pullup_dis(config->pins[i]); }{...} #if CONFIG_EXAMPLE_ENABLE_ADC_FEATURE adc_oneshot_unit_handle_t adc_handle; adc_oneshot_unit_init_cfg_t init_config = { .unit_id = CONFIG_EXAMPLE_ADC_UNIT, }{...}; ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle)); adc_cali_handle_t *adc_cali_handle = (adc_cali_handle_t *)malloc(sizeof(adc_cali_handle_t)*pin_count); bool *do_calibration = (bool *)malloc(sizeof(bool)*pin_count); for (int i = 0; i < pin_count; i++) { do_calibration[i] = adc_calibration_init(CONFIG_EXAMPLE_ADC_UNIT, i, ADC_ATTEN_DB, &adc_cali_handle[i]); }{...} printf("\n**** PIN voltage levels ****\n\n"); for (int i = 0; i < pin_count; ++i) { float voltage = get_pin_voltage(config->adc_channels[i], adc_handle, do_calibration[i], adc_cali_handle[i]); printf("PIN %2d %3s %.1fV\n", config->pins[i], config->names[i], voltage); }{...} printf("\n**** PIN voltage levels with weak pullup ****\n\n"); for (int i = 0; i < pin_count; ++i) { gpio_pullup_en(config->pins[i]); usleep(100); float voltage = get_pin_voltage(config->adc_channels[i], adc_handle, do_calibration[i], adc_cali_handle[i]); printf("PIN %2d %3s %.1fV\n", config->pins[i], config->names[i], voltage); gpio_pullup_dis(config->pins[i]); }{...} printf("\n**** PIN cross-talk ****\n\n"); printf(" "); for (int i = 0; i < pin_count; ++i) { printf("%3s ", config->names[i]); }{...} printf("\n"); for (int i = 0; i < pin_count; ++i) { gpio_set_direction(config->pins[i], GPIO_MODE_INPUT_OUTPUT_OD); gpio_set_level(config->pins[i], 0); printf("PIN %2d %3s ", config->pins[i], config->names[i]); for (int j = 0; j < pin_count; ++j) { if (j == i) { printf(" -- "); continue; }{...} usleep(100); float voltage = get_pin_voltage(config->adc_channels[j], adc_handle, do_calibration[i], adc_cali_handle[i]); printf("%1.1fV ", voltage); }{...} printf("\n"); gpio_set_direction(config->pins[i], GPIO_MODE_INPUT); }{...} printf("\n**** PIN cross-talk with weak pullup ****\n\n"); printf(" "); for (int i = 0; i < pin_count; ++i) { printf("%3s ", config->names[i]); }{...} printf("\n"); for (int i = 0; i < pin_count; ++i) { gpio_set_direction(config->pins[i], GPIO_MODE_INPUT_OUTPUT_OD); gpio_set_level(config->pins[i], 0); printf("PIN %2d %3s ", config->pins[i], config->names[i]); for (int j = 0; j < pin_count; ++j) { if (j == i) { printf(" -- "); continue; }{...} gpio_pullup_en(config->pins[j]); usleep(100); float voltage = get_pin_voltage(config->adc_channels[j], adc_handle, do_calibration[i], adc_cali_handle[i]); printf("%1.1fV ", voltage); gpio_pullup_dis(config->pins[j]); }{...} printf("\n"); gpio_set_direction(config->pins[i], GPIO_MODE_INPUT); }{...} for (int i = 0; i < pin_count; ++i) { if (do_calibration[i]) { example_adc_calibration_deinit(adc_cali_handle[i]); }{...} }{...} #endif/* ... */ //CONFIG_EXAMPLE_ENABLE_ADC_FEATURE }{ ... }
Details