Skip to content

Commit

Permalink
Guard added
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkaashoek committed Feb 21, 2023
1 parent e904ac9 commit 442a835
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 5 deletions.
25 changes: 25 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ static long_t my_atoi(const char *p);

uint8_t sweep_mode = SWEEP_ENABLE;
uint16_t sweep_once_count = 1;
#ifdef __GUARD__
uint16_t current_guard = 0;
#endif
uint16_t redraw_request = 0; // contains REDRAW_XXX flags
// Version text, displayed in Config->Version menu, also send by info command
const char * const info_about[]={
Expand Down Expand Up @@ -177,7 +180,29 @@ static THD_FUNCTION(Thread1, arg)
} else
#endif
{
#ifdef __GUARD__
if (setting.measurement == M_GUARD) {
while(!setting.guards[current_guard].enabled) {
current_guard++;
if (current_guard > GUARDS_MAX)
current_guard = 0;
}
if (setting.guards[current_guard].start - setting.guards[current_guard].end > 10000000) {
set_sweep_frequency(ST_START, setting.guards[current_guard].start);
set_sweep_frequency(ST_STOP, setting.guards[current_guard].end);
set_rbw(8000);
set_sweep_points((setting.guards[current_guard].end - setting.guards[current_guard].start) / 800000);
}
}
#endif
completed = sweep(true);
#ifdef __GUARD__
if (setting.measurement == M_GUARD) {
current_guard++;
if (current_guard>=GUARDS_MAX)
current_guard = 0;
}
#endif
if (sweep_once_count>1) {
sweep_once_count--;
} else
Expand Down
21 changes: 18 additions & 3 deletions nanovna.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
*/
#include "ch.h"

#ifdef TINYSA_F303
//#ifdef TINYSA_F303
#ifdef TINYSA_F072
#error "Remove comment for #ifdef TINYSA_F303"
#endif
#ifndef TINYSA4
#define TINYSA4
#endif
#define TINYSA4_PROTO
#endif
//#endif

#ifdef TINYSA_F072
#ifdef TINYSA_F303
Expand Down Expand Up @@ -88,6 +88,7 @@
#define __MCU_CLOCK_SHIFT__
#endif
#ifdef TINYSA4
#define __GUARD__
#define __MCU_CLOCK_SHIFT__
#define __ULTRA__
#define __USE_RTC__ // Enable RTC clock
Expand Down Expand Up @@ -1136,6 +1137,17 @@ void spi_init(void);
* flash.c
*/

#ifdef __GUARD__
#define GUARDS_MAX 8
typedef struct {
bool enabled;
freq_t start;
freq_t end;
float level;
} guard_t;
#endif


typedef struct setting
{
uint32_t magic;
Expand All @@ -1150,6 +1162,9 @@ typedef struct setting
bool pulse; // bool
bool stored[TRACES_MAX]; // enum
bool normalized[TRACES_MAX]; // enum
#ifdef __GUARD__
guard_t guards[GUARDS_MAX];
#endif

uint8_t mode; // enum
uint8_t below_IF; // enum
Expand Down Expand Up @@ -1725,7 +1740,7 @@ void interpolate_maximum(int m);
void calibrate_modulation(int modulation, int8_t *correction);

enum {
M_OFF, M_IMD, M_OIP3, M_PHASE_NOISE, M_SNR, M_PASS_BAND, M_LINEARITY, M_AM, M_FM, M_THD, M_CP, M_NF_TINYSA, M_NF_STORE, M_NF_VALIDATE, M_NF_AMPLIFIER, M_DECONV
M_OFF, M_IMD, M_OIP3, M_PHASE_NOISE, M_SNR, M_PASS_BAND, M_LINEARITY, M_AM, M_FM, M_THD, M_CP, M_NF_TINYSA, M_NF_STORE, M_NF_VALIDATE, M_NF_AMPLIFIER, M_GUARD, M_DECONV
};

enum {
Expand Down
13 changes: 11 additions & 2 deletions sa_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,11 @@ void limits_update(void)
}
#endif

#ifdef __GUARD__
void guards_update(void) {
}
#endif

void copy_trace(int f, int t)
{
if (f == t)
Expand Down Expand Up @@ -5182,9 +5187,13 @@ static volatile int dummy;
}
#endif


if (MODE_INPUT(setting.mode)) {
for (int t=0; t<TRACES_MAX;t++) {
#ifdef __GUARD__
if (setting.measurement == M_GUARD && RSSI > setting.guards[current_guard].level) {
// guard_exceeded();
}
#endif
for (int t=0; t<TRACES_MAX;t++) { // Calculate all traces
if (setting.stored[t])
continue;
#ifdef __ULTRA__
Expand Down
136 changes: 136 additions & 0 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,9 @@ enum {
KM_LEVEL,
#ifdef __LIMITS__
KM_LIMIT_FREQ, KM_LIMIT_LEVEL,
#endif
#ifdef __GUARD__
KM_GUARD_START, KM_GUARD_END, KM_GUARD_LEVEL,
#endif
KM_MARKER_TIME,
// #35
Expand Down Expand Up @@ -1409,6 +1412,11 @@ static const struct {
[KM_LIMIT_FREQ] = {keypads_freq , "FREQ"}, // KM_LIMIT_FREQ
[KM_LIMIT_LEVEL] = {keypads_plusmin_unit , "LEVEL"}, // KM_LIMIT_LEVEL
#endif
#ifdef __GUARD__
[KM_GUARD_START] = {keypads_freq , "START\nFREQ"}, // KM_GUARD_START
[KM_GUARD_END] = {keypads_freq , "END\nFREQ"}, // KM_GUARD_END
[KM_GUARD_LEVEL] = {keypads_plusmin_unit , "LEVEL"}, // KM_GUARD_LEVEL
#endif
[KM_MARKER_TIME] = {keypads_time , "MARKER\nTIME"}, // KM_MARKER_TIME
[KM_VAR] = {keypads_freq , "JOG\nSTEP"}, // jog step
#ifdef __NOISE_FIGURE__
Expand Down Expand Up @@ -1455,6 +1463,10 @@ static const menuitem_t menu_subtract_trace[];
static const menuitem_t menu_limit_modify[];
static const menuitem_t menu_limit_select[];
#endif
#ifdef __GUARD__
static const menuitem_t menu_guard_modify[];
static const menuitem_t menu_guard_select[];
#endif
static const menuitem_t menu_average[];
static const menuitem_t menu_reffer[];
static const menuitem_t menu_sweep_points[];
Expand Down Expand Up @@ -2844,6 +2856,12 @@ static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb)
}
break;
#endif
#ifdef __GUARD__
case M_GUARD:
menu_push_submenu(menu_guard_select);
goto leave;
break;
#endif
#ifdef __FFT_DECONV__
case M_DECONV:
set_average(0,AV_DECONV);
Expand Down Expand Up @@ -3146,6 +3164,28 @@ static UI_FUNCTION_ADV_CALLBACK(menu_limit_select_acb)

#endif

#ifdef __GUARD__
uint8_t active_guard = 0;
static UI_FUNCTION_ADV_CALLBACK(menu_guard_select_acb)
{
(void)item;
if(b){
int count = 0;
for (int i=0;i<GUARDS_MAX;i++) {if (setting.guards[i].enabled) count++; }
if (count == 0) setting.guards[0].enabled = true;
b->icon = (setting.guards[data].enabled?BUTTON_ICON_CHECK:BUTTON_ICON_NOCHECK) ;
plot_printf(b->text, sizeof(b->text), "%.6FHz\n%.6FHz", (float)setting.guards[data].start, (float)setting.guards[data].end);
return;
}
active_guard = data;
setting.guards[active_guard].enabled = true;
dirty = true;
guards_update();
menu_push_submenu(menu_guard_modify);
}

#endif

extern const menuitem_t menu_marker_select[];

static UI_FUNCTION_ADV_CALLBACK(menu_marker_modify_acb)
Expand Down Expand Up @@ -3268,6 +3308,28 @@ static UI_FUNCTION_CALLBACK(menu_limit_disable_cb)
}
#endif


#ifdef __GUARD__
static UI_FUNCTION_CALLBACK(menu_guard_disable_cb)
{
(void)item;
(void)data;
int count = 0;
for (int i=0;i<GUARDS_MAX;i++) {if (setting.guards[i].enabled) count++; }
if (count == 1 && setting.guards[active_guard].enabled) {
drawMessageBox("Error", "At least one entry",1000);
return;
}

if (active_guard<GUARDS_MAX){
setting.guards[active_guard].enabled = false;
dirty = true;
guards_update();
menu_move_back(false);
}
}
#endif

#ifdef TINYSA4
static const uint16_t rbwsel_x10[]={0,2,10,30,100,300,1000,3000,6000,8500};
static const char* rbwsel_text[]={"AUTO","200","1k","3k","10k","30k","100k","300k","600k","850k"};
Expand Down Expand Up @@ -3780,6 +3842,9 @@ enum {
FMT_CFG_FILE,
FMT_PRS_FILE,
FMT_TBL_FILE,
#ifdef __GUARD__
FMT_GRD_FILE,
#endif
};

// Save file extension
Expand All @@ -3793,6 +3858,7 @@ static const char *file_ext[] = {
[FMT_CFG_FILE] = "cfg",
[FMT_PRS_FILE] = "prs",
[FMT_TBL_FILE] = "tbl",
[FMT_GRD_FILE] = "grd",
};

static void sa_save_file(uint8_t format);
Expand Down Expand Up @@ -4055,6 +4121,28 @@ static const menuitem_t menu_limit_select[] = {
};
#endif

#ifdef __GUARD__
static const menuitem_t menu_guard_modify[] =
{
{ MT_KEYPAD, KM_GUARD_START, "START\n\b%s", "Start"},
{ MT_KEYPAD, KM_GUARD_END, "END\n\b%s", "End"},
{ MT_KEYPAD, KM_GUARD_LEVEL, "LEVEL\n\b%s", "Level"},
{ MT_CALLBACK,0, "DISABLE", menu_guard_disable_cb},
{ MT_NONE, 0, NULL, menu_back} // next-> menu_back
};

static const menuitem_t menu_guard_select[] = {
{ MT_ADV_CALLBACK | MT_REPEATS, DATA_STARTS_REPEATS(0,GUARDS_MAX), MT_CUSTOM_LABEL, menu_guard_select_acb },
#ifdef __USE_SD_CARD__
{ MT_CALLBACK, FMT_GRD_FILE, "GUARD"S_RARROW"\nSD", menu_sdcard_cb},
#ifdef __SD_FILE_BROWSER__
{ MT_CALLBACK, FMT_GRD_FILE, "SD"S_RARROW"\nGUARD", menu_sdcard_browse_cb },
#endif
#endif
{ MT_NONE, 0, NULL, menu_back} // next-> menu_back
};
#endif

#if 0
const menuitem_t menu_marker_sel[] = {
{ MT_CALLBACK, 1, "MARKER %d", menu_marker_sel_cb },
Expand Down Expand Up @@ -4385,6 +4473,9 @@ static const menuitem_t menu_measure2[] = {
#ifdef __NOISE_FIGURE__
{ MT_SUBMENU | MT_LOW, 0, "NOISE\nFIGURE", menu_measure_noise_figure},
#endif
#ifdef __GUARD__
{ MT_ADV_CALLBACK, M_GUARD, "GUARD", menu_measure_acb},
#endif
#ifdef __FFT_DECONV__
{ MT_ADV_CALLBACK, M_DECONV, "DECONV", menu_measure_acb},
#endif
Expand Down Expand Up @@ -4866,6 +4957,20 @@ static void fetch_numeric_target(uint8_t mode)
uistat.value = value(setting.limits[current_trace][active_limit].level);
plot_printf(uistat.text, sizeof uistat.text, "%.1f", uistat.value);
break;
#endif
#ifdef __GUARD__
case KM_GUARD_START:
uistat.freq_value = setting.guards[active_guard].start;
plot_printf(uistat.text, sizeof uistat.text, "%.3QHz", uistat.freq_value);
break;
case KM_GUARD_END:
uistat.freq_value = setting.guards[active_guard].start;
plot_printf(uistat.text, sizeof uistat.text, "%.3QHz", uistat.freq_value);
break;
case KM_GUARD_LEVEL:
uistat.value = value(setting.guards[active_guard].level);
plot_printf(uistat.text, sizeof uistat.text, "%.1f", uistat.value);
break;
#endif
case KM_NOISE:
uistat.value = setting.noise;
Expand Down Expand Up @@ -5091,6 +5196,23 @@ set_numeric_value(void)
dirty = true;
limits_update();
break;
#endif
#ifdef __GUARD__
case KM_GUARD_START:
setting.guards[active_guard].start = uistat.freq_value - (setting.frequency_offset - FREQUENCY_SHIFT);
dirty = true;
guards_update();
break;
case KM_GUARD_END:
setting.guards[active_guard].end = uistat.freq_value - (setting.frequency_offset - FREQUENCY_SHIFT);
dirty = true;
guards_update();
break;
case KM_GUARD_LEVEL:
setting.guards[active_guard].level = to_dBm(uistat.value);
dirty = true;
guards_update();
break;
#endif
case KM_NOISE:
set_noise(uistat.value);
Expand Down Expand Up @@ -7062,6 +7184,20 @@ static void sa_save_file(uint8_t format) {
}
}
break;
#ifdef __GUARD__
case FMT_GRD_FILE:
for (i = 0; i < GUARDS_MAX && res == FR_OK; i++) {
if (setting.guards[i].enabled) {
char *buf = (char *)spi_buffer;
buf += plot_printf(buf, 100, "%U, ", setting.guards[i].start);
buf += plot_printf(buf, 100, "%U, ", setting.guards[i].end);
buf += plot_printf(buf, 100, "%f ", setting.guards[i].level);
buf += plot_printf(buf, 100, "\r\n");
res = f_write(fs_file, (char *)spi_buffer, buf - (char *)spi_buffer, &size);
}
}
break;
#endif
#ifdef __SD_CARD_DUMP_FIRMWARE__
/*
* Dump firmware to SD card as bin file image
Expand Down

0 comments on commit 442a835

Please sign in to comment.