Koda: |
/* Specify the memory areas */
MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K } |
Koda: |
/* Specify the memory areas */
MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 896K CONFIG (rw) : ORIGIN = 0x80E0000, LENGTH = 128K } |
Koda: |
.config :
{ *(.config.TEMP_MAX) *(.config*) } > CONFIG |
Koda: |
__attribute__((section(".config.TEMP_MAX"))) uint16_t TEMP_MAX; |
Koda: |
HAL_FLASH_Unlock() |
Koda: |
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR); |
Koda: |
FLASH_Erase_Sector(FLASH_SECTOR_11, VOLTAGE_RANGE_3); |
Koda: |
HAL_FLASH_Program(TYPEPROGRAM_WORD, &TEMP_MAX, TEMP_MAX); |
Koda: |
HAL_FLASH_Lock(); |
Koda: |
// Struktura za tvoj flash
typedef struct { /* Control section */ uint32_t magic; uint32_t version; /* Data section */ int temp_max; int temp_min; } flash_str_t; |
Koda: |
#define FLASH_SETTINGS_START_ADDR 0x08001000 /* Določiš naslov, kje v flashu bos imel stranjene nastavitve */
#define FLASH_MAGIC 0x12345678 #define FLASH_VERSION 4 //Vsakic, ko posodobis flash_str_t strukturo, takrat povecas stevilko za 1 |
Koda: |
//Recimo main.c
flash_str_t flash_settings; void flash_settings_read(void); void flash_settings_write(void); int main(void) { //Preberi nastavitve na začetku programa in jih skopiraj v RAM flash_settings_read(); } void flash_settings_read(void) { //Preberes vrednosti iz flasha direktno v strukturo locirano v RAM-u memcpy(&flash_settings, (void *)FLASH_SETTINGS_START_ADDR, sizeof(flash_settings)); //Preveris, ce so podatki veljavni: if (flash_settings.magic != FLASH_MAGIC || /* Flash se ni bil nikoli vpisan s strani aplikacije */ flash_settings.version != FLASH_VERSION) { /* Flash format struktura se je spremenila in moramo resetirati vrednosti */ //Nastavi default vrednosti flash_settings.magic = FLASH_MAGIC; flash_settings.version = FLASH_VERSION; flash_settings.temp_max = 120; //Default max temperature by app flash_settings.temp_min = -10; //Default min temperature by app //Write data back to flash flash_settings_write(); } } void flash_settings_write(void) { const uint8_t* data = (const void *)&flash_settings; //Pointer na podatke za v flash size_t len = sizeof(flash_settings); //Velikost strukture za zapisat v flash uint32_t addr = FLASH_SETTINGS_START_ADDR; //Odkleni flash HAL_FLASH_Unlock() //Zbriši flash sector, ki ustreza FLASH_SETTINGS_START_ADDR naslovu //Zapiši vse podatke, tukaj pričakujem, da ima struktura 4-byte alignment v velikosti len = len >> 2; while (len-- > 0) { HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, data); addr += 4; } //Zakleni flash HAL_FLASH_Lock(); } |
Koda: |
flash_settings.temp_max = ...;
flash_settings_write(); |