NanoVNA/flash.c

197 lines
4.3 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014-2015, TAKAHASHI Tomohiro (TTRFTECH) edy555@gmail.com
* All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "nanovna.h"
#include <string.h>
static int flash_wait_for_last_operation(void)
{
while (FLASH->SR == FLASH_SR_BSY) {
//WWDG->CR = WWDG_CR_T;
}
return FLASH->SR;
}
static void flash_erase_page0(uint32_t page_address)
{
flash_wait_for_last_operation();
FLASH->CR |= FLASH_CR_PER;
FLASH->AR = page_address;
FLASH->CR |= FLASH_CR_STRT;
flash_wait_for_last_operation();
FLASH->CR &= ~FLASH_CR_PER;
}
int flash_erase_page(uint32_t page_address)
{
chSysLock();
flash_erase_page0(page_address);
chSysUnlock();
return 0;
}
void flash_program_half_word(uint32_t address, uint16_t data)
{
flash_wait_for_last_operation();
FLASH->CR |= FLASH_CR_PG;
*(__IO uint16_t*)address = data;
flash_wait_for_last_operation();
FLASH->CR &= ~FLASH_CR_PG;
}
2016-11-04 17:22:48 +01:00
void flash_unlock(void)
{
// unlock sequence
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
static uint32_t
2017-01-03 08:39:00 +01:00
checksum(const void *start, size_t len)
{
uint32_t *p = (uint32_t*)start;
uint32_t *tail = (uint32_t*)(start + len);
uint32_t value = 0;
while (p < tail)
value ^= *p++;
return value;
}
#define FLASH_PAGESIZE 0x800
const uint32_t save_config_area = 0x08018000;
int
config_save(void)
{
uint16_t *src = (uint16_t*)&config;
2017-01-03 08:39:00 +01:00
uint16_t *dst = (uint16_t*)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)
{
2017-01-03 08:39:00 +01:00
const config_t *src = (const config_t*)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;
}
2016-11-04 17:22:48 +01:00
#define SAVEAREA_MAX 5
const uint32_t saveareas[] =
2017-01-15 15:25:30 +01:00
{ 0x08018800, 0x0801a000, 0x0801b800, 0x0801d000, 0x0801e800 };
2016-11-04 17:22:48 +01:00
2016-11-17 16:53:40 +01:00
int16_t lastsaveid = 0;
2016-11-04 17:22:48 +01:00
int
2016-11-04 17:22:48 +01:00
caldata_save(int id)
{
2017-01-02 11:15:16 +01:00
uint16_t *src = (uint16_t*)&current_props;
2016-11-04 17:22:48 +01:00
uint16_t *dst;
2017-01-02 11:15:16 +01:00
int count = sizeof(properties_t) / sizeof(uint16_t);
2016-11-04 17:22:48 +01:00
if (id < 0 || id >= SAVEAREA_MAX)
return -1;
dst = (uint16_t*)saveareas[id];
2017-01-02 11:15:16 +01:00
current_props.magic = CONFIG_MAGIC;
current_props.checksum = 0;
current_props.checksum = checksum(&current_props, sizeof current_props);
flash_unlock();
/* erase flash pages */
2016-11-04 17:22:48 +01:00
void *p = dst;
2017-01-02 11:15:16 +01:00
void *tail = p + sizeof(properties_t);
2016-11-04 17:22:48 +01:00
while (p < tail) {
flash_erase_page((uint32_t)p);
p += FLASH_PAGESIZE;
}
/* write to flahs */
while(count-- > 0) {
flash_program_half_word((uint32_t)dst, *src++);
dst++;
}
2016-11-04 17:22:48 +01:00
/* after saving data, make active configuration points to flash */
2017-01-02 11:15:16 +01:00
active = (properties_t*)saveareas[id];
2016-11-17 16:53:40 +01:00
lastsaveid = id;
return 0;
}
int
2016-11-04 17:22:48 +01:00
caldata_recall(int id)
{
2017-01-02 11:15:16 +01:00
properties_t *src;
void *dst = &current_props;
2016-11-04 17:22:48 +01:00
if (id < 0 || id >= SAVEAREA_MAX)
return -1;
// point to saved area on the flash memory
2017-01-02 11:15:16 +01:00
src = (properties_t*)saveareas[id];
2016-11-04 17:22:48 +01:00
if (src->magic != CONFIG_MAGIC)
return -1;
2017-01-02 11:15:16 +01:00
if (checksum(src, sizeof(properties_t)) != 0)
return -1;
/* active configuration points to save data on flash memory */
2016-11-04 17:22:48 +01:00
active = src;
2016-11-17 16:53:40 +01:00
lastsaveid = id;
2016-11-04 19:37:11 +01:00
/* duplicated saved data onto sram to be able to modify marker/trace */
2017-01-02 11:15:16 +01:00
memcpy(dst, src, sizeof(properties_t));
return 0;
}