rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions | Variables
backup_ram.cpp File Reference

Detailed Description

NVRAM emulation using Internal Flash (flash_int driver)

Date
May 22, 2020

Definition in file backup_ram.cpp.

Functions

static void backupInit ()
 
uint32_t backupRamLoad (backup_ram_e idx)
 
void backupRamSave (backup_ram_e idx, uint32_t value)
 
void backupRamFlush (void)
 

Variables

static volatile uint32_t backupRam [(int) LAST_BACKUP_RAM_ENUM+1]
 
static bool wasLoaded = false
 
static const int backupStateOffset = 0
 
static const int backupDataOffset = 1
 
const size_t backupSize = ((int)LAST_BACKUP_RAM_ENUM + 1) * sizeof(uint32_t)
 

Function Documentation

◆ backupInit()

static void backupInit ( )
static

Definition at line 23 of file backup_ram.cpp.

23 {
24 static_assert(backupSize <= BACKUP_FLASH_SIZE, "Backup flash overflow");
25 static_assert(BACKUP_FLASH_ADDR != (flashaddr_t)nullptr, "Backup address undefined");
26
27 // first, load the whole buffer into the memory
28 intFlashRead((flashaddr_t)BACKUP_FLASH_ADDR, (char *)backupRam, backupSize);
29 // check if we have a reliable properly saved data
30 if (backupRam[backupStateOffset] != BACKUP_SAVED) {
31 // zero is the default value
32 memset((void *)backupRam, 0, backupSize);
33 }
34
35 // we cannot trust the saved data anymore, until it's saved in backupRamFlush()
36 // so we mark is as 'pending'
37 backupRam[backupStateOffset] = BACKUP_PENDING;
38 intFlashWrite((flashaddr_t)BACKUP_FLASH_ADDR + backupStateOffset, (char *)backupRam, sizeof(backupRam[backupStateOffset]));
39
40 wasLoaded = true;
41}
static const int backupStateOffset
const size_t backupSize
static volatile uint32_t backupRam[(int) LAST_BACKUP_RAM_ENUM+1]
static bool wasLoaded
uintptr_t flashaddr_t
Address in the flash memory.
Definition flash_int.h:86
int intFlashWrite(flashaddr_t address, const char *buffer, size_t size)
Copy data from a buffer to the flash memory.
int intFlashRead(flashaddr_t source, char *destination, size_t size)
Copy data from the flash memory to a destination.

Referenced by backupRamLoad(), and backupRamSave().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ backupRamFlush()

void backupRamFlush ( void  )

Definition at line 61 of file backup_ram.cpp.

61 {
62
63 // todo: implement an incremental "append-to-the-end" algorithm to minimize sector erasings?
64
65 // Enter the critical zone
66 syssts_t sts = chSysGetStatusAndLockX();
67
68 // rewrite the whole sector
69 intFlashErase((flashaddr_t)BACKUP_FLASH_ADDR, BACKUP_FLASH_SIZE);
70 // mark the data as valid & saved
71 backupRam[(int)backupStateOffset] = BACKUP_SAVED;
72 // save the data to the flash
73 intFlashWrite((flashaddr_t)BACKUP_FLASH_ADDR, (char *)backupRam, backupSize);
74
75 // Leaving the critical zone
76 chSysRestoreStatusX(sts);
77
78 // there should not be any backup-RAM activity after this call
79 // but if there is, at least try to reinitialize...
80 wasLoaded = false;
81}
int intFlashErase(flashaddr_t address, size_t size)
Erase the sectors containing the span of size bytes starting at address.

◆ backupRamLoad()

uint32_t backupRamLoad ( backup_ram_e  idx)

Definition at line 43 of file backup_ram.cpp.

43 {
44 // this is executed only once during the firmware init
45 if (!wasLoaded) {
46 backupInit();
47 }
48
49 return backupRam[(int)idx + backupDataOffset];
50}
static const int backupDataOffset
static void backupInit()

◆ backupRamSave()

void backupRamSave ( backup_ram_e  idx,
uint32_t  value 
)

Definition at line 52 of file backup_ram.cpp.

52 {
53 // this is executed only once during the firmware init
54 if (!wasLoaded) {
55 backupInit();
56 }
57
58 backupRam[(int)idx + backupDataOffset] = value;
59}

Variable Documentation

◆ backupDataOffset

const int backupDataOffset = 1
static

Definition at line 20 of file backup_ram.cpp.

Referenced by backupRamLoad(), and backupRamSave().

◆ backupRam

volatile uint32_t backupRam[(int) LAST_BACKUP_RAM_ENUM+1]
static

Definition at line 17 of file backup_ram.cpp.

Referenced by backupInit(), backupRamFlush(), backupRamLoad(), and backupRamSave().

◆ backupSize

const size_t backupSize = ((int)LAST_BACKUP_RAM_ENUM + 1) * sizeof(uint32_t)

Definition at line 21 of file backup_ram.cpp.

Referenced by backupInit(), and backupRamFlush().

◆ backupStateOffset

const int backupStateOffset = 0
static

Definition at line 20 of file backup_ram.cpp.

Referenced by backupInit(), and backupRamFlush().

◆ wasLoaded

bool wasLoaded = false
static

Definition at line 18 of file backup_ram.cpp.

Referenced by backupInit(), backupRamFlush(), backupRamLoad(), and backupRamSave().

Go to the source code of this file.