Skip to content

Commit

Permalink
Ass SWC5351 generator support
Browse files Browse the repository at this point in the history
Restore I2C read command
Fix 'measure' command
Little faster interpolation
  • Loading branch information
DiSlord committed Oct 29, 2024
1 parent 1ba7d1a commit 4e38aa6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
38 changes: 23 additions & 15 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static uint16_t p_sweep = 0;
float measured[2][SWEEP_POINTS_MAX][2];

#undef VERSION
#define VERSION "1.2.40"
#define VERSION "1.2.42"

// Version text, displayed in Config->Version menu, also send by info command
const char *info_about[]={
Expand Down Expand Up @@ -590,6 +590,7 @@ VNA_SHELL_FUNCTION(cmd_config) {
"|lcshunt" // Enable LC shunt measure option
"|lcseries" // Enable LC series measure option
"|xtal" // Enable XTAL measure option
"|filter" // Enable filter measure option
#endif
#ifdef __S11_CABLE_MEASURE__
"|cable" // Enable S11 cable measure option
Expand Down Expand Up @@ -990,9 +991,10 @@ static void load_settings(void) {
lever_mode = bk.leveler;
config._vna_mode = get_backup_data32(4) | (1<<VNA_MODE_BACKUP); // refresh backup settings
set_bandwidth(bk.bw);
}
} else // Try load 0 slot
caldata_recall(0);
} else
caldata_recall(0); // Try load 0 slot
} else
caldata_recall(0); // Try load 0 slot
update_frequencies();
#ifdef __VNA_MEASURE_MODULE__
plot_set_measure_mode(current_props._measure);
Expand Down Expand Up @@ -1829,7 +1831,7 @@ static void apply_CH1_error_term(float data[4], float c_data[CAL_TYPE_COUNT][2])
data[2] = s21mr * c_data[ETERM_ET][0] - s21mi * c_data[ETERM_ET][1];
data[3] = s21mi * c_data[ETERM_ET][0] + s21mr * c_data[ETERM_ET][1];
if (cal_status & CALSTAT_ENHANCED_RESPONSE) {
// S21a = S21m * Et` * (1 - Es * S11a)
// S21a*= 1 - Es * S11a
float esr = 1.0f - (c_data[ETERM_ES][0] * data[0] - c_data[ETERM_ES][1] * data[1]);
float esi = 0.0f - (c_data[ETERM_ES][1] * data[0] + c_data[ETERM_ES][0] * data[1]);
float re = data[2];
Expand Down Expand Up @@ -1964,7 +1966,7 @@ static void cal_interpolate(int idx, freq_t f, float data[CAL_TYPE_COUNT][2]){
idx = src_points;
goto copy_point;
}
// Search k1
// Calculate k for linear interpolation
freq_t span = cal_frequency1 - cal_frequency0;
idx = (uint64_t)(f - cal_frequency0) * (uint64_t)src_points / span;
uint64_t v = (uint64_t)span * idx + src_points/2;
Expand All @@ -1975,28 +1977,27 @@ static void cal_interpolate(int idx, freq_t f, float data[CAL_TYPE_COUNT][2]){
// Not need interpolate
if (f == src_f0) goto copy_point;

float k1 = (delta == 0) ? 0.0f : (float)(f - src_f0) / delta;
float k = (delta == 0) ? 0.0f : (float)(f - src_f0) / delta;
// avoid glitch between freqs in different harmonics mode
uint32_t hf0 = si5351_get_harmonic_lvl(src_f0);
if (hf0 != si5351_get_harmonic_lvl(src_f1)) {
// f in prev harmonic, need extrapolate from prev 2 points
if (hf0 == si5351_get_harmonic_lvl(f)){
if (idx < 1) goto copy_point; // point limit
idx--;
k1+= 1.0f;
k+= 1.0f;
}
// f in next harmonic, need extrapolate from next 2 points
else {
if (idx >= src_points) goto copy_point; // point limit
idx++;
k1-= 1.0f;
k-= 1.0f;
}
}
// Interpolate by k1
float k0 = 1.0f - k1;
// Interpolate by k
for (eterm = 0; eterm < CAL_TYPE_COUNT; eterm++) {
data[eterm][0] = cal_data[eterm][idx][0] * k0 + cal_data[eterm][idx+1][0] * k1;
data[eterm][1] = cal_data[eterm][idx][1] * k0 + cal_data[eterm][idx+1][1] * k1;
data[eterm][0] = cal_data[eterm][idx][0] + k * (cal_data[eterm][idx+1][0] - cal_data[eterm][idx][0]);
data[eterm][1] = cal_data[eterm][idx][1] + k * (cal_data[eterm][idx+1][1] - cal_data[eterm][idx][0]);
}
return;
// Direct point copy
Expand Down Expand Up @@ -2589,6 +2590,13 @@ VNA_SHELL_FUNCTION(cmd_si5351time)
#ifdef ENABLE_SI5351_REG_WRITE
VNA_SHELL_FUNCTION(cmd_si5351reg)
{
#if 0
(void) argc;
uint32_t reg = my_atoui(argv[0]);
uint8_t buf[1] = {0xAA};
if (si5351_bulk_read(reg, buf, 1))
shell_printf("si reg[%d] = 0x%02x" VNA_SHELL_NEWLINE_STR, reg, buf[0]);
#else
if (argc != 2) {
shell_printf("usage: si reg data" VNA_SHELL_NEWLINE_STR);
return;
Expand All @@ -2597,6 +2605,7 @@ VNA_SHELL_FUNCTION(cmd_si5351reg)
uint8_t dat = my_atoui(argv[1]);
uint8_t buf[] = { reg, dat };
si5351_bulk_write(buf, 2);
#endif
}
#endif

Expand Down Expand Up @@ -3450,8 +3459,7 @@ void hard_fault_handler_c(uint32_t *sp)
uint32_t psr = sp[7];
int y = 0;
int x = 20;
lcd_set_background(LCD_BG_COLOR);
lcd_set_foreground(LCD_FG_COLOR);
lcd_set_colors(LCD_FG_COLOR, LCD_BG_COLOR);
lcd_printf(x, y+=FONT_STR_HEIGHT, "SP 0x%08x", (uint32_t)sp);
lcd_printf(x, y+=FONT_STR_HEIGHT, "R0 0x%08x", r0);
lcd_printf(x, y+=FONT_STR_HEIGHT, "R1 0x%08x", r1);
Expand Down
2 changes: 1 addition & 1 deletion nanovna.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
* main.c
*/
// Minimum frequency set
#define FREQUENCY_MIN 800
#define FREQUENCY_MIN 600
// Maximum frequency set
#define FREQUENCY_MAX 2700000000U
// Frequency threshold (max frequency for si5351, harmonic mode after)
Expand Down
45 changes: 37 additions & 8 deletions si5351.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,12 @@ void si5351_bulk_write(const uint8_t *buf, int len)
}

#if 0
static bool si5351_bulk_read(uint8_t reg, uint8_t* buf, int len)
{
i2cAcquireBus(&I2CD1);
msg_t mr = i2cMasterTransmitTimeout(&I2CD1, SI5351_I2C_ADDR, &reg, 1, buf, len, 1000);
i2cReleaseBus(&I2CD1);
return mr == MSG_OK;
bool si5351_bulk_read(uint8_t reg, uint8_t* buf, int len) {
return i2c_receive(SI5351_I2C_ADDR, &reg, 1, buf, len);
}
#endif

#if 0
static void si5351_wait_pll_lock(void)
{
uint8_t status;
Expand Down Expand Up @@ -491,12 +489,43 @@ CONST_BAND band_strategy_t band_strategy_36H_MS5351[] = {
{ 11, SI5351_FIXED_MULT,{ 4},11,13, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_8MA, 95, 95, 11*12*4} // 10};
};

// Mode for board v3.6+ and SWC5351 installed
CONST_BAND band_strategy_t band_strategy_SWC5351[] = {
{ 0U, 0, { 0}, 0, 0, -1, -1, -1, -1, 1}, // 0
{ 26000U, SI5351_FIXED_PLL, { 6}, 1, 1, SI5351_CLK_DRIVE_STRENGTH_2MA, SI5351_CLK_DRIVE_STRENGTH_2MA, 0, 0, 1}, // 1
{ 120000000U, SI5351_FIXED_PLL, {32}, 1, 1, SI5351_CLK_DRIVE_STRENGTH_4MA, SI5351_CLK_DRIVE_STRENGTH_2MA, 0, 0, 1}, // 2

{ 150000000U, SI5351_FIXED_MULT,{ 8}, 1, 1, SI5351_CLK_DRIVE_STRENGTH_6MA, SI5351_CLK_DRIVE_STRENGTH_4MA, 0, 0, 1}, // 3

{ 200000000U, SI5351_FIXED_MULT,{ 6}, 1, 1, SI5351_CLK_DRIVE_STRENGTH_6MA, SI5351_CLK_DRIVE_STRENGTH_4MA, 0, 0, 1}, // 4
{ 1, SI5351_FIXED_MULT,{ 4}, 1, 1, SI5351_CLK_DRIVE_STRENGTH_6MA, SI5351_CLK_DRIVE_STRENGTH_4MA, 0, 0, 1}, // 5

{ 460000000U, SI5351_FIXED_MULT,{ 8}, 3, 5, SI5351_CLK_DRIVE_STRENGTH_6MA, SI5351_CLK_DRIVE_STRENGTH_4MA, 40, 40, 3*5*8}, // 6
{ 600000000U, SI5351_FIXED_MULT,{ 6}, 3, 5, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_4MA, 40, 40, 3*5*6}, // 7
{ 3, SI5351_FIXED_MULT,{ 4}, 3, 5, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_4MA, 50, 50, 3*5*4}, // 8

{ 1000000000U, SI5351_FIXED_MULT,{ 6}, 5, 7, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_6MA, 70, 70, 5*7*6}, // 9
{ 5, SI5351_FIXED_MULT,{ 4}, 5, 7, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_6MA, 70, 70, 5*7*4}, //10

{ 1800000000U, SI5351_FIXED_MULT,{ 4}, 7, 9, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_8MA, 70, 70, 7*9*4}, //11
{ 7, SI5351_FIXED_MULT,{ 4}, 7, 9, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_8MA, 70, 70, 7*9*4}, //12

{ 2400000000U, SI5351_FIXED_MULT,{ 4}, 9,11, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_8MA, 85, 85, 9*11*4}, //13
{ 9, SI5351_FIXED_MULT,{ 4}, 9,11, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_8MA, 95, 95, 9*11*4}, //14

{ 11, SI5351_FIXED_MULT,{ 4},11,12, SI5351_CLK_DRIVE_STRENGTH_8MA, SI5351_CLK_DRIVE_STRENGTH_8MA, 95, 95, 11*12*4} //15

};

void si5351_set_band_mode(uint16_t t) {
static const band_strategy_t *bs[] = {
#if defined(NANOVNA_F303)
band_s = t ? band_strategy_36H_MS5351 : band_strategy_H4_SI5351; // !!!! no test MS5351 on H4 board
band_strategy_H4_SI5351, band_strategy_36H_MS5351, band_strategy_SWC5351
#else
band_s = t ? band_strategy_36H_MS5351 : band_strategy_33H_SI5351;
band_strategy_33H_SI5351, band_strategy_36H_MS5351, band_strategy_SWC5351
#endif
};
band_s = bs[t];
}

uint32_t
Expand Down
1 change: 1 addition & 0 deletions si5351.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void si5351_set_band_mode(uint16_t t);

// Defug use functions
void si5351_bulk_write(const uint8_t *buf, int len);
bool si5351_bulk_read(uint8_t reg, uint8_t* buf, int len);
void si5351_set_timing(int i, int v);
void si5351_update_band_config(int idx, uint32_t pidx, uint32_t v);
void si5351_set_tcxo(uint32_t xtal);
Expand Down
9 changes: 7 additions & 2 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -1892,11 +1892,16 @@ static UI_FUNCTION_CALLBACK(menu_sdcard_cb) {

static UI_FUNCTION_ADV_CALLBACK(menu_band_sel_acb) {
(void)data;
static const char* gen_names[] = {
"Si5351",
"MS5351",
"SWC5351"
};
if (b) {
b->p1.text = config._band_mode == 0 ? "Si5351" : "MS5351";
b->p1.text = gen_names[config._band_mode];
return;
}
config._band_mode = config._band_mode == 0 ? 1 : 0;
if (++config._band_mode >= ARRAY_COUNT(gen_names)) config._band_mode = 0;
si5351_set_band_mode(config._band_mode);
}

Expand Down

0 comments on commit 4e38aa6

Please sign in to comment.