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

Detailed Description

Higher-level logic of saving data into internal flash memory.

Date
Sep 19, 2013
Author
Andrey Belomutskiy, (c) 2012-2020

Definition in file flash_main.cpp.

Functions

void writeToFlashNow ()
 
void setNeedToWriteConfiguration ()
 
bool settingsLtftRequestWriteToFlash ()
 
bool writeToFlashNowImpl ()
 
static StorageStatus validatePersistentState ()
 
static StorageStatus readConfiguration ()
 
void readFromFlash ()
 
static void doWriteConfigurationToFlash ()
 
static void doResetConfiguration ()
 
static void doRewriteConfig ()
 
static void doWriteLTFT ()
 
void initFlash ()
 

Function Documentation

◆ doResetConfiguration()

static void doResetConfiguration ( )
static

Definition at line 151 of file flash_main.cpp.

151 {
153}
void resetConfigurationExt(configuration_callback_t boardCallback, engine_type_e engineType)
static constexpr engine_configuration_s * engineConfiguration

Referenced by doRewriteConfig(), and initFlash().

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

◆ doRewriteConfig()

static void doRewriteConfig ( )
static

Definition at line 155 of file flash_main.cpp.

155 {
157 // force settings write to storage
159}
static void doResetConfiguration()
void writeToFlashNow()

Referenced by initFlash().

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

◆ doWriteConfigurationToFlash()

static void doWriteConfigurationToFlash ( )
static

Definition at line 146 of file flash_main.cpp.

146 {
147 // force settings write to storage
149}

Referenced by initFlash().

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

◆ doWriteLTFT()

static void doWriteLTFT ( )
static

Definition at line 161 of file flash_main.cpp.

161 {
163}
bool settingsLtftRequestWriteToFlash()

Referenced by initFlash().

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

◆ initFlash()

void initFlash ( )

This would write NOW (you should not be doing this while connected to real engine)

This would schedule write to flash once the engine is stopped

Definition at line 165 of file flash_main.cpp.

165 {
166 // Init storage(s) if any
167 // Note: may take some time
168 initStorage();
169
170 addConsoleAction("readconfig", readFromFlash);
171 /**
172 * This would write NOW (you should not be doing this while connected to real engine)
173 */
175
176 addConsoleAction("ltftwrite", doWriteLTFT);
177#if EFI_TUNER_STUDIO
178 /**
179 * This would schedule write to flash once the engine is stopped
180 */
181 addConsoleAction(CMD_BURNCONFIG, requestBurn);
182#endif
184 addConsoleAction("rewriteconfig", doRewriteConfig);
185}
void addConsoleAction(const char *token, Void callback)
Register console action without parameters.
static void doWriteLTFT()
static void doRewriteConfig()
void readFromFlash()
static void doWriteConfigurationToFlash()
void initStorage()
Definition storage.cpp:357
void requestBurn()

Referenced by initHardwareNoConfig().

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

◆ readConfiguration()

static StorageStatus readConfiguration ( )
static

this method could and should be executed before we have any connectivity so no console output here

in this method we read first copy of configuration in flash. if that first copy has CRC or other issues we read second copy.

Definition at line 96 of file flash_main.cpp.

