1 | /** |
---|
2 | ****************************************************************************** |
---|
3 | * @file eeprom.c |
---|
4 | * @author ECS - Zed Kazharov |
---|
5 | * @version V1.0.0 |
---|
6 | * @date 10-Jan-2023 |
---|
7 | * @brief Virtual EEPROM in FLASH memory |
---|
8 | * |
---|
9 | * Beschreibung |
---|
10 | * |
---|
11 | * |
---|
12 | ****************************************************************************** |
---|
13 | */ |
---|
14 | |
---|
15 | #include "eeprom.h" |
---|
16 | #include "stdio.h" |
---|
17 | #include "string.h" |
---|
18 | /********************************************************/ |
---|
19 | |
---|
20 | // Eeprom state related defines |
---|
21 | |
---|
22 | /*****************EEPROM_EMULATOR************************/ |
---|
23 | |
---|
24 | typedef struct |
---|
25 | { |
---|
26 | // Schnittstellenparameter |
---|
27 | uint32_t baudrate; |
---|
28 | uint16_t parityMode; |
---|
29 | uint16_t stopBits; |
---|
30 | uint16_t slaveAddress; |
---|
31 | |
---|
32 | // Cell Parameter |
---|
33 | uint16_t ovpAlarm; |
---|
34 | uint16_t lvpAlarm; |
---|
35 | uint16_t ovpStart; |
---|
36 | uint16_t ovpStop; |
---|
37 | uint16_t lvpStart; |
---|
38 | uint16_t lvpStop; |
---|
39 | uint16_t vShutdownStart; |
---|
40 | uint16_t vShutdownStop; |
---|
41 | int16_t otShutdownStart; |
---|
42 | int16_t otShutdownStop; |
---|
43 | int16_t utpChargeStart; |
---|
44 | int16_t utpChargeStop; |
---|
45 | int16_t utpDischargeStart; |
---|
46 | int16_t utpDischargeStop; |
---|
47 | uint16_t lvpTime; |
---|
48 | uint16_t defaultBalVoltage; |
---|
49 | |
---|
50 | // tempco |
---|
51 | int16_t refTemp; |
---|
52 | int16_t ovpAlarmTempco; |
---|
53 | int16_t lvpAlarmTempco; |
---|
54 | int16_t ovpStartTempco; |
---|
55 | int16_t ovpStopTempco; |
---|
56 | int16_t lvpStartTempco; |
---|
57 | int16_t lvpStopTempco; |
---|
58 | int16_t vShutdownStartTempco; |
---|
59 | int16_t vShutdownStopTempco; |
---|
60 | int16_t balancerVoltageTempco; |
---|
61 | uint16_t lockKey; |
---|
62 | }eeprom_data_t; |
---|
63 | |
---|
64 | typedef struct |
---|
65 | { |
---|
66 | // Geräteinformation |
---|
67 | uint32_t SN; |
---|
68 | uint8_t deviceInfoWritten; |
---|
69 | uint8_t resv[3]; |
---|
70 | }device_info_t; |
---|
71 | |
---|
72 | |
---|
73 | typedef struct |
---|
74 | { |
---|
75 | // Eeprom Status Infos |
---|
76 | uint32_t writeCounter; |
---|
77 | uint16_t structureSize; |
---|
78 | uint16_t revisionInfo; |
---|
79 | uint8_t firstStartId; |
---|
80 | }eeprom_state_t; |
---|
81 | |
---|
82 | |
---|
83 | typedef struct |
---|
84 | { |
---|
85 | // Eeprom Status Infos |
---|
86 | uint16_t cycleCounter; |
---|
87 | uint16_t minVoltage; |
---|
88 | uint16_t maxVoltage; |
---|
89 | int16_t minTemperature; |
---|
90 | int16_t maxTemperature; |
---|
91 | uint16_t criticalOverTempTime; |
---|
92 | uint16_t criticalUnderTempTime; |
---|
93 | uint16_t criticalOvervoltageTime; |
---|
94 | uint16_t criticalUndervoltageTime; |
---|
95 | |
---|
96 | }log_data_t; |
---|
97 | |
---|
98 | // fasse zu einer Struktur zusammen um nachher einfach darauf zugreifen zu können |
---|
99 | typedef struct |
---|
100 | { |
---|
101 | eeprom_data_t changedData; |
---|
102 | eeprom_state_t eepromState; |
---|
103 | device_info_t deviceInfo; |
---|
104 | log_data_t logData; |
---|
105 | }eeprom_stored_data_t; |
---|
106 | |
---|
107 | // Flash related defines |
---|
108 | #define USER_FLASH_BASE_ADRESS FLASH_BASE |
---|
109 | |
---|
110 | #define USER_FLASH_SIZE FLASH_SIZE |
---|
111 | //#define USER_FLASH_END ((USER_FLASH_BASE_ADRESS + USER_FLASH_SIZE) - 1) |
---|
112 | #define USER_FLASH_END (USER_FLASH_BASE_ADRESS + USER_FLASH_SIZE) |
---|
113 | #define USER_FLASH_PAGE_SIZE (0x800) |
---|
114 | |
---|
115 | // Eeprom emulator related defines |
---|
116 | #define NUMBER_OF_PAGES (2) |
---|
117 | #define USER_EEPROM_BASE_ADRESS (USER_FLASH_END - (USER_FLASH_PAGE_SIZE * NUMBER_OF_PAGES)) |
---|
118 | |
---|
119 | // Data to store reated defines |
---|
120 | #define SIZEOF_DEFAULT_EEPROM_DATA (sizeof(eeprom_data_t)) |
---|
121 | #define SIZEOF_CHANGED_EEPROM_DATA (sizeof(eeprom_data_t)) |
---|
122 | #define SIZEOF_DEVICE_INFO (sizeof(device_info_t)) |
---|
123 | #define SIZEOF_EEPROM_STATE (sizeof(eeprom_state_t)) |
---|
124 | |
---|
125 | #define SIZE_OF_DATA_TO_STORE (SIZEOF_CHANGED_EEPROM_DATA + SIZEOF_DEVICE_INFO + SIZEOF_EEPROM_STATE) |
---|
126 | |
---|
127 | // Adress related defines |
---|
128 | #define EEPROM_ADRESS_FIRST_START_ID (USER_EEPROM_BASE_ADRESS - 1 + SIZE_OF_DATA_TO_STORE - 1 - SIZEOF_DEVICE_INFO - 1 - 1) |
---|
129 | #define FIRST_START_ID (1) |
---|
130 | |
---|
131 | |
---|
132 | static uint32_t GetPage(uint32_t Address); |
---|
133 | static HAL_StatusTypeDef getEEPROMData(uint32_t address, uint8_t * data, uint32_t len); |
---|
134 | |
---|
135 | // muss modulo 8 noch hinzufügen wg 8 byte alignement |
---|
136 | static uint64_t eepromData[(SIZE_OF_DATA_TO_STORE / 8) + 1]; |
---|
137 | static uint64_t eepromLogData[(sizeof(log_data_t) / 8) + 1]; |
---|
138 | static FLASH_EraseInitTypeDef EraseInitStruct = {0}; |
---|
139 | |
---|
140 | extern uint16_t savedLockKey; |
---|
141 | |
---|
142 | static const eeprom_data_t defaultEepromData = |
---|
143 | { |
---|
144 | // Schnittstellenparameter |
---|
145 | MB_BAUDRATE_DEFAULT, |
---|
146 | MB_PARITY_MODE_DEFAULT, |
---|
147 | MB_STOPBIT_MODE_DEFAULT, /*uint16_t stopbitmode, reserviert;*/ |
---|
148 | MB_SLAVE_ADDRESS_DEFAULT, /*uint16_t slave_adress;*/ |
---|
149 | OVP_ALARM_VOLTAGE_DEFAULT, |
---|
150 | LVP_ALARM_VOLTAGE_DEFAULT, |
---|
151 | OVP_START_VOLTAGE_DEFAULT, |
---|
152 | OVP_STOPP_VOLTAGE_DEFAULT, |
---|
153 | LVP_START_VOLTAGE_DEFAULT, |
---|
154 | LVP_STOPP_VOLTAGE_DEFAULT, |
---|
155 | VOLTAGE_SHUTDOWN_START_VOLTAGE_DEFAULT, |
---|
156 | VOLTAGE_SHUTDOWN_STOPP_VOLTAGE_DEFAULT, |
---|
157 | OVERTEMPPROTECTION_START_DEFAULT, |
---|
158 | OVERTEMPPROTECTION_STOP_DEFAULT, |
---|
159 | UNDERTEMPPROTECTION_CHARGE_START_DEFAULT, |
---|
160 | UNDERTEMPPROTECTION_CHARGE_STOP_DEFAULT, |
---|
161 | UNDERTEMPPROTECTION_DISCHARGE_START_DEFAULT, |
---|
162 | UNDERTEMPPROTECTION_DISCHARGE_STOP_DEFAULT, |
---|
163 | LVP_TIME_DEFAULT, |
---|
164 | BAL_VOLTAGE_DEFAULT, |
---|
165 | BASIS_TEMP_DEFAULT, |
---|
166 | OVP_ALARM_TEMPCO_DEFAULT, |
---|
167 | LVP_ALARM_TEMPCO_DEFAULT, |
---|
168 | OVP_START_TEMPCO_DEFAULT, |
---|
169 | OVP_STOPP_TEMPCO_DEFAULT, |
---|
170 | LVP_START_TEMPCO_DEFAULT, |
---|
171 | LVP_STOPP_TEMPCO_DEFAULT, |
---|
172 | V_SHUTDOWN_START_TEMPCO_DEFAULT, |
---|
173 | V_SHUTDOWN_STOPP_TEMPCO_DEFAULT, |
---|
174 | BAL_TEMPCO_DEFAULT, |
---|
175 | 0, //Lockkey, Default keiner! |
---|
176 | }; |
---|
177 | |
---|
178 | /** |
---|
179 | * @brief Gets the page of a given address |
---|
180 | * @param Addr: Address of the FLASH Memory |
---|
181 | * @retval The page of a given address |
---|
182 | */ |
---|
183 | static uint32_t GetPage(uint32_t Addr) |
---|
184 | { |
---|
185 | return (Addr - FLASH_BASE) / FLASH_PAGE_SIZE; |
---|
186 | } |
---|
187 | |
---|
188 | static HAL_StatusTypeDef getEEPROMData(uint32_t address, uint8_t * data, uint32_t len) |
---|
189 | { |
---|
190 | uint32_t i = 0; |
---|
191 | uint8_t daten; |
---|
192 | if((address + len) > USER_FLASH_END) |
---|
193 | { |
---|
194 | return HAL_ERROR; |
---|
195 | } |
---|
196 | for(i = 0; i < len; i ++) |
---|
197 | { |
---|
198 | data[i] = *((uint8_t *) (address + i)); |
---|
199 | } |
---|
200 | return HAL_OK; |
---|
201 | } |
---|
202 | |
---|
203 | bool EEPROM_isFirstStart() |
---|
204 | { |
---|
205 | |
---|
206 | uint8_t firstStartCatcher; |
---|
207 | if(getEEPROMData(EEPROM_ADRESS_FIRST_START_ID, (uint8_t *)&firstStartCatcher, 1) != HAL_OK) return HAL_ERROR; |
---|
208 | if(firstStartCatcher != FIRST_START_ID) |
---|
209 | { |
---|
210 | printf ("First start detected\n"); |
---|
211 | return true; |
---|
212 | |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | printf ("previous config present\n"); |
---|
217 | return false; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | HAL_StatusTypeDef EEPROM_fullRestore(sys_data_t * data, bool keepSN) |
---|
222 | { |
---|
223 | eeprom_stored_data_t * dataToStore; |
---|
224 | uint32_t PageError; |
---|
225 | uint32_t Address; |
---|
226 | uint32_t x; |
---|
227 | |
---|
228 | printf("EEPROM FULL RESTORE!\n"); |
---|
229 | |
---|
230 | /****************LESE_DEFAULT_WERTE************************/ |
---|
231 | |
---|
232 | dataToStore = (eeprom_stored_data_t *) eepromData; |
---|
233 | |
---|
234 | memcpy( (void *) &dataToStore->changedData, (void*) &defaultEepromData, sizeof(dataToStore->changedData)) ; |
---|
235 | |
---|
236 | |
---|
237 | // Eeprom Status Infos |
---|
238 | dataToStore->eepromState.writeCounter ++; |
---|
239 | dataToStore->eepromState.structureSize = sizeof(eeprom_stored_data_t); |
---|
240 | dataToStore->eepromState.revisionInfo = 0; |
---|
241 | dataToStore->eepromState.firstStartId = FIRST_START_ID; |
---|
242 | |
---|
243 | /****************PAGE_LÖSCHEN********************/ |
---|
244 | HAL_FLASH_Unlock(); |
---|
245 | |
---|
246 | // check for PG bit set if so clear bit |
---|
247 | FLASH_WaitForLastOperation(1000); |
---|
248 | if(READ_BIT(FLASH->CR, FLASH_CR_PG)) |
---|
249 | { |
---|
250 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
---|
251 | } |
---|
252 | |
---|
253 | /* Fill EraseInit structure*/ |
---|
254 | //Speicherung in vorletzter page, deshalb diese löschen. |
---|
255 | //Log Daten kommen in die letzte page |
---|
256 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
---|
257 | EraseInitStruct.Page = FLASH_PAGE_NB - 2; //GetPage(USER_EEPROM_BASE_ADRESS); |
---|
258 | EraseInitStruct.NbPages = 1; |
---|
259 | |
---|
260 | /* erase Sektoren daten d+rfen nicht im instuction cache liegen */ |
---|
261 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) |
---|
262 | { |
---|
263 | while (1) |
---|
264 | { |
---|
265 | // ERROR HAL_FLASH_GetError() kann info geben |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /****************IM_FLASH_SPEICHERN********************/ |
---|
270 | |
---|
271 | /* programmiere uint64_t */ |
---|
272 | Address = USER_EEPROM_BASE_ADRESS; |
---|
273 | x = 0; |
---|
274 | while (Address < (USER_EEPROM_BASE_ADRESS + SIZE_OF_DATA_TO_STORE)) |
---|
275 | { |
---|
276 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, eepromData[x]) == HAL_OK) |
---|
277 | { |
---|
278 | Address = Address + 8; |
---|
279 | x ++; |
---|
280 | } |
---|
281 | else |
---|
282 | { |
---|
283 | /* Error occurred while writing data in Flash memory. |
---|
284 | User can add here some code to deal with this error */ |
---|
285 | while (1) |
---|
286 | { |
---|
287 | // ERROR |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | /* Lock the Flash */ |
---|
293 | HAL_FLASH_Lock(); |
---|
294 | |
---|
295 | /****************AUSLESEN_UND_PRÜFEN********************/ |
---|
296 | |
---|
297 | return EEPROM_readConfig(data); |
---|
298 | |
---|
299 | |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | HAL_StatusTypeDef EEPROM_storeConfig(sys_data_t * data,bool withSN, bool saveNewKey) |
---|
305 | { |
---|
306 | eeprom_stored_data_t * dataToStore; |
---|
307 | uint32_t PageError; |
---|
308 | uint32_t Address; |
---|
309 | uint32_t x; |
---|
310 | /****************LESE_WERTE_AUS_SYSDATA*********************/ |
---|
311 | printf("EEPROM STORE CONFIG!\n"); |
---|
312 | dataToStore = (eeprom_stored_data_t *) eepromData; |
---|
313 | |
---|
314 | // Schnittstellenparameter |
---|
315 | dataToStore->changedData.baudrate = data->s.baudrate; |
---|
316 | dataToStore->changedData.parityMode = data->s.parityMode; |
---|
317 | dataToStore->changedData.stopBits = data->s.stopBits; |
---|
318 | dataToStore->changedData.slaveAddress = data->s.slaveAddress; |
---|
319 | |
---|
320 | // Cell Parameter |
---|
321 | dataToStore->changedData.ovpAlarm = data->s.ovpAlarm; |
---|
322 | dataToStore->changedData.lvpAlarm = data->s.lvpAlarm; |
---|
323 | dataToStore->changedData.ovpStart = data->s.ovpStart; |
---|
324 | dataToStore->changedData.ovpStop = data->s.ovpStop; |
---|
325 | dataToStore->changedData.lvpStart = data->s.lvpStart; |
---|
326 | dataToStore->changedData.lvpStop = data->s.lvpStop; |
---|
327 | dataToStore->changedData.vShutdownStart = data->s.vShutdownStart; |
---|
328 | dataToStore->changedData.vShutdownStop = data->s.vShutdownStop; |
---|
329 | dataToStore->changedData.otShutdownStart = data->s.otShutdownStart; |
---|
330 | dataToStore->changedData.otShutdownStop = data->s.otShutdownStop; |
---|
331 | dataToStore->changedData.utpChargeStart = data->s.utpChargeStart; |
---|
332 | dataToStore->changedData.utpChargeStop = data->s.utpChargeStop; |
---|
333 | dataToStore->changedData.utpDischargeStart = data->s.utpDischargeStart; |
---|
334 | dataToStore->changedData.utpDischargeStop = data->s.utpDischargeStop; |
---|
335 | dataToStore->changedData.lvpTime = data->s.lvpTime; |
---|
336 | dataToStore->changedData.defaultBalVoltage = data->s.defaultBalVoltage; |
---|
337 | |
---|
338 | // tempco |
---|
339 | dataToStore->changedData.refTemp = data->s.refTemp; |
---|
340 | dataToStore->changedData.ovpAlarmTempco = data->s.ovpAlarmTempco; |
---|
341 | dataToStore->changedData.lvpAlarmTempco = data->s.lvpAlarmTempco; |
---|
342 | dataToStore->changedData.ovpStartTempco = data->s.ovpStartTempco; |
---|
343 | dataToStore->changedData.ovpStopTempco = data->s.ovpStopTempco; |
---|
344 | dataToStore->changedData.lvpStartTempco = data->s.lvpStartTempco; |
---|
345 | dataToStore->changedData.lvpStopTempco = data->s.lvpStopTempco; |
---|
346 | dataToStore->changedData.vShutdownStartTempco = data->s.vShutdownStartTempco; |
---|
347 | dataToStore->changedData.vShutdownStopTempco = data->s.vShutdownStopTempco; |
---|
348 | dataToStore->changedData.balancerVoltageTempco = data->s.balancerVoltageTempco; |
---|
349 | |
---|
350 | if (saveNewKey) |
---|
351 | { |
---|
352 | dataToStore->changedData.lockKey = data->s.newLockKey; |
---|
353 | } |
---|
354 | else |
---|
355 | { |
---|
356 | dataToStore->changedData.lockKey = savedLockKey; |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | // Eeprom Status Infos |
---|
361 | dataToStore->eepromState.writeCounter ++ ; |
---|
362 | dataToStore->eepromState.structureSize = sizeof(eeprom_stored_data_t); |
---|
363 | dataToStore->eepromState.revisionInfo = 0; |
---|
364 | dataToStore->eepromState.firstStartId = FIRST_START_ID; |
---|
365 | |
---|
366 | if (withSN) |
---|
367 | { |
---|
368 | printf("Writing SN!\n"); |
---|
369 | dataToStore->deviceInfo.SN = data->s.deviceSn; |
---|
370 | } |
---|
371 | |
---|
372 | /****************PAGE_LÖSCHEN********************/ |
---|
373 | HAL_FLASH_Unlock(); |
---|
374 | |
---|
375 | // check for PG bit set if so clear bit |
---|
376 | FLASH_WaitForLastOperation(1000); |
---|
377 | if(READ_BIT(FLASH->CR, FLASH_CR_PG)) |
---|
378 | { |
---|
379 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
---|
380 | } |
---|
381 | |
---|
382 | /* Fill EraseInit structure*/ |
---|
383 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
---|
384 | EraseInitStruct.Page = FLASH_PAGE_NB - 2; |
---|
385 | EraseInitStruct.NbPages = 1; |
---|
386 | |
---|
387 | /* erase Sektoren daten dürfen nicht im instuction cache liegen */ |
---|
388 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) |
---|
389 | { |
---|
390 | while (1) |
---|
391 | { |
---|
392 | // ERROR HAL_FLASH_GetError() kann info geben |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | /****************IM_FLASH_SPEICHERN********************/ |
---|
397 | |
---|
398 | /* programmiere uint64_t */ |
---|
399 | Address = USER_EEPROM_BASE_ADRESS; |
---|
400 | x = 0; |
---|
401 | while (Address < (USER_EEPROM_BASE_ADRESS + SIZE_OF_DATA_TO_STORE)) |
---|
402 | { |
---|
403 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, eepromData[x]) == HAL_OK) |
---|
404 | { |
---|
405 | Address = Address + 8; |
---|
406 | x ++; |
---|
407 | } |
---|
408 | else |
---|
409 | { |
---|
410 | /* Error occurred while writing data in Flash memory. |
---|
411 | User can add here some code to deal with this error */ |
---|
412 | while (1) |
---|
413 | { |
---|
414 | // ERROR |
---|
415 | } |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | /* Lock the Flash */ |
---|
420 | HAL_FLASH_Lock(); |
---|
421 | |
---|
422 | /****************AUSLESEN_UND_PRÜFEN********************/ |
---|
423 | |
---|
424 | return EEPROM_readConfig(data); |
---|
425 | |
---|
426 | |
---|
427 | } |
---|
428 | |
---|
429 | HAL_StatusTypeDef EEPROM_storeLogData() |
---|
430 | { |
---|
431 | |
---|
432 | uint32_t PageError; |
---|
433 | uint32_t Address; |
---|
434 | uint32_t x; |
---|
435 | log_data_t * logData; |
---|
436 | /****************LESE_WERTE_AUS_SYSDATA*********************/ |
---|
437 | printf("EEPROM STORE LOG DATA!\n"); |
---|
438 | |
---|
439 | //Zeiger auf Resevierten Ram Speicher, der für das speichern der Daten zuständig ist |
---|
440 | logData = (log_data_t *) eepromLogData; |
---|
441 | logData->cycleCounter = sysData.s.cycleCounter; |
---|
442 | logData->maxVoltage = sysData.s.maxVoltage; |
---|
443 | logData->minVoltage = sysData.s.minVoltage; |
---|
444 | logData->maxTemperature = sysData.s.maxTemperature; |
---|
445 | logData->minTemperature = sysData.s.minTemperature; |
---|
446 | logData->criticalOvervoltageTime = sysData.s.criticalOverVoltageTime; |
---|
447 | logData->criticalUndervoltageTime = sysData.s.criticalUnderVoltageTime; |
---|
448 | logData->criticalOverTempTime = sysData.s.criticalOverTempTime; |
---|
449 | logData->criticalUnderTempTime = sysData.s.criticalUnderTempTime; |
---|
450 | |
---|
451 | |
---|
452 | /****************PAGE_LÖSCHEN********************/ |
---|
453 | HAL_FLASH_Unlock(); |
---|
454 | |
---|
455 | // check for PG bit set if so clear bit |
---|
456 | FLASH_WaitForLastOperation(1000); |
---|
457 | if(READ_BIT(FLASH->CR, FLASH_CR_PG)) |
---|
458 | { |
---|
459 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
---|
460 | } |
---|
461 | |
---|
462 | /* Fill EraseInit structure*/ |
---|
463 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
---|
464 | EraseInitStruct.Page = FLASH_PAGE_NB - 1; // Page löschen |
---|
465 | EraseInitStruct.NbPages = 1; |
---|
466 | |
---|
467 | /* erase Sektoren daten dürfen nicht im instuction cache liegen */ |
---|
468 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) |
---|
469 | { |
---|
470 | printf("Error while deleting log data area in flash\n"); |
---|
471 | return HAL_ERROR; |
---|
472 | } |
---|
473 | |
---|
474 | /****************IM_FLASH_SPEICHERN********************/ |
---|
475 | |
---|
476 | /* programmiere uint64_t */ |
---|
477 | uint32_t startAdress = USER_EEPROM_BASE_ADRESS + FLASH_PAGE_SIZE; |
---|
478 | Address = startAdress; |
---|
479 | x = 0; |
---|
480 | while (Address < (startAdress + sizeof(log_data_t))) |
---|
481 | { |
---|
482 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, eepromLogData[x]) == HAL_OK) |
---|
483 | { |
---|
484 | Address = Address + 8; |
---|
485 | x ++; |
---|
486 | } |
---|
487 | else |
---|
488 | { |
---|
489 | printf("Error while saving data to flash\n"); |
---|
490 | return HAL_ERROR; |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | /* Lock the Flash */ |
---|
495 | HAL_FLASH_Lock(); |
---|
496 | |
---|
497 | return HAL_OK; |
---|
498 | } |
---|
499 | |
---|
500 | |
---|
501 | HAL_StatusTypeDef EEPROM_ResetLogData() |
---|
502 | { |
---|
503 | |
---|
504 | uint32_t PageError; |
---|
505 | uint32_t Address; |
---|
506 | uint32_t x; |
---|
507 | log_data_t * logData; |
---|
508 | /****************LESE_WERTE_AUS_SYSDATA*********************/ |
---|
509 | printf("EEPROM STORE LOG DATA!\n"); |
---|
510 | |
---|
511 | //Zeiger auf Resevierten Ram Speicher, der für das speichern der Daten zuständig ist |
---|
512 | logData = (log_data_t *) eepromLogData; |
---|
513 | logData->cycleCounter = 0; |
---|
514 | logData->maxVoltage = 0; |
---|
515 | logData->minVoltage = UINT16_MAX; |
---|
516 | logData->maxTemperature = INT16_MIN; |
---|
517 | logData->minTemperature = INT16_MAX; |
---|
518 | logData->criticalOvervoltageTime = 0; |
---|
519 | logData->criticalUndervoltageTime = 0; |
---|
520 | logData->criticalOverTempTime = 0; |
---|
521 | logData->criticalUnderTempTime = 0; |
---|
522 | |
---|
523 | |
---|
524 | /****************PAGE_LÖSCHEN********************/ |
---|
525 | HAL_FLASH_Unlock(); |
---|
526 | |
---|
527 | // check for PG bit set if so clear bit |
---|
528 | FLASH_WaitForLastOperation(1000); |
---|
529 | if(READ_BIT(FLASH->CR, FLASH_CR_PG)) |
---|
530 | { |
---|
531 | CLEAR_BIT(FLASH->CR, FLASH_CR_PG); |
---|
532 | } |
---|
533 | |
---|
534 | /* Fill EraseInit structure*/ |
---|
535 | EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; |
---|
536 | EraseInitStruct.Page = FLASH_PAGE_NB - 1; // Page löschen |
---|
537 | EraseInitStruct.NbPages = 1; |
---|
538 | |
---|
539 | /* erase Sektoren daten dürfen nicht im instuction cache liegen */ |
---|
540 | if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) |
---|
541 | { |
---|
542 | printf("Error while deleting log data area in flash\n"); |
---|
543 | return HAL_ERROR; |
---|
544 | } |
---|
545 | |
---|
546 | /****************IM_FLASH_SPEICHERN********************/ |
---|
547 | |
---|
548 | /* programmiere uint64_t */ |
---|
549 | uint32_t startAdress = USER_EEPROM_BASE_ADRESS + FLASH_PAGE_SIZE; |
---|
550 | Address = startAdress; |
---|
551 | x = 0; |
---|
552 | while (Address < (startAdress + sizeof(log_data_t))) |
---|
553 | { |
---|
554 | if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, eepromLogData[x]) == HAL_OK) |
---|
555 | { |
---|
556 | Address = Address + 8; |
---|
557 | x ++; |
---|
558 | } |
---|
559 | else |
---|
560 | { |
---|
561 | printf("Error while saving data to flash\n"); |
---|
562 | return HAL_ERROR; |
---|
563 | } |
---|
564 | } |
---|
565 | |
---|
566 | /* Lock the Flash */ |
---|
567 | HAL_FLASH_Lock(); |
---|
568 | |
---|
569 | return HAL_OK; |
---|
570 | } |
---|
571 | |
---|
572 | |
---|
573 | |
---|
574 | HAL_StatusTypeDef EEPROM_readConfig(sys_data_t * data) |
---|
575 | { |
---|
576 | eeprom_stored_data_t * dataToStore; |
---|
577 | |
---|
578 | /****************WERTE_AUS_FLASH_LESEN********************/ |
---|
579 | |
---|
580 | |
---|
581 | if(getEEPROMData(USER_EEPROM_BASE_ADRESS, (uint8_t *)eepromData, sizeof(eepromData)) != HAL_OK) return HAL_ERROR; |
---|
582 | |
---|
583 | dataToStore = (eeprom_stored_data_t *) eepromData; |
---|
584 | |
---|
585 | // Schnittstellenparameter |
---|
586 | data->s.baudrate = dataToStore->changedData.baudrate; |
---|
587 | data->s.parityMode = dataToStore->changedData.parityMode; |
---|
588 | data->s.stopBits = dataToStore->changedData.stopBits; |
---|
589 | data->s.slaveAddress = dataToStore->changedData.slaveAddress; |
---|
590 | |
---|
591 | // // Cell Parameter |
---|
592 | data->s.ovpAlarm = dataToStore ->changedData.ovpAlarm; |
---|
593 | data->s.lvpAlarm = dataToStore ->changedData.lvpAlarm; |
---|
594 | data->s.ovpStart = dataToStore ->changedData.ovpStart; |
---|
595 | data->s.ovpStop = dataToStore ->changedData.ovpStop; |
---|
596 | data->s.lvpStart = dataToStore ->changedData.lvpStart; |
---|
597 | data->s.lvpStop = dataToStore ->changedData.lvpStop; |
---|
598 | data->s.vShutdownStart = dataToStore->changedData.vShutdownStart; |
---|
599 | data->s.vShutdownStop = dataToStore->changedData.vShutdownStop; |
---|
600 | data->s.otShutdownStart = dataToStore->changedData.otShutdownStart; |
---|
601 | data->s.otShutdownStop = dataToStore->changedData.otShutdownStop; |
---|
602 | data->s.utpChargeStart = dataToStore->changedData.utpChargeStart; |
---|
603 | data->s.utpChargeStop = dataToStore->changedData.utpChargeStop; |
---|
604 | data->s.utpDischargeStart = dataToStore->changedData.utpDischargeStart; |
---|
605 | data->s.utpDischargeStop = dataToStore->changedData.utpDischargeStop; |
---|
606 | data->s.lvpTime = dataToStore->changedData.lvpTime; |
---|
607 | data->s.defaultBalVoltage = dataToStore->changedData.defaultBalVoltage; |
---|
608 | |
---|
609 | |
---|
610 | // tempco |
---|
611 | data->s.refTemp = dataToStore ->changedData.refTemp; |
---|
612 | data->s.ovpAlarmTempco = dataToStore ->changedData.ovpAlarmTempco; |
---|
613 | data->s.lvpAlarmTempco = dataToStore ->changedData.lvpAlarmTempco; |
---|
614 | data->s.ovpStartTempco = dataToStore ->changedData.ovpStartTempco; |
---|
615 | data->s.ovpStopTempco = dataToStore->changedData.ovpStopTempco; |
---|
616 | data->s.lvpStartTempco = dataToStore->changedData.lvpStartTempco; |
---|
617 | data->s.lvpStopTempco = dataToStore->changedData.lvpStopTempco; |
---|
618 | data->s.vShutdownStartTempco = dataToStore->changedData.vShutdownStartTempco; |
---|
619 | data->s.vShutdownStopTempco = dataToStore->changedData.vShutdownStopTempco; |
---|
620 | data->s.balancerVoltageTempco = dataToStore->changedData.balancerVoltageTempco; |
---|
621 | savedLockKey = dataToStore->changedData.lockKey; |
---|
622 | if (savedLockKey != 0) sysData.s.writeLocked = 1 ; |
---|
623 | // Geräteinformation |
---|
624 | data->s.deviceSn = dataToStore->deviceInfo.SN; |
---|
625 | |
---|
626 | |
---|
627 | // prüfe Eeprom Status Infos |
---|
628 | if(dataToStore->eepromState.structureSize != sizeof(eeprom_stored_data_t)) return HAL_ERROR; |
---|
629 | if(dataToStore->eepromState.revisionInfo != 0) return HAL_ERROR; |
---|
630 | if(dataToStore->eepromState.firstStartId != FIRST_START_ID) return HAL_ERROR; |
---|
631 | |
---|
632 | return HAL_OK; |
---|
633 | } |
---|
634 | |
---|
635 | |
---|
636 | |
---|
637 | HAL_StatusTypeDef EEPROM_readLogData() |
---|
638 | { |
---|
639 | log_data_t * logData; |
---|
640 | |
---|
641 | /****************WERTE_AUS_FLASH_LESEN********************/ |
---|
642 | |
---|
643 | |
---|
644 | if(getEEPROMData(USER_EEPROM_BASE_ADRESS + FLASH_PAGE_SIZE, (uint8_t *)eepromLogData, sizeof(eepromLogData)) != HAL_OK) return HAL_ERROR; |
---|
645 | |
---|
646 | logData = (log_data_t *) eepromLogData; |
---|
647 | sysData.s.cycleCounter = logData->cycleCounter; |
---|
648 | sysData.s.maxVoltage = logData->maxVoltage; |
---|
649 | sysData.s.minVoltage = logData->minVoltage; |
---|
650 | sysData.s.maxTemperature = logData->maxVoltage; |
---|
651 | sysData.s.minTemperature = logData->minTemperature; |
---|
652 | sysData.s.criticalOverVoltageTime = logData->criticalOvervoltageTime; |
---|
653 | sysData.s.criticalUnderVoltageTime = logData->criticalUndervoltageTime; |
---|
654 | sysData.s.criticalOverTempTime = logData->criticalOverTempTime; |
---|
655 | sysData.s.criticalUnderTempTime = logData->criticalUnderTempTime; |
---|
656 | |
---|
657 | return HAL_OK; |
---|
658 | } |
---|