fix: remove upper limit of signed int32 for frequency setting

This commit is contained in:
TT 2020-01-18 12:07:21 +09:00
parent 38e64a616f
commit fcb0be6def
4 changed files with 80 additions and 80 deletions

96
main.c
View file

@ -783,14 +783,12 @@ void
update_frequencies(void) update_frequencies(void)
{ {
uint32_t start, stop; uint32_t start, stop;
if (frequency1 > 0) { if (frequency0 < frequency1) {
start = frequency0; start = frequency0;
stop = frequency1; stop = frequency1;
} else { } else {
int32_t center = frequency0; start = frequency1;
int32_t span = -frequency1; stop = frequency0;
start = center - span/2;
stop = center + span/2;
} }
set_frequencies(start, stop, sweep_points); set_frequencies(start, stop, sweep_points);
@ -805,39 +803,36 @@ update_frequencies(void)
void void
freq_mode_startstop(void) freq_mode_startstop(void)
{ {
if (frequency1 <= 0) { if (frequency0 > frequency1) {
int center = frequency0;
int span = -frequency1;
ensure_edit_config(); ensure_edit_config();
frequency0 = center - span/2; uint32_t f = frequency1;
frequency1 = center + span/2; frequency1 = frequency0;
frequency0 = f;
} }
} }
void void
freq_mode_centerspan(void) freq_mode_centerspan(void)
{ {
if (frequency1 > 0) { if (frequency0 <= frequency1) {
int start = frequency0;
int stop = frequency1;
ensure_edit_config(); ensure_edit_config();
frequency0 = (start + stop)/2; // center uint32_t f = frequency1;
frequency1 = -(stop - start); // span frequency1 = frequency0;
frequency0 = f;
} }
} }
#define START_MIN 50000 #define START_MIN 50000
//#define STOP_MAX 900000000 #define STOP_MAX 2700000000U
#define STOP_MAX 1500000000
void void
set_sweep_frequency(int type, int32_t freq) set_sweep_frequency(int type, uint32_t freq)
{ {
int cal_applied = cal_status & CALSTAT_APPLY; int cal_applied = cal_status & CALSTAT_APPLY;
// negative value indicate overflow, do nothing /* // negative value indicate overflow, do nothing
if (freq < 0) if (freq < 0)
return; return;*/
switch (type) { switch (type) {
case ST_START: case ST_START:
freq_mode_startstop(); freq_mode_startstop();
@ -872,46 +867,47 @@ set_sweep_frequency(int type, int32_t freq)
case ST_CENTER: case ST_CENTER:
ensure_edit_config(); ensure_edit_config();
freq_mode_centerspan(); freq_mode_centerspan();
if (frequency0 != freq) { uint32_t center = frequency0/2 + frequency1/2;
if (center != freq) {
uint32_t span = frequency0 - frequency1;
ensure_edit_config(); ensure_edit_config();
frequency0 = freq; frequency0 = freq + span/2;
int center = frequency0; frequency1 = freq - span/2;
int span = -frequency1; if (frequency1 < START_MIN) {
if (center-span/2 < START_MIN) { frequency0 -= START_MIN - frequency1;
span = (center - START_MIN) * 2; frequency1 = START_MIN;
frequency1 = -span;
} }
if (center+span/2 > STOP_MAX) { if (frequency0 > STOP_MAX) {
span = (STOP_MAX - center) * 2; frequency1 += frequency0 - STOP_MAX;
frequency1 = -span; frequency0 = STOP_MAX;
} }
update_frequencies(); update_frequencies();
} }
break; break;
case ST_SPAN: case ST_SPAN:
freq_mode_centerspan(); freq_mode_centerspan();
if (frequency1 != -freq) { if (frequency0 - frequency1 != freq) {
ensure_edit_config(); ensure_edit_config();
frequency1 = -freq; uint32_t center = frequency0/2 + frequency1/2;
int center = frequency0; frequency1 = center - freq/2;
int span = -frequency1; frequency0 = center + freq/2;
if (center-span/2 < START_MIN) { if (frequency1 < START_MIN) {
center = START_MIN + span/2; frequency0 -= START_MIN - frequency1;
frequency0 = center; frequency1 = START_MIN;
} }
if (center+span/2 > STOP_MAX) { if (frequency0 > STOP_MAX) {
center = STOP_MAX - span/2; frequency1 += frequency0 - STOP_MAX;
frequency0 = center; frequency0 = STOP_MAX;
} }
update_frequencies(); update_frequencies();
} }
break; break;
case ST_CW: case ST_CW:
freq_mode_centerspan(); freq_mode_centerspan();
if (frequency0 != freq || frequency1 != 0) { if (frequency0 != freq || frequency1 != freq) {
ensure_edit_config(); ensure_edit_config();
frequency0 = freq; frequency0 = freq;
frequency1 = 0; frequency1 = freq;
update_frequencies(); update_frequencies();
} }
break; break;
@ -924,21 +920,21 @@ set_sweep_frequency(int type, int32_t freq)
uint32_t uint32_t
get_sweep_frequency(int type) get_sweep_frequency(int type)
{ {
if (frequency1 >= 0) { if (frequency0 <= frequency1) {
switch (type) { switch (type) {
case ST_START: return frequency0; case ST_START: return frequency0;
case ST_STOP: return frequency1; 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_SPAN: return frequency1 - frequency0;
case ST_CW: return (frequency0 + frequency1)/2; case ST_CW: return frequency0/2 + frequency1/2;
} }
} else { } else {
switch (type) { switch (type) {
case ST_START: return frequency0 + frequency1/2; case ST_START: return frequency1;
case ST_STOP: return frequency0 - frequency1/2; case ST_STOP: return frequency0;
case ST_CENTER: return frequency0; case ST_CENTER: return frequency0/2 + frequency1/2;
case ST_SPAN: return -frequency1; case ST_SPAN: return frequency0 - frequency1;
case ST_CW: return frequency0; case ST_CW: return frequency0/2 + frequency1/2;
} }
} }
return 0; return 0;

View file

@ -71,7 +71,7 @@ enum {
ST_START, ST_STOP, ST_CENTER, ST_SPAN, ST_CW 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); uint32_t get_sweep_frequency(int type);
float my_atof(const char *p); float my_atof(const char *p);
@ -292,9 +292,9 @@ void ili9341_read_memory_continue(int len, uint16_t* out);
#define SAVEAREA_MAX 5 #define SAVEAREA_MAX 5
typedef struct { typedef struct {
int32_t magic; uint32_t magic;
int32_t _frequency0; // start or center uint32_t _frequency0; // start or center
int32_t _frequency1; // stop or span uint32_t _frequency1; // stop or span
int16_t _sweep_points; int16_t _sweep_points;
uint16_t _cal_status; uint16_t _cal_status;

34
plot.c
View file

@ -8,7 +8,7 @@
#define SWAP(x,y) do { int z=x; x = y; y = z; } while(0) #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); 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 frequency_string_short(char *buf, size_t len, int32_t freq, char prefix);
void markmap_all_markers(void); void markmap_all_markers(void);
@ -1693,24 +1693,24 @@ cell_draw_marker_info(int m, int n, int w, int h)
} }
void 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; freq = -freq;
*buf++ = '-'; *buf++ = '-';
len -= 1; len -= 1;
} }*/
if (freq < 1000) { if (freq < 1000) {
chsnprintf(buf, len, "%d Hz", (int)freq); chsnprintf(buf, len, "%d Hz", (int)freq);
} else if (freq < 1000000) { } else if (freq < 1000000U) {
chsnprintf(buf, len, "%d.%03d kHz", chsnprintf(buf, len, "%d.%03d kHz",
(int)(freq / 1000), (freq / 1000U),
(int)(freq % 1000)); (freq % 1000U));
} else { } else {
chsnprintf(buf, len, "%d.%03d %03d MHz", chsnprintf(buf, len, "%d.%03d %03d MHz",
(int)(freq / 1000000), (freq / 1000000U),
(int)((freq / 1000) % 1000), ((freq / 1000U) % 1000U),
(int)(freq % 1000)); (freq % 1000U));
} }
} }
@ -1746,20 +1746,18 @@ draw_frequencies(void)
{ {
char buf[24]; char buf[24];
if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) {
if (frequency1 > 0) { if (frequency0 < frequency1) {
int start = frequency0;
int stop = frequency1;
strcpy(buf, "START "); strcpy(buf, "START ");
frequency_string(buf+6, 24-6, start); frequency_string(buf+6, 24-6, frequency0);
strcat(buf, " "); strcat(buf, " ");
ili9341_drawstring_5x7(buf, OFFSETX, 233, 0xffff, 0x0000); ili9341_drawstring_5x7(buf, OFFSETX, 233, 0xffff, 0x0000);
strcpy(buf, "STOP "); strcpy(buf, "STOP ");
frequency_string(buf+5, 24-5, stop); frequency_string(buf+5, 24-5, frequency1);
strcat(buf, " "); strcat(buf, " ");
ili9341_drawstring_5x7(buf, 205, 233, 0xffff, 0x0000); ili9341_drawstring_5x7(buf, 205, 233, 0xffff, 0x0000);
} else if (frequency1 < 0) { } else if (frequency0 > frequency1) {
int fcenter = frequency0; uint32_t fcenter = frequency0/2 + frequency1/2;
int fspan = -frequency1; uint32_t fspan = frequency0 - frequency1;
int x = OFFSETX; int x = OFFSETX;
strcpy(buf, "CENTER"); strcpy(buf, "CENTER");
ili9341_drawstring_5x7_inv(buf, x, 233, 0xffff, 0x0000, uistat.lever_mode == LM_CENTER); ili9341_drawstring_5x7_inv(buf, x, 233, 0xffff, 0x0000, uistat.lever_mode == LM_CENTER);

View file

@ -227,7 +227,7 @@ si5351_setupMultisynth(uint8_t output,
#define PLLFREQ (XTALFREQ * PLL_N) #define PLLFREQ (XTALFREQ * PLL_N)
void 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 rdiv, uint8_t drive_strength, int mul)
{ {
int denom = freq; int denom = freq;
@ -256,7 +256,7 @@ si5351_set_frequency_fixedpll(int channel, int pll, int pllfreq, int freq,
} }
void 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) uint8_t drive_strength, int mul)
{ {
int denom = XTALFREQ * 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 band;
int delay = DELAY_NORMAL; int delay = DELAY_NORMAL;
uint32_t ofreq = freq + offset; uint32_t ofreq = freq + offset;
int mul = 1, omul = 1; uint32_t mul = 1, omul = 1;
uint32_t rdiv = SI5351_R_DIV_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; mul = 5;
omul = 7; omul = 7;
} else if (freq >= config.harmonic_freq_threshold) { } else if (freq >= config.harmonic_freq_threshold) {
mul = 3; mul = 3;
omul = 5; omul = 5;
} }
if ((freq / mul) < 100000000) { if ((freq / mul) < 100000000U) {
band = 0; band = 0;
} else if ((freq / mul) < 150000000) { } else if ((freq / mul) < 150000000U) {
band = 1; band = 1;
} else { } else {
band = 2; band = 2;
} }
if (freq <= 500000) { if (freq <= 500000U) {
rdiv = SI5351_R_DIV_64; rdiv = SI5351_R_DIV_64;
} else if (freq <= 4000000) { } else if (freq <= 4000000U) {
rdiv = SI5351_R_DIV_8; rdiv = SI5351_R_DIV_8;
} }