96 {
97 auto firstCopy = storageRead(EFI_SETTINGS_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
98 if (firstCopy == StorageStatus::Ok) {
99 firstCopy = validatePersistentState();
100 if (firstCopy == StorageStatus::Ok) {
101 return StorageStatus::Ok;
102 }
103 }
104
105 auto secondCopy = storageRead(EFI_SETTINGS_BACKUP_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
106 if (secondCopy == StorageStatus::Ok) {
107 secondCopy = validatePersistentState();
108 if (secondCopy == StorageStatus::Ok) {
109 return StorageStatus::Ok;
110 }
111 }
112
113 return firstCopy;
114}
persistent_config_container_s persistentState
static StorageStatus validatePersistentState()
StorageStatus storageRead(StorageItemId id, uint8_t *ptr, size_t size)
Definition storage.cpp:160
@ EFI_SETTINGS_BACKUP_RECORD_ID
Definition storage.h:50
@ EFI_SETTINGS_RECORD_ID
Definition storage.h:49

Referenced by readFromFlash().

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

◆ readFromFlash()

void readFromFlash ( )

Definition at line 116 of file flash_main.cpp.

116 {
118
119 switch (result) {
122 efiPrintf("Need to reset flash to default due to CRC mismatch");
123 [[fallthrough]];
127 resetConfigurationExt(DEFAULT_ENGINE_TYPE);
128 break;
130 // Preserve engine type from old config
131 efiPrintf("Resetting due to version mismatch but preserving engine type [%d]", (int)engineConfiguration->engineType);
133 break;
135 // At this point we know that CRC and version number is what we expect. Safe to assume it's a valid configuration.
137 efiPrintf("Read valid configuration from flash!");
138 break;
139 }
140
141 // we can only change the state after the CRC check
144}
void preCalculate()
Definition engine.cpp:324
static EngineAccessor engine
Definition engine.h:413
void applyNonPersistentConfiguration()
bool warning(ObdCode code, const char *fmt,...)
int getRusEfiVersion()
static StorageStatus readConfiguration()
@ CUSTOM_ERR_FLASH_CRC_FAILED
StorageStatus
Definition storage.h:12

Referenced by initFlash(), and loadConfiguration().

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

◆ setNeedToWriteConfiguration()

void setNeedToWriteConfiguration ( )

Definition at line 43 of file flash_main.cpp.

43 {
44 efiPrintf("Scheduling write");
45
47}
bool storageRequestWriteID(StorageItemId id, bool forced)
Definition storage.cpp:186

Referenced by LongTermIdleTrim::checkIfShouldSave(), and requestBurn().

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

◆ settingsLtftRequestWriteToFlash()

bool settingsLtftRequestWriteToFlash ( )

Definition at line 49 of file flash_main.cpp.

49 {
51}
@ EFI_LTFT_RECORD_ID
Definition storage.h:51

Referenced by doWriteLTFT(), and LtftState::load().

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

◆ validatePersistentState()

static StorageStatus validatePersistentState ( )
static

Definition at line 73 of file flash_main.cpp.

73 {
74 auto flashCrc = persistentState.getCrc();
75
76 if (flashCrc != persistentState.crc) {
77 // If the stored crc is all 1s, that probably means the flash is actually blank, not that the crc failed.
78 if (persistentState.crc == ((decltype(persistentState.crc))-1)) {
80 } else {
82 }
83 } else if (persistentState.version != FLASH_DATA_VERSION || persistentState.size != sizeof(persistentState)) {
85 } else {
86 return StorageStatus::Ok;
87 }
88}

Referenced by readConfiguration().

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

◆ writeToFlashNow()

void writeToFlashNow ( )

Because of hardware-related issues, some stm32f4/7 chips are totally frozen while we are writing to internal flash. Writing the configuration takes about 1-2 seconds, we cannot afford to do that while the engine is running so we postpone the write until the engine is stopped.

Definition at line 37 of file flash_main.cpp.

37 {
38 efiPrintf("Scheduling FORCED write");
39
41}

Referenced by doRewriteConfig(), doWriteConfigurationToFlash(), handleCommandX14(), loadConfiguration(), and setEngineType().

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

◆ writeToFlashNowImpl()

bool writeToFlashNowImpl ( )

Definition at line 54 of file flash_main.cpp.

54 {
55 engine->configBurnTimer.reset();
56
57 // Set up the container
59 persistentState.version = FLASH_DATA_VERSION;
61
62 // Do actual write
63 auto result1 = storageWrite(EFI_SETTINGS_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
65
67
68 // Some MCU have no enough flash to store two copies. First one is mandatory.
69 return ((result1 == StorageStatus::Ok) &&
70 ((result2 == StorageStatus::Ok) || (result2 == StorageStatus::NotSupported)));
71}
Timer configBurnTimer
Definition engine.h:308
void resetMaxValues()
StorageStatus storageWrite(StorageItemId id, const uint8_t *ptr, size_t size)
Definition storage.cpp:142

Referenced by storageWriteID().

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

Go to the source code of this file.