Skip to content

Commit

Permalink
add save and restore dac value in flash
Browse files Browse the repository at this point in the history
  • Loading branch information
edy555 committed Jan 2, 2017
1 parent 9f52595 commit 9037593
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 29 deletions.
4 changes: 2 additions & 2 deletions STM32F072xB.ld
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 46 additions & 2 deletions flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
35 changes: 22 additions & 13 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
3 changes: 3 additions & 0 deletions nanovna.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
28 changes: 16 additions & 12 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
};
Expand Down

0 comments on commit 9037593

Please sign in to comment.