diff --git a/application.c b/application.c index 6481dfe..cde1c8b 100644 --- a/application.c +++ b/application.c @@ -52,19 +52,9 @@ static volatile bool second_flag = false; /**< flag set when a second passed */ static uint32_t boot_time = 0; #define TARGET_CHANNEL 1 /**< PA1/ADC1_IN1 used to measure target voltage */ -#define SIGNAL_CHANNEL 2 /**< PA2/ADC1_IN2 used to measure signal voltage */ -const uint8_t adc_channels[] = {ADC_CHANNEL17, ADC_CHANNEL(TARGET_CHANNEL), ADC_CHANNEL(SIGNAL_CHANNEL)}; /**< voltages to convert (channel 17 = internal voltage reference) */ - -#define SIGNAL_PD_PIN PA3 /**< pin to pull signal low for voltage measurement */ -#define SIGNAL_PU_PIN PA4 /**< pin to pull signal to target voltage (controlling gate of pMOS) */ +const uint8_t adc_channels[] = {ADC_CHANNEL17, ADC_CHANNEL(TARGET_CHANNEL)}; /**< voltages to convert (channel 17 = internal voltage reference) */ #define TARGET_EN PA5 /**< pin to provide target voltage to LV side of voltage shifter (pulling them high through 10 kO) */ -#define MUX_EN_PIN PB2 /**< pin to enable analog multiplexer (active low) */ -#define MUX_S0_PIN PB1 /**< pin to set S0 bit of analog multiplexer */ -#define MUX_S1_PIN PB0 /**< pin to set S1 bit of analog multiplexer */ -#define MUX_S2_PIN PA7 /**< pin to set S2 bit of analog multiplexer */ -#define MUX_S3_PIN PA6 /**< pin to set S3 bit of analog multiplexer */ - #define CHANNEL_NUMBERS 16 /**< number of target signals */ static const uint32_t channel_ports[] = {GPIO_PORT(PB12), GPIO_PORT(PB13), GPIO_PORT(PB14), GPIO_PORT(PB15), GPIO_PORT(PA8), GPIO_PORT(PA9), GPIO_PORT(PA10), GPIO_PORT(PA15), GPIO_PORT(PB3), GPIO_PORT(PB4), GPIO_PORT(PB5), GPIO_PORT(PB6), GPIO_PORT(PB7), GPIO_PORT(PB8), GPIO_PORT(PB9), GPIO_PORT(PB10)}; /**< GPIO ports for signal pin */ static const uint32_t channel_pins[] = {GPIO_PIN(PB12), GPIO_PIN(PB13), GPIO_PIN(PB14), GPIO_PIN(PB15), GPIO_PIN(PA8), GPIO_PIN(PA9), GPIO_PIN(PA10), GPIO_PIN(PA15), GPIO_PIN(PB3), GPIO_PIN(PB4), GPIO_PIN(PB5), GPIO_PIN(PB6), GPIO_PIN(PB7), GPIO_PIN(PB8), GPIO_PIN(PB9), GPIO_PIN(PB10)}; /**< GPIO pins for signal pin */ @@ -167,24 +157,6 @@ static float* measure_voltages(void) return voltages; } -/** measure and print target voltage */ -static void print_target(void) -{ - gpio_set(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // ensure we are not pulling up the signal - gpio_set(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // ensure we are not pulling down the signal - gpio_set(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // ensure the level shifters pulling up the signals are not enabled - float* voltages = measure_voltages(); // measure voltages - puts("target voltage: "); - print_fpu(voltages[1], 2); - puts(" V"); - if (voltages[1] > 3.25) { - puts(" (warning: signal voltage may exceed 3.30 V limit)"); - } else if (voltages[1] < 1.0) { - puts(" (warning: target voltage seems not connected)"); - } - putc('\n'); -} - /** print decoded IDCODE * @param[in] idcode IDCODE to decode */ @@ -198,40 +170,28 @@ static void print_idcode(uint32_t idcode) (idcode >> 28) & 0x0f); } -/** select channel of multiplexer - * @param[in] channel channel to select, or -1 to disable multiplexer +// menu commands + +/** measure and print target voltage + * @param[in] argument not used */ -static void mux_select(int8_t channel) +static void command_target_voltage(void* argument) { - gpio_set(GPIO_PORT(MUX_EN_PIN), GPIO_PIN(MUX_EN_PIN)); // disable multiplexer while we are switching - if (channel < 0 || channel > 15 || (channel > CHANNEL_NUMBERS - 1)) { - return; // no channel to select + (void)argument; // we won't use the argument + + gpio_set(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // ensure the level shifters pulling up the signals are not enabled + float* voltages = measure_voltages(); // measure voltages + puts("target voltage: "); + print_fpu(voltages[1], 2); + puts(" V"); + if (voltages[1] > 3.25) { + puts(" (warning: signal voltage may exceed 3.30 V limit)"); + } else if (voltages[1] < 1.0) { + puts(" (warning: target voltage seems not connected)"); } - // select channel using bit pattern - if (channel & 0x1) { - gpio_set(GPIO_PORT(MUX_S0_PIN), GPIO_PIN(MUX_S0_PIN)); - } else { - gpio_clear(GPIO_PORT(MUX_S0_PIN), GPIO_PIN(MUX_S0_PIN)); - } - if (channel & 0x2) { - gpio_set(GPIO_PORT(MUX_S1_PIN), GPIO_PIN(MUX_S1_PIN)); - } else { - gpio_clear(GPIO_PORT(MUX_S1_PIN), GPIO_PIN(MUX_S1_PIN)); - } - if (channel & 0x4) { - gpio_set(GPIO_PORT(MUX_S2_PIN), GPIO_PIN(MUX_S2_PIN)); - } else { - gpio_clear(GPIO_PORT(MUX_S2_PIN), GPIO_PIN(MUX_S2_PIN)); - } - if (channel & 0x8) { - gpio_set(GPIO_PORT(MUX_S3_PIN), GPIO_PIN(MUX_S3_PIN)); - } else { - gpio_clear(GPIO_PORT(MUX_S3_PIN), GPIO_PIN(MUX_S3_PIN)); - } - gpio_clear(GPIO_PORT(MUX_EN_PIN), GPIO_PIN(MUX_EN_PIN)); // enable multiplexer + putc('\n'); } -// menu commands static void command_swd_scan(void* argument) { @@ -243,9 +203,6 @@ static void command_swd_scan(void* argument) return; } - mux_select(-1); // disable multiplexer - gpio_set(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // ensure we are not pulling up the signal - gpio_set(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // ensure we are not pulling down the signal gpio_clear(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // power level shifter sleep_us(100); // wait a tiny bit for the pull-up to be active @@ -386,9 +343,6 @@ static void command_jtag_scan(void* argument) return; } - mux_select(-1); // disable multiplexer - gpio_set(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // ensure we are not pulling up the signal - gpio_set(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // ensure we are not pulling down the signal gpio_clear(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // power level shifter sleep_us(100); // wait a tiny bit for the pull-up to be active @@ -556,107 +510,6 @@ static void command_jtag_scan(void* argument) putc('\n'); // all combinations completed } -static void command_voltages(void* argument) -{ - (void)argument; // we won't use the argument - float* voltages; - print_target(); // print target voltage (also sets measurement conditions) - puts("signal voltages:\n"); - for (uint8_t i = channel_start; i <= channel_stop; i++) { - printf("- CH%02u ", i); - mux_select(i); // select the channel - voltages = measure_voltages(); // measure raw voltages - print_fpu(voltages[2], 2); - puts(" V\n"); - } -} - -/** 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 - print_target(); // 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 - gpio_set(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // ensure the level shifters pulling up the signals are not enabled - - 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 */ @@ -955,18 +808,10 @@ static const struct menu_command_t menu_commands[] = { { .shortcut = 'v', .name = "voltage", - .command_description = "measure target and signal voltages", + .command_description = "measure target voltage", .argument = MENU_ARGUMENT_NONE, .argument_description = NULL, - .command_handler = &command_voltages, - }, - { - .shortcut = 't', - .name = "type", - .command_description = "identify signal types", - .argument = MENU_ARGUMENT_NONE, - .argument_description = NULL, - .command_handler = &command_types, + .command_handler = &command_target_voltage, }, { .shortcut = 'c', @@ -1118,44 +963,12 @@ void main(void) puts("OK\n"); puts("setup voltage control: "); - rcc_periph_clock_enable(GPIO_RCC(SIGNAL_PD_PIN)); // enable clock for port domain - gpio_set(GPIO_PORT(SIGNAL_PD_PIN), GPIO_PIN(SIGNAL_PD_PIN)); // ensure we are not draining it - gpio_set_output_options(GPIO_PORT(SIGNAL_PD_PIN), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(SIGNAL_PD_PIN)); // set output as open-drain - gpio_mode_setup(GPIO_PORT(SIGNAL_PD_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(SIGNAL_PD_PIN)); // configure pin as output - rcc_periph_clock_enable(GPIO_RCC(SIGNAL_PU_PIN)); // enable clock for port domain - gpio_set(GPIO_PORT(SIGNAL_PU_PIN), GPIO_PIN(SIGNAL_PU_PIN)); // ensure we are do enable pMOS to pull up the signal - gpio_set_output_options(GPIO_PORT(SIGNAL_PU_PIN), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(SIGNAL_PU_PIN)); // set output as open-drain - gpio_mode_setup(GPIO_PORT(SIGNAL_PU_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(SIGNAL_PU_PIN)); // configure pin as output rcc_periph_clock_enable(GPIO_RCC(TARGET_EN)); // enable clock for port domain gpio_set(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // ensure we do not enable pMOS to power level shifters gpio_set_output_options(GPIO_PORT(TARGET_EN), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_EN)); // set output as open-drain gpio_mode_setup(GPIO_PORT(TARGET_EN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(TARGET_EN)); // configure pin as output puts("OK\n"); - puts("setup analog multiplexer: "); - rcc_periph_clock_enable(GPIO_RCC(MUX_EN_PIN)); // enable clock for port domain - gpio_set(GPIO_PORT(MUX_EN_PIN), GPIO_PIN(MUX_EN_PIN)); // ensure multiplexer is disabled - gpio_set_output_options(GPIO_PORT(MUX_EN_PIN), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(MUX_EN_PIN)); // set output as push-pull to drive correctly - gpio_mode_setup(GPIO_PORT(MUX_EN_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(MUX_EN_PIN)); // configure pin as output - rcc_periph_clock_enable(GPIO_RCC(MUX_S0_PIN)); // enable clock for port domain - gpio_clear(GPIO_PORT(MUX_S0_PIN), GPIO_PIN(MUX_S0_PIN)); // any channel selected is fine - gpio_set_output_options(GPIO_PORT(MUX_S0_PIN), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(MUX_S0_PIN)); // set output as push-pull to drive correctly - gpio_mode_setup(GPIO_PORT(MUX_S0_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(MUX_S0_PIN)); // configure pin as output - rcc_periph_clock_enable(GPIO_RCC(MUX_S1_PIN)); // enable clock for port domain - gpio_clear(GPIO_PORT(MUX_S1_PIN), GPIO_PIN(MUX_S1_PIN)); // any channel selected is fine - gpio_set_output_options(GPIO_PORT(MUX_S1_PIN), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(MUX_S1_PIN)); // set output as push-pull to drive correctly - gpio_mode_setup(GPIO_PORT(MUX_S1_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(MUX_S1_PIN)); // configure pin as output - rcc_periph_clock_enable(GPIO_RCC(MUX_S2_PIN)); // enable clock for port domain - gpio_clear(GPIO_PORT(MUX_S2_PIN), GPIO_PIN(MUX_S2_PIN)); // any channel selected is fine - gpio_set_output_options(GPIO_PORT(MUX_S2_PIN), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(MUX_S2_PIN)); // set output as push-pull to drive correctly - gpio_mode_setup(GPIO_PORT(MUX_S2_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(MUX_S2_PIN)); // configure pin as output - rcc_periph_clock_enable(GPIO_RCC(MUX_S3_PIN)); // enable clock for port domain - gpio_clear(GPIO_PORT(MUX_S3_PIN), GPIO_PIN(MUX_S3_PIN)); // any channel selected is fine - gpio_set_output_options(GPIO_PORT(MUX_S3_PIN), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(MUX_S3_PIN)); // set output as push-pull to drive correctly - gpio_mode_setup(GPIO_PORT(MUX_S3_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(MUX_S3_PIN)); // configure pin as output - mux_select(-1); // ensure it is disabled - puts("OK\n"); - puts("setup signal pins: "); for (uint8_t i = 0; i < CHANNEL_NUMBERS; i++) { rcc_periph_clock_enable(port2rcc(channel_ports[i])); // enable clock for port domain @@ -1178,8 +991,6 @@ void main(void) sleep_us(3); // wait t_stab for the ADC to stabilize rcc_periph_clock_enable(RCC_ADC1_IN(TARGET_CHANNEL)); // enable clock for GPIO domain for target voltage channel gpio_mode_setup(ADC1_IN_PORT(TARGET_CHANNEL), GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC1_IN_PIN(TARGET_CHANNEL)); // set target voltage channel as analog input for the ADC - rcc_periph_clock_enable(RCC_ADC1_IN(SIGNAL_CHANNEL)); // enable clock for GPIO domain for signal channel - gpio_mode_setup(ADC1_IN_PORT(SIGNAL_CHANNEL), GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC1_IN_PIN(SIGNAL_CHANNEL)); // set signal channel as analog input for the ADC measure_voltages(); // try to measure voltages puts("OK\n");