diff --git a/src/platforms/f3/README.md b/src/platforms/f3/README.md index 04ff09fc0f2..48f0f58d346 100644 --- a/src/platforms/f3/README.md +++ b/src/platforms/f3/README.md @@ -7,9 +7,10 @@ For simpicity, the ST system bootloader is used. This saves additional 4 kB for ## Connections * PA2: UART RX -* PS3: UART TX -* PA0: TDI +* PA3: UART TX +* PA0: VTref * PA1: TMS/SWDIO +* PA4: TDI * PA7: TCK/SWCLK * PA6: TDO/TRACESWO * PA5: TRST diff --git a/src/platforms/f3/platform.c b/src/platforms/f3/platform.c index 3c13a242f63..fe7be4b2368 100644 --- a/src/platforms/f3/platform.c +++ b/src/platforms/f3/platform.c @@ -29,10 +29,13 @@ #include #include #include +#include #include #include #include +static void adc_init(void); + extern uint32_t _ebss; // NOLINT(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) int platform_hwversion(void) @@ -94,6 +97,9 @@ void platform_init(void) gpio_mode_setup(NRST_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, NRST_PIN); gpio_set(NRST_PORT, NRST_PIN); gpio_set_output_options(NRST_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, NRST_PIN); + + adc_init(); + platform_timing_init(); /* Set up USB pins and alternate function */ gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); @@ -112,9 +118,71 @@ bool platform_nrst_get_val(void) return (gpio_get(NRST_PORT, NRST_PIN)) ? false : true; } + +static void adc_init(void) +{ + + //ADC + rcc_periph_clock_enable(RCC_ADC12); + //ADC + gpio_mode_setup(VTREF_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, VTREF_PIN); + //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); + adc_power_off(ADC1); + adc_set_clk_prescale(ADC1, ADC_CCR_CKMODE_DIV2); + adc_set_single_conversion_mode(ADC1); + adc_disable_external_trigger_regular(ADC1); + adc_set_right_aligned(ADC1); + /* We want to read the temperature sensor, so we have to enable it. */ + //adc_enable_temperature_sensor(); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_601DOT5CYC); + uint8_t channel_array[] = { 1 }; /* ADC1_IN1 (PA0) */ + adc_calibrate(ADC1); + adc_set_regular_sequence(ADC1, 1, channel_array); + adc_set_resolution(ADC1, ADC_CFGR1_RES_12_BIT); + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + int i; + for (i = 0; i < 800000; i++) + __asm__("nop"); + + + +} + + +uint32_t platform_target_voltage_sense(void) +{ + /* + * Returns the voltage in tenths of a volt (so 33 means 3.3V) + */ + adc_start_conversion_regular(ADC1); + while (!(adc_eoc(ADC1))) + continue; + uint32_t val=adc_read_regular(ADC1); + return (val * 99U) / 8191U;; + //return val; + + return 0; +} + + + const char *platform_target_voltage(void) { - return "ABSENT!"; + static char ret[] = "0.0V"; + uint32_t val = platform_target_voltage_sense(); + ret[0] = '0' + val / 10U; + ret[2] = '0' + val % 10U; + + /*static char ret[] = "0000000000"; + uint32_t val = platform_target_voltage_sense(); + for(uint8_t i = 10; i > 0; i--){ + ret[i-1] = '0' + val % 10U; + val = val / 10; + }*/ + + return ret; } #pragma GCC diagnostic push diff --git a/src/platforms/f3/platform.h b/src/platforms/f3/platform.h index 3344f2b46f0..a50a22de325 100644 --- a/src/platforms/f3/platform.h +++ b/src/platforms/f3/platform.h @@ -37,8 +37,9 @@ * LED1 = PB6 (Orange LED : Idle) * LED2 = PB7 (Red LED : Error) * - * TDI = PA0 + * VTref = PA0 * TMS = PA1 (input for SWDP) + * TDI = PA4 * TCK = PA7/SWCLK * TDO = PA6 (input for TRACESWO * nRST = PA5 @@ -48,11 +49,13 @@ /* Hardware definitions... */ #define JTAG_PORT GPIOA +#define VTREF_PORT JTAG_PORT #define TDI_PORT JTAG_PORT #define TMS_PORT JTAG_PORT #define TCK_PORT JTAG_PORT #define TDO_PORT JTAG_PORT -#define TDI_PIN GPIO0 +#define VTREF_PIN GPIO0 +#define TDI_PIN GPIO4 #define TMS_PIN GPIO1 #define TCK_PIN GPIO7 #define TDO_PIN GPIO6