From 28493ab90dde818458d5272b1fe522844032b00a Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Tue, 11 May 2021 10:15:50 +0200 Subject: [PATCH 1/4] actual_freq times 100 --- main.c | 6 +++--- nanovna.h | 7 ++++--- sa_core.c | 4 ++-- si4468.c | 43 ++++++++++++++++++++++--------------------- ui.c | 2 ++ 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/main.c b/main.c index 4d83902..905b979 100644 --- a/main.c +++ b/main.c @@ -906,17 +906,17 @@ config_t config = { { 10.5, +4.9, +1.6, +0.6, 0, 0, +2, +0, +2, +2, +2, +2, +6, +9, +11.5, +14.5, +23, +25, +36, +46,}, // LNA in { 11.5, 7, 6, 3.5, 1.5, 0.5, -0.2, 0, 0, -0.5, +1.5, +2, +4, +6.5, +9, +13, +13, +13, +13, +13, }, // low out }, - .setting_frequency_30mhz = 30000000, + .setting_frequency_30mhz = 30000000ULL * FREQ_MULTIPLIER, .cor_am = 0, .cor_wfm = 0, .cor_nfm = 0, .ultra = false, .high_out_adf4350 = true, .ext_zero_level = 174, + .receive_switch_offset = 0.0, #endif .sweep_voltage = 3.3, .switch_offset = 0.0, - .receive_switch_offset = 0.0, }; //properties_t current_props; @@ -2312,7 +2312,7 @@ void HardFault_Handler(void) void hard_fault_handler_c(uint32_t *sp) { -#ifdef TINYSA4 +#ifndef TINYSA4 uint32_t r0 = sp[0]; uint32_t r1 = sp[1]; uint32_t r2 = sp[2]; diff --git a/nanovna.h b/nanovna.h index 654a8db..a078e98 100644 --- a/nanovna.h +++ b/nanovna.h @@ -18,7 +18,7 @@ */ #include "ch.h" -//#ifdef TINYSA_F303 +#ifdef TINYSA_F303 #include "adc_F303.h" #ifdef TINYSA_F072 #error "Remove comment for #ifdef TINYSA_F303" @@ -27,7 +27,7 @@ #define TINYSA4 #endif #define TINYSA4_PROTO -//#endif +#endif #ifdef TINYSA_F072 #ifdef TINYSA_F303 @@ -100,6 +100,7 @@ #define HIGH_MAX_FREQ_MHZ 960 #endif #ifdef TINYSA4 +#define FREQ_MULTIPLIER 100 // Multiplier of the 30MHz reference to get accurate frequency correction #define VARIANT(X,Y) (Y) #define DEFAULT_IF ((freq_t)977400000) #define DEFAULT_SPUR_OFFSET ((freq_t)(actual_rbw_x10 > 3000 ? 1500000 : 1000000)) @@ -1185,7 +1186,7 @@ typedef struct properties { //sizeof(properties_t) == 0x1200 -#define CONFIG_MAGIC 0x434f4e4f /* 'CONF' */ +#define CONFIG_MAGIC 0x434f4f4f /* 'CONF' */ extern int16_t lastsaveid; //extern properties_t *active_props; diff --git a/sa_core.c b/sa_core.c index d76a906..0debc2e 100644 --- a/sa_core.c +++ b/sa_core.c @@ -428,8 +428,8 @@ void set_gridlines(int d) #ifdef TINYSA4 void set_30mhz(freq_t f) { - if (f < 29000000 || f > 31000000) - return; +// if (f < 29000000 || f > 31000000) +// return; config.setting_frequency_30mhz = f; ADF4351_recalculate_PFDRFout(); config_save(); diff --git a/si4468.c b/si4468.c index 9664523..61ee806 100644 --- a/si4468.c +++ b/si4468.c @@ -234,7 +234,7 @@ bool ADF4351_frequency_changed = false; #define DEBUG(X) #define DEBUGLN(X) -#define XTAL 30000000 +#define XTAL 300000000 uint64_t PFDRFout[6] = {XTAL,XTAL,XTAL,10000000,10000000,10000000}; //Reference freq in MHz int64_t @@ -429,32 +429,33 @@ static uint32_t gcd(uint32_t x, uint32_t y) } #endif + uint64_t ADF4351_prepare_frequency(int channel, uint64_t freq) // freq / 10Hz { - uint8_t OutputDivider; + uint32_t output_divider; target_freq = freq; if (freq >= 2200000000) { - OutputDivider = 1; + output_divider = 1 * FREQ_MULTIPLIER; bitWrite (registers[4], 22, 0); bitWrite (registers[4], 21, 0); bitWrite (registers[4], 20, 0); } else if (freq >= 1100000000) { - OutputDivider = 2; + output_divider = 2 * FREQ_MULTIPLIER; bitWrite (registers[4], 22, 0); bitWrite (registers[4], 21, 0); bitWrite (registers[4], 20, 1); } else if (freq >= 550000000) { - OutputDivider = 4; + output_divider = 4 * FREQ_MULTIPLIER; bitWrite (registers[4], 22, 0); bitWrite (registers[4], 21, 1); bitWrite (registers[4], 20, 0); } else if (freq >= 275000000) { - OutputDivider = 8; + output_divider = 8 * FREQ_MULTIPLIER; bitWrite (registers[4], 22, 0); bitWrite (registers[4], 21, 1); bitWrite (registers[4], 20, 1); } else { // > 137500000 - OutputDivider = 16; + output_divider = 16 * FREQ_MULTIPLIER; bitWrite (registers[4], 22, 1); bitWrite (registers[4], 21, 0); bitWrite (registers[4], 20, 0); @@ -465,7 +466,7 @@ uint64_t ADF4351_prepare_frequency(int channel, uint64_t freq) // freq / 10Hz if (MOD == 0) MOD = 60; uint32_t MOD_X2 = MOD<<1; - uint32_t INTA_F = ((freq * (uint64_t)OutputDivider) * (uint64_t)MOD_X2/ PFDR) + 1; + uint32_t INTA_F = ((freq * (uint64_t)output_divider) * (uint64_t)MOD_X2/ PFDR) + 1; uint32_t INTA = INTA_F / MOD_X2; uint32_t FRAC = (INTA_F - INTA * MOD_X2)>>1; if (FRAC >= MOD) { @@ -481,10 +482,10 @@ uint64_t ADF4351_prepare_frequency(int channel, uint64_t freq) // freq / 10Hz MOD=2; } #endif - uint64_t actual_freq = ((uint64_t)PFDR *(INTA * MOD +FRAC))/OutputDivider / MOD; + uint64_t actual_freq = ((uint64_t)PFDR *(INTA * MOD +FRAC))/output_divider / MOD; #if 0 // Only for debugging - int max_delta = PFDRFout[channel]/OutputDivider/MOD/100; + int max_delta = PFDRFout[channel]/output_divider/MOD/100; if (actual_freq < freq - max_delta || actual_freq > freq + max_delta ){ while(1) my_microsecond_delay(10); @@ -554,7 +555,6 @@ bool SI4463_offset_changed = false; int SI4463_offset_value = 0; static int SI4463_band = -1; -static int64_t SI4463_outdiv = -1; //static freq_t SI4463_prev_freq = 0; //static float SI4463_step_size = 100; // Will be recalculated once used static uint8_t SI4463_channel = 0; @@ -1571,28 +1571,29 @@ uint16_t set_rbw(uint16_t WISH) { freq_t SI4463_set_freq(freq_t freq) { + uint32_t output_divider; // SI4463_set_gpio(3,SI446X_GPIO_MODE_DRIVE1); // For measuring duration of set_freq int S = 4 ; // Approx 100 Hz channels SI4463_channel = 0; if (freq >= 822000000 && freq <= 1130000000) { // 822 to 1130MHz SI4463_band = 0; - SI4463_outdiv = 4; + output_divider = 4 * FREQ_MULTIPLIER; } else if (freq >= 411000000 && freq <= 566000000) { // 411 to 568MHz SI4463_band = 2; - SI4463_outdiv = 8; + output_divider = 8 * FREQ_MULTIPLIER ; } else if (freq >= 329000000 && freq <= 454000000) { // 329 to 454MHz SI4463_band = 1; - SI4463_outdiv = 10; + output_divider = 10 * FREQ_MULTIPLIER; } else if (freq >= 274000000 && freq <= 378000000) { // 274 to 378 SI4463_band = 3; - SI4463_outdiv = 12; + output_divider = 12 * FREQ_MULTIPLIER; } else if (freq >= 137000000 && freq <= 189000000){ // 137 to 189 SI4463_band = 5; - SI4463_outdiv = 24; + output_divider = 24 * FREQ_MULTIPLIER; #if 0 // Band 4, 6 and 7 do not function } else if (freq >= 137000000 && freq <= 189000000){ // 220 to 266 SI4463_band = 4; - SI4463_outdiv = 12; + output_divider = 12; #endif } else return 0; @@ -1600,10 +1601,10 @@ freq_t SI4463_set_freq(freq_t freq) si_set_offset(0); SI4463_offset_active = false; } - uint32_t R = (freq * SI4463_outdiv) / (Npresc ? 2*config.setting_frequency_30mhz : 4*config.setting_frequency_30mhz) - 1; // R between 0x00 and 0x7f (127) + uint32_t R = (freq * output_divider) / (Npresc ? 2*config.setting_frequency_30mhz : 4*config.setting_frequency_30mhz) - 1; // R between 0x00 and 0x7f (127) uint64_t MOD = 524288; // = 2^19 - uint32_t F = ((freq * SI4463_outdiv*MOD) / (Npresc ? 2*config.setting_frequency_30mhz : 4*config.setting_frequency_30mhz)) - R*MOD; - freq_t actual_freq = (R*MOD + F) * (Npresc ? 2*config.setting_frequency_30mhz : 4*config.setting_frequency_30mhz)/ SI4463_outdiv/MOD; + uint32_t F = ((freq * output_divider*MOD) / (Npresc ? 2*config.setting_frequency_30mhz : 4*config.setting_frequency_30mhz)) - R*MOD; + freq_t actual_freq = (R*MOD + F) * (Npresc ? 2*config.setting_frequency_30mhz : 4*config.setting_frequency_30mhz)/ output_divider/MOD; #if 0 // Only for debugging int delta = freq - actual_freq; if (delta < -100 || delta > 100 ){ @@ -1618,7 +1619,7 @@ freq_t SI4463_set_freq(freq_t freq) #if 0 // Hopping is fast but frequency setting is not yet reliable !!!!! if (SI4463_band == prev_band) { - int vco = 2091 + ((((freq / 4 ) * SI4463_outdiv - 850000000)/1000) * 492) / 200000; + int vco = 2091 + ((((freq / 4 ) * output_divider - 850000000)/1000) * 492) / 200000; if (SI4463_in_tx_mode) { uint8_t data[] = { diff --git a/ui.c b/ui.c index e28d49b..ee73cda 100644 --- a/ui.c +++ b/ui.c @@ -2434,6 +2434,8 @@ ui_mode_normal(void) { if (ui_mode == UI_NORMAL) return; + if (current_menu_is_form()) + return; leave_ui_mode(); area_width = AREA_WIDTH_NORMAL; area_height = AREA_HEIGHT_NORMAL; From 0a36fbab2be3253099c295397d6f0aab93abeb80 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Sun, 9 May 2021 01:47:32 +0300 Subject: [PATCH 2/4] Remove --- si4468.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/si4468.c b/si4468.c index 61ee806..df10f3c 100644 --- a/si4468.c +++ b/si4468.c @@ -1175,19 +1175,8 @@ static char Si446x_readRSSI(void){ while (SPI_RX_IS_NOT_EMPTY(SI4432_SPI)) (void)SPI_READ_8BIT(SI4432_SPI); // Remove lingering bytes -#if 0 SI4463_WAIT_CTS; // Wait for CTS -#else - while (!SI4463_READ_CTS) { - if (sleep) { - __disable_irq(); -// CS_PE_HIGH; - __WFI(); -// CS_PE_LOW; - __enable_irq(); - } - } - #endif + SI_CS_LOW; SPI_WRITE_8BIT(SI4432_SPI, SI446X_CMD_READ_CMD_BUFF); // read answer while (SPI_IS_BUSY(SI4432_SPI)) ; // wait tx From 4ded8b57feaf5d8683494bbed6bb116b5fd64301 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Sun, 9 May 2021 10:20:50 +0300 Subject: [PATCH 3/4] Add option use or not string lib for fatFS --- FatFs/ff.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- FatFs/ffconf.h | 6 ++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/FatFs/ff.c b/FatFs/ff.c index 3c98aa7..580a916 100644 --- a/FatFs/ff.c +++ b/FatFs/ff.c @@ -19,7 +19,6 @@ /----------------------------------------------------------------------------*/ -#include #include "ff.h" /* Declarations of FatFs API */ #include "diskio.h" /* Declarations of device I/O functions */ @@ -697,6 +696,58 @@ static void st_qword (BYTE* ptr, QWORD val) /* Store an 8-byte word in little-en /*-----------------------------------------------------------------------*/ /* String functions */ /*-----------------------------------------------------------------------*/ +#if FF_USE_STRINGLIB == 1 +#include +#else +// Redefine default tu use self string functions +#define memcpy mem_cpy +#define memset mem_set +#define memcmp mem_cmp +#define strchr chk_chr +/* Copy memory to memory */ +static void mem_cpy (void* dst, const void* src, UINT cnt) +{ + BYTE *d = (BYTE*)dst; + const BYTE *s = (const BYTE*)src; + + if (cnt != 0) { + do { + *d++ = *s++; + } while (--cnt); + } +} + +/* Fill memory block */ +static void mem_set (void* dst, int val, UINT cnt) +{ + BYTE *d = (BYTE*)dst; + + do { + *d++ = (BYTE)val; + } while (--cnt); +} + + +/* Compare memory block */ +static int mem_cmp (const void* dst, const void* src, UINT cnt) /* ZR:same, NZ:different */ +{ + const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src; + int r = 0; + + do { + r = *d++ - *s++; + } while (--cnt && r == 0); + + return r; +} + +/* Check if chr is contained in the string */ +static int chk_chr (const char* str, int chr) /* NZ:contained, ZR:not contained */ +{ + while (*str && *str != chr) str++; + return *str; +} +#endif /* Test if the byte is DBC 1st byte */ static int dbc_1st (BYTE c) diff --git a/FatFs/ffconf.h b/FatFs/ffconf.h index d1fc937..7c6accc 100644 --- a/FatFs/ffconf.h +++ b/FatFs/ffconf.h @@ -320,4 +320,10 @@ / PIC32 0 H8/300H 0 x86 0/1 */ +#define FF_USE_STRINGLIB 1 /* 0 or 1 */ +/* + * if 1 Use standard string.h for memcpy, memcmp, memset, strchr + * if 0 use own + */ + /*--- End of configuration options ---*/ From ab5326bea26eaa6ad6de6ae73fc480209cc6d84f Mon Sep 17 00:00:00 2001 From: DiSlord Date: Tue, 11 May 2021 11:00:17 +0300 Subject: [PATCH 4/4] Add brightness support --- ili9341.c | 30 +++++++++++++++++++++++++++++- main.c | 8 ++++++++ nanovna.h | 8 +++++++- ui_sa.c | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/ili9341.c b/ili9341.c index 327dd47..3602bcc 100644 --- a/ili9341.c +++ b/ili9341.c @@ -501,9 +501,37 @@ static const uint8_t ili9341_init_seq[] = { #define LCD_INIT ili9341_init_seq #endif +#ifdef __LCD_BRIGHTNESS__ +#if HAL_USE_DAC == FALSE +#error "Need set HAL_USE_DAC in halconf.h for use __LCD_BRIGHTNESS__" +#endif + +static const DACConfig dac1cfg1 = { + init: 0, + datamode: DAC_DHRM_12BIT_RIGHT +}; + +static void lcd_initBrightness(void){ + dacStart(&DACD2, &dac1cfg1); +} + +#define BRIGHTNESS_MIN_LEVEL 0 +#define BRIGHTNESS_MAX_LEVEL 3300 +// Brightness control range 0 - 100 +void lcd_setBrightness(uint16_t b){ + b = BRIGHTNESS_MIN_LEVEL + b*((BRIGHTNESS_MAX_LEVEL-BRIGHTNESS_MIN_LEVEL)/100); + dacPutChannelX(&DACD2, 0, b); +} +#else +#define lcd_initBrightness() +#endif + void ili9341_init(void) { spi_init(); + // Init Brightness if LCD support + lcd_initBrightness(); + LCD_DC_DATA; LCD_RESET_ASSERT; chThdSleepMilliseconds(10); @@ -514,7 +542,7 @@ void ili9341_init(void) p += 2 + p[1]; chThdSleepMilliseconds(5); } -// ili9341_clear_screen(); + ili9341_clear_screen(); LCD_CS_HIGH; } diff --git a/main.c b/main.c index 905b979..ecd22da 100644 --- a/main.c +++ b/main.c @@ -885,6 +885,7 @@ config_t config = { .ext_zero_level = 128, #endif #ifdef TINYSA4 + ._brightness = DEFAULT_BRIGHTNESS, .vbat_offset = 220, .frequency_IF1 = DEFAULT_IF, .frequency_IF2 = 0, @@ -2265,6 +2266,13 @@ int main(void) // ui_mode_menu(); // Show menu when autostarting mode ui_mode_normal(); + /* + * Set LCD display brightness + */ + #ifdef __LCD_BRIGHTNESS__ + lcd_setBrightness(config._brightness); + #endif + chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO-1, Thread1, NULL); diff --git a/nanovna.h b/nanovna.h index a078e98..dc6fed0 100644 --- a/nanovna.h +++ b/nanovna.h @@ -80,6 +80,7 @@ #ifdef TINYSA4 #define __USE_RTC__ // Enable RTC clock #define __USE_SD_CARD__ // Enable SD card support +#define __LCD_BRIGHTNESS__ // LCD or hardware allow change brightness, add menu item for this #define __HARMONIC__ #define __VBW__ #define __SWEEP_RESTART__ @@ -696,6 +697,7 @@ typedef struct config { int8_t cor_am; int8_t cor_wfm; int8_t cor_nfm; + uint8_t _brightness; uint8_t high_out_adf4350; float sweep_voltage; float switch_offset; @@ -858,6 +860,9 @@ typedef uint16_t pixel_t; #define LCD_HEIGHT 240 #endif +// Default LCD brightness if display support it +#define DEFAULT_BRIGHTNESS 70 + #define LCD_BG_COLOR 0 #define LCD_FG_COLOR 1 #define LCD_GRID_COLOR 2 @@ -967,6 +972,7 @@ void ili9341_drawfont(uint8_t ch, int x, int y); void ili9341_read_memory(int x, int y, int w, int h, uint16_t* out); void ili9341_line(int x0, int y0, int x1, int y1); void show_version(void); +void lcd_setBrightness(uint16_t b); /* * flash.c @@ -1186,7 +1192,7 @@ typedef struct properties { //sizeof(properties_t) == 0x1200 -#define CONFIG_MAGIC 0x434f4f4f /* 'CONF' */ +#define CONFIG_MAGIC 0x434f4e50 /* 'CONF' */ extern int16_t lastsaveid; //extern properties_t *active_props; diff --git a/ui_sa.c b/ui_sa.c index 0a15f56..54f6bc2 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1702,6 +1702,39 @@ static UI_FUNCTION_ADV_CALLBACK(menu_settings_bpf_acb){ toggle_tracking(); } +#ifdef __LCD_BRIGHTNESS__ +static UI_FUNCTION_CALLBACK(menu_brightness_cb) +{ + (void)item; + (void)data; + int16_t value = config._brightness; + ili9341_set_foreground(LCD_MENU_TEXT_COLOR); + ili9341_set_background(LCD_MENU_COLOR); + ili9341_fill(LCD_WIDTH/2-80, LCD_HEIGHT/2-20, 160, 40); + ili9341_drawstring_7x13("BRIGHTNESS", LCD_WIDTH/2-35, LCD_HEIGHT/2-13); + ili9341_drawstring_7x13(S_LARROW" USE LEVELER BUTTON "S_RARROW, LCD_WIDTH/2-72, LCD_HEIGHT/2+2); + while (TRUE) { + int status = btn_check(); + if (status & (EVT_UP|EVT_DOWN)) { + do { + if (status & EVT_UP ) value+=5; + if (status & EVT_DOWN) value-=5; + if (value < 0) value = 0; + if (value > 100) value = 100; + lcd_setBrightness(value); + status = btn_wait_release(); + } while (status != 0); + } + if (status == EVT_BUTTON_SINGLE_CLICK) + break; + } + config._brightness = (uint8_t)value; + lcd_setBrightness(value); + redraw_request|= REDRAW_AREA; + ui_mode_normal(); +} +#endif + static UI_FUNCTION_ADV_CALLBACK(menu_settings_pulse_acb){ (void)item; (void)data; @@ -2561,7 +2594,9 @@ static const menuitem_t menu_config[] = { #ifndef TINYSA4 { MT_SUBMENU, 0, S_RARROW" DFU", menu_dfu}, #endif - +#ifdef __LCD_BRIGHTNESS__ + { MT_CALLBACK, 0, "BRIGHTHESS", menu_brightness_cb}, +#endif { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel };