From b7e50056795287dddf5445ddc51d40dd5e94c0e9 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sun, 14 Feb 2016 22:41:51 -0800 Subject: [PATCH] [native] Check for hardware version by pull-up and -down. Print HW Version in mon command. Until now the native hardware was pulling PB5-7 down and checking if they were asserted high. BMPMV2b is pulling the pins down instead of high. The hardware version routine now determines the hardware version based on the fact if a pin is asserted at all. This means that if a pin is left floating, the version number bit will be 0, and if the pin is asserted either high or low the bit will be set to 1. While we were already at it the "monitor version" command in GDB will now also print the hardware version number. --- src/command.c | 2 +- src/platforms/launchpad-icdi/platform.h | 5 +++++ src/platforms/libftdi/platform.h | 5 +++++ src/platforms/native/platform.c | 30 ++++++++++++++++++++++--- src/platforms/swlink/platform.h | 5 +++++ 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/command.c b/src/command.c index 8e20960..7a5444c 100644 --- a/src/command.c +++ b/src/command.c @@ -108,7 +108,7 @@ int command_process(target *t, char *cmd) bool cmd_version(void) { - gdb_out("Black Magic Probe (Firmware " FIRMWARE_VERSION ")\n"); + gdb_outf("Black Magic Probe (Firmware " FIRMWARE_VERSION ") (Hardware Version %d)\n", platform_hwversion()); gdb_out("Copyright (C) 2015 Black Sphere Technologies Ltd.\n"); gdb_out("License GPLv3+: GNU GPL version 3 or later " "\n\n"); diff --git a/src/platforms/launchpad-icdi/platform.h b/src/platforms/launchpad-icdi/platform.h index 87e53d5..fe86e03 100644 --- a/src/platforms/launchpad-icdi/platform.h +++ b/src/platforms/launchpad-icdi/platform.h @@ -116,4 +116,9 @@ inline static uint8_t gpio_get(uint32_t port, uint8_t pin) { #define disconnect_usb() do { usbd_disconnect(usbdev,1); nvic_disable_irq(USB_IRQ);} while(0) +static inline int platform_hwversion(void) +{ + return 0; +} + #endif diff --git a/src/platforms/libftdi/platform.h b/src/platforms/libftdi/platform.h index 8611674..969109c 100644 --- a/src/platforms/libftdi/platform.h +++ b/src/platforms/libftdi/platform.h @@ -42,5 +42,10 @@ void platform_buffer_flush(void); int platform_buffer_write(const uint8_t *data, int size); int platform_buffer_read(uint8_t *data, int size); +static inline int platform_hwversion(void) +{ + return 0; +} + #endif diff --git a/src/platforms/native/platform.c b/src/platforms/native/platform.c index babb498..9526ce7 100644 --- a/src/platforms/native/platform.c +++ b/src/platforms/native/platform.c @@ -45,13 +45,37 @@ static void setup_vbus_irq(void); int platform_hwversion(void) { static int hwversion = -1; + uint16_t hwversion_pins = GPIO7 | GPIO6 | GPIO5; + uint16_t unused_pins = hwversion_pins ^ 0xFFFF; + + /* Only check for version if this is the first time. */ if (hwversion == -1) { + /* Configure the hardware version pins as input pull-up/down */ gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, - GPIO7 | GPIO6 | GPIO5); - gpio_clear(GPIOB, GPIO7 | GPIO6 | GPIO5); - hwversion = gpio_get(GPIOB, GPIO7 | GPIO6 | GPIO5) >> 5; + hwversion_pins); + + /* Enable the weak pull up. */ + gpio_set(GPIOB, hwversion_pins); + /* Get all pins that are pulled low in hardware. + * This also sets all the "unused" pins to 1. + */ + uint16_t pins_negative = gpio_get(GPIOB, hwversion_pins) | unused_pins; + + /* Enable the weak pull down. */ + gpio_clear(GPIOB, hwversion_pins); + /* Get all the pins that are pulled high in hardware. */ + uint16_t pins_positive = gpio_get(GPIOB, hwversion_pins); + + + /* Hardware version is the id defined by the pins that are + * asserted low or high by the hardware. This means that pins + * that are left floating are 0 and those that are either + * pulled high or low are 1. + */ + hwversion = (((pins_positive ^ pins_negative) ^ 0xFFFF) & hwversion_pins) >> 5; } + return hwversion; } diff --git a/src/platforms/swlink/platform.h b/src/platforms/swlink/platform.h index b1690e7..def38e8 100644 --- a/src/platforms/swlink/platform.h +++ b/src/platforms/swlink/platform.h @@ -127,6 +127,11 @@ #define SET_IDLE_STATE(state) {gpio_set_val(LED_PORT, LED_IDLE_RUN, state);} #define SET_ERROR_STATE(x) +static inline int platform_hwversion(void) +{ + return 0; +} + /* Use newlib provided integer only stdio functions */ #define sscanf siscanf #define sprintf siprintf