From 28b6712338b5283c30d38b96ddbbf302152296b6 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Tue, 15 Sep 2020 16:36:32 +0200 Subject: [PATCH] Some code size squeezing --- main.c | 6 +++--- nanovna.h | 33 ++++++++++++++++++--------------- plot.c | 29 +++++++++++++++-------------- sa_cmd.c | 4 ++++ sa_core.c | 2 +- ui_sa.c | 4 ++++ 6 files changed, 45 insertions(+), 33 deletions(-) diff --git a/main.c b/main.c index a4c70fa..36bce4f 100644 --- a/main.c +++ b/main.c @@ -476,7 +476,7 @@ calculate: return value; } -double +float my_atof(const char *p) { int neg = FALSE; @@ -484,11 +484,11 @@ my_atof(const char *p) neg = TRUE; if (*p == '-' || *p == '+') p++; - double x = my_atoi(p); + float x = my_atoi(p); while (_isdigit((int)*p)) p++; if (*p == '.') { - double d = 1.0f; + float d = 1.0f; p++; while (_isdigit((int)*p)) { d /= 10; diff --git a/nanovna.h b/nanovna.h index 2cd6787..5c67285 100644 --- a/nanovna.h +++ b/nanovna.h @@ -130,7 +130,7 @@ void update_frequencies(void); void set_sweep_frequency(int type, uint32_t frequency); uint32_t get_sweep_frequency(int type); void my_microsecond_delay(int t); -double my_atof(const char *p); +float my_atof(const char *p); int shell_printf(const char *fmt, ...); void toggle_sweep(void); @@ -710,7 +710,7 @@ extern uint32_t frequencies[POINTS_COUNT]; extern const float unit_scale_value[]; extern const char * const unit_scale_text[]; -#if 1 +#if 1 // Still sufficient flash // Flash save area - flash7 : org = 0x0801B000, len = 20k in *.ld file // 2k - for config save // 9 * 2k for setting_t + stored trace @@ -725,20 +725,23 @@ extern const char * const unit_scale_text[]; #define SAVE_PROP_CONFIG_SIZE 0x00000800 // Should include all save slots #define SAVE_CONFIG_AREA_SIZE (SAVE_CONFIG_SIZE + SAVEAREA_MAX * SAVE_PROP_CONFIG_SIZE) - -#else -#define SAVEAREA_MAX 4 -// Begin addr 0x0801C000 -#define SAVE_CONFIG_AREA_SIZE 0x00004000 -// config save area -#define SAVE_CONFIG_ADDR 0x0801C000 -// properties_t save area -#define SAVE_PROP_CONFIG_0_ADDR 0x0801C800 -#define SAVE_PROP_CONFIG_1_ADDR 0x0801D000 -#define SAVE_PROP_CONFIG_2_ADDR 0x0801D800 -#define SAVE_PROP_CONFIG_3_ADDR 0x0801E000 -#define SAVE_PROP_CONFIG_4_ADDR 0x0801e800 +#else // Just in case flash runs out +// Flash save area - flash7 : org = 0x0801D000, len = 12k in *.ld file +// 2k - for config save +// 9 * 2k for setting_t + stored trace +#define SAVEAREA_MAX 5 +// STM32 minimum page size for write +#define FLASH_PAGESIZE 0x800 +// config save area (flash7 addr) +#define SAVE_CONFIG_ADDR 0x0801D000 +#define SAVE_CONFIG_SIZE 0x00000800 +// setting_t save area (save area + config size) +#define SAVE_PROP_CONFIG_ADDR (SAVE_CONFIG_ADDR + SAVE_CONFIG_SIZE) +#define SAVE_PROP_CONFIG_SIZE 0x00000800 +// Should include all save slots +#define SAVE_CONFIG_AREA_SIZE (SAVE_CONFIG_SIZE + SAVEAREA_MAX * SAVE_PROP_CONFIG_SIZE) #endif + #if 0 typedef struct properties { uint32_t magic; diff --git a/plot.c b/plot.c index d64f136..1234526 100644 --- a/plot.c +++ b/plot.c @@ -466,30 +466,31 @@ draw_on_strut(int v0, int d, int color) #define SQRT_50 ((float)7.0710678118654) #define LOG_10_SQRT_50 ((float)0.84948500216800) #define POW_30_20 ((float) 0.215443469) -#define POW_SQRT 1.5234153789 +#define POW_SQRT ((float)1.5234153789) /* - * calculate log10(abs(gamma)) + * calculate log10f(abs(gamma)) */ + float value(const float v) { switch(setting.unit) { case U_DBMV: -// return v + 30.0 + 20.0*log10(sqrt(50)); +// return v + 30.0 + 20.0*log10f(sqrt(50)); return v + 30.0 + 20.0*LOG_10_SQRT_50; //TODO convert constants to single float number as GCC compiler does runtime calculation break; case U_DBUV: -// return v + 90.0 + 20.0*log10(sqrt(50.0)); //TODO convert constants to single float number as GCC compiler does runtime calculation +// return v + 90.0 + 20.0*log10f(sqrt(50.0)); //TODO convert constants to single float number as GCC compiler does runtime calculation return v + 90.0 + 20.0*LOG_10_SQRT_50; break; case U_VOLT: -// return pow(10, (v-30.0)/20.0) * sqrt(50.0); - return pow(10, (v-30.0)/20.0)*SQRT_50; +// return pow(10, (v-30.0)/20.0) * sqrt((float)50.0); + return pow((float)10.0, (v-(float)30.0)/(float)20.0)*SQRT_50; // Do NOT change pow to powf as this will increase the size // return pow(10, v/20.0) * POW_SQRT; //TODO there is an error in this calculation as the outcome is different from the not optimized version break; case U_WATT: - return pow(10, v/10.0)/1000.0; + return pow((float)10.0, v/10.0)/1000.0; // Do NOT change pow to powf as this will increase the size break; } // case U_DBM: @@ -503,19 +504,19 @@ to_dBm(const float v) switch(setting.unit) { case U_DBMV: -// return v - 30.0 - 20.0*log10(sqrt(50)); +// return v - 30.0 - 20.0*log10f(sqrt(50)); return v - 30.0 - 20.0*LOG_10_SQRT_50; break; case U_DBUV: -// return v - 90.0 - 20.0*log10(sqrt(50.0)); //TODO convert constants to single float number as GCC compiler does runtime calculation +// return v - 90.0 - 20.0*log10f(sqrt(50.0)); //TODO convert constants to single float number as GCC compiler does runtime calculation return v - 90.0 - 20.0*LOG_10_SQRT_50; break; case U_VOLT: -// return log10( v / (sqrt(50.0))) * 20.0 + 30.0 ; - return log10( v / (SQRT_50)) * 20.0 + 30.0 ; +// return log10f( v / (sqrt(50.0))) * 20.0 + 30.0 ; + return log10f( v / (SQRT_50)) * 20.0 + 30.0 ; break; case U_WATT: - return log10(v*1000.0)*10.0; + return log10f(v*1000.0)*10.0; break; } // case U_DBM: @@ -846,7 +847,7 @@ trace_get_value_string_delta(int t, char *buf, int len, float array[POINTS_COUNT extern const char *unit_string[]; -static void trace_get_value_string( +void trace_get_value_string( int t, char *buf, int len, int i, float coeff[POINTS_COUNT], int ri, int mtype, @@ -916,7 +917,7 @@ static void trace_get_value_string( #endif v = value(coeff[i]); if (mtype & M_NOISE) - v = v - 10*log10(actual_rbw_x10*100.0); + v = v - 10*log10f(actual_rbw_x10*100.0); if (v == -INFINITY) plot_printf(buf, len, "-INF"); else { diff --git a/sa_cmd.c b/sa_cmd.c index 732894f..73e4357 100644 --- a/sa_cmd.c +++ b/sa_cmd.c @@ -14,6 +14,8 @@ * the Free Software Foundation, Inc., 51 Franklin Street, * Boston, MA 02110-1301, USA. */ +#pragma GCC push_options +#pragma GCC optimize ("Os") static int VFO = 0; @@ -606,4 +608,6 @@ VNA_SHELL_FUNCTION(cmd_scanraw) redraw_request = 0; // disable screen update in this mode } +#pragma GCC pop_options + diff --git a/sa_core.c b/sa_core.c index 559e92c..9317d6c 100644 --- a/sa_core.c +++ b/sa_core.c @@ -22,7 +22,7 @@ #include "stdlib.h" #pragma GCC push_options -#pragma GCC optimize ("Og") +#pragma GCC optimize ("Os") //#define __DEBUG_AGC__ If set the AGC value will be shown in the stored trace and FAST_SWEEP rmmode will be disabled diff --git a/ui_sa.c b/ui_sa.c index 280f003..24ac1f7 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -16,6 +16,9 @@ * the Free Software Foundation, Inc., 51 Franklin Street, * Boston, MA 02110-1301, USA. */ +#pragma GCC push_options +#pragma GCC optimize ("Os") + #define FORM_ICON_WIDTH 16 #define FORM_ICON_HEIGHT 16 @@ -1998,3 +2001,4 @@ menu_move_top(void) menu_move_back(); } +#pragma GCC pop_options