From f10da7082b580f74a354095452a3c28a88c13e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 11 Mar 2021 13:53:00 +0100 Subject: [PATCH] application: add pull detection to type --- application.c | 60 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/application.c b/application.c index 2c2239a..ec77516 100644 --- a/application.c +++ b/application.c @@ -238,9 +238,9 @@ static void command_types(void* argument) { (void)argument; // we won't use the argument float* voltages; - print_voltage_target(); // print target voltage (also sets measurement conditions) - puts("signal voltage pulled pulled\n"); - puts(" pin raw down up \n"); + print_target(); // print target voltage (also sets measurement conditions) + 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 @@ -252,17 +252,59 @@ static void command_types(void* argument) puts(" "); mux_select(i); // select the channel voltages = measure_voltages(); // measure raw voltages - print_voltage(voltages[2], 2); + 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 + + gpio_clear(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // pull down signal voltages = measure_voltages(); // measure pulled down voltages - print_voltage(voltages[2], 2); gpio_set(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // remove pull-down - puts(" "); - gpio_clear(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // pull-up signal + 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 voltages = measure_voltages(); // measure pulled up voltages - print_voltage(voltages[2], 2); 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