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:
DiSlord 2020-08-01 19:44:34 +03:00
parent 6b25d86128
commit f90b920d5e
5 changed files with 94 additions and 107 deletions

145
flash.c
View file

@ -22,7 +22,8 @@
#include "nanovna.h" #include "nanovna.h"
#include <string.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) 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; FLASH->CR &= ~FLASH_CR_PER;
} }
int flash_erase_page(uint32_t page_address) static void flash_erase_page(uint32_t page_address)
{ {
chSysLock(); chSysLock();
flash_erase_page0(page_address); flash_erase_page0(page_address);
chSysUnlock(); chSysUnlock();
return 0;
} }
void flash_program_half_word(uint32_t address, uint16_t data) static inline void flash_unlock(void)
{
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)
{ {
// unlock sequence // unlock sequence
FLASH->KEYR = 0x45670123; FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB; 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 static uint32_t
checksum(const void *start, size_t len) checksum(const void *start, size_t len)
{ {
uint32_t *p = (uint32_t*)start; uint32_t *p = (uint32_t*)start;
uint32_t *tail = (uint32_t*)(start + len);
uint32_t value = 0; 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++; value = __ROR(value, 31) + *p++;
return value; return value;
} }
const uint32_t save_config_area = SAVE_CONFIG_ADDR;
int int
config_save(void) config_save(void)
{ {
uint16_t *src = (uint16_t*)&config; // Apply magic word and calculate checksum
uint16_t *dst = (uint16_t*)save_config_area;
int count = sizeof(config_t) / sizeof(uint16_t);
config.magic = CONFIG_MAGIC; config.magic = CONFIG_MAGIC;
config.checksum = checksum(&config, sizeof config - sizeof config.checksum); config.checksum = checksum(&config, sizeof config - sizeof config.checksum);
flash_unlock(); // write to flash
flash_program_half_word_buffer((uint16_t*)SAVE_CONFIG_ADDR, (uint16_t*)&config, sizeof(config_t));
/* erase flash pages */
flash_erase_page((uint32_t)dst);
/* write to flahs */
while (count-- > 0) {
flash_program_half_word((uint32_t)dst, *src++);
dst++;
}
return 0; return 0;
} }
int int
config_recall(void) config_recall(void)
{ {
const config_t *src = (const config_t*)save_config_area; const config_t *src = (const config_t*)SAVE_CONFIG_ADDR;
void *dst = &config;
if (src->magic != CONFIG_MAGIC) if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
return -1; return -1;
if (checksum(src, sizeof *src - sizeof src->checksum) != src->checksum) // duplicated saved data onto sram to be able to modify marker/trace
return -1; memcpy(&config, src, sizeof(config_t));
/* duplicated saved data onto sram to be able to modify marker/trace */
memcpy(dst, src, sizeof(config_t));
return 0; return 0;
} }
@ -124,33 +116,16 @@ caldata_save(uint32_t id)
{ {
if (id >= SAVEAREA_MAX) if (id >= SAVEAREA_MAX)
return -1; return -1;
uint16_t *src = (uint16_t*)&current_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.magic = CONFIG_MAGIC;
current_props.checksum = checksum( current_props.checksum = checksum(&current_props, sizeof current_props - sizeof current_props.checksum);
&current_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*)&current_props, sizeof(properties_t));
/* erase flash pages */ // after saving data, make active configuration points to flash
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 */
active_props = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE); active_props = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
lastsaveid = id; lastsaveid = id;
@ -162,56 +137,50 @@ caldata_recall(uint32_t id)
{ {
if (id >= SAVEAREA_MAX) if (id >= SAVEAREA_MAX)
return -1; return -1;
properties_t *src;
void *dst = &current_props;
// point to saved area on the flash memory // 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) if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
goto load_default;
if (checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
goto load_default; goto load_default;
/* active configuration points to save data on flash memory */ // active configuration points to save data on flash memory
active_props = src; active_props = src;
lastsaveid = id; lastsaveid = id;
/* duplicated saved data onto sram to be able to modify marker/trace */ // duplicated saved data onto sram to be able to modify marker/trace
memcpy(dst, src, sizeof(properties_t)); memcpy(&current_props, src, sizeof(properties_t));
return 0; return 0;
load_default: load_default:
load_default_properties(); load_default_properties();
return -1; return lastsaveid;
} }
// Used in interpolate
const properties_t * const properties_t *
caldata_ref(uint32_t id) caldata_reference(void)
{ {
if (id >= SAVEAREA_MAX) if (lastsaveid >= SAVEAREA_MAX)
return NULL; return NULL;
const properties_t *src; const properties_t *src;
src = (const properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE); src = (const properties_t*)(SAVE_PROP_CONFIG_ADDR + lastsaveid * SAVE_PROP_CONFIG_SIZE);
// Check crc cache mask (made it only 1 time)
if (src->magic != CONFIG_MAGIC) if (checksum_ok&(1<<lastsaveid))
return NULL; return src;
if (checksum(src, sizeof *src - sizeof src->checksum) != src->checksum) if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
return NULL; return NULL;
checksum_ok|=1<<lastsaveid;
return src; return src;
} }
void void
clear_all_config_prop_data(void) clear_all_config_prop_data(void)
{ {
uint32_t i;
flash_unlock(); flash_unlock();
/* erase flash pages */ // erase flash pages
void *p = (void*)save_config_area; for (i = 0; i < SAVE_FULL_AREA_SIZE; i+=FLASH_PAGESIZE)
void *tail = p + SAVE_FULL_AREA_SIZE; flash_erase_page(SAVE_CONFIG_ADDR + i);
while (p < tail) {
flash_erase_page((uint32_t)p);
p += FLASH_PAGESIZE;
}
} }

34
main.c
View file

@ -83,13 +83,15 @@ static volatile vna_shellcmd_t shell_function = 0;
//#define ENABLE_PORT_COMMAND //#define ENABLE_PORT_COMMAND
// Enable si5351 timing command, used for debug // Enable si5351 timing command, used for debug
#define ENABLE_SI5351_TIMINGS #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_CH0_error_term_at(int i);
static void apply_CH1_error_term_at(int i); static void apply_CH1_error_term_at(int i);
static void apply_edelay(void); static void apply_edelay(void);
static uint16_t get_sweep_mode(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 update_frequencies(bool interpolate);
static void set_frequencies(uint32_t start, uint32_t stop, uint16_t points); static void set_frequencies(uint32_t start, uint32_t stop, uint16_t points);
static bool sweep(bool break_on_operation, uint16_t sweep_mode); 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); set_frequencies(start, stop, points);
if (cal_status & CALSTAT_APPLY) if (cal_status & CALSTAT_APPLY)
cal_interpolate(lastsaveid); cal_interpolate();
pause_sweep(); pause_sweep();
sweep(false, sweep_mode); sweep(false, sweep_mode);
// Output data after if set (faster data recive) // Output data after if set (faster data recive)
@ -1062,7 +1064,7 @@ update_frequencies(bool interpolate)
// set grid layout // set grid layout
update_grid(); update_grid();
if (interpolate) if (interpolate)
cal_interpolate(lastsaveid); cal_interpolate();
RESET_SWEEP; RESET_SWEEP;
} }
@ -1510,9 +1512,9 @@ cal_done(void)
} }
static 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; uint32_t i, j;
int eterm; int eterm;
if (src == NULL) if (src == NULL)
@ -2174,8 +2176,17 @@ VNA_SHELL_FUNCTION(cmd_vbat_offset)
VNA_SHELL_FUNCTION(cmd_si5351time) VNA_SHELL_FUNCTION(cmd_si5351time)
{ {
(void)argc; (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) | uint32_t tim = STM32_TIMINGR_PRESC(0U) |
STM32_TIMINGR_SCLDEL(my_atoui(argv[0])) | STM32_TIMINGR_SDADEL(my_atoui(argv[1])) | 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])); STM32_TIMINGR_SCLH(my_atoui(argv[2])) | STM32_TIMINGR_SCLL(my_atoui(argv[3]));
@ -2384,6 +2395,9 @@ static const VNAShellCommand commands[] =
#endif #endif
#ifdef ENABLE_SI5351_TIMINGS #ifdef ENABLE_SI5351_TIMINGS
{"t" , cmd_si5351time , CMD_WAIT_MUTEX}, {"t" , cmd_si5351time , CMD_WAIT_MUTEX},
#endif
#ifdef ENABLE_I2C_TIMINGS
{"i" , cmd_i2ctime , CMD_WAIT_MUTEX},
#endif #endif
{NULL , NULL , 0} {NULL , NULL , 0}
}; };
@ -2531,10 +2545,10 @@ static const I2CConfig i2ccfg = {
// STM32_TIMINGR_PRESC(5U) | // STM32_TIMINGR_PRESC(5U) |
// STM32_TIMINGR_SCLDEL(3U) | STM32_TIMINGR_SDADEL(3U) | // STM32_TIMINGR_SCLDEL(3U) | STM32_TIMINGR_SDADEL(3U) |
// STM32_TIMINGR_SCLH(3U) | STM32_TIMINGR_SCLL(9U), // STM32_TIMINGR_SCLH(3U) | STM32_TIMINGR_SCLL(9U),
// 600kHz @ SYSCLK 48MHz, manually get values, x1.5 I2C speed, but need calc timings // 600kHz @ SYSCLK 48MHz, manually get values, x1.5 I2C speed
STM32_TIMINGR_PRESC(3U) | STM32_TIMINGR_PRESC(0U) |
STM32_TIMINGR_SCLDEL(2U) | STM32_TIMINGR_SDADEL(2U) | STM32_TIMINGR_SCLDEL(10U) | STM32_TIMINGR_SDADEL(10U) |
STM32_TIMINGR_SCLH(4U) | STM32_TIMINGR_SCLL(4U), STM32_TIMINGR_SCLH(30U) | STM32_TIMINGR_SCLL(50U),
#else #else
#error "Need Define STM32_I2C1SW and set correct TIMINGR settings" #error "Need Define STM32_I2C1SW and set correct TIMINGR settings"
#endif #endif

View file

@ -52,7 +52,6 @@
#define STM32_ADCSW STM32_ADCSW_HSI14 #define STM32_ADCSW STM32_ADCSW_HSI14
#define STM32_ADCPRE STM32_ADCPRE_DIV4 #define STM32_ADCPRE STM32_ADCPRE_DIV4
#define STM32_MCOSEL STM32_MCOSEL_PLLDIV2 #define STM32_MCOSEL STM32_MCOSEL_PLLDIV2
#define STM32_ADCPRE STM32_ADCPRE_DIV4
#define STM32_ADCSW STM32_ADCSW_HSI14 #define STM32_ADCSW STM32_ADCSW_HSI14
#define STM32_USBSW STM32_USBSW_HSI48 #define STM32_USBSW STM32_USBSW_HSI48
#define STM32_CECSW STM32_CECSW_HSI #define STM32_CECSW STM32_CECSW_HSI

View file

@ -41,8 +41,7 @@
#define FREQUENCY_OFFSET 8000 #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 ) // 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 //#define FREQUENCY_OFFSET 5000
// Apply calibration after made sweep, if set to 1, calibration move out from sweep cycle // Apply calibration after made sweep, (if set 1, then 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)
#define APPLY_CALIBRATION_AFTER_SWEEP 0 #define APPLY_CALIBRATION_AFTER_SWEEP 0
// Use real time build table (undef for use constant) // Use real time build table (undef for use constant)
//#define USE_VARIABLE_OFFSET //#define USE_VARIABLE_OFFSET
@ -131,8 +130,9 @@ extern const char *info_about[];
/* /*
* dsp.c * 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 AUDIO_CLOCK_REF ( 8000000U)
// Define aic3204 source clock frequency (on 10752000U used integer multiplier)
//#define AUDIO_CLOCK_REF (10752000U) //#define AUDIO_CLOCK_REF (10752000U)
// Disable AIC PLL clock, use input as CODEC_CLKIN (not stable on some devices, on long work) // Disable AIC PLL clock, use input as CODEC_CLKIN (not stable on some devices, on long work)
//#define AUDIO_CLOCK_REF (86016000U) //#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_WIDTH 320
#define LCD_HEIGHT 240 #define LCD_HEIGHT 240
// Used marker size settings
#define _USE_BIG_MARKER_ 0
// Used font settings // Used font settings
#define _USE_FONT_ 0 #define _USE_FONT_ 0
@ -226,7 +228,8 @@ extern const uint8_t x7x11b_bits[];
#elif _USE_FONT_ == 2 #elif _USE_FONT_ == 2
extern const uint8_t x10x14_bits[]; extern const uint8_t x10x14_bits[];
#define FONT_START_CHAR 0x17 #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_GET_HEIGHT 14
#define FONT_STR_HEIGHT 16 #define FONT_STR_HEIGHT 16
#define FONT_GET_DATA(ch) ( &x10x14_bits[(ch-FONT_START_CHAR)*2*FONT_GET_HEIGHT ]) #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 #define NUM_INPUT_HEIGHT 32
// On screen keyboard button size // On screen keyboard button size
// Use full screen keyboard
#if 1 #if 1
#define KP_WIDTH ((LCD_WIDTH) / 4) // numeric keypad button width #define KP_WIDTH ((LCD_WIDTH) / 4) // numeric keypad button width
#define KP_HEIGHT ((LCD_HEIGHT - NUM_INPUT_HEIGHT) / 4) // numeric keypad button height #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_X(posx) ((posx) * KP_WIDTH) // numeric keypad left
#define KP_GET_Y(posy) ((posy) * KP_HEIGHT) // numeric keypad top #define KP_GET_Y(posy) ((posy) * KP_HEIGHT) // numeric keypad top
#else #else
// Use less size keyboard
#define KP_WIDTH 48 #define KP_WIDTH 48
#define KP_HEIGHT 48 #define KP_HEIGHT 48
// Key x, y position (0 - 15) on screen // 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' */ #define CONFIG_MAGIC 0x434f4e45 /* 'CONF' */
extern int16_t lastsaveid; extern uint16_t lastsaveid;
#define frequency0 current_props._frequency0 #define frequency0 current_props._frequency0
#define frequency1 current_props._frequency1 #define frequency1 current_props._frequency1
@ -578,7 +583,7 @@ extern int16_t lastsaveid;
int caldata_save(uint32_t id); int caldata_save(uint32_t id);
int caldata_recall(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_save(void);
int config_recall(void); int config_recall(void);

4
plot.c
View file

@ -967,7 +967,7 @@ static const uint8_t reference_bitmap[]={
_BMP8(0b11000000), _BMP8(0b11000000),
}; };
#if _USE_FONT_ == 0 #if _USE_BIG_MARKER_ == 0
#define MARKER_WIDTH 7 #define MARKER_WIDTH 7
#define MARKER_HEIGHT 10 #define MARKER_HEIGHT 10
#define X_MARKER_OFFSET 3 #define X_MARKER_OFFSET 3
@ -1031,7 +1031,7 @@ static const uint8_t marker_bitmap[]={
_BMP8(0b00000000), _BMP8(0b00000000),
}; };
#elif _USE_FONT_ == 1 #elif _USE_BIG_MARKER_ == 1
#define MARKER_WIDTH 10 #define MARKER_WIDTH 10
#define MARKER_HEIGHT 13 #define MARKER_HEIGHT 13
#define X_MARKER_OFFSET 4 #define X_MARKER_OFFSET 4