application: add pull detection to type

This commit is contained in:
King Kévin 2021-03-11 13:53:00 +01:00
parent e5dbb79b0e
commit f10da7082b
1 changed files with 51 additions and 9 deletions

View File

@ -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