2017-01-01 12:03:21 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2016-10-17 00:19:41 +02:00
|
|
|
#include "ch.h"
|
|
|
|
|
#include "hal.h"
|
|
|
|
|
#include "nanovna.h"
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
uint16_t lastsaveid = 0;
|
|
|
|
|
static uint32_t checksum_ok = 0;
|
2020-04-04 07:43:32 +02:00
|
|
|
|
2016-10-17 00:19:41 +02:00
|
|
|
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)
|
|
|
|
|
{
|
2020-02-23 13:45:37 +01:00
|
|
|
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;
|
2016-10-17 00:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
static void flash_erase_page(uint32_t page_address)
|
2016-10-17 00:19:41 +02:00
|
|
|
{
|
|
|
|
|
chSysLock();
|
|
|
|
|
flash_erase_page0(page_address);
|
|
|
|
|
chSysUnlock();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
static inline void flash_unlock(void)
|
2016-11-04 17:22:48 +01:00
|
|
|
{
|
2016-10-17 00:19:41 +02:00
|
|
|
// unlock sequence
|
|
|
|
|
FLASH->KEYR = 0x45670123;
|
|
|
|
|
FLASH->KEYR = 0xCDEF89AB;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
static void flash_program_half_word_buffer(uint16_t* dst, uint16_t *data, uint16_t size)
|
|
|
|
|
{
|
|
|
|
|
uint32_t i;
|
|
|
|
|
flash_unlock();
|
|
|
|
|
// erase flash pages for buffer (aligned to FLASH_PAGESIZE)
|
|
|
|
|
for (i = 0; i < size; i+=FLASH_PAGESIZE)
|
|
|
|
|
flash_erase_page((uint32_t)dst + i);
|
|
|
|
|
// Save buffer
|
|
|
|
|
__IO uint16_t* p = dst;
|
|
|
|
|
for (i = 0; i < size/sizeof(uint16_t); i++){
|
|
|
|
|
flash_wait_for_last_operation();
|
|
|
|
|
FLASH->CR |= FLASH_CR_PG;
|
|
|
|
|
p[i] = data[i];
|
|
|
|
|
flash_wait_for_last_operation();
|
|
|
|
|
FLASH->CR &= ~FLASH_CR_PG;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 00:19:41 +02:00
|
|
|
static uint32_t
|
2017-01-03 08:39:00 +01:00
|
|
|
checksum(const void *start, size_t len)
|
2016-10-17 00:19:41 +02:00
|
|
|
{
|
|
|
|
|
uint32_t *p = (uint32_t*)start;
|
|
|
|
|
uint32_t value = 0;
|
2020-08-01 18:44:34 +02:00
|
|
|
// align by sizeof(uint32_t)
|
|
|
|
|
len = (len + sizeof(uint32_t)-1)/sizeof(uint32_t);
|
|
|
|
|
while (len-- > 0)
|
2020-02-23 13:45:37 +01:00
|
|
|
value = __ROR(value, 31) + *p++;
|
2016-10-17 00:19:41 +02:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-02 14:03:20 +01:00
|
|
|
int
|
|
|
|
|
config_save(void)
|
|
|
|
|
{
|
2020-08-01 18:44:34 +02:00
|
|
|
// Apply magic word and calculate checksum
|
2017-01-02 14:03:20 +01:00
|
|
|
config.magic = CONFIG_MAGIC;
|
2020-02-22 17:48:22 +01:00
|
|
|
config.checksum = checksum(&config, sizeof config - sizeof config.checksum);
|
2017-01-02 14:03:20 +01:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// write to flash
|
|
|
|
|
flash_program_half_word_buffer((uint16_t*)SAVE_CONFIG_ADDR, (uint16_t*)&config, sizeof(config_t));
|
2017-01-02 14:03:20 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
config_recall(void)
|
|
|
|
|
{
|
2020-08-01 18:44:34 +02:00
|
|
|
const config_t *src = (const config_t*)SAVE_CONFIG_ADDR;
|
2017-01-02 14:03:20 +01:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
2017-01-02 14:03:20 +01:00
|
|
|
return -1;
|
2020-08-01 18:44:34 +02:00
|
|
|
// duplicated saved data onto sram to be able to modify marker/trace
|
|
|
|
|
memcpy(&config, src, sizeof(config_t));
|
2017-01-02 14:03:20 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 00:19:41 +02:00
|
|
|
int
|
2020-04-04 07:43:32 +02:00
|
|
|
caldata_save(uint32_t id)
|
2016-10-17 00:19:41 +02:00
|
|
|
{
|
2020-04-04 07:43:32 +02:00
|
|
|
if (id >= SAVEAREA_MAX)
|
|
|
|
|
return -1;
|
2016-10-17 00:19:41 +02:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// Apply magic word and calculate checksum
|
2017-01-02 11:15:16 +01:00
|
|
|
current_props.magic = CONFIG_MAGIC;
|
2020-08-01 18:44:34 +02:00
|
|
|
current_props.checksum = checksum(¤t_props, sizeof current_props - sizeof current_props.checksum);
|
2016-11-05 15:06:24 +01:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// write to flash
|
|
|
|
|
uint16_t *dst = (uint16_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
|
|
|
|
flash_program_half_word_buffer(dst, (uint16_t*)¤t_props, sizeof(properties_t));
|
2016-11-04 17:22:48 +01:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// after saving data, make active configuration points to flash
|
2020-04-04 07:43:32 +02:00
|
|
|
active_props = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
2016-11-17 16:53:40 +01:00
|
|
|
lastsaveid = id;
|
|
|
|
|
|
2016-10-17 00:19:41 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2020-04-04 07:43:32 +02:00
|
|
|
caldata_recall(uint32_t id)
|
2016-10-17 00:19:41 +02:00
|
|
|
{
|
2020-04-04 07:43:32 +02:00
|
|
|
if (id >= SAVEAREA_MAX)
|
|
|
|
|
return -1;
|
2016-12-04 08:19:31 +01:00
|
|
|
// point to saved area on the flash memory
|
2020-08-01 18:44:34 +02:00
|
|
|
properties_t *src = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
|
2016-11-04 17:22:48 +01:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
2020-02-28 21:15:38 +01:00
|
|
|
goto load_default;
|
2016-10-17 00:19:41 +02:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// active configuration points to save data on flash memory
|
2017-01-16 16:33:15 +01:00
|
|
|
active_props = src;
|
2016-11-17 16:53:40 +01:00
|
|
|
lastsaveid = id;
|
2016-11-04 19:37:11 +01:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// duplicated saved data onto sram to be able to modify marker/trace
|
|
|
|
|
memcpy(¤t_props, src, sizeof(properties_t));
|
2016-10-17 00:19:41 +02:00
|
|
|
return 0;
|
2020-02-28 21:15:38 +01:00
|
|
|
load_default:
|
2020-03-21 00:03:09 +01:00
|
|
|
load_default_properties();
|
2020-08-01 18:44:34 +02:00
|
|
|
return lastsaveid;
|
2016-10-17 00:19:41 +02:00
|
|
|
}
|
2017-01-20 00:57:17 +01:00
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// Used in interpolate
|
2017-09-15 15:13:17 +02:00
|
|
|
const properties_t *
|
2020-08-01 18:44:34 +02:00
|
|
|
caldata_reference(void)
|
2017-09-15 15:13:17 +02:00
|
|
|
{
|
2020-08-01 18:44:34 +02:00
|
|
|
if (lastsaveid >= SAVEAREA_MAX)
|
2017-09-15 15:13:17 +02:00
|
|
|
return NULL;
|
2020-04-04 07:43:32 +02:00
|
|
|
const properties_t *src;
|
|
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
src = (const properties_t*)(SAVE_PROP_CONFIG_ADDR + lastsaveid * SAVE_PROP_CONFIG_SIZE);
|
|
|
|
|
// Check crc cache mask (made it only 1 time)
|
|
|
|
|
if (checksum_ok&(1<<lastsaveid))
|
|
|
|
|
return src;
|
|
|
|
|
if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
|
2017-09-15 15:13:17 +02:00
|
|
|
return NULL;
|
2020-08-01 18:44:34 +02:00
|
|
|
checksum_ok|=1<<lastsaveid;
|
2017-09-15 15:13:17 +02:00
|
|
|
return src;
|
|
|
|
|
}
|
2017-01-20 00:57:17 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
clear_all_config_prop_data(void)
|
|
|
|
|
{
|
2020-08-01 18:44:34 +02:00
|
|
|
uint32_t i;
|
2017-01-20 00:57:17 +01:00
|
|
|
flash_unlock();
|
|
|
|
|
|
2020-08-01 18:44:34 +02:00
|
|
|
// erase flash pages
|
|
|
|
|
for (i = 0; i < SAVE_FULL_AREA_SIZE; i+=FLASH_PAGESIZE)
|
|
|
|
|
flash_erase_page(SAVE_CONFIG_ADDR + i);
|
2017-01-20 00:57:17 +01:00
|
|
|
}
|
|
|
|
|
|