From 423a0ec90b3ff0b616e376d2d5977033dd7c9a8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 24 Mar 2021 00:54:53 +0100 Subject: [PATCH] application: add command to identify I/O channels --- application.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/application.c b/application.c index fb863ee..3c7eebd 100644 --- a/application.c +++ b/application.c @@ -301,6 +301,91 @@ static void command_target_reset(void* argument) } } +/** identify if signal is an input or output + * @param[in] argument no argument required + */ +static void command_types(void* argument) +{ + (void)argument; // we won't use the argument + command_target_voltage(NULL); // print target voltage (also sets measurement conditions) + float* voltages = measure_voltages(); // measure voltages + if (voltages[1] < 0.5) { // check target voltage connection + puts("connect target voltage to test channel type\n"); + return; + } + + puts("signal voltage pulled pull-up pulled pull-down signal\n"); + puts(" name raw (V) down (V) (kOhm) up (V) (kOhm) type \n"); + + // just to be sure, reset measurement conditions + gpio_set(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // ensure pull-down is not active + gpio_set(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // ensure pull-up is not active + + for (uint8_t i = channel_start; i <= channel_stop; i++) { + printf("CH%02u", i); + puts(" "); + mux_select(i); // select the channel + voltages = measure_voltages(); // measure raw voltages + print_fpu(voltages[2], 2); + const float raw = voltages[2]; // remember un-pulled voltage + puts(" "); + + gpio_clear(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // pull down signal + sleep_us(10); // wait a tiny bit for voltage to settle + voltages = measure_voltages(); // measure pulled down voltages + gpio_set(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // remove pull-down + voltages[2] *= 2.0; // pulling creates a voltage divider (to ground) + const bool low = (voltages[2] < 0.5); // remember if we were able to pull it down + const float pullup = (2000.0 * (raw - voltages[2]) / voltages[2]) / 1000.0; // estimate external pull-up + print_fpu(voltages[2], 2); + puts(" "); + if (pullup > 100.0) { + puts(">100"); + } else if (pullup < 1.0) { + puts(" <1 "); + } else { + printf(" %02u ", (uint32_t)round(pullup)); + } + puts(" "); + + gpio_clear(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // pull up signal + sleep_us(10); // wait a tiny bit for voltage to settle + voltages = measure_voltages(); // measure pulled up voltages + gpio_set(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // remove pull-up + voltages[2] = voltages[2] * 2.0 - voltages[1]; // pulling creates a voltage divider (to target) + const bool high = (voltages[2] > 3.2 || voltages[2] > voltages[1] * 0.5); // remember if we were able to pull it up + const float pulldown = (2000.0 * voltages[2] / (voltages[1] - voltages[2])) / 1000.0; // estimate external pull-down + print_fpu(voltages[2], 2); + puts(" "); + if (pulldown > 100.0) { + puts(">100"); + } else if (pulldown < 1.0) { + puts(" <1 "); + } else { + printf(" %02u ", (uint32_t)round(pulldown)); + } + puts(" "); + + if (low && high) { + if (pullup > 1.0 && pullup < 100.0 && (pulldown < 1.0 || pulldown > 100.0)) { + puts("pulled-up"); + } else if (pulldown > 1.0 && pulldown < 100.0 && (pullup < 1.0 || pullup > 100.0)) { + puts("pulled-down"); + } else { + puts("floating"); + } + } else if (low) { + puts("low"); + } else if (high) { + puts("high"); + } else { + puts("unknown"); + } + putc('\n'); + } + mux_select(-1); // disable multiplexer +} + /** set first channel of range to scan * @param[in] argument optional pointer to first channel number */ @@ -596,6 +681,14 @@ static const struct menu_command_t menu_commands[] = { .argument_description = "[0|1|ODL|ODH|PPL|PPH]", .command_handler = &command_target_reset, }, + { + .shortcut = 't', + .name = "type", + .command_description = "identify signal types", + .argument = MENU_ARGUMENT_NONE, + .argument_description = NULL, + .command_handler = &command_types, + }, { .shortcut = 'c', .name = "start",