From f2650df73189285b787ed7cf251f554463e5dd80 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Wed, 6 Apr 2016 19:20:43 -0700 Subject: [PATCH] Fixed a hardware version detection glitch. If you try to read out the GPIO immediately after setting the weak pull on the pin it is possible that you will not read the correct value on a floating pin. We need to use a busy wait loop instead of the platform_delay because the platform timing is not initialized yet. We also can not initialize the platform_delay code yet because it requires LED gpio to be configured. A busy wait seems to do the job and is easier than refactoring the codebase to use the platform_delay function. --- src/platforms/native/platform.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/platforms/native/platform.c b/src/platforms/native/platform.c index 9526ce7..a5a3508 100644 --- a/src/platforms/native/platform.c +++ b/src/platforms/native/platform.c @@ -41,6 +41,7 @@ static void setup_vbus_irq(void); /* Pins PB[7:5] are used to detect hardware revision. * 000 - Original production build. * 001 - Mini production build. + * 010 - Mini V2.0e and later. */ int platform_hwversion(void) { @@ -57,6 +58,10 @@ int platform_hwversion(void) /* Enable the weak pull up. */ gpio_set(GPIOB, hwversion_pins); + + /* Wait a little to make sure the pull up is in effect... */ + for(int i = 0; i < 100; i++) asm("nop"); + /* Get all pins that are pulled low in hardware. * This also sets all the "unused" pins to 1. */ @@ -64,10 +69,13 @@ int platform_hwversion(void) /* Enable the weak pull down. */ gpio_clear(GPIOB, hwversion_pins); + + /* Wait a little to make sure the pull down is in effect... */ + for(int i = 0; i < 100; i++) asm("nop"); + /* 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