Add vbat_offset to config

Implement vbat_offset command (if defined ENABLE_VBAT_OFFSET_COMMAND)
Reduce code size
This commit is contained in:
DiSlord 2020-03-07 23:37:39 +03:00
parent bb7127fdd0
commit fc6e090595
5 changed files with 103 additions and 96 deletions

121
adc.c
View file

@ -28,80 +28,79 @@
#define ADC_SMPR_SMP_239P5 7U /**< @brief 252 cycles conversion time. */
#define ADC_CFGR1_RES_12BIT (0U << 3U)
#define VNA_ADC ADC1
void adc_init(void)
{
rccEnableADC1(FALSE);
/* Ensure flag states */
ADC1->IER = 0;
VNA_ADC->IER = 0;
/* Calibration procedure.*/
ADC->CCR = 0;
if (ADC1->CR & ADC_CR_ADEN) {
ADC1->CR |= ~ADC_CR_ADDIS; /* Disable ADC */
if (VNA_ADC->CR & ADC_CR_ADEN) {
VNA_ADC->CR |= ~ADC_CR_ADDIS; /* Disable ADC */
}
while (ADC1->CR & ADC_CR_ADEN)
while (VNA_ADC->CR & ADC_CR_ADEN)
;
ADC1->CFGR1 &= ~ADC_CFGR1_DMAEN;
ADC1->CR |= ADC_CR_ADCAL;
while (ADC1->CR & ADC_CR_ADCAL)
VNA_ADC->CFGR1 &= ~ADC_CFGR1_DMAEN;
VNA_ADC->CR |= ADC_CR_ADCAL;
while (VNA_ADC->CR & ADC_CR_ADCAL)
;
if (ADC1->ISR & ADC_ISR_ADRDY) {
ADC1->ISR |= ADC_ISR_ADRDY; /* clear ADRDY */
if (VNA_ADC->ISR & ADC_ISR_ADRDY) {
VNA_ADC->ISR |= ADC_ISR_ADRDY; /* clear ADRDY */
}
/* Enable ADC */
ADC1->CR |= ADC_CR_ADEN;
while (!(ADC1->ISR & ADC_ISR_ADRDY))
VNA_ADC->CR |= ADC_CR_ADEN;
while (!(VNA_ADC->ISR & ADC_ISR_ADRDY))
;
}
uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel)
uint16_t adc_single_read(uint32_t chsel)
{
/* ADC setup */
adc->ISR = adc->ISR;
adc->IER = 0;
adc->TR = ADC_TR(0, 0);
adc->SMPR = ADC_SMPR_SMP_239P5;
adc->CFGR1 = ADC_CFGR1_RES_12BIT;
adc->CHSELR = chsel;
VNA_ADC->ISR = VNA_ADC->ISR;
VNA_ADC->IER = 0;
VNA_ADC->TR = ADC_TR(0, 0);
VNA_ADC->SMPR = ADC_SMPR_SMP_239P5;
VNA_ADC->CFGR1 = ADC_CFGR1_RES_12BIT;
VNA_ADC->CHSELR = chsel;
/* ADC conversion start.*/
adc->CR |= ADC_CR_ADSTART;
VNA_ADC->CR |= ADC_CR_ADSTART;
while (adc->CR & ADC_CR_ADSTART)
while (VNA_ADC->CR & ADC_CR_ADSTART)
;
return adc->DR;
return VNA_ADC->DR;
}
int16_t adc_vbat_read(ADC_TypeDef *adc)
int16_t adc_vbat_read(void)
{
#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);
uint16_t vbat_raw = (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;
adc_stop();
float vbat = 0;
float vrefint = 0;
ADC->CCR |= ADC_CCR_VREFEN | ADC_CCR_VBATEN;
// VREFINT == ADC_IN17
vrefint = adc_single_read(ADC_CHSELR_CHSEL17);
// VBAT == ADC_IN18
// VBATEN enables resiter devider circuit. It consume vbat power.
vbat = adc_single_read(ADC_CHSELR_CHSEL18);
ADC->CCR &= ~(ADC_CCR_VREFEN | ADC_CCR_VBATEN);
touch_start_watchdog();
uint16_t vbat_raw = (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 + config.vbat_offset;
}
void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel)
void adc_start_analog_watchdogd(uint32_t chsel)
{
uint32_t cfgr1;
@ -111,38 +110,38 @@ void adc_start_analog_watchdogd(ADC_TypeDef *adc, uint32_t chsel)
/* ADC setup, if it is defined a callback for the analog watch dog then it
is enabled.*/
adc->ISR = adc->ISR;
adc->IER = ADC_IER_AWDIE;
adc->TR = ADC_TR(0, TOUCH_THRESHOLD);
adc->SMPR = ADC_SMPR_SMP_1P5;
adc->CHSELR = chsel;
VNA_ADC->ISR = VNA_ADC->ISR;
VNA_ADC->IER = ADC_IER_AWDIE;
VNA_ADC->TR = ADC_TR(0, TOUCH_THRESHOLD);
VNA_ADC->SMPR = ADC_SMPR_SMP_1P5;
VNA_ADC->CHSELR = chsel;
/* ADC configuration and start.*/
adc->CFGR1 = cfgr1;
VNA_ADC->CFGR1 = cfgr1;
/* ADC conversion start.*/
adc->CR |= ADC_CR_ADSTART;
VNA_ADC->CR |= ADC_CR_ADSTART;
}
void adc_stop(ADC_TypeDef *adc)
void adc_stop(void)
{
if (adc->CR & ADC_CR_ADEN) {
if (adc->CR & ADC_CR_ADSTART) {
adc->CR |= ADC_CR_ADSTP;
while (adc->CR & ADC_CR_ADSTP)
if (VNA_ADC->CR & ADC_CR_ADEN) {
if (VNA_ADC->CR & ADC_CR_ADSTART) {
VNA_ADC->CR |= ADC_CR_ADSTP;
while (VNA_ADC->CR & ADC_CR_ADSTP)
;
}
/* adc->CR |= ADC_CR_ADDIS;
while (adc->CR & ADC_CR_ADDIS)
/* VNA_ADC->CR |= ADC_CR_ADDIS;
while (VNA_ADC->CR & ADC_CR_ADDIS)
;*/
}
}
void adc_interrupt(ADC_TypeDef *adc)
void adc_interrupt(void)
{
uint32_t isr = adc->ISR;
adc->ISR = isr;
uint32_t isr = VNA_ADC->ISR;
VNA_ADC->ISR = isr;
if (isr & ADC_ISR_OVR) {
/* ADC overflow condition, this could happen only if the DMA is unable
@ -159,7 +158,7 @@ OSAL_IRQ_HANDLER(STM32_ADC1_HANDLER)
{
OSAL_IRQ_PROLOGUE();
adc_interrupt(ADC1);
adc_interrupt();
OSAL_IRQ_EPILOGUE();
}

31
main.c
View file

@ -57,6 +57,7 @@ static char shell_line[VNA_SHELL_MAX_LENGTH];
//#define ENABLED_DUMP
//#define ENABLE_THREADS_COMMAND
//#define ENABLE_TIME_COMMAND
#define ENABLE_VBAT_OFFSET_COMMAND
static void apply_error_term_at(int i);
static void apply_edelay_at(int i);
@ -79,9 +80,7 @@ static int32_t frequency_offset = 5000;
static uint32_t frequency = 10000000;
static int8_t drive_strength = DRIVE_STRENGTH_AUTO;
int8_t sweep_mode = SWEEP_ENABLE;
volatile uint8_t redraw_request = 0; // contains REDRAW_XXX flags
int16_t vbat = 0;
static THD_WORKING_AREA(waThread1, 640);
static THD_FUNCTION(Thread1, arg)
@ -104,19 +103,12 @@ static THD_FUNCTION(Thread1, arg)
ui_process();
if (sweep_mode&SWEEP_ENABLE) {
if (vbat != -1) {
adc_stop(ADC1);
vbat = adc_vbat_read(ADC1);
touch_start_watchdog();
draw_battery_status();
}
// calculate trace coordinates and plot only if scan completed
if (completed) {
if ((domain_mode & DOMAIN_MODE) == DOMAIN_TIME)
transform_domain();
plot_into_index(measured);
redraw_request |= REDRAW_CELLS;
redraw_request |= REDRAW_CELLS|REDRAW_BATTERY;
if (uistat.marker_tracking) {
int i = marker_search();
@ -710,7 +702,8 @@ config_t config = {
.trace_color = { DEFAULT_TRACE_1_COLOR, DEFAULT_TRACE_2_COLOR, DEFAULT_TRACE_3_COLOR, DEFAULT_TRACE_4_COLOR },
// .touch_cal = { 693, 605, 124, 171 }, // 2.4 inch LCD panel
.touch_cal = { 338, 522, 153, 192 }, // 2.8 inch LCD panel
.harmonic_freq_threshold = 300000000
.harmonic_freq_threshold = 300000000,
.vbat_offset = 500
};
properties_t current_props;
@ -1942,9 +1935,20 @@ VNA_SHELL_FUNCTION(cmd_vbat)
{
(void)argc;
(void)argv;
shell_printf("%d mV\r\n", vbat);
shell_printf("%d mV\r\n", adc_vbat_read());
}
#ifdef ENABLE_VBAT_OFFSET_COMMAND
VNA_SHELL_FUNCTION(cmd_vbat_offset)
{
if (argc != 1) {
shell_printf("%d\r\n", config.vbat_offset);
return;
}
config.vbat_offset = (int16_t)my_atoi(argv[0]);
}
#endif
#ifdef ENABLE_THREADS_COMMAND
#if CH_CFG_USE_REGISTRY == FALSE
#error "Threads Requite enabled CH_CFG_USE_REGISTRY in chconf.h"
@ -2028,6 +2032,9 @@ static const VNAShellCommand commands[] =
{"edelay" , cmd_edelay , 0},
{"capture" , cmd_capture , CMD_WAIT_MUTEX},
{"vbat" , cmd_vbat , 0},
#ifdef ENABLE_VBAT_OFFSET_COMMAND
{"vbat_offset" , cmd_vbat_offset , 0},
#endif
{"transform" , cmd_transform , 0},
{"threshold" , cmd_threshold , 0},
{"help" , cmd_help , 0},

View file

@ -218,8 +218,8 @@ typedef struct config {
int16_t touch_cal[4];
int8_t reserved_1;
uint32_t harmonic_freq_threshold;
uint8_t _reserved[24];
uint16_t vbat_offset;
uint8_t _reserved[22];
uint32_t checksum;
} config_t;
@ -234,7 +234,6 @@ void set_trace_refpos(int t, float refpos);
float get_trace_scale(int t);
float get_trace_refpos(int t);
const char *get_trace_typename(int t);
void draw_battery_status(void);
void set_electrical_delay(float picoseconds);
float get_electrical_delay(void);
@ -282,10 +281,9 @@ int marker_search_right(int from);
#define REDRAW_FREQUENCY (1<<1)
#define REDRAW_CAL_STATUS (1<<2)
#define REDRAW_MARKER (1<<3)
#define REDRAW_BATTERY (1<<4)
extern volatile uint8_t redraw_request;
extern int16_t vbat;
/*
* ili9341.c
*/
@ -468,11 +466,11 @@ void enter_dfu(void);
*/
void adc_init(void);
uint16_t adc_single_read(ADC_TypeDef *adc, uint32_t chsel);
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);
uint16_t adc_single_read(uint32_t chsel);
void adc_start_analog_watchdogd(uint32_t chsel);
void adc_stop(void);
void adc_interrupt(void);
int16_t adc_vbat_read(void);
/*
* misclinous

9
plot.c
View file

@ -6,6 +6,7 @@
#include "nanovna.h"
static void cell_draw_marker_info(int x0, int y0);
static void draw_battery_status(void);
int16_t grid_offset;
int16_t grid_width;
@ -1388,6 +1389,8 @@ draw_all(bool flush)
draw_frequencies();
if (redraw_request & REDRAW_CAL_STATUS)
draw_cal_status();
if (redraw_request & REDRAW_BATTERY)
draw_battery_status();
redraw_request = 0;
}
@ -1661,10 +1664,10 @@ draw_cal_status(void)
#define BATTERY_BOTTOM_LEVEL 3100
#define BATTERY_WARNING_LEVEL 3300
void
draw_battery_status(void)
static void draw_battery_status(void)
{
if (vbat<=0)
int16_t vbat = adc_vbat_read();
if (vbat <= 0)
return;
uint8_t string_buf[16];
// Set battery color

20
ui.c
View file

@ -212,7 +212,7 @@ touch_measure_y(void)
palSetPad(GPIOA, 6);
chThdSleepMilliseconds(2);
v = adc_single_read(ADC1, ADC_CHSELR_CHSEL7);
v = adc_single_read(ADC_CHSELR_CHSEL7);
//chThdSleepMilliseconds(2);
//v += adc_single_read(ADC1, ADC_CHSELR_CHSEL7);
return v;
@ -232,7 +232,7 @@ touch_measure_x(void)
palClearPad(GPIOA, 7);
chThdSleepMilliseconds(2);
v = adc_single_read(ADC1, ADC_CHSELR_CHSEL6);
v = adc_single_read(ADC_CHSELR_CHSEL6);
//chThdSleepMilliseconds(2);
//v += adc_single_read(ADC1, ADC_CHSELR_CHSEL6);
return v;
@ -255,14 +255,14 @@ void
touch_start_watchdog(void)
{
touch_prepare_sense();
adc_start_analog_watchdogd(ADC1, ADC_CHSELR_CHSEL7);
adc_start_analog_watchdogd(ADC_CHSELR_CHSEL7);
}
static int
touch_status(void)
{
touch_prepare_sense();
return adc_single_read(ADC1, ADC_CHSELR_CHSEL7) > TOUCH_THRESHOLD;
return adc_single_read(ADC_CHSELR_CHSEL7) > TOUCH_THRESHOLD;
}
static int
@ -302,7 +302,7 @@ touch_cal_exec(void)
{
int x1, x2, y1, y2;
adc_stop(ADC1);
adc_stop();
setForegroundColor(DEFAULT_FG_COLOR);
setBackgroundColor(DEFAULT_BG_COLOR);
clearScreen();
@ -338,7 +338,7 @@ touch_draw_test(void)
int x0, y0;
int x1, y1;
adc_stop(ADC1);
adc_stop();
setForegroundColor(DEFAULT_FG_COLOR);
setBackgroundColor(DEFAULT_BG_COLOR);
@ -372,7 +372,7 @@ void
show_version(void)
{
int x = 5, y = 5;
adc_stop(ADC1);
adc_stop();
setForegroundColor(DEFAULT_FG_COLOR);
setBackgroundColor(DEFAULT_BG_COLOR);
@ -404,7 +404,7 @@ show_version(void)
void
enter_dfu(void)
{
adc_stop(ADC1);
adc_stop();
int x = 5, y = 5;
setForegroundColor(DEFAULT_FG_COLOR);
@ -2003,7 +2003,7 @@ static void
ui_process_keypad(void)
{
int status;
adc_stop(ADC1);
adc_stop();
kp_index = 0;
while (TRUE) {
@ -2145,7 +2145,7 @@ static
void ui_process_touch(void)
{
// awd_count++;
adc_stop(ADC1);
adc_stop();
int status = touch_check();
if (status == EVT_TOUCH_PRESSED || status == EVT_TOUCH_DOWN) {