Skip to content

Instantly share code, notes, and snippets.

@igrr

igrr/test_io.c

Created Feb 7, 2020
Embed
What would you like to do?
#include <stdio.h>
#include <unistd.h>
#include "driver/adc.h"
#include "driver/gpio.h"
#include "driver/rtc_io.h"
#include "soc/cpu.h"
const char* names[] = {"D2", "D3", "CLK", "CMD", "D0", "D1"};
const int pins[] = {12, 13, 14, 15, 2, 4};
const int adc_channels[] = {5, 4, 6, 3, 2, 0};
const int pin_count = sizeof(pins)/sizeof(pins[0]);
const int adc_sample_count = 100;
const float adc_fs_v = 3.3f;
static float get_pin_voltage(int i) {
ESP_ERROR_CHECK(adc2_config_channel_atten(adc_channels[i], ADC_ATTEN_DB_11));
int avg_val = 0;
for (int sample = 0; sample < adc_sample_count; ++sample) {
int val;
ESP_ERROR_CHECK(adc2_get_raw(adc_channels[i], ADC_WIDTH_BIT_10, &val));
avg_val += val;
}
avg_val = (avg_val + adc_sample_count / 2) / adc_sample_count;
return avg_val * adc_fs_v / 1024;
}
static uint32_t get_cycles_until_pin_level(int i, int level, int timeout) {
uint32_t start = esp_cpu_get_ccount();
while(rtc_gpio_get_level(pins[i]) == !level && esp_cpu_get_ccount() - start < timeout) {
;
}
uint32_t end = esp_cpu_get_ccount();
return end - start;
}
void check_sd_card_pins(void)
{
printf("\nSD Card I/O check. Make sure the SD card is NOT plugged in!\n");
for (int i = 0; i < pin_count; ++i) {
rtc_gpio_init(pins[i]);
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_ONLY);
rtc_gpio_pullup_dis(pins[i]);
rtc_gpio_pulldown_dis(pins[i]);
}
printf("\n\t**** Checking idle levels ****\n\n");
for (int i = 0; i < pin_count; ++i) {
float voltage = get_pin_voltage(i);
printf("PIN %2d %3s %.1fV\n", pins[i], names[i], voltage);
}
printf("\n\t**** Checking levels with weak pullup ****\n\n");
for (int i = 0; i < pin_count; ++i) {
rtc_gpio_pullup_en(pins[i]);
usleep(100);
float voltage = get_pin_voltage(i);
printf("PIN %2d %3s %.1fV\n", pins[i], names[i], voltage);
rtc_gpio_pullup_dis(pins[i]);
}
printf("\n\t**** Checking pin recovery time ****\n\n");
for (int i = 0; i < pin_count; ++i) {
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_OUTPUT_OD);
rtc_gpio_set_level(pins[i], 0);
usleep(100);
rtc_gpio_set_level(pins[i], 1);
uint32_t cycles = get_cycles_until_pin_level(i, 1, 10000);
printf("PIN %2d %3s %u cycles\n", pins[i], names[i], cycles);
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_ONLY);
}
printf("\n\t**** Checking pin recovery time with weak pullup ****\n\n");
for (int i = 0; i < pin_count; ++i) {
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_OUTPUT_OD);
rtc_gpio_pullup_en(pins[i]);
rtc_gpio_set_level(pins[i], 0);
usleep(100);
rtc_gpio_set_level(pins[i], 1);
uint32_t cycles = get_cycles_until_pin_level(i, 1, 10000);
printf("PIN %2d %3s %u cycles\n", pins[i], names[i], cycles);
rtc_gpio_pullup_dis(pins[i]);
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_ONLY);
}
printf("\n\t**** Checking pin cross-talk ****\n\n");
printf(" ");
for (int i = 0; i < pin_count; ++i) {
printf("%3s ", names[i]);
}
printf("\n");
for (int i = 0; i < pin_count; ++i) {
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_OUTPUT_OD);
rtc_gpio_set_level(pins[i], 0);
printf("PIN %2d %3s ", pins[i], names[i]);
for (int j = 0; j < pin_count; ++j) {
if (j == i) {
printf(" -- ");
continue;
}
usleep(100);
float voltage = get_pin_voltage(j);
printf("%1.1fV ", voltage);
}
printf("\n");
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_ONLY);
}
printf("\n\t**** Checking pin cross-talk with weak pull-up ****\n\n");
printf(" ");
for (int i = 0; i < pin_count; ++i) {
printf("%3s ", names[i]);
}
printf("\n");
for (int i = 0; i < pin_count; ++i) {
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_OUTPUT_OD);
rtc_gpio_set_level(pins[i], 0);
printf("PIN %2d %3s ", pins[i], names[i]);
for (int j = 0; j < pin_count; ++j) {
if (j == i) {
printf(" -- ");
continue;
}
rtc_gpio_pullup_en(pins[j]);
usleep(100);
float voltage = get_pin_voltage(j);
printf("%1.1fV ", voltage);
rtc_gpio_pullup_dis(pins[j]);
}
printf("\n");
rtc_gpio_set_direction(pins[i], RTC_GPIO_MODE_INPUT_ONLY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment