From 90375938318b268609a3e745a6051cf54fe0577c Mon Sep 17 00:00:00 2001 From: TT Date: Mon, 2 Jan 2017 22:03:20 +0900 Subject: [PATCH] add save and restore dac value in flash --- STM32F072xB.ld | 4 ++-- flash.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- main.c | 35 ++++++++++++++++++++++------------- nanovna.h | 3 +++ ui.c | 28 ++++++++++++++++------------ 5 files changed, 89 insertions(+), 29 deletions(-) diff --git a/STM32F072xB.ld b/STM32F072xB.ld index cf2d3930..577e87fc 100644 --- a/STM32F072xB.ld +++ b/STM32F072xB.ld @@ -19,14 +19,14 @@ */ MEMORY { - flash0 : org = 0x08000000, len = 124k + flash0 : org = 0x08000000, len = 96k flash1 : org = 0x00000000, len = 0 flash2 : org = 0x00000000, len = 0 flash3 : org = 0x00000000, len = 0 flash4 : org = 0x00000000, len = 0 flash5 : org = 0x00000000, len = 0 flash6 : org = 0x00000000, len = 0 - flash7 : org = 0x08018800, len = 30k + flash7 : org = 0x08018000, len = 32k ram0 : org = 0x20000000, len = 16k ram1 : org = 0x00000000, len = 0 ram2 : org = 0x00000000, len = 0 diff --git a/flash.c b/flash.c index 97ab0041..7e67a48c 100644 --- a/flash.c +++ b/flash.c @@ -76,13 +76,57 @@ checksum(void *start, size_t len) return value; } + +#define FLASH_PAGESIZE 0x800 + +const uint32_t save_config_area = 0x08018000; + +int +config_save(void) +{ + uint16_t *src = (uint16_t*)&config; + uint16_t *dst = save_config_area; + int count = sizeof(config_t) / sizeof(uint16_t); + + config.magic = CONFIG_MAGIC; + config.checksum = 0; + config.checksum = checksum(&config, sizeof config); + + flash_unlock(); + + /* erase flash pages */ + flash_erase_page((uint32_t)dst); + + /* write to flahs */ + while(count-- > 0) { + flash_program_half_word((uint32_t)dst, *src++); + dst++; + } + + return 0; +} + +int +config_recall(void) +{ + config_t *src = save_config_area; + void *dst = &config; + + if (src->magic != CONFIG_MAGIC) + return -1; + if (checksum(src, sizeof(config_t)) != 0) + return -1; + + /* duplicated saved data onto sram to be able to modify marker/trace */ + memcpy(dst, src, sizeof(config_t)); + return 0; +} + #define SAVEAREA_MAX 5 const uint32_t saveareas[] = { 0x08018800, 0x0801a000, 0x0801b800, 0x0801d000, 0x0801e8000 }; -#define FLASH_PAGESIZE 0x800 - int16_t lastsaveid = 0; diff --git a/main.c b/main.c index 384cba24..cc1e647b 100644 --- a/main.c +++ b/main.c @@ -156,14 +156,21 @@ static void cmd_dac(BaseSequentialStream *chp, int argc, char *argv[]) int value; if (argc != 1) { chprintf(chp, "usage: dac {value(0-4095)}\r\n"); + chprintf(chp, "current value: %d\r\n", config.dac_value); return; } value = atoi(argv[0]); + config.dac_value = value; dacPutChannelX(&DACD2, 0, value); } - - +static void cmd_saveconfig(BaseSequentialStream *chp, int argc, char *argv[]) +{ + (void)argc; + (void)argv; + config_save(); + chprintf(chp, "Config saved.\r\n"); +} static struct { int16_t rms[2]; @@ -1290,6 +1297,7 @@ static const ShellCommand commands[] = { "offset", cmd_offset }, { "time", cmd_time }, { "dac", cmd_dac }, + { "saveconfig", cmd_saveconfig }, { "data", cmd_data }, { "dump", cmd_dump }, { "frequencies", cmd_frequencies }, @@ -1325,7 +1333,7 @@ static const I2CConfig i2ccfg = { 0 }; -static const DACConfig dac1cfg1 = { +static DACConfig dac1cfg1 = { //init: 2047U, init: 1922U, datamode: DAC_DHRM_12BIT_RIGHT @@ -1338,15 +1346,6 @@ int main(void) chMtxObjectInit(&mutex); - /* - * Starting DAC1 driver, setting up the output pin as analog as suggested - * by the Reference Manual. - */ - //palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG); - //palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); - //palSetPadMode(GPIOA, 5, PAL_MODE_INPUT); - dacStart(&DACD2, &dac1cfg1); - //palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); //palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN); i2cStart(&I2CD1, &i2ccfg); @@ -1380,7 +1379,17 @@ int main(void) */ plot_init(); - /* restore config and calibration data from flash memory */ + /* restore config */ + config_recall(); + + dac1cfg1.init = config.dac_value; + /* + * Starting DAC1 driver, setting up the output pin as analog as suggested + * by the Reference Manual. + */ + dacStart(&DACD2, &dac1cfg1); + + /* restore frequencies and calibration properties from flash memory */ caldata_recall(0); /* initial frequencies */ diff --git a/nanovna.h b/nanovna.h index 82319d9e..4b1206bb 100644 --- a/nanovna.h +++ b/nanovna.h @@ -275,6 +275,9 @@ extern properties_t current_props; int caldata_save(int id); int caldata_recall(int id); +int config_save(void); +int config_recall(void); + /* * ui.c */ diff --git a/ui.c b/ui.c index f2d86714..7a15619f 100644 --- a/ui.c +++ b/ui.c @@ -354,10 +354,14 @@ menu_calop_cb(int item) static void menu_caldone_cb(int item) { + extern const menuitem_t menu_save[]; + extern const menuitem_t menu_cal[]; (void)item; cal_done(); draw_cal_status(); menu_move_back(); + menu_push_submenu(menu_cal); + menu_push_submenu(menu_save); } static void @@ -550,10 +554,21 @@ const menuitem_t menu_calop[] = { { MT_NONE, NULL, NULL } // sentinel }; +const menuitem_t menu_save[] = { + { MT_CALLBACK, "0", menu_save_cb }, + { MT_CALLBACK, "1", menu_save_cb }, + { MT_CALLBACK, "2", menu_save_cb }, + { MT_CALLBACK, "3", menu_save_cb }, + { MT_CALLBACK, "4", menu_save_cb }, + { MT_CANCEL, "BACK", NULL }, + { MT_NONE, NULL, NULL } // sentinel +}; + const menuitem_t menu_cal[] = { { MT_CALLBACK, "RESET", menu_cal2_cb }, { MT_CALLBACK, "OFF", menu_cal2_cb }, { MT_CALLBACK, "ON", menu_cal2_cb }, + { MT_SUBMENU, "SAVE", menu_save }, { MT_CANCEL, "BACK", NULL }, { MT_NONE, NULL, NULL } // sentinel }; @@ -621,7 +636,7 @@ const menuitem_t menu_stimulus[] = { { MT_CALLBACK, "STOP", menu_stimulus_cb }, { MT_CALLBACK, "CENTER", menu_stimulus_cb }, { MT_CALLBACK, "SPAN", menu_stimulus_cb }, - { MT_CALLBACK, "CW", menu_stimulus_cb }, + { MT_CALLBACK, "CW FREQ", menu_stimulus_cb }, { MT_CALLBACK, "PAUSE", menu_stimulus_cb }, { MT_CANCEL, "BACK", NULL }, { MT_NONE, NULL, NULL } // sentinel @@ -657,23 +672,12 @@ const menuitem_t menu_recall[] = { { MT_NONE, NULL, NULL } // sentinel }; -const menuitem_t menu_save[] = { - { MT_CALLBACK, "0", menu_save_cb }, - { MT_CALLBACK, "1", menu_save_cb }, - { MT_CALLBACK, "2", menu_save_cb }, - { MT_CALLBACK, "3", menu_save_cb }, - { MT_CALLBACK, "4", menu_save_cb }, - { MT_CANCEL, "BACK", NULL }, - { MT_NONE, NULL, NULL } // sentinel -}; - const menuitem_t menu_top[] = { { MT_SUBMENU, "DISPLAY", menu_display }, { MT_SUBMENU, "MARKER", menu_marker }, { MT_SUBMENU, "STIMULUS", menu_stimulus }, { MT_CALLBACK, "CAL", menu_cal_cb }, { MT_SUBMENU, "RECALL", menu_recall }, - { MT_SUBMENU, "SAVE", menu_save }, { MT_CLOSE, "CLOSE", NULL }, { MT_NONE, NULL, NULL } // sentinel };