diff --git a/flash.c b/flash.c index 9ab6519..d7454a0 100644 --- a/flash.c +++ b/flash.c @@ -75,16 +75,11 @@ checksum(const void *start, size_t len) return value; } - -#define FLASH_PAGESIZE 0x800 - -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; + uint16_t *dst = (uint16_t*)SAVE_CONFIG_ADDR; int count = sizeof(config_t) / sizeof(uint16_t); config.magic = CONFIG_MAGIC; @@ -107,7 +102,7 @@ config_save(void) int 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) @@ -120,31 +115,19 @@ config_recall(void) return 0; } -const uint32_t saveareas[SAVEAREA_MAX] = -{ - SAVE_PROP_CONFIG_0_ADDR, - SAVE_PROP_CONFIG_1_ADDR, - SAVE_PROP_CONFIG_2_ADDR, - SAVE_PROP_CONFIG_3_ADDR, - SAVE_PROP_CONFIG_4_ADDR, - SAVE_PROP_CONFIG_5_ADDR, - SAVE_PROP_CONFIG_6_ADDR, - SAVE_PROP_CONFIG_7_ADDR, - SAVE_PROP_CONFIG_8_ADDR, -}; - -int16_t lastsaveid = 0; + +//int16_t lastsaveid = 0; int -caldata_save(int id) +caldata_save(uint16_t id) { + if (id >= SAVEAREA_MAX) + return -1; uint16_t *src = (uint16_t*)&setting; uint16_t *dst; int count = sizeof(setting_t) / sizeof(uint16_t); - if (id < 0 || id >= SAVEAREA_MAX) - return -1; - dst = (uint16_t*)saveareas[id]; + dst = (uint16_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE); setting.magic = CONFIG_MAGIC; setting.checksum = checksum( @@ -160,13 +143,12 @@ caldata_save(int id) p += FLASH_PAGESIZE; } - /* write to flash */ + /* write settings to flash */ while (count-- > 0) { flash_program_half_word((uint32_t)dst, *src++); dst++; } - - // Flash stored trace + // Flash stored trace to flash count = sizeof(stored_t) / sizeof(uint16_t); src = (uint16_t*)&stored_t[0]; while (count-- > 0) { @@ -176,22 +158,22 @@ caldata_save(int id) /* after saving data, make active configuration points to flash */ // active_props = (setting_t*)saveareas[id]; - lastsaveid = id; +// lastsaveid = id; return 0; } int -caldata_recall(int id) +caldata_recall(uint16_t id) { setting_t *src; void *dst = &setting; - if (id < 0 || id >= SAVEAREA_MAX) + if (id >= SAVEAREA_MAX) return -1; // point to saved area on the flash memory - src = (setting_t*)saveareas[id]; + src = (setting_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE); if (src->magic != CONFIG_MAGIC) return -1; @@ -200,7 +182,7 @@ caldata_recall(int id) /* active configuration points to save data on flash memory */ // active_props = src; - lastsaveid = id; +// lastsaveid = id; /* duplicated saved data onto sram to be able to modify marker/trace */ memcpy(dst, src, sizeof(setting_t)); @@ -229,16 +211,14 @@ caldata_ref(int id) } #endif -const uint32_t save_config_prop_area_size = SAVE_CONFIG_AREA_SIZE; - void clear_all_config_prop_data(void) { flash_unlock(); /* erase flash pages */ - void *p = (void*)save_config_area; - void *tail = p + save_config_prop_area_size; + void *p = (void*)SAVE_CONFIG_ADDR; + void *tail = p + SAVE_CONFIG_AREA_SIZE; while (p < tail) { flash_erase_page((uint32_t)p); p += FLASH_PAGESIZE; diff --git a/nanovna.h b/nanovna.h index a83c568..a2d23d7 100644 --- a/nanovna.h +++ b/nanovna.h @@ -443,8 +443,8 @@ typedef struct config { uint32_t harmonic_freq_threshold; #endif uint16_t vbat_offset; - int16_t low_level_offset; - int16_t high_level_offset; + float low_level_offset; + float high_level_offset; uint32_t correction_frequency[CORRECTION_POINTS]; float correction_value[CORRECTION_POINTS]; uint32_t deviceid; @@ -454,7 +454,7 @@ typedef struct config { extern config_t config; //#define settingLevelOffset config.level_offset -int get_level_offset(void); +float get_level_offset(void); void set_trace_type(int t, int type); void set_trace_channel(int t, int channel); @@ -706,6 +706,9 @@ extern const float unit_scale_value[]; extern const char * const unit_scale_text[]; #if 1 +// Flash save area - flash7 : org = 0x0801B000, len = 20k in *.ld file +// 2k - for config save +// 9 * 2k for setting_t + stored trace #define SAVEAREA_MAX 9 // config save area (see flash7 area start) #define SAVE_CONFIG_ADDR 0x0803B000 @@ -726,6 +729,7 @@ extern const char * const unit_scale_text[]; // Used for erase all config/prop data see flash7 area size #define SAVE_CONFIG_AREA_SIZE (SAVE_CONFIG_SIZE + SAVEAREA_MAX*SAVE_PROP_SIZE) // Should include all save slots + #else #define SAVEAREA_MAX 4 // Begin addr 0x0801C000 @@ -809,8 +813,8 @@ extern int16_t lastsaveid; #define FREQ_IS_STARTSTOP() (!(setting.freq_mode&FREQ_MODE_CENTER_SPAN)) #define FREQ_IS_CENTERSPAN() (setting.freq_mode&FREQ_MODE_CENTER_SPAN) #define FREQ_IS_CW() (setting.frequency0 == setting.frequency1) -int caldata_recall(int id); -int caldata_save(int id); +int caldata_recall(uint16_t id); +int caldata_save(uint16_t id); //const properties_t *caldata_ref(int id); int config_save(void); int config_recall(void); diff --git a/plot.c b/plot.c index a152ce6..965a4ed 100644 --- a/plot.c +++ b/plot.c @@ -845,13 +845,10 @@ extern const char *unit_string[]; static void trace_get_value_string( int t, char *buf, int len, int i, float coeff[POINTS_COUNT], - uint32_t freq[POINTS_COUNT], - int point_count, - int ri, int mtype) + int ri, int mtype, + uint32_t i_freq, uint32_t ref_freq) { (void) t; - (void)freq; - (void) point_count; float v; char buf2[16]; char buf3[8]; @@ -864,17 +861,17 @@ static void trace_get_value_string( *ptr2++ = S_DELTA[0]; unit_index = setting.unit+5; if (ri > i) { - dfreq = frequencies[ri] - frequencies[i]; + dfreq = ref_freq - i_freq; ii = ri - i; *ptr2++ = '-'; } else { - dfreq = frequencies[i] - frequencies[ri]; + dfreq = i_freq - ref_freq; ii = i - ri; *ptr2++ = '+'; } rlevel = value(coeff[ri]); } else { - dfreq = frequencies[i]; + dfreq = i_freq; } if (FREQ_IS_CW()) { float t = ii*(setting.actual_sweep_time_us)/(sweep_points - 1); @@ -2112,7 +2109,7 @@ static void cell_draw_marker_info(int x0, int y0) // cell_drawstring_7x13(buf, xpos, ypos); trace_get_value_string( t, &buf[k], (sizeof buf) - k, - idx, measured[trace[t].channel], frequencies, sweep_points, ridx, markers[i].mtype); + idx, measured[trace[t].channel], ridx, markers[i].mtype,markers[i].frequency, markers[ref_marker].frequency); if (/* strlen(buf)*7> WIDTH/2 && */active > 1) cell_drawstring(buf, xpos, ypos); else diff --git a/sa_cmd.c b/sa_cmd.c index 9282362..c2515c6 100644 --- a/sa_cmd.c +++ b/sa_cmd.c @@ -175,8 +175,8 @@ VNA_SHELL_FUNCTION(cmd_levelchange) VNA_SHELL_FUNCTION(cmd_leveloffset) { if (argc == 0) { - shell_printf("leveloffset low %.1f\r\n", (float) config.low_level_offset); - shell_printf("leveloffset high %.1f\r\n", (float)config.high_level_offset); + shell_printf("leveloffset low %.1f\r\n", config.low_level_offset); + shell_printf("leveloffset high %.1f\r\n", config.high_level_offset); return; } else if (argc == 2) { float v = my_atof(argv[1]); @@ -612,6 +612,8 @@ VNA_SHELL_FUNCTION(cmd_scanraw) old_points = points; } operation_requested = false; + dirty = true; + for (uint32_t i = 0; i - 45) - v = 0x50; // Disable AGC and enable LNA + auto_set_AGC_LNA(false); else - v = 0x60; // Enable AGC and disable LNA - if (old_v != v) { - SI4432_Write_Byte(SI4432_AGC_OVERRIDE, v); - old_v = v; - } - + auto_set_AGC_LNA(TRUE); } @@ -2070,6 +2073,19 @@ sweep_again: // stay in sweep loop when output mo if (markers[m].enabled && markers[m].mtype & M_TRACKING) { // Available marker found markers[m].index = max_index[i]; markers[m].frequency = frequencies[markers[m].index]; +#if 1 // Hyperbolic interpolation, can be removed to save memory + const int idx = markers[m].index; + if (idx > 0 && idx < sweep_points-1) + { + const float y1 = actual_t[idx - 1]; + const float y2 = actual_t[idx + 0]; + const float y3 = actual_t[idx + 1]; + const float d = 0.5f * (y1 - y3) / ((y1 - (2 * y2) + y3) + 1e-12f); + //const float bin = (float)idx + d; + const int32_t delta_Hz = abs((int64_t)frequencies[idx + 0] - frequencies[idx + 1]); + markers[m].frequency += (int32_t)(delta_Hz * d); + } +#endif m++; break; // Next maximum } diff --git a/si4432.c b/si4432.c index 9f500bf..71d980f 100644 --- a/si4432.c +++ b/si4432.c @@ -640,7 +640,7 @@ void SI4432_Sub_Init(void) // SI4432_Write_3_Byte(SI4432_IF_FILTER_BW, 0x81, 0x3C, 0x02) ; // <---------- // SI4432_Write_Byte(SI4432_IF_FILTER_BW, 0x81) ; // <---------- SI4432_Write_Byte(SI4432_AFC_LOOP_GEARSHIFT_OVERRIDE, 0x00) ; -// SI4432_Write_Byte(SI4432_AFC_TIMING_CONTROL, 0x02) ; // <---------- + SI4432_Write_Byte(SI4432_AFC_TIMING_CONTROL, 0x02) ; SI4432_Write_Byte(SI4432_CLOCK_RECOVERY_GEARSHIFT, 0x03) ; // SI4432_Write_Byte(SI4432_CLOCK_RECOVERY_OVERSAMPLING, 0x78) ; // <---------- // SI4432_Write_3_Byte(SI4432_CLOCK_RECOVERY_OFFSET2, 0x01, 0x11, 0x11) ; // <---------- diff --git a/ui.c b/ui.c index b7979e8..12ab58b 100644 --- a/ui.c +++ b/ui.c @@ -69,7 +69,7 @@ volatile uint8_t operation_requested = OP_NONE; int8_t previous_marker = -1; enum { - UI_NORMAL, UI_MENU, UI_NUMERIC, UI_KEYPAD + UI_NORMAL, UI_MENU, UI_KEYPAD }; #ifdef __VNA__ @@ -138,13 +138,11 @@ static int16_t last_touch_y; void ui_mode_normal(void); //static void ui_mode_menu(void); -static void ui_mode_numeric(int _keypad_mode); static void ui_mode_keypad(int _keypad_mode); // static void draw_menu(void); static void leave_ui_mode(void); static void erase_menu_buttons(void); static void ui_process_keypad(void); -static void ui_process_numeric(void); static void choose_active_marker(void); static void menu_move_back(void); static void menu_move_back_and_leave_ui(void); @@ -1256,7 +1254,6 @@ menu_move_top(void) static void menu_invoke(int item) { - int status; const menuitem_t *menu = menu_stack[menu_current_level]; menu = &menu[item]; @@ -1289,25 +1286,19 @@ menu_invoke(int item) break; case MT_KEYPAD: - status = btn_wait_release(); - if (status & EVT_BUTTON_DOWN_LONG) { - ui_mode_numeric(menu->data); - // ui_process_numeric(); - } else { - if (menu->type & MT_FORM) { - area_width = AREA_WIDTH_NORMAL - MENU_BUTTON_WIDTH; - redraw_frame(); // Remove form numbers - } - kp_help_text = (char *)menu->reference; - if (menu->data <= KM_CW) { // One of the frequency input keypads - if (MODE_LOW(setting.mode)) - kp_help_text = "0..350MHz"; - else - kp_help_text = "240..960Mhz"; - } - ui_mode_keypad(menu->data); - ui_process_keypad(); + if (menu->type & MT_FORM) { + area_width = AREA_WIDTH_NORMAL - MENU_BUTTON_WIDTH; + redraw_frame(); // Remove form numbers + } + kp_help_text = (char *)menu->reference; + if (menu->data <= KM_CW) { // One of the frequency input keypads + if (MODE_LOW(setting.mode)) + kp_help_text = "0..350MHz"; + else + kp_help_text = "240..960Mhz"; } + ui_mode_keypad(menu->data); + ui_process_keypad(); redraw_request |= REDRAW_CAL_STATUS; break; } @@ -1513,12 +1504,6 @@ draw_numeric_input(const char *buf) else// if (c >= '0' && c <= '9') c = c - '0'; - if (ui_mode == UI_NUMERIC && uistat.digit == 8-i) { - fg = DEFAULT_SPEC_INPUT_COLOR; - focused = TRUE; -// if (uistat.digit_mode) -// bg = DEFAULT_MENU_COLOR; - } ili9341_set_foreground(fg); ili9341_set_background(bg); if (c >= 0) // c is number @@ -1834,10 +1819,11 @@ menu_select_touch(int i) keypad_mode = v; fetch_numeric_target(); float m = 1.0; - if (touch_x < LCD_WIDTH/2 - 10) { - m = 1/pow(10, ((LCD_WIDTH/2 - 10) - touch_x)/1000.0); +#define TOUCH_DEAD_ZONE 5 + if (touch_x < LCD_WIDTH/2 - TOUCH_DEAD_ZONE) { + m = 1 / (1 + pow(10, -6 + ((LCD_WIDTH/2 - TOUCH_DEAD_ZONE) - touch_x)/20.0)); } else if (touch_x > LCD_WIDTH/2 + 10) { - m = pow(10, (touch_x - (LCD_WIDTH/2 + 10))/1000.0); + m = 1 + pow(10, -6 + (touch_x - (LCD_WIDTH/2 + TOUCH_DEAD_ZONE))/20.0); } uistat.value *= m; set_numeric_value(); @@ -1931,9 +1917,6 @@ leave_ui_mode() // if (ui_mode == UI_MENU) { // request_to_draw_cells_behind_menu(); // erase_menu_buttons(); -// } else if (ui_mode == UI_NUMERIC) { -// request_to_draw_cells_behind_numeric_input(); -// erase_numeric_input(); // } // Erase bottom area (not redraw on area update) if (MENU_BUTTON_HEIGHT*MENU_BUTTON_MAX - area_height > 0) @@ -2024,14 +2007,6 @@ set_numeric_value(void) } #endif -static void -draw_numeric_area(void) -{ - char buf[10]; - plot_printf(buf, sizeof buf, "%9d", ((int32_t)uistat.value)); - draw_numeric_input(buf); -} - void ui_mode_menu(void) { @@ -2046,25 +2021,6 @@ ui_mode_menu(void) draw_menu(); } -static void -ui_mode_numeric(int _keypad_mode) -{ - if (ui_mode == UI_NUMERIC) - return; - - leave_ui_mode(); - - // keypads array - keypad_mode = _keypad_mode; - ui_mode = UI_NUMERIC; - area_width = AREA_WIDTH_NORMAL; - area_height = LCD_HEIGHT-NUM_INPUT_HEIGHT;//AREA_HEIGHT_NORMAL - 32; - - draw_numeric_area_frame(); - fetch_numeric_target(); - draw_numeric_area(); -} - static void ui_mode_keypad(int _keypad_mode) { @@ -2412,107 +2368,6 @@ keypad_apply_touch(void) return -1; } -static void -numeric_apply_touch(void) -{ - int touch_x, touch_y; - touch_position(&touch_x, &touch_y); - - if (touch_x < 64) { - ui_mode_normal(); - return; - } - if (touch_x > 64+9*20+8+8) { - ui_mode_keypad(keypad_mode); - ui_process_keypad(); - return; - } - - if (touch_y > LCD_HEIGHT-40) { - int n = 9 - (touch_x - 64) / 20; - uistat.digit = n; - uistat.digit_mode = TRUE; - } else { - int step, n; - if (touch_y < 100) { - step = 1; - } else { - step = -1; - } - - for (n = uistat.digit; n > 0; n--) - step *= 10; - uistat.value += step; - } - draw_numeric_area(); - - touch_wait_release(); - uistat.digit_mode = FALSE; - draw_numeric_area(); - - return; -} - -static void -ui_process_numeric(void) -{ - int status = btn_check(); - - if (status != 0) { - if (status == EVT_BUTTON_SINGLE_CLICK) { - status = btn_wait_release(); - if (uistat.digit_mode) { - if (status & (EVT_BUTTON_SINGLE_CLICK | EVT_BUTTON_DOWN_LONG)) { - uistat.digit_mode = FALSE; - draw_numeric_area(); - } - } else { - if (status & EVT_BUTTON_DOWN_LONG) { - uistat.digit_mode = TRUE; - draw_numeric_area(); - } else if (status & EVT_BUTTON_SINGLE_CLICK) { - set_numeric_value(); - ui_mode_normal(); - } - } - } else { - do { - if (uistat.digit_mode) { - if (status & EVT_DOWN) { - if (uistat.digit < 8) - uistat.digit++; - else - goto exit; - } - if (status & EVT_UP) { - if (uistat.digit > 0) - uistat.digit--; - else - goto exit; - } - } else { - int32_t step = 1; - int n; - for (n = uistat.digit; n > 0; n--) - step *= 10; - if (status & EVT_DOWN) - uistat.value += step; - if (status & EVT_UP) - uistat.value -= step; - } - draw_numeric_area(); - status = btn_wait_release(); - } while (status != 0); - } - } - - return; - - exit: - // cancel operation - ui_mode_normal(); -} - static void ui_process_keypad(void) { @@ -2573,9 +2428,6 @@ ui_process_lever(void) case UI_MENU: ui_process_menu(); break; - case UI_NUMERIC: - ui_process_numeric(); - break; case UI_KEYPAD: ui_process_keypad(); break; @@ -2731,10 +2583,6 @@ void ui_process_touch(void) case UI_MENU: menu_apply_touch(); break; - - case UI_NUMERIC: - numeric_apply_touch(); - break; } } touch_start_watchdog(); diff --git a/ui_sa.c b/ui_sa.c index 666c713..d44b13d 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1587,11 +1587,21 @@ static const menuitem_t menu_config[] = { { MT_NONE, 0, NULL, NULL } // sentinel }; -static const menuitem_t menu_display[] = { - { MT_ADV_CALLBACK,0, "PAUSE\nSWEEP", menu_pause_acb}, +static const menuitem_t menu_storage[] = { { MT_ADV_CALLBACK,0, "STORE\nTRACE", menu_storage_acb}, { MT_ADV_CALLBACK,1, "CLEAR\nSTORED", menu_storage_acb}, { MT_ADV_CALLBACK,2, "SUBTRACT\nSTORED",menu_storage_acb}, + { MT_CANCEL, 0, S_LARROW" BACK", NULL }, + { MT_NONE, 0, NULL, NULL } // sentinel +}; + +static const menuitem_t menu_display[] = { + { MT_ADV_CALLBACK,0, "PAUSE\nSWEEP", menu_pause_acb}, + { MT_SUBMENU, 0, "CALC", menu_average}, + { MT_SUBMENU, 0, "STORAGE", menu_storage}, +// { MT_ADV_CALLBACK,0, "STORE\nTRACE", menu_storage_acb}, +// { MT_ADV_CALLBACK,1, "CLEAR\nSTORED", menu_storage_acb}, +// { MT_ADV_CALLBACK,2, "SUBTRACT\nSTORED",menu_storage_acb}, { MT_ADV_CALLBACK,3, "NORMALIZE", menu_storage_acb}, { MT_ADV_CALLBACK,4, "WATER\nFALL", menu_waterfall_acb}, { MT_SUBMENU, 0, "SWEEP\nSETTINGS", menu_sweep_speed}, @@ -1631,7 +1641,7 @@ static const menuitem_t menu_level[] = { //{ MT_SUBMENU, 0, "SCALE/DIV", menu_scale_per}, { MT_KEYPAD, KM_SCALE, "SCALE/DIV", NULL}, { MT_SUBMENU, 0, "ATTENUATE", menu_atten}, - { MT_SUBMENU,0, "CALC", menu_average}, +// { MT_SUBMENU,0, "CALC", menu_average}, { MT_SUBMENU, 0, "UNIT", menu_unit}, { MT_KEYPAD, KM_OFFSET, "EXTERNAL\nAMP",NULL}, { MT_SUBMENU, 0, "TRIGGER", menu_trigger},