From 8d2c0ff9e5ea05cc8223bf1f7e6758c0ccd0d224 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Wed, 23 May 2012 21:44:39 +1200 Subject: [PATCH] Report target voltage on scan. ADC is used on mini hardware, standard hardware reports ok/absent. --- src/command.c | 4 +++ src/stm32/platform.c | 60 ++++++++++++++++++++++++++++++++++++++++++-- src/stm32/platform.h | 1 + 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/command.c b/src/command.c index a9f3d028..e4e856b1 100644 --- a/src/command.c +++ b/src/command.c @@ -110,6 +110,8 @@ void cmd_help(void) void cmd_jtag_scan(void) { + gdb_outf("Target voltage: %s\n", platform_target_voltage()); + int devs = jtag_scan(); if(devs < 0) { @@ -131,6 +133,8 @@ void cmd_jtag_scan(void) void cmd_swdp_scan(void) { + gdb_outf("Target voltage: %s\n", platform_target_voltage()); + if(adiv5_swdp_scan() < 0) { gdb_out("SW-DP scan failed!\n"); return; diff --git a/src/stm32/platform.c b/src/stm32/platform.c index 930ffd50..522e920d 100644 --- a/src/stm32/platform.c +++ b/src/stm32/platform.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "platform.h" #include "jtag_scan.h" @@ -46,6 +47,7 @@ static void morse_update(void); #ifdef INCLUDE_UART_INTERFACE static void uart_init(void); #endif +static void adc_init(void); /* Pins PB[7:5] are used to detect hardware revision. * 000 - Original production build. @@ -55,7 +57,7 @@ int platform_hwversion(void) { static int hwversion = -1; if (hwversion == -1) { - gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO7 | GPIO6 | GPIO5); gpio_clear(GPIOB, GPIO7 | GPIO6 | GPIO5); @@ -108,6 +110,14 @@ int platform_init(void) !(SCS_DEMCR & SCS_DEMCR_TRCENA)) uart_init(); #endif + if (platform_hwversion() > 0) { + adc_init(); + } else { + gpio_clear(GPIOB, GPIO0); + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0); + } + SCB_VTOR = 0x2000; // Relocate interrupt vector table here cdcacm_init(); @@ -207,7 +217,7 @@ static void morse_update(void) } #ifdef INCLUDE_UART_INTERFACE -void uart_init(void) +static void uart_init(void) { rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN); @@ -239,3 +249,49 @@ void usart1_isr(void) usbd_ep_write_packet(0x83, &c, 1); } #endif + +static void adc_init(void) +{ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN); + + gpio_set_mode(GPIOB, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_ANALOG, GPIO0); + + adc_off(ADC1); + adc_disable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + adc_enable_discontinous_mode_regular(ADC1); + adc_disable_external_trigger_regular(ADC1); + adc_set_right_aligned(ADC1); + adc_set_conversion_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_on(ADC1); + + /* Wait for ADC starting up. */ + for (int i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + adc_calibration(ADC1); +} + +const char *platform_target_voltage(void) +{ + if (platform_hwversion() == 0) + return gpio_get(GPIOB, GPIO0) ? "OK" : "ABSENT!"; + + static char ret[] = "0.0V"; + const u8 channel = 8; + adc_set_regular_sequence(ADC1, 1, (u8*)&channel); + + adc_on(ADC1); + + /* Wait for end of conversion. */ + while (!(ADC_SR(ADC1) & ADC_SR_EOC)); + + u32 val = ADC_DR(ADC1) * 99; /* 0-4095 */ + ret[0] = '0' + val / 81910; + ret[2] = '0' + (val / 8191) % 10; + + return ret; +} diff --git a/src/stm32/platform.h b/src/stm32/platform.h index 075717c5..9598dd85 100644 --- a/src/stm32/platform.h +++ b/src/stm32/platform.h @@ -112,6 +112,7 @@ extern const char *morse_msg; int platform_init(void); void morse(const char *msg, char repeat); +const char *platform_target_voltage(void); /* */ void cdcacm_init(void);