From 93d9f9b45c639b66f885acce6cae976286a8d3f9 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Wed, 21 Feb 2024 23:29:27 -0500 Subject: [PATCH] Use 10-bit ADC value for pin strap detection Previously we thought we were configuring the ADC for 8-bit values, but we did so incorrectly. We were throwing away the top two bits of a 10-bit value. This never caused a problem because the only fixed voltages we used were at the extreme minimum or maximum of the ADC range. --- firmware/common/platform_detect.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/firmware/common/platform_detect.c b/firmware/common/platform_detect.c index 5430c5d8..263ebf10 100644 --- a/firmware/common/platform_detect.c +++ b/firmware/common/platform_detect.c @@ -60,12 +60,14 @@ static struct gpio_t gpio_led1 = GPIO(2, 1); static struct gpio_t gpio_led2 = GPIO(2, 2); static struct gpio_t gpio_led3 = GPIO(2, 8); -uint8_t adc_read(uint8_t pin) +/* + * Return 10-bit ADC result. + */ +uint16_t adc_read(uint8_t pin) { pin &= 0x7; uint8_t pin_mask = (1 << pin); - ADC0_CR = ADC_CR_SEL(pin_mask) | ADC_CR_CLKDIV(45) | ADC_CR_CLKS(2) | ADC_CR_PDN | - ADC_CR_START(1); + ADC0_CR = ADC_CR_SEL(pin_mask) | ADC_CR_CLKDIV(45) | ADC_CR_PDN | ADC_CR_START(1); while (!(ADC0_GDR & ADC_DR_DONE) || (((ADC0_GDR >> 24) & 0x7) != pin)) {} return (ADC0_GDR >> 6) & 0x03FF; } @@ -82,7 +84,7 @@ void adc_off(void) */ #define NUM_SAMPLES (10) #define LOW_THRESHOLD (2 * NUM_SAMPLES) -#define HIGH_THRESHOLD (253 * NUM_SAMPLES) +#define HIGH_THRESHOLD (1022 * NUM_SAMPLES) typedef enum { PIN_STRAP_HIGH,