diff --git a/main.c b/main.c index dbc6370..187d7e7 100644 --- a/main.c +++ b/main.c @@ -270,6 +270,8 @@ const int8_t gain_table[] = { 95 // 1400MHz ~ }; +#define DELAY_GAIN_CHANGE 10 + static int adjust_gain(int newfreq) { @@ -278,19 +280,14 @@ 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; } 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; @@ -656,6 +653,8 @@ ensure_edit_config(void) cal_status = 0; } +#define DELAY_CHANNEL_CHANGE 1 + // main loop for measurement bool sweep(bool break_on_operation) { @@ -673,7 +672,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]); @@ -797,6 +796,8 @@ update_frequencies(void) } set_frequencies(start, stop, sweep_points); + operation_requested = OP_FREQCHANGE; + update_marker_index(); // set grid layout @@ -1440,7 +1441,7 @@ const struct { } trace_info[] = { { "LMG", 7, 10 }, { "PHA", 4, 90 }, - { "DEL", 4, 1 }, + { "DEL", 4, 1e-9 }, { "SMI", 0, 1 }, { "POL", 0, 1 }, { "LIN", 0, 0.125 }, diff --git a/nanovna.h b/nanovna.h index 5ce7adb..0bf5fc7 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/plot.c b/plot.c index 13a3088..e87193b 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, "%.1f" 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, "%.1f", 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]); @@ -1523,7 +1554,7 @@ cell_draw_marker_info(int m, int n, int w, int h) xpos += strwidthpx + 4; slen = trace_get_info(t, buf, sizeof buf); - trace_get_value_string(t, buf+slen, (sizeof(buf))-slen, measured[trace[t].channel][idx], frequencies[idx]); + trace_get_value_string(t, buf+slen, (sizeof(buf))-slen, measured[trace[t].channel][idx], idx); cell_drawstring_8x8_var(w, h, buf, xpos, ypos, config.trace_color[t], FALSE); j++; 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; diff --git a/ui.c b/ui.c index 9bd595b..657b0d6 100644 --- a/ui.c +++ b/ui.c @@ -60,7 +60,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; @@ -70,7 +69,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; @@ -757,12 +756,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(); } } @@ -1065,7 +1068,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 }; @@ -1236,13 +1238,14 @@ 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 + 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 @@ -1513,6 +1516,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; } { @@ -1673,13 +1679,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(); } @@ -1687,6 +1696,10 @@ ui_process_menu(void) } while (status != 0); } } + return; + +menuclose: + ui_mode_normal(); } static int @@ -1732,6 +1745,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;