From a7ae6d419751632abd442325147368fcf4c1875d Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Wed, 15 Apr 2020 16:12:58 +0200 Subject: [PATCH 1/8] Serial experiements --- halconf.h | 2 +- main.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- mcuconf.h | 2 +- sa_core.c | 2 +- 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/halconf.h b/halconf.h index 1450b9f..af6c283 100644 --- a/halconf.h +++ b/halconf.h @@ -132,7 +132,7 @@ * @brief Enables the SERIAL subsystem. */ #if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__) -#define HAL_USE_SERIAL FALSE +#define HAL_USE_SERIAL TRUE #endif /** diff --git a/main.c b/main.c index bba6558..84a5b5f 100644 --- a/main.c +++ b/main.c @@ -16,9 +16,14 @@ * the Free Software Foundation, Inc., 51 Franklin Street, * Boston, MA 02110-1301, USA. */ +//#define HAL_USE_SERIAL 1 +//#define STM32_SERIAL_USE_USART1 1 #include "ch.h" #include "hal.h" + +//#include "hal_serial.h" + #include "usbcfg.h" #ifdef __VNA__ #include "si5351.h" @@ -2591,6 +2596,39 @@ static DACConfig dac1cfg1 = { }; #endif +#if 0 +/* + * UART driver configuration structure. + */ +static UARTConfig uart_cfg_1 = { + NULL, //txend1, + NULL, //txend2, + NULL, //rxend, + NULL, //rxchar, + NULL, //rxerr, + 800000, + 0, + 0, //USART_CR2_LINEN, + 0 +}; +#endif + +#if 1 +static const SerialConfig default_config = +{ + 9600, + 0, + USART_CR2_STOP2_BITS, + 0 +}; +#endif + +myWrite(char *buf) +{ + int len = strlen(buf); + while(len-- > 0) + sdPut(&SD1,*buf++); +} // Main thread stack size defined in makefile USE_PROCESS_STACKSIZE = 0x200 // Profile stack usage (enable threads command by def ENABLE_THREADS_COMMAND) show: @@ -2625,7 +2663,35 @@ int main(void) usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); -/* +#if 0 + /* + * UART initialize + */ + uartStart(&UARTD1, &uart_cfg_1); + + uartStartSend(&UARTD1, 1, "H"); + uartStartReceive(&UARTD1, 1, buf); +#endif + +#if 1 + palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(1)); // USART1 TX. + palSetPadMode(GPIOA,10, PAL_MODE_ALTERNATE(1)); // USART1 RX. + + uint8_t buf[10]; + sdStart(&SD1,&default_config); + osalThreadSleepMilliseconds(10); + myWrite("Hallo!?"); + + osalThreadSleepMilliseconds(10); + + sdReadTimeout(&SD1,buf,10, 10); + + sdWrite(&SD1,(const uint8_t *)"Test123",7); + osalThreadSleepMilliseconds(10); + sdReadTimeout(&SD1,buf,10, 10); +#endif + + /* * SPI LCD Initialize */ ili9341_init(); diff --git a/mcuconf.h b/mcuconf.h index 6d4550a..e04096c 100644 --- a/mcuconf.h +++ b/mcuconf.h @@ -201,7 +201,7 @@ /* * UART driver system settings. */ -#define STM32_UART_USE_USART1 TRUE +#define STM32_UART_USE_USART1 FALSE #define STM32_UART_USE_USART2 FALSE #define STM32_UART_USART1_IRQ_PRIORITY 3 #define STM32_UART_USART2_IRQ_PRIORITY 3 diff --git a/sa_core.c b/sa_core.c index ebf9998..3175531 100644 --- a/sa_core.c +++ b/sa_core.c @@ -888,7 +888,7 @@ float perform(bool break_on_operation, int i, int32_t f, int tracking) return(0); float signal_path_loss; if (setting_mode == M_LOW) - signal_path_loss = -9.5; // Loss in dB + signal_path_loss = -9.5; // Loss in dB, -9.5 for v0.1, -12.5 for v0.2 else signal_path_loss = 7; // Loss in dB (+ is gain) float subRSSI = SI4432_RSSI(lf, MODE_SELECT(setting_mode))+settingLevelOffset()+ setting_attenuate - signal_path_loss; From 2d430a4f582a354c85b6c9c280060aca9a144a9d Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Thu, 16 Apr 2020 10:54:32 +0200 Subject: [PATCH 2/8] Small improvements --- ili9341.c | 4 +- main.c | 39 ++++++++++++++--- nanovna.h | 1 + plot.c | 22 ++++++++++ sa_core.c | 129 ++++++++++++++++++++++++++---------------------------- ui_sa.c | 2 +- 6 files changed, 120 insertions(+), 77 deletions(-) diff --git a/ili9341.c b/ili9341.c index 3b21d4e..bba3dd9 100644 --- a/ili9341.c +++ b/ili9341.c @@ -294,9 +294,11 @@ static const uint8_t ili9341_init_seq[] = { // POWER_CONTROL_2 ILI9341_POWER_CONTROL_2, 1, 0x11, // VCOM_CONTROL_1 - ILI9341_VCOM_CONTROL_1, 2, 0x35, 0x3E, +// ILI9341_VCOM_CONTROL_1, 2, 0x35, 0x3E, + ILI9341_VCOM_CONTROL_1, 2, 0x3e, 0x28, // VCOM_CONTROL_2 ILI9341_VCOM_CONTROL_2, 1, 0xBE, +// ILI9341_VCOM_CONTROL_2, 1, 0x86, // MEMORY_ACCESS_CONTROL //ILI9341_MEMORY_ACCESS_CONTROL, 1, 0x48, // portlait ILI9341_MEMORY_ACCESS_CONTROL, 1, DISPLAY_ROTATION_0, // landscape diff --git a/main.c b/main.c index 84a5b5f..78790ba 100644 --- a/main.c +++ b/main.c @@ -2613,7 +2613,7 @@ static UARTConfig uart_cfg_1 = { }; #endif -#if 1 +#if 0 static const SerialConfig default_config = { 9600, @@ -2621,14 +2621,34 @@ static const SerialConfig default_config = USART_CR2_STOP2_BITS, 0 }; -#endif -myWrite(char *buf) + +void myWrite(char *buf) { int len = strlen(buf); - while(len-- > 0) + while(len-- > 0) { sdPut(&SD1,*buf++); + osalThreadSleepMicroseconds(1000); + } +} + +static int serial_count = 0; +int mySerialReadline(unsigned char *buf, int len) +{ + int i; + do { + i = sdReadTimeout(&SD1,&buf[serial_count], 20-serial_count,TIME_IMMEDIATE); + serial_count += i; + if (i > 0) + osalThreadSleepMicroseconds(1000); + } while (serial_count < len && i > 0); + if (buf[serial_count-1] == '\n') { + serial_count = 0; + return(i); + } else + return 0; } +#endif // Main thread stack size defined in makefile USE_PROCESS_STACKSIZE = 0x200 // Profile stack usage (enable threads command by def ENABLE_THREADS_COMMAND) show: @@ -2673,22 +2693,27 @@ int main(void) uartStartReceive(&UARTD1, 1, buf); #endif -#if 1 +#if 0 palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(1)); // USART1 TX. palSetPadMode(GPIOA,10, PAL_MODE_ALTERNATE(1)); // USART1 RX. uint8_t buf[10]; sdStart(&SD1,&default_config); osalThreadSleepMilliseconds(10); - myWrite("Hallo!?"); + mySerialWrite("Hallo!?\n"); osalThreadSleepMilliseconds(10); + mySerialReadline(buf, 10); + sdReadTimeout(&SD1,buf,10, 10); sdWrite(&SD1,(const uint8_t *)"Test123",7); - osalThreadSleepMilliseconds(10); + osalThreadSleepMicroseconds(10); + sdReadTimeout(&SD1,buf,10,TIME_IMMEDIATE); sdReadTimeout(&SD1,buf,10, 10); + int i = sdReadTimeout(&SD1,buf,10,TIME_IMMEDIATE); + #endif /* diff --git a/nanovna.h b/nanovna.h index 0c3ee99..40ca6b3 100644 --- a/nanovna.h +++ b/nanovna.h @@ -29,6 +29,7 @@ #define __ICONS__ #define __MEASURE__ #define __SELFTEST__ +#define __CALIBRATE__ /* * main.c diff --git a/plot.c b/plot.c index 0f246fe..893fe6a 100644 --- a/plot.c +++ b/plot.c @@ -1534,7 +1534,28 @@ draw_all_cells(bool flush_markmap) g = 255 - b - r return r, g, b */ + int r,g,b; +#if 0 + int ratio = (int)(1024 * (actual_t[i] - w_min) / (w_max - w_min)); + + r = ratio - 512; + if (r<0) r=0; + b = (1024 - ratio*4) - 512; + if (b<0) b=0; + g = 512-r-b; + if (r>255) r=255; + if (g>255) g=255; + if (b>255) b=255; + +#define gamma_correct(X,L) X = (L + X * (255 - L)/255 ) + gamma_correct(r,160); + gamma_correct(g,160); + gamma_correct(b,160); + +#endif + +#if 1 float ratio = (int)(510.0 * (actual_t[i] - w_min) / (w_max - w_min)); // float ratio = (i*2); // Uncomment for testing the waterfall colors b = 255 - ratio; @@ -1549,6 +1570,7 @@ draw_all_cells(bool flush_markmap) gamma_correct(r,128); gamma_correct(g,128); gamma_correct(b,128); +#endif #if 0 int k = (actual_t[i]+120)* 2 * 8; k &= 255; diff --git a/sa_core.c b/sa_core.c index 3175531..2aa9f9b 100644 --- a/sa_core.c +++ b/sa_core.c @@ -513,7 +513,11 @@ switch(m) { case M_LOW: // Mixed into 0 SI4432_Sel = 0; SI4432_Receive(); - SetSwitchReceive(); + if (setting_step_atten) { + SetSwitchTransmit(); + } else { + SetSwitchReceive(); + } SetAGCLNA(); SI4432_Sel = 1; @@ -1424,17 +1428,16 @@ void draw_cal_status(void) } // -------------------- Self testing ------------------------------------------------- -#ifdef __SELFTEST__ enum { TC_SIGNAL, TC_BELOW, TC_ABOVE, TC_FLAT, TC_MEASURE, TC_SET, TC_END, }; enum { - TP_SILENT, TPH_SILENT, TP_10MHZ, TP_10MHZEXTRA, TP_30MHZ, TPH_30MHZ + TP_SILENT, TPH_SILENT, TP_10MHZ, TP_10MHZEXTRA, TP_10MHZ_SWITCH, TP_30MHZ, TPH_30MHZ }; -#define TEST_COUNT 16 +#define TEST_COUNT 17 static const struct { int kind; @@ -1455,12 +1458,13 @@ static const struct { {TC_SIGNAL, TP_10MHZEXTRA, 10, 8, -13, 55, -60 }, // 7 BPF loss and stop band {TC_FLAT, TP_10MHZEXTRA, 10, 4, -18, 20, -60}, // 8 BPF pass band flatness {TC_BELOW, TP_30MHZ, 430, 60, -65, 0, -75}, // 9 LPF cutoff + {TC_SIGNAL, TP_10MHZ_SWITCH,20, 7, -58, 30, -90 }, // 10 Switch isolation {TC_END, 0, 0, 0, 0, 0, 0}, - {TC_MEASURE, TP_30MHZ, 30, 7, -22.5, 30, -70 }, // 11 Measure power level and noise - {TC_MEASURE, TP_30MHZ, 270, 4, -45, 30, -75 }, // 12 Measure powerlevel and noise - {TC_MEASURE, TPH_30MHZ, 270, 4, -45, 30, -75 }, // 13 Calibrate power high mode + {TC_MEASURE, TP_30MHZ, 30, 7, -22.5, 30, -70 }, // 12 Measure power level and noise + {TC_MEASURE, TP_30MHZ, 270, 4, -45, 30, -75 }, // 13 Measure powerlevel and noise + {TC_MEASURE, TPH_30MHZ, 270, 4, -45, 30, -65 }, // 14 Calibrate power high mode {TC_END, 0, 0, 0, 0, 0, 0}, - {TC_MEASURE, TP_30MHZ, 30, 1, -20, 30, -70 }, // 15 Measure RBW step time + {TC_MEASURE, TP_30MHZ, 30, 1, -20, 30, -70 }, // 16 Measure RBW step time {TC_END, 0, 0, 0, 0, 0, 0}, }; @@ -1539,20 +1543,29 @@ void cell_draw_test_info(int x0, int y0) #define fabs(X) ((X)<0?-(X):(X)) -int validate_peak_within(int i, float margin) +int validate_signal_within(int i, float margin) { - if (fabs(peakLevel-test_case[i].pass) > margin) - return false; - return(test_case[i].center * 1000000 - 100000 < peakFreq && peakFreq < test_case[i].center * 1000000 + 100000 ); + test_fail_cause[i] = "Signal level "; + if (fabs(peakLevel-test_case[i].pass) > 2*margin) { + return TS_FAIL; + } + if (fabs(peakLevel-test_case[i].pass) > margin) { + return TS_CRITICAL; + } + test_fail_cause[i] = "Frequency "; + if (peakFreq < test_case[i].center * 1000000 - 100000 || test_case[i].center * 1000000 + 100000 < peakFreq ) + return TS_FAIL; + test_fail_cause[i] = ""; + return TS_PASS; } int validate_peak_below(int i, float margin) { return(test_case[i].pass - peakLevel > margin); } -int validate_below(void) { +int validate_below(int tc, int from, int to) { int status = TS_PASS; - for (int j = 0; j < POINTS_COUNT; j++) { + for (int j = from; j < to; j++) { if (actual_t[j] > stored_t[j] - 5) status = TS_CRITICAL; else if (actual_t[j] > stored_t[j]) { @@ -1560,11 +1573,14 @@ int validate_below(void) { break; } } + if (status != TS_PASS) + test_fail_cause[tc] = "Above "; return(status); } int validate_flatness(int i) { volatile int j; + test_fail_cause[i] = "Passband "; for (j = peakIndex; j < POINTS_COUNT; j++) { if (actual_t[j] < peakLevel - 3) // Search right -3dB break; @@ -1577,10 +1593,11 @@ int validate_flatness(int i) { } if (peakIndex - j < test_case[i].width) return(TS_FAIL); + test_fail_cause[i] = ""; return(TS_PASS); } -int validate_above(void) { +int validate_above(int tc) { int status = TS_PASS; for (int j = 0; j < POINTS_COUNT; j++) { if (actual_t[j] < stored_t[j] + 5) @@ -1590,6 +1607,8 @@ int validate_above(void) { break; } } + if (status != TS_PASS) + test_fail_cause[tc] = "Below "; return(status); } @@ -1607,32 +1626,12 @@ int test_validate(int i) SetPowerLevel(test_case[i].pass); goto common; case TC_MEASURE: - case TC_SIGNAL: // Validate signal - common: - if (validate_peak_within(i, 5.0)) // Validate Peak - current_test_status = TS_PASS; - else if (validate_peak_within(i, 10.0)) - current_test_status = TS_CRITICAL; - else - current_test_status = TS_FAIL; - if (current_test_status != TS_PASS) - test_fail_cause[i] = "Peak "; + case TC_SIGNAL: // Validate signal + common: current_test_status = validate_signal_within(i, 5.0); if (current_test_status == TS_PASS) { // Validate noise floor - for (int j = 0; j < POINTS_COUNT/2 - test_case[i].width; j++) { - if (actual_t[j] > test_case[i].stop - 5) - current_test_status = TS_CRITICAL; - else if (actual_t[j] > test_case[i].stop) { - current_test_status = TS_FAIL; - break; - } - } - for (int j = POINTS_COUNT/2 + test_case[i].width; j < POINTS_COUNT; j++) { - if (actual_t[j] > test_case[i].stop - 5) - current_test_status = TS_CRITICAL; - else if (actual_t[j] > test_case[i].stop) { - current_test_status = TS_FAIL; - break; - } + current_test_status = validate_below(i, 0, POINTS_COUNT/2 - test_case[i].width); + if (current_test_status == TS_PASS) { + current_test_status = validate_below(i, POINTS_COUNT/2 + test_case[i].width, POINTS_COUNT); } if (current_test_status != TS_PASS) test_fail_cause[i] = "Stopband "; @@ -1641,28 +1640,15 @@ int test_validate(int i) test_value = peakLevel; else test_value = 0; // Not valid - break; + break; case TC_ABOVE: // Validate signal above curve - for (int j = 0; j < POINTS_COUNT; j++) { - if (actual_t[j] < test_case[i].pass + 5) - current_test_status = TS_CRITICAL; - else if (actual_t[j] < test_case[i].pass) { - current_test_status = TS_FAIL; - break; - } - } - if (current_test_status != TS_PASS) - test_fail_cause[i] = "Above "; + current_test_status = validate_above(i); break; case TC_BELOW: // Validate signal below curve - current_test_status = validate_below(); - if (current_test_status != TS_PASS) - test_fail_cause[i] = "Above "; - break; + current_test_status = validate_below(i, 0, POINTS_COUNT); + break; case TC_FLAT: // Validate passband flatness current_test_status = validate_flatness(i); - if (current_test_status != TS_PASS) - test_fail_cause[i] = "Passband "; break; } @@ -1682,6 +1668,8 @@ int test_validate(int i) void test_prepare(int i) { setting_tracking = false; //Default test setup + setting_step_atten = false; + SetAttenuation(0); switch(test_case[i].setup) { // Prepare test conditions case TPH_SILENT: // No input signal SetMode(M_HIGH); @@ -1693,9 +1681,15 @@ common_silent: for (int j = 0; j < POINTS_COUNT; j++) stored_t[j] = test_case[i].pass; break; + case TP_10MHZ_SWITCH: + SetMode(M_LOW); + set_refer_output(2); + setting_step_atten = true; + goto common; case TP_10MHZEXTRA: // Swept receiver SetMode(M_LOW); setting_tracking = true; //Sweep BPF + frequency_IF = 434000000; // Center on SAW filters set_refer_output(2); goto common; case TP_10MHZ: // 10MHz input @@ -1719,11 +1713,12 @@ common_silent: set_refer_output(0); goto common; } + setting_auto_attenuation = false; + setting_attenuate = 0; trace[TRACE_STORED].enabled = true; SetReflevel(test_case[i].pass+10); set_sweep_frequency(ST_CENTER, (int32_t)(test_case[i].center * 1000000)); set_sweep_frequency(ST_SPAN, (int32_t)(test_case[i].span * 1000000)); - SetAttenuation(0); draw_cal_status(); } @@ -1745,11 +1740,10 @@ int add_spur(int f) } return 1; } -#endif + void self_test(void) { -#ifdef __SELFTEST__ #if 0 in_selftest = true; @@ -1800,7 +1794,7 @@ void self_test(void) int local_test_status; in_selftest = true; reset_settings(M_LOW); - int i = 14; // calibrate low mode power on 30 MHz; + int i = 15; // calibrate low mode power on 30 MHz; test_prepare(i); for (int j= 0; j < 32; j++ ) { test_prepare(i); @@ -1815,7 +1809,7 @@ void self_test(void) int local_test_status; in_selftest = true; reset_settings(M_LOW); - int i = 14; // calibrate low mode power on 30 MHz; + int i = 15; // calibrate low mode power on 30 MHz; test_prepare(i); setting_step_delay = 6000; for (int j= 0; j < 57; j++ ) { @@ -1845,7 +1839,7 @@ void self_test(void) } return; #else - + int old_IF = frequency_IF; in_selftest = true; menu_autosettings_cb(0); for (int i=0; i < TEST_COUNT; i++) { // All test cases waiting @@ -1857,6 +1851,7 @@ void self_test(void) show_test_info = TRUE; int i=0; while (test_case[i].kind != TC_END) { + frequency_IF = old_IF; test_prepare(i); test_acquire(i); // Acquire test test_status[i] = test_validate(i); // Validate test @@ -1877,8 +1872,6 @@ void self_test(void) reset_settings(M_LOW); in_selftest = false; #endif - -#endif } void reset_calibration(void) @@ -1897,7 +1890,7 @@ void calibrate(void) in_selftest = true; SetPowerLevel(100); reset_settings(M_LOW); - int i = 10; // calibrate low mode power on 30 MHz; + int i = 11; // calibrate low mode power on 30 MHz; for (int j= 0; j < CALIBRATE_RBWS; j++ ) { SetRBW(power_rbw[j]); test_prepare(i); @@ -1913,7 +1906,7 @@ void calibrate(void) chThdSleepMilliseconds(1000); } } - i = 11; // Measure 270MHz in low mode + i = 12; // Measure 270MHz in low mode SetRBW(100); test_prepare(i); test_acquire(i); // Acquire test @@ -1923,7 +1916,7 @@ void calibrate(void) config.high_level_offset = 0; /// Preliminary setting - i = 12; // Calibrate 270MHz in high mode + i = 13; // Calibrate 270MHz in high mode for (int j = 0; j < CALIBRATE_RBWS; j++) { SetRBW(power_rbw[j]); test_prepare(i); diff --git a/ui_sa.c b/ui_sa.c index eaf102f..b21fa89 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1384,7 +1384,7 @@ static void menu_item_modify_attribute( mark = true; } - } else if (MT_MASK(menu[item].type) != MT_CALLBACK && (menu == menu_drive || menu == menu_drive_wide || menu == menu_drive_wide2|| menu == menu_drive_wide3)) { + } else if (MT_MASK(menu[item].type) == MT_CALLBACK && (menu == menu_drive || menu == menu_drive_wide || menu == menu_drive_wide2|| menu == menu_drive_wide3)) { if (data == setting_drive){ mark = true; } From 8bffd8ba369d813ad4f1283387064372484c5a58 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Fri, 17 Apr 2020 09:09:25 +0200 Subject: [PATCH 3/8] Added harmonic mixer --- main.c | 8 ++--- nanovna.h | 11 ++++-- sa_core.c | 105 ++++++++++++++++++++++++++++++++++++++---------------- ui.c | 2 +- ui_sa.c | 35 +++++++++++++++--- 5 files changed, 118 insertions(+), 43 deletions(-) diff --git a/main.c b/main.c index 78790ba..157e039 100644 --- a/main.c +++ b/main.c @@ -37,10 +37,10 @@ #include #include -extern uint32_t minFreq; -extern uint32_t maxFreq; -uint32_t frequencyStart; -uint32_t frequencyStop; +extern float minFreq; +extern float maxFreq; +float frequencyStart; +float frequencyStop; int32_t frequencyExtra; #define START_MIN minFreq #define STOP_MAX maxFreq diff --git a/nanovna.h b/nanovna.h index 40ca6b3..3c6b621 100644 --- a/nanovna.h +++ b/nanovna.h @@ -30,6 +30,7 @@ #define __MEASURE__ #define __SELFTEST__ #define __CALIBRATE__ +#define __ULTRA__ /* * main.c @@ -124,12 +125,12 @@ int shell_printf(const char *fmt, ...); void toggle_sweep(void); void load_default_properties(void); -extern float perform(bool b, int i, int32_t f, int e); +extern float perform(bool b, int i, uint32_t f, int e); enum { AV_OFF, AV_MIN, AV_MAX_HOLD, AV_MAX_DECAY, AV_4, AV_16 }; enum { - M_LOW, M_HIGH, M_GENLOW, M_GENHIGH, + M_LOW, M_HIGH, M_GENLOW, M_GENHIGH, M_ULTRA }; enum { @@ -137,7 +138,11 @@ enum { }; #define MODE_OUTPUT(x) ((x) == M_GENLOW || (x) == M_GENHIGH ) +#ifdef __ULTRA__ +#define MODE_INPUT(x) ((x) == M_LOW || (x) == M_HIGH || (x) == M_ULTRA ) +#else #define MODE_INPUT(x) ((x) == M_LOW || (x) == M_HIGH ) +#endif #define MODE_HIGH(x) ((x) == M_HIGH || (x) == M_GENHIGH ) #define MODE_LOW(x) ((x) == M_LOW || (x) == M_GENLOW ) #define MODE_SELECT(x) (MODE_HIGH(x) ? 1 : 0) @@ -575,7 +580,7 @@ typedef struct uistat { int8_t digit; /* 0~5 */ int8_t digit_mode; int8_t current_trace; /* 0..3 */ - int32_t value; // for editing at numeric input area + float value; // for editing at numeric input area // uint32_t previous_value; uint8_t lever_mode; uint8_t marker_delta; diff --git a/sa_core.c b/sa_core.c index 2aa9f9b..631c45e 100644 --- a/sa_core.c +++ b/sa_core.c @@ -33,9 +33,9 @@ int setting_measurement; int vbwSteps = 1; -//int setting_spur = 0; -uint32_t minFreq = 0; -uint32_t maxFreq = 520000000; +int setting_spur = 0; +float minFreq = 0; +float maxFreq = 520000000; int setting_refer = -1; // Off by default const int reffer_freq[] = {30000000, 15000000, 10000000, 4000000, 3000000, 2000000, 1000000}; @@ -69,15 +69,24 @@ void reset_settings(int m) trace[TRACE_TEMP].enabled = false; setting_measurement = M_OFF; -// setting_spur = 0; + setting_spur = 0; switch(m) { case M_LOW: minFreq = 0; maxFreq = 520000000; - set_sweep_frequency(ST_START, (int32_t) 0); - set_sweep_frequency(ST_STOP, (int32_t) 350000000); + set_sweep_frequency(ST_START, (uint32_t) 0); + set_sweep_frequency(ST_STOP, (uint32_t) 350000000); setting_attenuate = 30; break; +#ifdef __ULTRA__ + case M_ULTRA: + minFreq = 770000000; + maxFreq = 4360000000; + set_sweep_frequency(ST_START, (uint32_t) 960000000); + set_sweep_frequency(ST_STOP, (uint32_t) 2100000000); + setting_attenuate = 30; + break; +#endif case M_GENLOW: setting_drive=8; minFreq = 0; @@ -290,6 +299,10 @@ void SetPowerLevel(int o) config.high_level_offset = new_offset; else if (setting_mode == M_LOW) config.low_level_offset = new_offset; +#ifdef __ULTRA__ + else if (setting_mode == M_ULTRA) + config.low_level_offset = new_offset; +#endif } else { config.low_level_offset = 100; @@ -338,13 +351,13 @@ int GetActualRBW(void) { return((int) actual_rbw); } -#if 0 + + void SetSpur(int v) { -// setting_spur = v; + setting_spur = v; dirty = true; } -#endif void SetStepDelay(int d) { @@ -422,6 +435,10 @@ void SetScale(int s) { //} void SetMode(int m) { +#ifdef __ULTRA__ + if (m == 6) + m = M_ULTRA; +#endif if (setting_mode == m) return; reset_settings(m); @@ -511,6 +528,9 @@ void SetRX(int m) { switch(m) { case M_LOW: // Mixed into 0 +#ifdef __ULTRA__ +case M_ULTRA: +#endif SI4432_Sel = 0; SI4432_Receive(); if (setting_step_atten) { @@ -821,14 +841,14 @@ static int modulation_counter = 0; char age[POINTS_COUNT]; -float perform(bool break_on_operation, int i, int32_t f, int tracking) +float perform(bool break_on_operation, int i, uint32_t f, int tracking) { - // long local_IF = (MODE_LOW(setting_mode)?frequency_IF + (int)(actual_rbw < 300.0?setting_spur * 1000 * actual_rbw :0):0); long local_IF; if (MODE_HIGH(setting_mode)) local_IF = 0; else - local_IF = frequency_IF; + local_IF = frequency_IF + (int)(actual_rbw < 300.0?setting_spur * 1000 * actual_rbw:0); +// local_IF = frequency_IF; if (i == 0 && dirty) { apply_settings(); @@ -865,7 +885,7 @@ float perform(bool break_on_operation, int i, int32_t f, int tracking) float RSSI = -150.0; int t = 0; do { - int lf = (uint32_t)(f + (int)((t * 500 - vbwSteps * 250) * actual_rbw)); + uint32_t lf = (uint32_t)(f + (int)((t * 500 - vbwSteps * 250) * actual_rbw)); if (lf < 0) lf = 0; if (setting_mode == M_LOW && tracking) { setFreq (0, frequency_IF + lf - reffer_freq[setting_refer]); // Offset so fundamental of reffer is visible @@ -874,11 +894,15 @@ float perform(bool break_on_operation, int i, int32_t f, int tracking) if (setting_mode == M_LOW && !in_selftest && avoid_spur(f)) { local_IF = spur_alternate_IF; } else { - local_IF = frequency_IF ; +// local_IF = frequency_IF ; } if (setting_mode == M_GENLOW && setting_modulation == MO_EXTERNAL) local_IF += lf; setFreq (0, local_IF); +#ifdef __ULTRA__ + } else if (setting_mode == M_ULTRA) { +// local_IF = frequency_IF; +#endif } else local_IF= 0; #if 0 @@ -887,13 +911,32 @@ float perform(bool break_on_operation, int i, int32_t f, int tracking) break; } #endif - setFreq (1, local_IF + lf); +#ifdef __ULTRA__ + if (setting_mode == M_ULTRA) { +// if (lf > 3406000000 ) +// setFreq (1, local_IF/5 + lf/5); +// else + if (lf > 2446000000 ) + setFreq (1, local_IF/5 + lf/5); + else +// if (lf > 1486000000) + setFreq (1, local_IF/3 + lf/3); +// else +// setFreq (1, local_IF/2 + lf/2); + } else +#endif + setFreq (1, local_IF + lf); if (MODE_OUTPUT(setting_mode)) // No substepping in output mode return(0); float signal_path_loss; - if (setting_mode == M_LOW) - signal_path_loss = -9.5; // Loss in dB, -9.5 for v0.1, -12.5 for v0.2 +#ifdef __ULTRA__ + if (setting_mode == M_ULTRA) + signal_path_loss = -15; // Loss in dB, -9.5 for v0.1, -12.5 for v0.2 else +#endif + if (setting_mode == M_LOW) + signal_path_loss = -9.5; // Loss in dB, -9.5 for v0.1, -12.5 for v0.2 + else signal_path_loss = 7; // Loss in dB (+ is gain) float subRSSI = SI4432_RSSI(lf, MODE_SELECT(setting_mode))+settingLevelOffset()+ setting_attenuate - signal_path_loss; if (RSSI < subRSSI) @@ -918,7 +961,7 @@ static bool sweep(bool break_on_operation) temppeakLevel = -150; float temp_min_level = 100; // spur_old_stepdelay = 0; - //again: +again: for (int i = 0; i < sweep_points; i++) { RSSI = perform(break_on_operation, i, frequencies[i], setting_tracking); @@ -930,12 +973,12 @@ static bool sweep(bool break_on_operation) } if (MODE_INPUT(setting_mode)) { - // if (setting_spur == 1) { // First pass - // temp_t[i] = RSSI; - // continue; // Skip all other processing - // } - // if (setting_spur == -1) // Second pass - // RSSI = ( RSSI < temp_t[i] ? RSSI : temp_t[i]); // Minimum of two passes + if (setting_spur == 1) { // First pass + temp_t[i] = RSSI; + continue; // Skip all other processing + } + if (setting_spur == -1) // Second pass + RSSI = ( RSSI < temp_t[i] ? RSSI : temp_t[i]); // Minimum of two passes temp_t[i] = RSSI; if (setting_subtract_stored) { RSSI = RSSI - stored_t[i] ; @@ -1023,11 +1066,11 @@ static bool sweep(bool break_on_operation) temp_min_level = actual_t[i]; } - // if (setting_spur == 1) { - // setting_spur = -1; - // goto again; - // } else if (setting_spur == -1) - // setting_spur = 1; + if (setting_spur == 1) { + setting_spur = -1; + goto again; + } else if (setting_spur == -1) + setting_spur = 1; if (scandirty) { scandirty = false; @@ -1335,7 +1378,7 @@ void draw_cal_status(void) buf[5]=0; ili9341_drawstring(buf, x, y); } -#if 0 +#if 1 if (setting_spur) { ili9341_set_foreground(BRIGHT_COLOR_BLUE); y += YSTEP*2; @@ -1383,7 +1426,7 @@ void draw_cal_status(void) ili9341_drawstring("Scan:", x, y); y += YSTEP; - int32_t t = (int)((2* vbwSteps * sweep_points * ( actualStepDelay / 100) )) /10 /* * (setting_spur ? 2 : 1) */; // in mS + int32_t t = (int)((2* vbwSteps * sweep_points * ( actualStepDelay / 100) )) /10 * (setting_spur ? 2 : 1); // in mS if (t>1000) plot_printf(buf, BLEN, "%dS",(t+500)/1000); else diff --git a/ui.c b/ui.c index e508c71..a665681 100644 --- a/ui.c +++ b/ui.c @@ -2051,7 +2051,7 @@ keypad_click(int key) /* numeric input done */ double value = my_atof(kp_buf) * scale; #if 1 - uistat.value = (int)value; + uistat.value = value; set_numeric_value(); #else switch (keypad_mode) { diff --git a/ui_sa.c b/ui_sa.c index b21fa89..a29ce65 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -23,7 +23,7 @@ void SetDrive(int d); void SetIF(int f); void SetStepDelay(int t); extern int setting_rbw; -void SetSpur(int); +extern int setting_spur; int GetSpur(void); void SetAverage(int); int GetAverage(void); @@ -473,6 +473,7 @@ extern const menuitem_t menu_highoutputmode[]; extern const menuitem_t menu_modulation[]; extern const menuitem_t menu_top[]; extern const menuitem_t menu_tophigh[]; +extern const menuitem_t menu_topultra[]; static void menu_mode_cb(int item, uint8_t data) { @@ -492,6 +493,9 @@ static void menu_mode_cb(int item, uint8_t data) case 4: menu_push_submenu(menu_highoutputmode); break; + case 7: + menu_push_submenu(menu_topultra); + break; } // draw_cal_status(); } @@ -614,13 +618,13 @@ static void menu_drive_cb(int item, uint8_t data) -#if 0 +#if 1 static void menu_spur_cb(int item, uint8_t data) { (void)data; (void)item; - if (GetSpur()) + if (setting_spur) SetSpur(0); else SetSpur(1); // must be 0 or 1 !!!! @@ -1250,6 +1254,7 @@ static const menuitem_t menu_stimulus[] = { { MT_KEYPAD, KM_SPAN, "SPAN", NULL}, { MT_KEYPAD, KM_CW, "\2ZERO\0SPAN", NULL}, { MT_SUBMENU,0, "RBW", menu_rbw}, + { MT_CALLBACK,0, "\2SPUR\0REMOVAL", menu_spur_cb}, { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; @@ -1264,10 +1269,29 @@ static const menuitem_t menu_mode[] = { { MT_FORM | MT_CALLBACK | MT_ICON, I_HIGH_OUTPUT+I_GEN, "HIGH OUTPUT", menu_mode_cb}, { MT_FORM | MT_SUBMENU | MT_ICON, I_CONNECT+I_GEN, "CAL OUTPUT: %s", menu_reffer}, { MT_FORM | MT_SUBMENU | MT_ICON, I_EMPTY+I_CONFIG, "CONFIG", menu_config}, -// { MT_CANCEL, 0, S_LARROW" BACK", NULL }, +#ifdef __ULTRA__ + { MT_FORM | MT_CALLBACK | MT_ICON, I_LOW_INPUT+I_SA, "ULTRA HIGH INPUT",menu_mode_cb}, +#endif + // { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_FORM | MT_NONE, 0, NULL, NULL } // sentinel }; #if 1 + +#ifdef __ULTRA__ +const menuitem_t menu_topultra[] = { + { MT_CALLBACK, 0, "RESET", menu_autosettings_cb}, + { MT_SUBMENU, 0, "FREQ", menu_stimulus}, + { MT_SUBMENU, 0, "LEVEL", menu_level}, + { MT_SUBMENU, 0, "DISPLAY", menu_display}, + { MT_SUBMENU, 0, "MARKER", menu_marker}, + { MT_SUBMENU, 0, "MEASURE", menu_measure}, + { MT_SUBMENU, 0, "SETTINGS", menu_settings}, + { MT_CANCEL, 0, S_LARROW" MODE",NULL}, + { MT_NONE, 0, NULL, NULL } // sentinel, + // MENUITEM_CLOSE, +}; +#endif + const menuitem_t menu_top[] = { { MT_CALLBACK, 0, "RESET", menu_autosettings_cb}, { MT_SUBMENU, 0, "FREQ", menu_stimulus}, @@ -1367,6 +1391,9 @@ static void menu_item_modify_attribute( if (item == 5 /* PAUSE */ && !(sweep_mode&SWEEP_ENABLE)) { mark = true; } + if (item == 6 && setting_spur) { + mark = true; + } } else if (menu == menu_average) { if (item == GetAverage()){ mark = true; From feb6f5e81a54e53348e537d8bc4f67e3b9ae7fc7 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Fri, 17 Apr 2020 09:16:42 +0200 Subject: [PATCH 4/8] Harmonic made optional --- nanovna.h | 2 +- ui_sa.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/nanovna.h b/nanovna.h index 3c6b621..75b20f5 100644 --- a/nanovna.h +++ b/nanovna.h @@ -30,7 +30,7 @@ #define __MEASURE__ #define __SELFTEST__ #define __CALIBRATE__ -#define __ULTRA__ +//#define __ULTRA__ /* * main.c diff --git a/ui_sa.c b/ui_sa.c index a29ce65..502947f 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -24,7 +24,7 @@ void SetIF(int f); void SetStepDelay(int t); extern int setting_rbw; extern int setting_spur; -int GetSpur(void); +void SetSpur(int v); void SetAverage(int); int GetAverage(void); extern int setting_average; @@ -493,9 +493,11 @@ static void menu_mode_cb(int item, uint8_t data) case 4: menu_push_submenu(menu_highoutputmode); break; +#ifdef __ULTRA__ case 7: menu_push_submenu(menu_topultra); break; +#endif } // draw_cal_status(); } From aa6c0623410b4fa07bc0a78bbe461c0f19bb3406 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Sat, 18 Apr 2020 18:15:50 +0200 Subject: [PATCH 5/8] Modify marker menu created adn ADF4351 core added --- NANOVNA_STM32_F072/board.h | 22 ++- main.c | 2 + nanovna.h | 3 +- sa_core.c | 32 +++- si4432.c | 362 ++++++++++++++++++++++++++++++++++++- si4432.h | 22 +++ ui.c | 17 +- ui_sa.c | 88 ++++++--- 8 files changed, 500 insertions(+), 48 deletions(-) diff --git a/NANOVNA_STM32_F072/board.h b/NANOVNA_STM32_F072/board.h index 5724a6c..7d5a539 100644 --- a/NANOVNA_STM32_F072/board.h +++ b/NANOVNA_STM32_F072/board.h @@ -56,6 +56,7 @@ #define GPIOA_XP 6 #define GPIOA_YP 7 #define GPIOA_MCO 8 +#define GPIOA_TX 9 #define GPIOA_USB_DISC 10 #define GPIOA_USB_DM 11 #define GPIOA_USB_DP 12 @@ -116,16 +117,23 @@ * PA13 - SWDIO (alternate 0). * PA14 - SWCLK (alternate 0). */ +#define __ULTRA_SA__ +#ifdef __ULTRA_SA__ +#define PIN_MOD_ULTRA(X) PIN_MODE_OUTPUT(X) +#else +#define PIN_MOD_ULTRA(X) PIN_MODE_INPUT(X) +#endif + #define VAL_GPIOA_MODER (PIN_MODE_OUTPUT(GPIOA_PE_SEL) | \ - PIN_MODE_INPUT(1U) | \ - PIN_MODE_INPUT(2U) | \ - PIN_MODE_INPUT(3U) | \ + PIN_MOD_ULTRA(1U) | \ + PIN_MOD_ULTRA(2U) | \ + PIN_MOD_ULTRA(3U) | \ PIN_MODE_OUTPUT(GPIOA_RX_SEL) | \ PIN_MODE_OUTPUT(GPIOA_LO_SEL) | \ PIN_MODE_ANALOG(GPIOA_XP) | \ PIN_MODE_ANALOG(GPIOA_YP) | \ PIN_MODE_ALTERNATE(GPIOA_MCO) | \ - PIN_MODE_INPUT(9U) | \ + PIN_MOD_ULTRA(9U) | \ PIN_MODE_OUTPUT(GPIOA_USB_DISC) | \ PIN_MODE_INPUT(GPIOA_USB_DM) | \ PIN_MODE_INPUT(GPIOA_USB_DP) | \ @@ -149,9 +157,9 @@ PIN_OTYPE_PUSHPULL(GPIOA_JTCK) | \ PIN_OTYPE_PUSHPULL(GPIOA_LCD_RESET)) #define VAL_GPIOA_OSPEEDR (PIN_OSPEED_100M(GPIOA_PE_SEL) | \ - PIN_OSPEED_2M(1) | \ - PIN_OSPEED_2M(2) | \ - PIN_OSPEED_2M(3) | \ + PIN_OSPEED_100M(1) | \ + PIN_OSPEED_100M(2) | \ + PIN_OSPEED_100M(3) | \ PIN_OSPEED_100M(4) | \ PIN_OSPEED_100M(5) | \ PIN_OSPEED_2M(6) | \ diff --git a/main.c b/main.c index 157e039..acfac0c 100644 --- a/main.c +++ b/main.c @@ -2716,6 +2716,8 @@ int main(void) #endif + ADF4351_Setup(); + /* * SPI LCD Initialize */ diff --git a/nanovna.h b/nanovna.h index 75b20f5..2d2a230 100644 --- a/nanovna.h +++ b/nanovna.h @@ -31,6 +31,7 @@ #define __SELFTEST__ #define __CALIBRATE__ //#define __ULTRA__ +#define __ULTRA_SA__ /* * main.c @@ -329,7 +330,7 @@ float groupdelay_from_array(int i, float array[POINTS_COUNT][2]); #endif // marker enum { - M_NORMAL=0,M_REFERENCE=1, M_DELTA=2, M_NOISE=4, M_TRACKING=8 // Tracking must be last. + M_NORMAL=0,M_REFERENCE=1, M_DELTA=2, M_NOISE=4, M_TRACKING=8, M_DELETE=16 // Tracking must be last. }; enum { diff --git a/sa_core.c b/sa_core.c index 631c45e..446a4a0 100644 --- a/sa_core.c +++ b/sa_core.c @@ -32,8 +32,9 @@ int setting_tracking_output; int setting_measurement; int vbwSteps = 1; - +#ifdef __ULTRA__ int setting_spur = 0; +#endif float minFreq = 0; float maxFreq = 520000000; @@ -69,7 +70,9 @@ void reset_settings(int m) trace[TRACE_TEMP].enabled = false; setting_measurement = M_OFF; +#ifdef __ULTRA__ setting_spur = 0; +#endif switch(m) { case M_LOW: minFreq = 0; @@ -352,12 +355,13 @@ int GetActualRBW(void) return((int) actual_rbw); } - +#ifdef __ULTRA void SetSpur(int v) { setting_spur = v; dirty = true; } +#endif void SetStepDelay(int d) { @@ -847,8 +851,11 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) if (MODE_HIGH(setting_mode)) local_IF = 0; else - local_IF = frequency_IF + (int)(actual_rbw < 300.0?setting_spur * 1000 * actual_rbw:0); -// local_IF = frequency_IF; + local_IF = frequency_IF +#ifdef __ULTRA__ + + (int)(actual_rbw < 300.0?setting_spur * 1000 * actual_rbw:0) +#endif + ; if (i == 0 && dirty) { apply_settings(); @@ -961,7 +968,9 @@ static bool sweep(bool break_on_operation) temppeakLevel = -150; float temp_min_level = 100; // spur_old_stepdelay = 0; +#ifdef __ULTRA__ again: +#endif for (int i = 0; i < sweep_points; i++) { RSSI = perform(break_on_operation, i, frequencies[i], setting_tracking); @@ -973,12 +982,14 @@ again: } if (MODE_INPUT(setting_mode)) { - if (setting_spur == 1) { // First pass +#ifdef __ULTRA__ + if (setting_spur == 1) { // First pass temp_t[i] = RSSI; continue; // Skip all other processing } if (setting_spur == -1) // Second pass RSSI = ( RSSI < temp_t[i] ? RSSI : temp_t[i]); // Minimum of two passes +#endif temp_t[i] = RSSI; if (setting_subtract_stored) { RSSI = RSSI - stored_t[i] ; @@ -1066,12 +1077,13 @@ again: temp_min_level = actual_t[i]; } +#ifdef __ULTRA__ if (setting_spur == 1) { setting_spur = -1; goto again; } else if (setting_spur == -1) setting_spur = 1; - +#endif if (scandirty) { scandirty = false; draw_cal_status(); @@ -1378,7 +1390,7 @@ void draw_cal_status(void) buf[5]=0; ili9341_drawstring(buf, x, y); } -#if 1 +#ifdef __ULTRA__ if (setting_spur) { ili9341_set_foreground(BRIGHT_COLOR_BLUE); y += YSTEP*2; @@ -1426,7 +1438,11 @@ void draw_cal_status(void) ili9341_drawstring("Scan:", x, y); y += YSTEP; - int32_t t = (int)((2* vbwSteps * sweep_points * ( actualStepDelay / 100) )) /10 * (setting_spur ? 2 : 1); // in mS + int32_t t = (int)((2* vbwSteps * sweep_points * ( actualStepDelay / 100) )) /10 +#ifdef __ULTRA__ + * (setting_spur ? 2 : 1) +#endif + ; // in mS if (t>1000) plot_printf(buf, BLEN, "%dS",(t+500)/1000); else diff --git a/si4432.c b/si4432.c index b79572c..b0492bf 100644 --- a/si4432.c +++ b/si4432.c @@ -19,7 +19,7 @@ #include "ch.h" #include "hal.h" #include "nanovna.h" - +#include #include "si4432.h" #define CS_SI0_HIGH palSetPad(GPIOA, GPIOA_RX_SEL) @@ -502,4 +502,364 @@ float Simulated_SI4432_RSSI(uint32_t i, int s) return(v); } +#endif +//------------------------------- ADF4351 ------------------------------------- + + +#ifdef __ULTRA_SA__ + + +#define bitClear(X,n) (X) ^= ((uint32_t)0xfffffffe) << (n) +#define bitSet(X,n) (X) |= ((uint32_t)1) << (n) +#define bitWrite(X,n,v) if (v) bitSet(X,n); else bitClear(X,n) + + + +#define CS_ADF0_HIGH palSetPad(GPIOA, 9) +#define CS_ADF1_HIGH palSetPad(GPIOA, 10) + +#define CS_ADF0_LOW palClearPad(GPIOA, 9) +#define CS_ADF1_LOW palClearPad(GPIOA, 10) + +#define SPI3_CLK_HIGH palSetPad(GPIOA, 1) +#define SPI3_CLK_LOW palClearPad(GPIOA, 1) + +#define SPI3_SDI_HIGH palSetPad(GPIOA, 2) +#define SPI3_SDI_LOW palClearPad(GPIOA, 2) + + +void ADF_shiftOut(uint8_t val) +{ + uint8_t i; + for (i = 0; i < 8; i++) { + if (val & (1 << (7 - i))) + SPI3_SDI_HIGH; + else + SPI3_SDI_LOW; + chThdSleepMicroseconds(1); + SPI3_CLK_HIGH; + SPI3_CLK_LOW; + } +} + +//unsigned long registers[6] = {0x4580A8, 0x80080C9, 0x4E42, 0x4B3, 0xBC803C, 0x580005} ; +//unsigned long registers[6] = {0x4C82C8, 0x80083E9, 0x6E42, 0x8004B3, 0x8C81FC, 0x580005} ; +unsigned long registers[6] = {0x320000, 0x8008011, 0x18004E42, 0x4B3,0x8C803C , 0x00580005} ; +int debug = 0; +int ADF4351_LE[2] = { 9, 10}; +int ADF4351_Mux = 7; + + +//#define DEBUG(X) // Serial.print( X ) +//#define DEBUGLN(X) Serial.println( X ) +//#define DEBUGFLN(X,Y) Serial.println( X,Y ) +//#define DEBUGF(X,Y) Serial.print( X,Y ) +#define DEBUG(X) +#define DEBUGLN(X) + + +double RFout, //Output freq in MHz +#if 1 //Black modules + PFDRFout[6] = {25.0,25.0,25.0,10.0,10.0,10.0}, //Reference freq in MHz + Chrystal[6] = {25.0,25.0,25.0,10.0,10.0,10.0}, +#else // Green modules + PFDRFout[6] = {10.0,10.0,10.0,10.0,10.0,10.0}, //Reference freq in MHz + Chrystal[6] = {10.0,10.0,10.0,10.0,10.0,10.0}, +#endif + + OutputChannelSpacing = 0.010, // = 0.01 + FRACF; // Temp + +unsigned int long RFint, // Output freq/10Hz + INTA, // Temp + RFcalc, //UI + MOD, //Temp + FRAC; //Temp + +byte OutputDivider; // Temp +byte lock=2; //Not used + +// Lock = A4 + +void ADF4351_Setup() +{ +// palSetPadMode(GPIOA, 1, PAL_MODE_OUTPUT_PUSHPULL ); +// palSetPadMode(GPIOA, 2, PAL_MODE_OUTPUT_PUSHPULL ); + + SPI3_CLK_HIGH; + SPI3_SDI_HIGH; + CS_ADF0_HIGH; + CS_ADF1_HIGH; +// bitSet (registers[2], 17); // R set to 8 +// bitClear (registers[2], 14); // R set to 8 + + ADF4351_R_counter(1); + ADF4351_level(3); + ADF4351_channel_spacing(10); + + while(1) { +// + ADF4351_set_frequency(1,100000000,0); +// ADF4351_set_frequency(1,150000000,0); +// ADF4351_Set(0); +// ADF4351_Set(1); + chThdSleepMilliseconds(1000); + } +// bitSet (registers[2], 17); // R set to 8 +// bitClear (registers[2], 14); // R set to 8 +// for (int i=0; i<6; i++) pinMode(ADF4351_LE[i], OUTPUT); // Setup pins +// for (int i=0; i<6; i++) digitalWrite(ADF4351_LE[i], HIGH); +// pinMode(ADF4351_Mux, INPUT); +// SPI.begin(); // Init SPI bus +// SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0)); + //SPI.setDataMode(SPI_MODE0); // CPHA = 0 Clock positive + //SPI.setBitOrder(MSBFIRST); +} + +void ADF4351_WriteRegister32(int channel, const uint32_t value) +{ + palClearPad(GPIOA, ADF4351_LE[channel]); +// chThdSleepMicroseconds(SELECT_DELAY); + for (int i = 3; i >= 0; i--) ADF_shiftOut((value >> 8 * i) & 0xFF); + palSetPad(GPIOA, ADF4351_LE[channel]); +// chThdSleepMicroseconds(SELECT_DELAY); + palClearPad(GPIOA, ADF4351_LE[channel]); +// chThdSleepMicroseconds(SELECT_DELAY); +} + +void ADF4351_disable_output() +{ + bitClear (registers[4], 5); // digital lock + ADF4351_Set(0); +} + +void ADF4351_enable_output() +{ + bitSet (registers[4], 5); // digital lock + ADF4351_Set(0); +} +void ADF4351_Set(int channel) +{ for (int i = 5; i >= 0; i--) { + ADF4351_WriteRegister32(channel, registers[i]); +// if (debug) Serial.println(registers[i],HEX); +} +} + +void ADF4351_set_frequency(int channel, unsigned long freq, int drive) // freq / 10Hz +{ + ADF4351_prep_frequency(channel,freq, drive); + ADF4351_Set(channel); +} + +void ADF4351_spur_mode(int S) +{ + if (S & 1) + bitSet (registers[2], 29); // R set to 8 + else + bitClear (registers[2], 29); // R set to 8 + if (S & 2) + bitSet (registers[2], 30); // R set to 8 + else + bitClear (registers[2], 30); // R set to 8 +} + +void ADF4351_R_counter(int R) +{ + int dbl = false; + if (R < 0) { + dbl = true; + R = -R; + } + if (R<1) + return; + if (dbl) { + bitSet (registers[2], 25); // Reference doubler + } else { + bitClear (registers[2], 25); // Reference doubler + } + for (int channel=0; channel < 6; channel++) { + PFDRFout[channel] = Chrystal[channel] * (dbl?2:1) / R; + } + registers[2] &= ~ (((unsigned long)0x3FF) << 14); + registers[2] |= (((unsigned long)R) << 14); +} + +void ADF4351_CP(int p) +{ + registers[2] &= ~ (((unsigned long)0xF) << 9); + registers[2] |= (((unsigned long)p) << 9); +} + +void ADF4351_level(int p) +{ + registers[4] &= ~ (((unsigned long)0x3) << 3); + registers[4] |= (((unsigned long)p) << 3); +} + +void ADF4351_channel_spacing(int spacing) +{ + OutputChannelSpacing = 0.001 * spacing; +} + +static uint32_t gcd(uint32_t x, uint32_t y) +{ + uint32_t z; + while (y != 0) { + z = x % y; + x = y; + y = z; + } + return x; +} + +void ADF4351_prep_frequency(int channel, unsigned long freq, int drive) // freq / 10Hz +{ + (void)drive; +// if (channel == 0) + RFout=freq/1000000.0; // To MHz +// else + // RFout=freq/1000002.764; // To MHz + + if (RFout >= 2200) { + OutputDivider = 1; + bitWrite (registers[4], 22, 0); + bitWrite (registers[4], 21, 0); + bitWrite (registers[4], 20, 0); + } else if (RFout >= 1100) { + OutputDivider = 2; + bitWrite (registers[4], 22, 0); + bitWrite (registers[4], 21, 0); + bitWrite (registers[4], 20, 1); + } else if (RFout >= 550) { + OutputDivider = 4; + bitWrite (registers[4], 22, 0); + bitWrite (registers[4], 21, 1); + bitWrite (registers[4], 20, 0); + } else if (RFout >= 275) { + OutputDivider = 8; + bitWrite (registers[4], 22, 0); + bitWrite (registers[4], 21, 1); + bitWrite (registers[4], 20, 1); + } else if (RFout >= 137.5) { + OutputDivider = 16; + bitWrite (registers[4], 22, 1); + bitWrite (registers[4], 21, 0); + bitWrite (registers[4], 20, 0); + } else if (RFout >= 68.75) { + OutputDivider = 32; + bitWrite (registers[4], 22, 1); + bitWrite (registers[4], 21, 0); + bitWrite (registers[4], 20, 1); + } else { + OutputDivider = 64; + bitWrite (registers[4], 22, 1); + bitWrite (registers[4], 21, 1); + bitWrite (registers[4], 20, 0); + } + + INTA = (RFout * OutputDivider) / PFDRFout[channel]; + MOD = (PFDRFout[channel] / OutputChannelSpacing) + 0.01; +// MOD = 3125; + FRACF = (((RFout * OutputDivider) / PFDRFout[channel]) - INTA) * MOD; + FRAC = round(FRACF); + + while (FRAC > 4095 || MOD > 4095) { + FRAC = FRAC >> 1; + MOD = MOD >> 1; + // Serial.println( "MOD/FRAC reduced"); + } + + int32_t k = gcd(FRAC, MOD); + if (k > 1) { + FRAC /= k; + MOD /= k; +// Serial.print( "MOD/FRAC gcd reduced"); + } +// while (denom >= (1<<20)) { +// num >>= 1; +// denom >>= 1; +// } + + +// if (INTA <= 75) Serial.println( "INTA <= 75"); +// if (FRAC > 4095) Serial.println( "FRAC > 4095"); +// if (MOD > 4095) Serial.println( "MOD > 4095"); + + +// if (FRAC > 4095) Serial.println( "FRAC > 4095"); +// if (MOD > 4095) Serial.println( "MOD > 4095"); +// if (INTA > 4095) Serial.println( "INT > 4095"); + + if (debug) { + DEBUG(" ODIV="); + DEBUG(OutputDivider); + DEBUG(" INT="); + DEBUG(INTA); + DEBUG(" FRAC="); + DEBUG(FRAC); + DEBUG(" MOD="); + DEBUG(MOD); + DEBUG( " CalF="); +// DEBUGFLN(PFDRFout[channel] *(INTA + ((double)FRAC)/MOD)/OutputDivider,6); + +// DEBUG(" FRACF="); +// DEBUGF(FRACF,6); + } + registers[0] = 0; + registers[0] = INTA << 15; // OK + FRAC = FRAC << 3; + registers[0] = registers[0] + FRAC; + //if (MOD == 1) MOD = 2; + registers[1] = 0; + registers[1] = MOD << 3; + registers[1] = registers[1] + 1 ; // restore address "001" + bitSet (registers[1], 27); // Prescaler at 8/9 +/* + drive = 1; + if (drive == 0) { + bitClear (registers[4], 3); // +5dBm + out + bitClear (registers[4], 4); // +5dBm + bitClear (registers[4], 6); // +5dBm - out + bitClear (registers[4], 7); // +5dBm + } else if (drive == 1) { + bitSet (registers[4], 6); // +5dBm + bitClear (registers[4], 7); // +5dBm - out + bitSet (registers[4], 3); // +5dBm + bitClear (registers[4], 4); // +5dBm + out + } else if (drive == 2) { + bitClear (registers[4], 6); // +5dBm - out + bitSet (registers[4], 7); // +5dBm + bitClear (registers[4], 3); // +5dBm + out + bitSet (registers[4], 4); // +5dBm + } + else { + bitSet (registers[4], 6); // +5dBm - out + bitSet (registers[4], 7); // +5dBm + bitSet (registers[4], 3); // +5dBm + out + bitSet (registers[4], 4); // +5dBm + } +*/ + bitSet (registers[4], 5); // enable + output + bitClear (registers[4], 8); // enable B output + +#if 0 + if (FRAC == 0) + bitSet (registers[2], 8); // INT mode + else + bitClear (registers[2], 8); // INT mode + bitSet (registers[2], 13); // Double buffered + + bitSet (registers[2], 28); // Digital lock == "110" sur b28 b27 b26 + bitSet (registers[2], 27); // digital lock + bitClear (registers[2], 26); // digital lock + + //bitSet (registers[4], 10); // Mute till lock + bitSet (registers[3], 23); // Fast lock + #endif + bitSet (registers[4], 10); // Mute till lock +// ADF4351_Set(channel); +} + + + #endif diff --git a/si4432.h b/si4432.h index 8b2774e..8473302 100644 --- a/si4432.h +++ b/si4432.h @@ -19,4 +19,26 @@ float SI4432_SET_RBW(float WISH); void PE4302_Write_Byte(unsigned char DATA ); void PE4302_init(void); +#ifdef __ULTRA_SA__ +extern int ADF4351_LE[]; +extern int debug; +void ADF4351_Setup(void); + + +void ADF4351_WriteRegister32(int channel, const uint32_t value); +void ADF4351_set_frequency(int channel, uint32_t freq, int drive_strength); +void ADF4351_prep_frequency(int channel, uint32_t freq, int drive_strength); +//int ADF4351_set_frequency_with_offset(uint32_t freq, int offset, uint8_t drive_strength); +void ADF4351_Set(int channel); +void ADF4351_enable_output(void); +void ADF4351_disable_output(void); +void ADF4351_spur_mode(int S); +void ADF4351_R_counter(int R); +void ADF4351_channel_spacing(int spacing); +void ADF4351_CP(int p); +void ADF4351_level(int p); +int ADF4351_locked(void); +#endif + + #endif //__SI4432_H__ diff --git a/ui.c b/ui.c index a665681..899d0d0 100644 --- a/ui.c +++ b/ui.c @@ -53,7 +53,11 @@ uistat_t uistat = { #define BIT_DOWN1 1 #define READ_PORT() palReadPort(GPIOA) +#ifdef __ULTRA_SA__ +#define BUTTON_MASK 0 +#else #define BUTTON_MASK 0b1110 +#endif static uint16_t last_button = 0b0000; static uint32_t last_button_down_ticks; @@ -128,7 +132,7 @@ static void choose_active_marker(void); static void menu_move_back(void); static void menu_push_submenu(const menuitem_t *submenu); -static const menuitem_t menu_marker_type[]; +//static const menuitem_t menu_marker_type[]; static int btn_check(void) { @@ -1109,7 +1113,7 @@ const menuitem_t menu_top[] = { #include "ui_sa.c" -#define MENU_STACK_DEPTH_MAX 4 +#define MENU_STACK_DEPTH_MAX 5 const menuitem_t *menu_stack[MENU_STACK_DEPTH_MAX] = { menu_mode, NULL, NULL, NULL }; @@ -1784,7 +1788,7 @@ static void draw_numeric_area(void) { char buf[10]; - plot_printf(buf, sizeof buf, "%9d", uistat.value); + plot_printf(buf, sizeof buf, "%9d", ((int32_t)uistat.value)); draw_numeric_input(buf); } @@ -2497,8 +2501,13 @@ static void extcb1(EXTDriver *extp, expchannel_t channel) static const EXTConfig extcfg = { { {EXT_CH_MODE_DISABLED, NULL}, +#ifdef __ULTRA_SA__ + {EXT_CH_MODE_DISABLED, NULL}, + {EXT_CH_MODE_DISABLED, NULL}, +#else {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOA, extcb1}, {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOA, extcb1}, +#endif {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOA, extcb1}, {EXT_CH_MODE_DISABLED, NULL}, {EXT_CH_MODE_DISABLED, NULL}, @@ -2556,8 +2565,8 @@ ui_init() /* * Activates the EXT driver 1. */ - extStart(&EXTD1, &extcfg); + extStart(&EXTD1, &extcfg); #if 1 gptStart(&GPTD3, &gpt3cfg); gptPolledDelay(&GPTD3, 10); /* Small delay.*/ diff --git a/ui_sa.c b/ui_sa.c index 502947f..745bf81 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1,6 +1,6 @@ void markmap_all_markers(void); -static void menu_marker_type_cb(int item, uint8_t data); - +static void menu_marker_modify_cb(int item, uint8_t data); +extern const menuitem_t menu_marker_modify[]; void set_sweep_frequency(int type, uint32_t frequency); uint32_t get_sweep_frequency(int type); void clearDisplay(void); @@ -23,8 +23,10 @@ void SetDrive(int d); void SetIF(int f); void SetStepDelay(int t); extern int setting_rbw; +#ifdef __ULTRA__ extern int setting_spur; void SetSpur(int v); +#endif void SetAverage(int); int GetAverage(void); extern int setting_average; @@ -620,8 +622,7 @@ static void menu_drive_cb(int item, uint8_t data) -#if 1 - +#ifdef __ULTRA__ static void menu_spur_cb(int item, uint8_t data) { (void)data; @@ -791,12 +792,31 @@ static void menu_average_cb(int item, uint8_t data) draw_cal_status(); } -static void menu_marker_type_cb(int item, uint8_t data) +static void +menu_marker_select_cb(int item, uint8_t data) +{ + (void)data; +// int t; + if (item >= 0 && item < MARKERS_MAX) { + markers[item].enabled = true; + active_marker_select(item); + menu_push_submenu(menu_marker_modify); + redraw_marker(active_marker); + draw_menu(); + } +} + +static void menu_marker_modify_cb(int item, uint8_t data) { (void)item; if (markers[active_marker].enabled == M_ENABLED) { - if (data == M_NORMAL) { + if (data == M_DELETE) { + markers[active_marker].enabled = false; + menu_move_back(); +// ui_mode_normal(); +// return; + } else if (data == M_NORMAL) { markers[active_marker].mtype = M_NORMAL; } else if (data == M_REFERENCE) { for (int i = 0; i= 0 && markers[active_marker].enabled == M_ENABLED) { + } else if (menu == menu_marker_modify && active_marker >= 0 && markers[active_marker].enabled == M_ENABLED) { if (data & markers[active_marker].mtype) mark = true; else if (item < 5 && data==markers[active_marker].mtype) // This catches the M_NORMAL case @@ -1460,8 +1494,8 @@ static void menu_item_modify_attribute( mark = true; if (item == 4 && markers[active_marker].mtype & M_TRACKING) mark = true; - } else if (menu == menu_marker_sel) { - if (item < MARKERS_MAX && markers[item].enabled) + } else if (menu == menu_marker_sel || menu == menu_marker_select) { + if (item < 4 && markers[item].enabled) mark = true; else if (item == 4 && uistat.marker_delta) mark = true; @@ -1516,19 +1550,19 @@ static void fetch_numeric_target(void) break; case KM_SCALE: uistat.value = setting_scale; - plot_printf(uistat.text, sizeof uistat.text, "%ddB/", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%ddB/", ((int32_t)uistat.value)); break; case KM_REFPOS: uistat.value = setting_reflevel; - plot_printf(uistat.text, sizeof uistat.text, "%ddB", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%ddB", ((int32_t)uistat.value)); break; case KM_ATTENUATION: uistat.value = GetAttenuation(); - plot_printf(uistat.text, sizeof uistat.text, "%ddB", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%ddB", ((int32_t)uistat.value)); break; case KM_ACTUALPOWER: uistat.value = settingLevelOffset(); - plot_printf(uistat.text, sizeof uistat.text, "%ddB", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%ddB", ((int32_t)uistat.value)); break; case KM_IF: uistat.value = frequency_IF; @@ -1536,23 +1570,23 @@ static void fetch_numeric_target(void) break; case KM_SAMPLETIME: uistat.value = setting_step_delay; - plot_printf(uistat.text, sizeof uistat.text, "%3duS", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%3duS", ((int32_t)uistat.value)); break; case KM_DRIVE: uistat.value = setting_drive; - plot_printf(uistat.text, sizeof uistat.text, "%3ddB", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%3ddB", ((int32_t)uistat.value)); break; case KM_LOWOUTLEVEL: uistat.value = GetAttenuation(); // compensation for dB offset during low output mode - plot_printf(uistat.text, sizeof uistat.text, "%ddB", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%ddB", ((int32_t)uistat.value)); break; case KM_DECAY: uistat.value = setting_decay; - plot_printf(uistat.text, sizeof uistat.text, "%3d", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%3d", ((int32_t)uistat.value)); break; case KM_NOISE: uistat.value = setting_noise; - plot_printf(uistat.text, sizeof uistat.text, "%3d", uistat.value); + plot_printf(uistat.text, sizeof uistat.text, "%3d", ((int32_t)uistat.value)); break; case KM_10MHZ: uistat.value = setting_10mhz; From 20d9c2bbd6687fd7a236b76e0a9f580fb72366de Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Tue, 21 Apr 2020 10:34:46 +0200 Subject: [PATCH 6/8] Harmonic mode implemented --- .cproject | 17 ++++++-- main.c | 56 +++++++++++++++++++++---- nanovna.h | 2 +- sa_core.c | 120 +++++++++++++++++++++++++++++++----------------------- si4432.c | 35 ++++++++-------- ui_sa.c | 26 ++++++++++++ 6 files changed, 178 insertions(+), 78 deletions(-) diff --git a/.cproject b/.cproject index ec54bde..4714753 100644 --- a/.cproject +++ b/.cproject @@ -21,12 +21,21 @@