diff --git a/src/platforms/swlink/platform.c b/src/platforms/swlink/platform.c index 71f8c9b..42dfd0b 100644 --- a/src/platforms/swlink/platform.c +++ b/src/platforms/swlink/platform.c @@ -39,6 +39,8 @@ uint32_t led_error_port; uint16_t led_error_pin; static uint8_t rev; +static void adc_init(void); + int platform_hwversion(void) { return rev; @@ -80,6 +82,7 @@ void platform_init(void) /* LED GPIO already set in detect_rev()*/ led_error_port = GPIOA; led_error_pin = GPIO8; + adc_init(); break; case 1: led_error_port = GPIOC; @@ -137,9 +140,47 @@ bool platform_srst_get_val(void) return gpio_get(JRST_PORT, JRST_PIN) == 0; } +static void adc_init(void) +{ + rcc_periph_clock_enable(RCC_ADC1); + /* PA0 measures CN7 Pin 1 VDD divided by two.*/ + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_ANALOG, GPIO0); + adc_power_off(ADC1); + adc_disable_scan_mode(ADC1); + adc_set_single_conversion_mode(ADC1); + adc_disable_external_trigger_regular(ADC1); + adc_set_right_aligned(ADC1); + adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); + + adc_power_on(ADC1); + + /* Wait for ADC starting up. */ + for (int i = 0; i < 800000; i++) /* Wait a bit. */ + __asm__("nop"); + + adc_reset_calibration(ADC1); + adc_calibrate(ADC1); +} + const char *platform_target_voltage(void) { - return "unknown"; + static char ret[] = "0.0V"; + const uint8_t channel = 0; + switch (rev) { + case 0: + adc_set_regular_sequence(ADC1, 1, (uint8_t*)&channel); + adc_start_conversion_direct(ADC1); + /* Wait for end of conversion. */ + while (!adc_eoc(ADC1)); + /* Referencevoltage is 3.3 Volt, measured voltage is half of + * actual voltag. */ + uint32_t val_in_100mV = (adc_read_regular(ADC1) * 33 * 2) / 4096; + ret[0] = '0' + val_in_100mV / 10; + ret[2] = '0' + val_in_100mV % 10; + return ret; + } + return "ABSENT!"; } void set_idle_state(int state)