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