|
@@ -18,26 +18,12 @@
|
18
|
18
|
*/
|
19
|
19
|
#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7))
|
20
|
20
|
|
21
|
|
-/**
|
22
|
|
- * Arduino-style interface for Flash emulated EEPROM
|
23
|
|
- */
|
24
|
|
-
|
25
|
|
-// Include configs and pins to get all EEPROM flags
|
26
|
21
|
#include "../../inc/MarlinConfig.h"
|
27
|
22
|
|
28
|
23
|
#if ENABLED(FLASH_EEPROM_EMULATION)
|
29
|
24
|
|
30
|
|
-// ------------------------
|
31
|
|
-// Includes
|
32
|
|
-// ------------------------
|
33
|
|
-
|
34
|
|
-#include "HAL.h"
|
|
25
|
+#include "../shared/eeprom_api.h"
|
35
|
26
|
#include "eeprom_emul.h"
|
36
|
|
-#include "../shared/eeprom_if.h"
|
37
|
|
-
|
38
|
|
-// ------------------------
|
39
|
|
-// Local defines
|
40
|
|
-// ------------------------
|
41
|
27
|
|
42
|
28
|
// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
|
43
|
29
|
// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
|
|
@@ -48,18 +34,9 @@
|
48
|
34
|
//#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
|
49
|
35
|
#endif
|
50
|
36
|
|
51
|
|
-// ------------------------
|
52
|
|
-// Private Variables
|
53
|
|
-// ------------------------
|
54
|
|
-
|
55
|
|
-static bool eeprom_initialized = false;
|
56
|
|
-
|
57
|
|
-// ------------------------
|
58
|
|
-// Public functions
|
59
|
|
-// ------------------------
|
60
|
|
-
|
61
|
|
-void eeprom_init() {
|
62
|
|
- if (!eeprom_initialized) {
|
|
37
|
+void ee_init() {
|
|
38
|
+ static bool ee_initialized = false;
|
|
39
|
+ if (!ee_initialized) {
|
63
|
40
|
HAL_FLASH_Unlock();
|
64
|
41
|
|
65
|
42
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
|
|
@@ -69,12 +46,12 @@ void eeprom_init() {
|
69
|
46
|
for (;;) HAL_Delay(1); // Spin forever until watchdog reset
|
70
|
47
|
|
71
|
48
|
HAL_FLASH_Lock();
|
72
|
|
- eeprom_initialized = true;
|
|
49
|
+ ee_initialized = true;
|
73
|
50
|
}
|
74
|
51
|
}
|
75
|
52
|
|
76
|
|
-void eeprom_write_byte(uint8_t *pos, unsigned char value) {
|
77
|
|
- eeprom_init();
|
|
53
|
+void ee_write_byte(uint8_t *pos, unsigned char value) {
|
|
54
|
+ ee_init();
|
78
|
55
|
|
79
|
56
|
HAL_FLASH_Unlock();
|
80
|
57
|
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
|
|
@@ -86,8 +63,8 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) {
|
86
|
63
|
HAL_FLASH_Lock();
|
87
|
64
|
}
|
88
|
65
|
|
89
|
|
-uint8_t eeprom_read_byte(uint8_t *pos) {
|
90
|
|
- eeprom_init();
|
|
66
|
+uint8_t ee_read_byte(uint8_t *pos) {
|
|
67
|
+ ee_init();
|
91
|
68
|
|
92
|
69
|
uint16_t data = 0xFF;
|
93
|
70
|
const unsigned eeprom_address = (unsigned)pos;
|
|
@@ -96,19 +73,39 @@ uint8_t eeprom_read_byte(uint8_t *pos) {
|
96
|
73
|
return uint8_t(data);
|
97
|
74
|
}
|
98
|
75
|
|
99
|
|
-void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
|
100
|
|
- eeprom_init();
|
101
|
|
-
|
102
|
|
- uint16_t data = 0xFF;
|
103
|
|
- const unsigned eeprom_address = (unsigned)__src;
|
104
|
|
- LOOP_L_N(c, __n) {
|
105
|
|
- EE_ReadVariable(eeprom_address+c, &data);
|
106
|
|
- *((uint8_t*)__dst + c) = data;
|
107
|
|
- }
|
|
76
|
+size_t PersistentStore::capacity() { return E2END + 1; }
|
|
77
|
+bool PersistentStore::access_start() { return true; }
|
|
78
|
+bool PersistentStore::access_finish() { return true; }
|
|
79
|
+
|
|
80
|
+bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
|
|
81
|
+ while (size--) {
|
|
82
|
+ uint8_t * const p = (uint8_t * const)pos;
|
|
83
|
+ uint8_t v = *value;
|
|
84
|
+ // EEPROM has only ~100,000 write cycles,
|
|
85
|
+ // so only write bytes that have changed!
|
|
86
|
+ if (v != ee_read_byte(p)) {
|
|
87
|
+ ee_write_byte(p, v);
|
|
88
|
+ if (ee_read_byte(p) != v) {
|
|
89
|
+ SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE);
|
|
90
|
+ return true;
|
|
91
|
+ }
|
|
92
|
+ }
|
|
93
|
+ crc16(crc, &v, 1);
|
|
94
|
+ pos++;
|
|
95
|
+ value++;
|
|
96
|
+ };
|
|
97
|
+ return false;
|
108
|
98
|
}
|
109
|
99
|
|
110
|
|
-void eeprom_update_block(const void *__src, void *__dst, size_t __n) {
|
111
|
|
-
|
|
100
|
+bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
|
|
101
|
+ do {
|
|
102
|
+ uint8_t c = ee_read_byte((uint8_t*)pos);
|
|
103
|
+ if (writing) *value = c;
|
|
104
|
+ crc16(crc, &c, 1);
|
|
105
|
+ pos++;
|
|
106
|
+ value++;
|
|
107
|
+ } while (--size);
|
|
108
|
+ return false;
|
112
|
109
|
}
|
113
|
110
|
|
114
|
111
|
#endif // FLASH_EEPROM_EMULATION
|