Files
@ 72630eaa8151
Branch filter:
Location: therm-ng/src/system/flash.c - annotation
72630eaa8151
1.7 KiB
text/plain
Refactor settings a bit, move stuff around
667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 72630eaa8151 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 72630eaa8151 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 72630eaa8151 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 667b32311f8f 72630eaa8151 667b32311f8f 667b32311f8f 667b32311f8f | //
// Flash: Nonvolatile storage for settings / etc
//
#include "stm32f3xx_hal.h"
#include "flash.h"
#include "config.h"
// Takes up 1 page (1k size)
static __attribute__((__section__(".eeprom"))) uint16_t eeprom_emulation[512];
therm_settings_t settings;
// Initialize flash and restore settings
void flash_init(void)
{
flash_restoresettings();
}
// Save settings to flash memory
void flash_savesettings()
{
// Unlock flash memory
HAL_FLASH_Unlock();
// Initialize eraser to erase one page of flash
FLASH_EraseInitTypeDef eraser =
{
.TypeErase = TYPEERASE_PAGES,
.PageAddress = eeprom_emulation,
.NbPages = 1,
};
uint32_t errvar = 0;
// Erase flash page
HAL_FLASHEx_Erase(&eraser, &errvar);
// Write new flash data
uint16_t writectr;
for(writectr = 0; writectr < 128; writectr++)// 128 bytes data
{
HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+writectr,settings.data[writectr]);
}
// Write magic value to flash
HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, eeprom_emulation+FLASH_MAGIC_LOC,FLASH_MAGIC_VALUE);
// Lock flash memory
HAL_FLASH_Lock();
HAL_Delay(2);
}
// Restore configuration from flash memory, if any was previously saved
void flash_restoresettings(void)
{
// Check for magic flash value
if(eeprom_emulation[FLASH_MAGIC_LOC] == FLASH_MAGIC_VALUE)
{
// Read page of flash into settings structure
uint16_t readctr = 0;
for(readctr = 0; readctr < 128; readctr++)
{
settings.data[readctr] = eeprom_emulation[readctr];
}
}
// No data in flash! Set defaults here
else
{
//torestore.values.can_id = 22;
}
}
// Accessor to retrieve settings structure
inline therm_settings_t* flash_getsettings(void)
{
return &settings;
}
|