mirror of
https://github.com/ttrftech/NanoVNA.git
synced 2025-12-06 03:31:59 +01:00
More comments
Rewrite flash.c for less size Add cache for check checksum (more faster interpolate, used in multi segments sweep) More fixes for font and select size of marker icon More debug functions
This commit is contained in:
parent
6b25d86128
commit
f90b920d5e
145
flash.c
145
flash.c
|
|
@ -22,7 +22,8 @@
|
|||
#include "nanovna.h"
|
||||
#include <string.h>
|
||||
|
||||
int16_t lastsaveid = 0;
|
||||
uint16_t lastsaveid = 0;
|
||||
static uint32_t checksum_ok = 0;
|
||||
|
||||
static int flash_wait_for_last_operation(void)
|
||||
{
|
||||
|
|
@ -42,80 +43,71 @@ static void flash_erase_page0(uint32_t page_address)
|
|||
FLASH->CR &= ~FLASH_CR_PER;
|
||||
}
|
||||
|
||||
int flash_erase_page(uint32_t page_address)
|
||||
static void flash_erase_page(uint32_t page_address)
|
||||
{
|
||||
chSysLock();
|
||||
flash_erase_page0(page_address);
|
||||
chSysUnlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void flash_program_half_word(uint32_t address, uint16_t data)
|
||||
{
|
||||
flash_wait_for_last_operation();
|
||||
FLASH->CR |= FLASH_CR_PG;
|
||||
*(__IO uint16_t*)address = data;
|
||||
flash_wait_for_last_operation();
|
||||
FLASH->CR &= ~FLASH_CR_PG;
|
||||
}
|
||||
|
||||
void flash_unlock(void)
|
||||
static inline void flash_unlock(void)
|
||||
{
|
||||
// unlock sequence
|
||||
FLASH->KEYR = 0x45670123;
|
||||
FLASH->KEYR = 0xCDEF89AB;
|
||||
}
|
||||
|
||||
static void flash_program_half_word_buffer(uint16_t* dst, uint16_t *data, uint16_t size)
|
||||
{
|
||||
uint32_t i;
|
||||
flash_unlock();
|
||||
// erase flash pages for buffer (aligned to FLASH_PAGESIZE)
|
||||
for (i = 0; i < size; i+=FLASH_PAGESIZE)
|
||||
flash_erase_page((uint32_t)dst + i);
|
||||
// Save buffer
|
||||
__IO uint16_t* p = dst;
|
||||
for (i = 0; i < size/sizeof(uint16_t); i++){
|
||||
flash_wait_for_last_operation();
|
||||
FLASH->CR |= FLASH_CR_PG;
|
||||
p[i] = data[i];
|
||||
flash_wait_for_last_operation();
|
||||
FLASH->CR &= ~FLASH_CR_PG;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
checksum(const void *start, size_t len)
|
||||
{
|
||||
uint32_t *p = (uint32_t*)start;
|
||||
uint32_t *tail = (uint32_t*)(start + len);
|
||||
uint32_t value = 0;
|
||||
while (p < tail)
|
||||
// align by sizeof(uint32_t)
|
||||
len = (len + sizeof(uint32_t)-1)/sizeof(uint32_t);
|
||||
while (len-- > 0)
|
||||
value = __ROR(value, 31) + *p++;
|
||||
return value;
|
||||
}
|
||||
|
||||
const uint32_t save_config_area = SAVE_CONFIG_ADDR;
|
||||
|
||||
int
|
||||
config_save(void)
|
||||
{
|
||||
uint16_t *src = (uint16_t*)&config;
|
||||
uint16_t *dst = (uint16_t*)save_config_area;
|
||||
int count = sizeof(config_t) / sizeof(uint16_t);
|
||||
|
||||
// Apply magic word and calculate checksum
|
||||
config.magic = CONFIG_MAGIC;
|
||||
config.checksum = checksum(&config, sizeof config - sizeof config.checksum);
|
||||
|
||||
flash_unlock();
|
||||
|
||||
/* erase flash pages */
|
||||
flash_erase_page((uint32_t)dst);
|
||||
|
||||
/* write to flahs */
|
||||
while (count-- > 0) {
|
||||
flash_program_half_word((uint32_t)dst, *src++);
|
||||
dst++;
|
||||
}
|
||||
|
||||
// write to flash
|
||||
flash_program_half_word_buffer((uint16_t*)SAVE_CONFIG_ADDR, (uint16_t*)&config, sizeof(config_t));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
config_recall(void)
|
||||
{
|
||||
const config_t *src = (const config_t*)save_config_area;
|
||||
void *dst = &config;
|
||||
const config_t *src = (const config_t*)SAVE_CONFIG_ADDR;
|
||||
|
||||
if (src->magic != CONFIG_MAGIC)
|
||||
if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
||||
return -1;
|
||||
if (checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
||||
return -1;
|
||||
|
||||
/* duplicated saved data onto sram to be able to modify marker/trace */
|
||||
memcpy(dst, src, sizeof(config_t));
|
||||
// duplicated saved data onto sram to be able to modify marker/trace
|
||||
memcpy(&config, src, sizeof(config_t));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -124,33 +116,16 @@ caldata_save(uint32_t id)
|
|||
{
|
||||
if (id >= SAVEAREA_MAX)
|
||||
return -1;
|
||||
uint16_t *src = (uint16_t*)¤t_props;
|
||||
uint16_t *dst;
|
||||
int count = sizeof(properties_t) / sizeof(uint16_t);
|
||||
|
||||
dst = (uint16_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
||||
|
||||
// Apply magic word and calculate checksum
|
||||
current_props.magic = CONFIG_MAGIC;
|
||||
current_props.checksum = checksum(
|
||||
¤t_props, sizeof current_props - sizeof current_props.checksum);
|
||||
current_props.checksum = checksum(¤t_props, sizeof current_props - sizeof current_props.checksum);
|
||||
|
||||
flash_unlock();
|
||||
// write to flash
|
||||
uint16_t *dst = (uint16_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
||||
flash_program_half_word_buffer(dst, (uint16_t*)¤t_props, sizeof(properties_t));
|
||||
|
||||
/* erase flash pages */
|
||||
void *p = dst;
|
||||
void *tail = p + sizeof(properties_t);
|
||||
while (p < tail) {
|
||||
flash_erase_page((uint32_t)p);
|
||||
p += FLASH_PAGESIZE;
|
||||
}
|
||||
|
||||
/* write to flahs */
|
||||
while (count-- > 0) {
|
||||
flash_program_half_word((uint32_t)dst, *src++);
|
||||
dst++;
|
||||
}
|
||||
|
||||
/* after saving data, make active configuration points to flash */
|
||||
// after saving data, make active configuration points to flash
|
||||
active_props = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
||||
lastsaveid = id;
|
||||
|
||||
|
|
@ -162,56 +137,50 @@ caldata_recall(uint32_t id)
|
|||
{
|
||||
if (id >= SAVEAREA_MAX)
|
||||
return -1;
|
||||
properties_t *src;
|
||||
void *dst = ¤t_props;
|
||||
|
||||
// point to saved area on the flash memory
|
||||
src = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
||||
properties_t *src = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
||||
|
||||
if (src->magic != CONFIG_MAGIC)
|
||||
goto load_default;
|
||||
if (checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
||||
if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
||||
goto load_default;
|
||||
|
||||
/* active configuration points to save data on flash memory */
|
||||
// active configuration points to save data on flash memory
|
||||
active_props = src;
|
||||
lastsaveid = id;
|
||||
|
||||
/* duplicated saved data onto sram to be able to modify marker/trace */
|
||||
memcpy(dst, src, sizeof(properties_t));
|
||||
// duplicated saved data onto sram to be able to modify marker/trace
|
||||
memcpy(¤t_props, src, sizeof(properties_t));
|
||||
return 0;
|
||||
load_default:
|
||||
load_default_properties();
|
||||
return -1;
|
||||
return lastsaveid;
|
||||
}
|
||||
|
||||
// Used in interpolate
|
||||
const properties_t *
|
||||
caldata_ref(uint32_t id)
|
||||
caldata_reference(void)
|
||||
{
|
||||
if (id >= SAVEAREA_MAX)
|
||||
if (lastsaveid >= SAVEAREA_MAX)
|
||||
return NULL;
|
||||
const properties_t *src;
|
||||
|
||||
src = (const properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
||||
|
||||
if (src->magic != CONFIG_MAGIC)
|
||||
return NULL;
|
||||
if (checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
||||
src = (const properties_t*)(SAVE_PROP_CONFIG_ADDR + lastsaveid * SAVE_PROP_CONFIG_SIZE);
|
||||
// Check crc cache mask (made it only 1 time)
|
||||
if (checksum_ok&(1<<lastsaveid))
|
||||
return src;
|
||||
if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
||||
return NULL;
|
||||
checksum_ok|=1<<lastsaveid;
|
||||
return src;
|
||||
}
|
||||
|
||||
void
|
||||
clear_all_config_prop_data(void)
|
||||
{
|
||||
uint32_t i;
|
||||
flash_unlock();
|
||||
|
||||
/* erase flash pages */
|
||||
void *p = (void*)save_config_area;
|
||||
void *tail = p + SAVE_FULL_AREA_SIZE;
|
||||
while (p < tail) {
|
||||
flash_erase_page((uint32_t)p);
|
||||
p += FLASH_PAGESIZE;
|
||||
}
|
||||
// erase flash pages
|
||||
for (i = 0; i < SAVE_FULL_AREA_SIZE; i+=FLASH_PAGESIZE)
|
||||
flash_erase_page(SAVE_CONFIG_ADDR + i);
|
||||
}
|
||||
|
||||
|
|
|
|||
34
main.c
34
main.c
|
|
@ -83,13 +83,15 @@ static volatile vna_shellcmd_t shell_function = 0;
|
|||
//#define ENABLE_PORT_COMMAND
|
||||
// Enable si5351 timing command, used for debug
|
||||
#define ENABLE_SI5351_TIMINGS
|
||||
// Enable i2c timing command, used for debug
|
||||
#define ENABLE_I2C_TIMINGS
|
||||
|
||||
static void apply_CH0_error_term_at(int i);
|
||||
static void apply_CH1_error_term_at(int i);
|
||||
static void apply_edelay(void);
|
||||
|
||||
static uint16_t get_sweep_mode(void);
|
||||
static void cal_interpolate(int s);
|
||||
static void cal_interpolate(void);
|
||||
static void update_frequencies(bool interpolate);
|
||||
static void set_frequencies(uint32_t start, uint32_t stop, uint16_t points);
|
||||
static bool sweep(bool break_on_operation, uint16_t sweep_mode);
|
||||
|
|
@ -985,7 +987,7 @@ VNA_SHELL_FUNCTION(cmd_scan)
|
|||
}
|
||||
set_frequencies(start, stop, points);
|
||||
if (cal_status & CALSTAT_APPLY)
|
||||
cal_interpolate(lastsaveid);
|
||||
cal_interpolate();
|
||||
pause_sweep();
|
||||
sweep(false, sweep_mode);
|
||||
// Output data after if set (faster data recive)
|
||||
|
|
@ -1062,7 +1064,7 @@ update_frequencies(bool interpolate)
|
|||
// set grid layout
|
||||
update_grid();
|
||||
if (interpolate)
|
||||
cal_interpolate(lastsaveid);
|
||||
cal_interpolate();
|
||||
RESET_SWEEP;
|
||||
}
|
||||
|
||||
|
|
@ -1510,9 +1512,9 @@ cal_done(void)
|
|||
}
|
||||
|
||||
static void
|
||||
cal_interpolate(int s)
|
||||
cal_interpolate(void)
|
||||
{
|
||||
const properties_t *src = caldata_ref(s);
|
||||
const properties_t *src = caldata_reference();
|
||||
uint32_t i, j;
|
||||
int eterm;
|
||||
if (src == NULL)
|
||||
|
|
@ -2174,8 +2176,17 @@ VNA_SHELL_FUNCTION(cmd_vbat_offset)
|
|||
VNA_SHELL_FUNCTION(cmd_si5351time)
|
||||
{
|
||||
(void)argc;
|
||||
// si5351_set_timing(my_atoui(argv[0]), my_atoui(argv[1]));
|
||||
int idx = my_atoui(argv[0]);
|
||||
uint16_t value = my_atoui(argv[1]);
|
||||
si5351_set_timing(idx, value);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef ENABLE_I2C_TIMINGS
|
||||
VNA_SHELL_FUNCTION(cmd_i2ctime)
|
||||
{
|
||||
(void)argc;
|
||||
uint32_t tim = STM32_TIMINGR_PRESC(0U) |
|
||||
STM32_TIMINGR_SCLDEL(my_atoui(argv[0])) | STM32_TIMINGR_SDADEL(my_atoui(argv[1])) |
|
||||
STM32_TIMINGR_SCLH(my_atoui(argv[2])) | STM32_TIMINGR_SCLL(my_atoui(argv[3]));
|
||||
|
|
@ -2384,6 +2395,9 @@ static const VNAShellCommand commands[] =
|
|||
#endif
|
||||
#ifdef ENABLE_SI5351_TIMINGS
|
||||
{"t" , cmd_si5351time , CMD_WAIT_MUTEX},
|
||||
#endif
|
||||
#ifdef ENABLE_I2C_TIMINGS
|
||||
{"i" , cmd_i2ctime , CMD_WAIT_MUTEX},
|
||||
#endif
|
||||
{NULL , NULL , 0}
|
||||
};
|
||||
|
|
@ -2531,10 +2545,10 @@ static const I2CConfig i2ccfg = {
|
|||
// STM32_TIMINGR_PRESC(5U) |
|
||||
// STM32_TIMINGR_SCLDEL(3U) | STM32_TIMINGR_SDADEL(3U) |
|
||||
// STM32_TIMINGR_SCLH(3U) | STM32_TIMINGR_SCLL(9U),
|
||||
// 600kHz @ SYSCLK 48MHz, manually get values, x1.5 I2C speed, but need calc timings
|
||||
STM32_TIMINGR_PRESC(3U) |
|
||||
STM32_TIMINGR_SCLDEL(2U) | STM32_TIMINGR_SDADEL(2U) |
|
||||
STM32_TIMINGR_SCLH(4U) | STM32_TIMINGR_SCLL(4U),
|
||||
// 600kHz @ SYSCLK 48MHz, manually get values, x1.5 I2C speed
|
||||
STM32_TIMINGR_PRESC(0U) |
|
||||
STM32_TIMINGR_SCLDEL(10U) | STM32_TIMINGR_SDADEL(10U) |
|
||||
STM32_TIMINGR_SCLH(30U) | STM32_TIMINGR_SCLL(50U),
|
||||
#else
|
||||
#error "Need Define STM32_I2C1SW and set correct TIMINGR settings"
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -52,7 +52,6 @@
|
|||
#define STM32_ADCSW STM32_ADCSW_HSI14
|
||||
#define STM32_ADCPRE STM32_ADCPRE_DIV4
|
||||
#define STM32_MCOSEL STM32_MCOSEL_PLLDIV2
|
||||
#define STM32_ADCPRE STM32_ADCPRE_DIV4
|
||||
#define STM32_ADCSW STM32_ADCSW_HSI14
|
||||
#define STM32_USBSW STM32_USBSW_HSI48
|
||||
#define STM32_CECSW STM32_CECSW_HSI
|
||||
|
|
|
|||
17
nanovna.h
17
nanovna.h
|
|
@ -41,8 +41,7 @@
|
|||
#define FREQUENCY_OFFSET 8000
|
||||
// Frequency offset for 48k ADC (sin_cos table in dsp.c generated for 3k, 4k, 5k, 6k, if change need create new table )
|
||||
//#define FREQUENCY_OFFSET 5000
|
||||
// Apply calibration after made sweep, if set to 1, calibration move out from sweep cycle
|
||||
// On fast CPU it slow down sweep, on slow CPU can made faster (not need wait additional measure time)
|
||||
// Apply calibration after made sweep, (if set 1, then calibration move out from sweep cycle)
|
||||
#define APPLY_CALIBRATION_AFTER_SWEEP 0
|
||||
// Use real time build table (undef for use constant)
|
||||
//#define USE_VARIABLE_OFFSET
|
||||
|
|
@ -131,8 +130,9 @@ extern const char *info_about[];
|
|||
/*
|
||||
* dsp.c
|
||||
*/
|
||||
// Define aic3204 source clock frequency (for 8MHz used fractional multiplier, and possible little phase error)
|
||||
// Define aic3204 source clock frequency (on 8MHz used fractional multiplier, and possible little phase error)
|
||||
#define AUDIO_CLOCK_REF ( 8000000U)
|
||||
// Define aic3204 source clock frequency (on 10752000U used integer multiplier)
|
||||
//#define AUDIO_CLOCK_REF (10752000U)
|
||||
// Disable AIC PLL clock, use input as CODEC_CLKIN (not stable on some devices, on long work)
|
||||
//#define AUDIO_CLOCK_REF (86016000U)
|
||||
|
|
@ -200,6 +200,8 @@ void tlv320aic3204_write_reg(uint8_t page, uint8_t reg, uint8_t data);
|
|||
#define LCD_WIDTH 320
|
||||
#define LCD_HEIGHT 240
|
||||
|
||||
// Used marker size settings
|
||||
#define _USE_BIG_MARKER_ 0
|
||||
// Used font settings
|
||||
#define _USE_FONT_ 0
|
||||
|
||||
|
|
@ -226,7 +228,8 @@ extern const uint8_t x7x11b_bits[];
|
|||
#elif _USE_FONT_ == 2
|
||||
extern const uint8_t x10x14_bits[];
|
||||
#define FONT_START_CHAR 0x17
|
||||
#define FONT_MAX_WIDTH 12
|
||||
#define FONT_MAX_WIDTH 14
|
||||
#define FONT_WIDTH 11
|
||||
#define FONT_GET_HEIGHT 14
|
||||
#define FONT_STR_HEIGHT 16
|
||||
#define FONT_GET_DATA(ch) ( &x10x14_bits[(ch-FONT_START_CHAR)*2*FONT_GET_HEIGHT ])
|
||||
|
|
@ -287,6 +290,7 @@ extern int16_t area_height;
|
|||
#define NUM_INPUT_HEIGHT 32
|
||||
|
||||
// On screen keyboard button size
|
||||
// Use full screen keyboard
|
||||
#if 1
|
||||
#define KP_WIDTH ((LCD_WIDTH) / 4) // numeric keypad button width
|
||||
#define KP_HEIGHT ((LCD_HEIGHT - NUM_INPUT_HEIGHT) / 4) // numeric keypad button height
|
||||
|
|
@ -294,6 +298,7 @@ extern int16_t area_height;
|
|||
#define KP_GET_X(posx) ((posx) * KP_WIDTH) // numeric keypad left
|
||||
#define KP_GET_Y(posy) ((posy) * KP_HEIGHT) // numeric keypad top
|
||||
#else
|
||||
// Use less size keyboard
|
||||
#define KP_WIDTH 48
|
||||
#define KP_HEIGHT 48
|
||||
// Key x, y position (0 - 15) on screen
|
||||
|
|
@ -555,7 +560,7 @@ void rtc_set_time(uint32_t dr, uint32_t tr);
|
|||
|
||||
#define CONFIG_MAGIC 0x434f4e45 /* 'CONF' */
|
||||
|
||||
extern int16_t lastsaveid;
|
||||
extern uint16_t lastsaveid;
|
||||
|
||||
#define frequency0 current_props._frequency0
|
||||
#define frequency1 current_props._frequency1
|
||||
|
|
@ -578,7 +583,7 @@ extern int16_t lastsaveid;
|
|||
|
||||
int caldata_save(uint32_t id);
|
||||
int caldata_recall(uint32_t id);
|
||||
const properties_t *caldata_ref(uint32_t id);
|
||||
const properties_t *caldata_reference(void);
|
||||
|
||||
int config_save(void);
|
||||
int config_recall(void);
|
||||
|
|
|
|||
4
plot.c
4
plot.c
|
|
@ -967,7 +967,7 @@ static const uint8_t reference_bitmap[]={
|
|||
_BMP8(0b11000000),
|
||||
};
|
||||
|
||||
#if _USE_FONT_ == 0
|
||||
#if _USE_BIG_MARKER_ == 0
|
||||
#define MARKER_WIDTH 7
|
||||
#define MARKER_HEIGHT 10
|
||||
#define X_MARKER_OFFSET 3
|
||||
|
|
@ -1031,7 +1031,7 @@ static const uint8_t marker_bitmap[]={
|
|||
_BMP8(0b00000000),
|
||||
};
|
||||
|
||||
#elif _USE_FONT_ == 1
|
||||
#elif _USE_BIG_MARKER_ == 1
|
||||
#define MARKER_WIDTH 10
|
||||
#define MARKER_HEIGHT 13
|
||||
#define X_MARKER_OFFSET 4
|
||||
|
|
|
|||
Loading…
Reference in a new issue