From 3fc17e18f900c6bb31fb1e72bab345adf576c15c Mon Sep 17 00:00:00 2001 From: TT Date: Mon, 7 Oct 2019 22:24:32 +0900 Subject: [PATCH 1/6] fix: break sweep loop on frequency change --- main.c | 2 ++ nanovna.h | 3 +++ ui.c | 1 - 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index acd7c1a..7a933b3 100644 --- a/main.c +++ b/main.c @@ -793,6 +793,8 @@ update_frequencies(void) } set_frequencies(start, stop, sweep_points); + operation_requested = OP_FREQCHANGE; + update_marker_index(); // set grid layout diff --git a/nanovna.h b/nanovna.h index 809921e..7f35500 100644 --- a/nanovna.h +++ b/nanovna.h @@ -86,6 +86,9 @@ extern int8_t sweep_enabled; extern void ui_init(void); extern void ui_process(void); +enum { OP_NONE = 0, OP_LEVER, OP_TOUCH, OP_FREQCHANGE }; +extern uint8_t operation_requested; + /* * dsp.c */ diff --git a/ui.c b/ui.c index 80147b9..3cbac63 100644 --- a/ui.c +++ b/ui.c @@ -59,7 +59,6 @@ static uint32_t last_button_down_ticks; static uint32_t last_button_repeat_ticks; static int8_t inhibit_until_release = FALSE; -enum { OP_NONE = 0, OP_LEVER, OP_TOUCH }; uint8_t operation_requested = OP_NONE; int8_t previous_marker = -1; From 342c5ff6694d4016530cee1da127adb1e2a795e5 Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 8 Sep 2019 18:44:33 +0900 Subject: [PATCH 2/6] add trace format of group delay --- main.c | 2 +- plot.c | 37 ++++++++++++++++++++++++++++++++++--- ui.c | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 7a933b3..1bb87a0 100644 --- a/main.c +++ b/main.c @@ -1438,7 +1438,7 @@ const struct { } trace_info[] = { { "LOGMAG", 7, 10 }, { "PHASE", 4, 90 }, - { "DELAY", 4, 1 }, + { "DELAY", 4, 1e-9 }, { "SMITH", 0, 1 }, { "POLAR", 0, 1 }, { "LINEAR", 0, 0.125 }, diff --git a/plot.c b/plot.c index 234ca94..dff5354 100644 --- a/plot.c +++ b/plot.c @@ -439,6 +439,24 @@ float phase(float *v) return 2 * atan2f(v[1], v[0]) / M_PI * 90; } +/* + * calculate groupdelay + */ +float groupdelay(float *v, float deltaf) +{ + float *w = &v[2]; // point to next coeff +#if 1 + // w = w[0]/w[1] + // v = v[0]/v[1] + // atan(w)-atan(v) = atan((w-v)/(1+wv)) + float r = w[0]*v[1] - w[1]*v[0]; + float i = w[0]*v[0] + w[1]*v[1]; + return atan2f(r, i) / (2 * M_PI * deltaf); +#else + return (atan2f(w[0], w[1]) - atan2f(v[0], v[1])) / (2 * M_PI * deltaf); +#endif +} + /* * calculate abs(gamma) */ @@ -502,6 +520,12 @@ trace_into_index(int x, int t, int i, float coeff[2]) case TRC_PHASE: v = refpos - phase(coeff) * scale; break; + case TRC_DELAY: + if (i != 100) { + float deltaf = frequencies[i+1] - frequencies[i]; + v = refpos - groupdelay(coeff, deltaf) * scale; + } + break; case TRC_LINEAR: v = refpos + linear(coeff) * scale; break; @@ -631,7 +655,7 @@ gamma2reactance(char *buf, int len, const float coeff[2]) } static void -trace_get_value_string(int t, char *buf, int len, float coeff[2], uint32_t frequency) +trace_get_value_string(int t, char *buf, int len, float coeff[2], int i) { float v; switch (trace[t].type) { @@ -646,6 +670,13 @@ trace_get_value_string(int t, char *buf, int len, float coeff[2], uint32_t frequ v = phase(coeff); chsnprintf(buf, len, "%.2f" S_DEGREE, v); break; + case TRC_DELAY: + { + float deltaf = frequencies[i+1] - frequencies[i]; + v = groupdelay(coeff, deltaf); + string_value_with_prefix(buf, len, v, 's'); + } + break; case TRC_LINEAR: v = linear(coeff); chsnprintf(buf, len, "%.2f", v); @@ -655,7 +686,7 @@ trace_get_value_string(int t, char *buf, int len, float coeff[2], uint32_t frequ chsnprintf(buf, len, "%.2f", v); break; case TRC_SMITH: - gamma2imp(buf, len, coeff, frequency); + gamma2imp(buf, len, coeff, frequencies[i]); break; case TRC_REAL: chsnprintf(buf, len, "%.2f", coeff[0]); @@ -1379,7 +1410,7 @@ cell_draw_marker_info(int m, int n, int w, int h) trace_get_info(t, buf, sizeof buf); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); xpos += 64; - trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel][idx], frequencies[idx]); + trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel][idx], idx); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); j++; } diff --git a/ui.c b/ui.c index 3cbac63..d369429 100644 --- a/ui.c +++ b/ui.c @@ -1221,7 +1221,7 @@ const keypads_t * const keypads_mode_tbl[] = { keypads_freq, // span keypads_freq, // cw freq keypads_scale, // scale - keypads_scale, // respos + keypads_scale, // refpos keypads_time, // electrical delay keypads_scale // velocity factor }; From 9d41719fdc8d2d818e055f221f974e2adb63ebf1 Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 8 Sep 2019 21:39:57 +0900 Subject: [PATCH 3/6] use time mode for scale for group delay --- ui.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ui.c b/ui.c index d369429..0344c30 100644 --- a/ui.c +++ b/ui.c @@ -68,7 +68,7 @@ enum { }; enum { - KM_START, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_SCALE, KM_REFPOS, KM_EDELAY, KM_VELOCITY_FACTOR + KM_START, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_SCALE, KM_REFPOS, KM_EDELAY, KM_VELOCITY_FACTOR, KM_SCALEDELAY }; uint8_t ui_mode = UI_NORMAL; @@ -742,12 +742,16 @@ static void menu_scale_cb(int item) { int status; + int km = KM_SCALE + item; + if (km == KM_SCALE && trace[uistat.current_trace].type == TRC_DELAY) { + km = KM_SCALEDELAY; + } status = btn_wait_release(); if (status & EVT_BUTTON_DOWN_LONG) { - ui_mode_numeric(KM_SCALE + item); + ui_mode_numeric(km); ui_process_numeric(); } else { - ui_mode_keypad(KM_SCALE + item); + ui_mode_keypad(km); ui_process_keypad(); } } @@ -1223,11 +1227,12 @@ const keypads_t * const keypads_mode_tbl[] = { keypads_scale, // scale keypads_scale, // refpos keypads_time, // electrical delay - keypads_scale // velocity factor + keypads_scale, // velocity factor + keypads_time // scale of delay }; const char * const keypad_mode_label[] = { - "START", "STOP", "CENTER", "SPAN", "CW FREQ", "SCALE", "REFPOS", "EDELAY", "VELOCITY%" + "START", "STOP", "CENTER", "SPAN", "CW FREQ", "SCALE", "REFPOS", "EDELAY", "VELOCITY%", "DELAY" }; void @@ -1487,6 +1492,9 @@ fetch_numeric_target(void) case KM_VELOCITY_FACTOR: uistat.value = velocity_factor; break; + case KM_SCALEDELAY: + uistat.value = get_trace_scale(uistat.current_trace) * 1e12; + break; } { @@ -1706,6 +1714,9 @@ keypad_click(int key) case KM_VELOCITY_FACTOR: velocity_factor = value; break; + case KM_SCALEDELAY: + set_trace_scale(uistat.current_trace, value * 1e-12); // pico second + break; } return KP_DONE; From 07de5cd579218be7ae0820a23c9931123d1afbb7 Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 7 Sep 2019 13:13:25 +0900 Subject: [PATCH 4/6] enhancement: adjust delay --- main.c | 8 ++++++-- si5351.c | 10 ++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 7a933b3..23ccda4 100644 --- a/main.c +++ b/main.c @@ -269,6 +269,8 @@ const int8_t gain_table[] = { 95 // 1400MHz ~ }; +#define DELAY_GAIN_CHANGE 10 + static int adjust_gain(int newfreq) { @@ -277,7 +279,7 @@ adjust_gain(int newfreq) int old_order = frequency / FREQ_HARMONICS; if (new_order != old_order) { tlv320aic3204_set_gain(gain_table[new_order], gain_table[new_order]); - delay += 10; + delay += DELAY_GAIN_CHANGE; } return delay; } @@ -655,6 +657,8 @@ ensure_edit_config(void) cal_status = 0; } +#define DELAY_CHANNEL_CHANGE 1 + // main loop for measurement bool sweep(bool break_on_operation) { @@ -672,7 +676,7 @@ bool sweep(bool break_on_operation) (*sample_func)(measured[0][i]); tlv320aic3204_select_in1(); // CH1:TRANSMISSION - wait_dsp(delay); + wait_dsp(delay + DELAY_CHANNEL_CHANGE); /* calculate transmission coeficient */ (*sample_func)(measured[1][i]); diff --git a/si5351.c b/si5351.c index 49de637..76ee744 100644 --- a/si5351.c +++ b/si5351.c @@ -296,6 +296,10 @@ si5351_set_frequency(int channel, int freq, uint8_t drive_strength) int current_band = -1; +#define DELAY_NORMAL 2 +#define DELAY_BANDCHANGE 1 +#define DELAY_LOWBAND 1 + /* * configure output as follows: * CLK0: frequency + offset @@ -307,7 +311,7 @@ int si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength) { int band; - int delay = 3; + int delay = DELAY_NORMAL; uint32_t ofreq = freq + offset; uint32_t rdiv = SI5351_R_DIV_1; if (freq >= config.harmonic_freq_threshold * 3) { @@ -389,8 +393,10 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng #if 1 si5351_enable_output(); #endif - delay += 10; + delay += DELAY_BANDCHANGE; } + if (band == 0) + delay += DELAY_LOWBAND; current_band = band; return delay; From e3a10a78682c40b57347e4569f5e7f54f62d9035 Mon Sep 17 00:00:00 2001 From: TT Date: Tue, 8 Oct 2019 23:59:16 +0900 Subject: [PATCH 5/6] fixed: #70 first measurement point sometimes has wrong value --- main.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/main.c b/main.c index 37f679d..86e3055 100644 --- a/main.c +++ b/main.c @@ -286,12 +286,7 @@ adjust_gain(int newfreq) int set_frequency(uint32_t freq) { - int delay = 0; - if (frequency == freq) - return delay; - - delay += adjust_gain(freq); - + int delay = adjust_gain(freq); int8_t ds = drive_strength; if (ds == DRIVE_STRENGTH_AUTO) { ds = freq > FREQ_HARMONICS ? SI5351_CLK_DRIVE_STRENGTH_8MA : SI5351_CLK_DRIVE_STRENGTH_2MA; From 4a9dee96a41af9a3f8b4a251636413323baebf48 Mon Sep 17 00:00:00 2001 From: TT Date: Tue, 8 Oct 2019 23:59:49 +0900 Subject: [PATCH 6/6] ui: remove close menu item --- ui.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/ui.c b/ui.c index 0344c30..2aa2e86 100644 --- a/ui.c +++ b/ui.c @@ -1054,7 +1054,6 @@ const menuitem_t menu_top[] = { { MT_SUBMENU, "CAL", menu_cal }, { MT_SUBMENU, "RECALL", menu_recall }, { MT_SUBMENU, "CONFIG", menu_config }, - { MT_CLOSE, "CLOSE", NULL }, { MT_NONE, NULL, NULL } // sentinel }; @@ -1655,13 +1654,16 @@ ui_process_menu(void) menu_invoke(selection); } else { do { - if (status & EVT_UP - && menu_stack[menu_current_level][selection+1].type != MT_NONE) { + if (status & EVT_UP) { + // close menu if next item is sentinel + if (menu_stack[menu_current_level][selection+1].type == MT_NONE) + goto menuclose; selection++; draw_menu(); } - if (status & EVT_DOWN - && selection > 0) { + if (status & EVT_DOWN) { + if (selection == 0) + goto menuclose; selection--; draw_menu(); } @@ -1669,6 +1671,10 @@ ui_process_menu(void) } while (status != 0); } } + return; + +menuclose: + ui_mode_normal(); } static int