diff --git a/main.c b/main.c index 6d52c88..49fdf6f 100644 --- a/main.c +++ b/main.c @@ -783,14 +783,12 @@ void update_frequencies(void) { uint32_t start, stop; - if (frequency1 > 0) { + if (frequency0 < frequency1) { start = frequency0; stop = frequency1; } else { - int32_t center = frequency0; - int32_t span = -frequency1; - start = center - span/2; - stop = center + span/2; + start = frequency1; + stop = frequency0; } set_frequencies(start, stop, sweep_points); @@ -805,39 +803,36 @@ update_frequencies(void) void freq_mode_startstop(void) { - if (frequency1 <= 0) { - int center = frequency0; - int span = -frequency1; + if (frequency0 > frequency1) { ensure_edit_config(); - frequency0 = center - span/2; - frequency1 = center + span/2; + uint32_t f = frequency1; + frequency1 = frequency0; + frequency0 = f; } } void freq_mode_centerspan(void) { - if (frequency1 > 0) { - int start = frequency0; - int stop = frequency1; + if (frequency0 <= frequency1) { ensure_edit_config(); - frequency0 = (start + stop)/2; // center - frequency1 = -(stop - start); // span + uint32_t f = frequency1; + frequency1 = frequency0; + frequency0 = f; } } #define START_MIN 50000 -//#define STOP_MAX 900000000 -#define STOP_MAX 1500000000 +#define STOP_MAX 2700000000U void -set_sweep_frequency(int type, int32_t freq) +set_sweep_frequency(int type, uint32_t freq) { int cal_applied = cal_status & CALSTAT_APPLY; - // negative value indicate overflow, do nothing +/* // negative value indicate overflow, do nothing if (freq < 0) - return; + return;*/ switch (type) { case ST_START: freq_mode_startstop(); @@ -872,46 +867,47 @@ set_sweep_frequency(int type, int32_t freq) case ST_CENTER: ensure_edit_config(); freq_mode_centerspan(); - if (frequency0 != freq) { + uint32_t center = frequency0/2 + frequency1/2; + if (center != freq) { + uint32_t span = frequency0 - frequency1; ensure_edit_config(); - frequency0 = freq; - int center = frequency0; - int span = -frequency1; - if (center-span/2 < START_MIN) { - span = (center - START_MIN) * 2; - frequency1 = -span; + frequency0 = freq + span/2; + frequency1 = freq - span/2; + if (frequency1 < START_MIN) { + frequency0 -= START_MIN - frequency1; + frequency1 = START_MIN; } - if (center+span/2 > STOP_MAX) { - span = (STOP_MAX - center) * 2; - frequency1 = -span; + if (frequency0 > STOP_MAX) { + frequency1 += frequency0 - STOP_MAX; + frequency0 = STOP_MAX; } update_frequencies(); } break; case ST_SPAN: freq_mode_centerspan(); - if (frequency1 != -freq) { + if (frequency0 - frequency1 != freq) { ensure_edit_config(); - frequency1 = -freq; - int center = frequency0; - int span = -frequency1; - if (center-span/2 < START_MIN) { - center = START_MIN + span/2; - frequency0 = center; + uint32_t center = frequency0/2 + frequency1/2; + frequency1 = center - freq/2; + frequency0 = center + freq/2; + if (frequency1 < START_MIN) { + frequency0 -= START_MIN - frequency1; + frequency1 = START_MIN; } - if (center+span/2 > STOP_MAX) { - center = STOP_MAX - span/2; - frequency0 = center; + if (frequency0 > STOP_MAX) { + frequency1 += frequency0 - STOP_MAX; + frequency0 = STOP_MAX; } update_frequencies(); } break; case ST_CW: freq_mode_centerspan(); - if (frequency0 != freq || frequency1 != 0) { + if (frequency0 != freq || frequency1 != freq) { ensure_edit_config(); frequency0 = freq; - frequency1 = 0; + frequency1 = freq; update_frequencies(); } break; @@ -924,21 +920,21 @@ set_sweep_frequency(int type, int32_t freq) uint32_t get_sweep_frequency(int type) { - if (frequency1 >= 0) { + if (frequency0 <= frequency1) { switch (type) { case ST_START: return frequency0; case ST_STOP: return frequency1; - case ST_CENTER: return (frequency0 + frequency1)/2; + case ST_CENTER: return frequency0/2 + frequency1/2; case ST_SPAN: return frequency1 - frequency0; - case ST_CW: return (frequency0 + frequency1)/2; + case ST_CW: return frequency0/2 + frequency1/2; } } else { switch (type) { - case ST_START: return frequency0 + frequency1/2; - case ST_STOP: return frequency0 - frequency1/2; - case ST_CENTER: return frequency0; - case ST_SPAN: return -frequency1; - case ST_CW: return frequency0; + case ST_START: return frequency1; + case ST_STOP: return frequency0; + case ST_CENTER: return frequency0/2 + frequency1/2; + case ST_SPAN: return frequency0 - frequency1; + case ST_CW: return frequency0/2 + frequency1/2; } } return 0; diff --git a/nanovna.h b/nanovna.h index 7028228..f4d2c9b 100644 --- a/nanovna.h +++ b/nanovna.h @@ -71,7 +71,7 @@ enum { ST_START, ST_STOP, ST_CENTER, ST_SPAN, ST_CW }; -void set_sweep_frequency(int type, int32_t frequency); +void set_sweep_frequency(int type, uint32_t frequency); uint32_t get_sweep_frequency(int type); float my_atof(const char *p); @@ -292,9 +292,9 @@ void ili9341_read_memory_continue(int len, uint16_t* out); #define SAVEAREA_MAX 5 typedef struct { - int32_t magic; - int32_t _frequency0; // start or center - int32_t _frequency1; // stop or span + uint32_t magic; + uint32_t _frequency0; // start or center + uint32_t _frequency1; // stop or span int16_t _sweep_points; uint16_t _cal_status; diff --git a/plot.c b/plot.c index af18742..0760ae8 100644 --- a/plot.c +++ b/plot.c @@ -8,7 +8,7 @@ #define SWAP(x,y) do { int z=x; x = y; y = z; } while(0) static void cell_draw_marker_info(int m, int n, int w, int h); -void frequency_string(char *buf, size_t len, int32_t freq); +void frequency_string(char *buf, size_t len, uint32_t freq); void frequency_string_short(char *buf, size_t len, int32_t freq, char prefix); void markmap_all_markers(void); @@ -1693,24 +1693,24 @@ cell_draw_marker_info(int m, int n, int w, int h) } void -frequency_string(char *buf, size_t len, int32_t freq) +frequency_string(char *buf, size_t len, uint32_t freq) { - if (freq < 0) { +/* if (freq < 0) { freq = -freq; *buf++ = '-'; len -= 1; - } + }*/ if (freq < 1000) { chsnprintf(buf, len, "%d Hz", (int)freq); - } else if (freq < 1000000) { + } else if (freq < 1000000U) { chsnprintf(buf, len, "%d.%03d kHz", - (int)(freq / 1000), - (int)(freq % 1000)); + (freq / 1000U), + (freq % 1000U)); } else { chsnprintf(buf, len, "%d.%03d %03d MHz", - (int)(freq / 1000000), - (int)((freq / 1000) % 1000), - (int)(freq % 1000)); + (freq / 1000000U), + ((freq / 1000U) % 1000U), + (freq % 1000U)); } } @@ -1746,20 +1746,18 @@ draw_frequencies(void) { char buf[24]; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { - if (frequency1 > 0) { - int start = frequency0; - int stop = frequency1; + if (frequency0 < frequency1) { strcpy(buf, "START "); - frequency_string(buf+6, 24-6, start); + frequency_string(buf+6, 24-6, frequency0); strcat(buf, " "); ili9341_drawstring_5x7(buf, OFFSETX, 233, 0xffff, 0x0000); strcpy(buf, "STOP "); - frequency_string(buf+5, 24-5, stop); + frequency_string(buf+5, 24-5, frequency1); strcat(buf, " "); ili9341_drawstring_5x7(buf, 205, 233, 0xffff, 0x0000); - } else if (frequency1 < 0) { - int fcenter = frequency0; - int fspan = -frequency1; + } else if (frequency0 > frequency1) { + uint32_t fcenter = frequency0/2 + frequency1/2; + uint32_t fspan = frequency0 - frequency1; int x = OFFSETX; strcpy(buf, "CENTER"); ili9341_drawstring_5x7_inv(buf, x, 233, 0xffff, 0x0000, uistat.lever_mode == LM_CENTER); diff --git a/si5351.c b/si5351.c index 148a227..8c548a3 100644 --- a/si5351.c +++ b/si5351.c @@ -227,7 +227,7 @@ si5351_setupMultisynth(uint8_t output, #define PLLFREQ (XTALFREQ * PLL_N) void -si5351_set_frequency_fixedpll(int channel, int pll, int pllfreq, int freq, +si5351_set_frequency_fixedpll(int channel, int pll, uint32_t pllfreq, uint32_t freq, int rdiv, uint8_t drive_strength, int mul) { int denom = freq; @@ -256,7 +256,7 @@ si5351_set_frequency_fixedpll(int channel, int pll, int pllfreq, int freq, } void -si5351_set_frequency_fixeddiv(int channel, int pll, int freq, int div, +si5351_set_frequency_fixeddiv(int channel, int pll, uint32_t freq, int div, uint8_t drive_strength, int mul) { int denom = XTALFREQ * mul; @@ -324,25 +324,31 @@ si5351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_streng int band; int delay = DELAY_NORMAL; uint32_t ofreq = freq + offset; - int mul = 1, omul = 1; + uint32_t mul = 1, omul = 1; uint32_t rdiv = SI5351_R_DIV_1; - if (freq >= config.harmonic_freq_threshold * 3) { + if (freq >= config.harmonic_freq_threshold * 7U) { + mul = 9; + omul = 11; + } else if (freq >= config.harmonic_freq_threshold * 5U) { + mul = 7; + omul = 9; + } else if (freq >= config.harmonic_freq_threshold * 3U) { mul = 5; omul = 7; } else if (freq >= config.harmonic_freq_threshold) { mul = 3; omul = 5; } - if ((freq / mul) < 100000000) { + if ((freq / mul) < 100000000U) { band = 0; - } else if ((freq / mul) < 150000000) { + } else if ((freq / mul) < 150000000U) { band = 1; } else { band = 2; } - if (freq <= 500000) { + if (freq <= 500000U) { rdiv = SI5351_R_DIV_64; - } else if (freq <= 4000000) { + } else if (freq <= 4000000U) { rdiv = SI5351_R_DIV_8; }