From 5bc0bded7436b5324582a7fe914a35082dcd96dc Mon Sep 17 00:00:00 2001 From: qrp73 Date: Wed, 23 Oct 2019 20:17:34 +0300 Subject: [PATCH] battery live update --- adc.c | 63 +++++++++++++++++++++++++------------- nanovna.h | 2 ++ ui.c | 90 ++++++++++++++++++++++++++++++++----------------------- 3 files changed, 97 insertions(+), 58 deletions(-) diff --git a/adc.c b/adc.c index 631d7203..5c3d9011 100644 --- a/adc.c +++ b/adc.c @@ -75,30 +75,51 @@ uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel) return adc->DR; } + +#define ADCFILTER 32 +uint16_t adc_single_read_filtered(ADC_TypeDef *adc, uint32_t chsel) +{ + uint32_t value = 0; + for (int i=0; i < ADCFILTER; i++) + value += adc_single_read(adc, chsel); + value /= ADCFILTER; + return (uint16_t)value; +} + + +#define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2)) +#define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8)) +#define VDD_CALIB ((uint16_t) (330)) +#define VDD_APPLI ((uint16_t) (300)) +#define VREFINT_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7BA)) + int16_t adc_vbat_read(ADC_TypeDef *adc) { -#define ADC_FULL_SCALE 3300 -#define VBAT_DIODE_VF 500 -#define VREFINT_CAL (*((uint16_t*)0x1FFFF7BA)) - float vbat = 0; - float vrefint = 0; - - ADC->CCR |= ADC_CCR_VREFEN | ADC_CCR_VBATEN; - // VREFINT == ADC_IN17 - vrefint = adc_single_read(adc, ADC_CHSELR_CHSEL17); - // VBAT == ADC_IN18 - // VBATEN enables resiter devider circuit. It consume vbat power. - vbat = adc_single_read(adc, ADC_CHSELR_CHSEL18); - ADC->CCR &= ~(ADC_CCR_VREFEN | ADC_CCR_VBATEN); - - uint32_t vbat_raw = (uint32_t)(ADC_FULL_SCALE * VREFINT_CAL * vbat * 2 / (vrefint * ((1<<12)-1))); -// if (vbat_raw < 100) { -// // maybe D2 is not installed -// return -1; -// } - - return vbat_raw + VBAT_DIODE_VF; + // VREFINT == ADC_IN17 + // VBAT == ADC_IN18 + // VBATEN enables resiter devider circuit. It consume vbat power. + ADC->CCR |= ADC_CCR_VREFEN | ADC_CCR_VBATEN; + int32_t adc_ref = adc_single_read_filtered(adc, ADC_CHSELR_CHSEL17); + int32_t adc_bat = adc_single_read_filtered(adc, ADC_CHSELR_CHSEL18); + ADC->CCR &= ~(ADC_CCR_VREFEN | ADC_CCR_VBATEN); + + int16_t vbat_raw = (int16_t)((2 * 3300 * (int64_t)(*VREFINT_CAL_ADDR) * adc_bat) / ((int64_t)adc_ref * ((1<<12)-1))); + return vbat_raw + 500; +} +int16_t adc_tjun_read(ADC_TypeDef *adc) +{ + // TJUN == ADC_IN16 + ADC->CCR |= ADC_CCR_VREFEN | ADC_CCR_TSEN; + int32_t adc_ref = adc_single_read_filtered(adc, ADC_CHSELR_CHSEL17); + int32_t adc_t = adc_single_read_filtered(adc, ADC_CHSELR_CHSEL16); + ADC->CCR &= ~(ADC_CCR_VREFEN | ADC_CCR_TSEN); + + int32_t t = ((adc_t * (*VREFINT_CAL_ADDR)) / adc_ref) - (int32_t) *TEMP30_CAL_ADDR; + t *= (int32_t)(110 - 30); + t = t / (int32_t)(*TEMP110_CAL_ADDR - *TEMP30_CAL_ADDR); + t += 30; + return (int16_t)t; } void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel) diff --git a/nanovna.h b/nanovna.h index af4a33dc..1bfb8e4e 100644 --- a/nanovna.h +++ b/nanovna.h @@ -397,6 +397,8 @@ void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel); void adc_stop(ADC_TypeDef *adc); void adc_interrupt(ADC_TypeDef *adc); int16_t adc_vbat_read(ADC_TypeDef *adc); +int16_t adc_tjun_read(ADC_TypeDef *adc); + /* * misclinous diff --git a/ui.c b/ui.c index a7f739d0..c4f98bde 100644 --- a/ui.c +++ b/ui.c @@ -401,48 +401,64 @@ touch_position(int *x, int *y) void show_version(void) { - int x = 5, y = 5; + int x = 5, y = 5; - adc_stop(ADC1); - ili9341_fill(0, 0, 320, 240, 0); + adc_stop(ADC1); + ili9341_fill(0, 0, 320, 240, 0); - ili9341_drawstring_size(BOARD_NAME, x, y, 0xffff, 0x0000, 4); - y += 25; + ili9341_drawstring_size(BOARD_NAME, x, y, 0xffff, 0x0000, 4); + y += 25; - ili9341_drawstring_5x7("https://github.com/qrp73/NanoVNA-Q", x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("", x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("https://github.com/qrp73/NanoVNA-Q", x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("", x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("Original code 2016-2019 Copyright @edy555", x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("Licensed under GPL. See: https://github.com/ttrftech/NanoVNA", x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("", x, y += 10, 0xffff, 0x0000); - - ili9341_drawstring_5x7("Version: " VERSION, x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("Build Time: " __DATE__ " - " __TIME__, x, y += 10, 0xffff, 0x0000); - y += 10; - ili9341_drawstring_5x7("Kernel: " CH_KERNEL_VERSION, x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("Compiler: " PORT_COMPILER_NAME, x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME, x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("Port Info: " PORT_INFO, x, y += 10, 0xffff, 0x0000); - ili9341_drawstring_5x7("Platform: " PLATFORM_NAME, x, y += 10, 0xffff, 0x0000); - y+= 7*3; - - ili9341_drawstring_5x7("Battery:", x, y, 0xffff, 0x0000); - x += 5*9; - char buf[8]; - int len = chsnprintf(buf, sizeof buf, "%d", vbat); - ili9341_drawstring_5x7(buf, x, y, 0xffff, 0x0000); - x += len*5 + 5; - ili9341_drawstring_5x7("mV", x, y, 0xffff, 0x0000); - - - while (true) { - if (touch_check() == EVT_TOUCH_PRESSED) - break; - if (btn_check() & EVT_BUTTON_SINGLE_CLICK) - break; - } + ili9341_drawstring_5x7("Original code 2016-2019 Copyright @edy555", x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Licensed under GPL. See: https://github.com/ttrftech/NanoVNA", x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("", x, y += 10, 0xffff, 0x0000); + + ili9341_drawstring_5x7("Version: " VERSION, x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Build Time: " __DATE__ " - " __TIME__, x, y += 10, 0xffff, 0x0000); + y += 10; + ili9341_drawstring_5x7("Kernel: " CH_KERNEL_VERSION, x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Compiler: " PORT_COMPILER_NAME, x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Architecture: " PORT_ARCHITECTURE_NAME " Core Variant: " PORT_CORE_VARIANT_NAME, x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Port Info: " PORT_INFO, x, y += 10, 0xffff, 0x0000); + ili9341_drawstring_5x7("Platform: " PLATFORM_NAME, x, y += 10, 0xffff, 0x0000); + y+= 7*3; + + int sy = y; + ili9341_drawstring_5x7("VBAT:", x, y, 0xffff, 0x0000); + y += 10; + ili9341_drawstring_5x7("Tjun:", x, y, 0xffff, 0x0000); + + char buf[8]; + int len; + while (true) { + vbat = adc_vbat_read(ADC1); + int16_t tjun = adc_tjun_read(ADC1); + + y = sy; + x = 5 + 5*9; + len = chsnprintf(buf, sizeof buf, "%d", vbat); + ili9341_drawstring_5x7(buf, x, y, 0xffff, 0x0000); + x += len*5; + ili9341_drawstring_5x7(" mV ", x, y, 0xffff, 0x0000); + y += 10; + x = 5 + 5*9; + len = chsnprintf(buf, sizeof buf, "%d", tjun); + ili9341_drawstring_5x7(buf, x, y, 0xffff, 0x0000); + x += len*5; + ili9341_drawstring_5x7(" C ", x, y, 0xffff, 0x0000); + y += 10; + + if (touch_check() == EVT_TOUCH_PRESSED) + break; + if (btn_check() & EVT_BUTTON_SINGLE_CLICK) + break; + chThdSleepMilliseconds(100); + } - touch_start_watchdog(); + touch_start_watchdog(); } void