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 149 of file flash_main.cpp.

149 {
151}
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 153 of file flash_main.cpp.

153 {
155 // force settings write to storage
157}
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 144 of file flash_main.cpp.

144 {
145 // force settings write to storage
147}

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 159 of file flash_main.cpp.

159 {
161}
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 163 of file flash_main.cpp.

163 {
164 // Init storage(s) if any
165 // Note: may take some time
166 initStorage();
167
168 addConsoleAction("readconfig", readFromFlash);
169 /**
170 * This would write NOW (you should not be doing this while connected to real engine)
171 */
173
174 addConsoleAction("ltftwrite", doWriteLTFT);
175#if EFI_TUNER_STUDIO
176 /**
177 * This would schedule write to flash once the engine is stopped
178 */
179 addConsoleAction(CMD_BURNCONFIG, requestBurn);
180#endif
182 addConsoleAction("rewriteconfig", doRewriteConfig);
183}
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 94 of file flash_main.cpp.

94 {
95 auto firstCopy = storageRead(EFI_SETTINGS_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
96 if (firstCopy == StorageStatus::Ok) {
97 firstCopy = validatePersistentState();
98 if (firstCopy == StorageStatus::Ok) {
99 return StorageStatus::Ok;
100 }
101 }
102
103 auto secondCopy = storageRead(EFI_SETTINGS_BACKUP_RECORD_ID, (uint8_t *)&persistentState, sizeof(persistentState));
104 if (secondCopy == StorageStatus::Ok) {
105 secondCopy = validatePersistentState();
106 if (secondCopy == StorageStatus::Ok) {
107 return StorageStatus::Ok;
108 }
109 }
110
111 return firstCopy;
112}
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 114 of file flash_main.cpp.

114 {
116
117 switch (result) {
120 efiPrintf("Need to reset flash to default due to CRC mismatch");
121 [[fallthrough]];
125 resetConfigurationExt(DEFAULT_ENGINE_TYPE);
126 break;
128 // Preserve engine type from old config
129 efiPrintf("Resetting due to version mismatch but preserving engine type [%d]", (int)engineConfiguration->engineType);
131 break;
133 // At this point we know that CRC and version number is what we expect. Safe to assume it's a valid configuration.
135 efiPrintf("Read valid configuration from flash!");
136 break;
137 }
138
139 // we can only change the state after the CRC check
142}
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 71 of file flash_main.cpp.

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

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 return ((result1 == StorageStatus::Ok) && (result2 == StorageStatus::Ok));
69}
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.