Browse Source

Minimal interface for F4/F7 flash

Scott Lahteine 5 years ago
parent
commit
7f5730ea3b

+ 5
- 2
Marlin/src/HAL/STM32_F4_F7/eeprom_emul.cpp View File

49
   */
49
   */
50
 #if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7))
50
 #if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7))
51
 
51
 
52
-#include "../../inc/MarlinConfigPre.h"
52
+#include "../../inc/MarlinConfig.h"
53
 
53
 
54
 #if ENABLED(FLASH_EEPROM_EMULATION)
54
 #if ENABLED(FLASH_EEPROM_EMULATION)
55
 
55
 
65
 uint16_t VirtAddVarTab[NB_OF_VAR];
65
 uint16_t VirtAddVarTab[NB_OF_VAR];
66
 
66
 
67
 /* Private function prototypes -----------------------------------------------*/
67
 /* Private function prototypes -----------------------------------------------*/
68
-/* Private functions ---------------------------------------------------------*/
68
+
69
 static HAL_StatusTypeDef EE_Format();
69
 static HAL_StatusTypeDef EE_Format();
70
 static uint16_t EE_FindValidPage(uint8_t Operation);
70
 static uint16_t EE_FindValidPage(uint8_t Operation);
71
 static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
71
 static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
79
   * @retval - Flash error code: on write Flash error
79
   * @retval - Flash error code: on write Flash error
80
   *         - FLASH_COMPLETE: on success
80
   *         - FLASH_COMPLETE: on success
81
   */
81
   */
82
+
83
+/* Private functions ---------------------------------------------------------*/
84
+
82
 uint16_t EE_Initialize() {
85
 uint16_t EE_Initialize() {
83
   /* Get Page0 and Page1 status */
86
   /* Get Page0 and Page1 status */
84
   uint16_t PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS),
87
   uint16_t PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS),

+ 0
- 1
Marlin/src/HAL/STM32_F4_F7/eeprom_emul.h View File

50
 // ------------------------
50
 // ------------------------
51
 
51
 
52
 #include "../../inc/MarlinConfig.h"
52
 #include "../../inc/MarlinConfig.h"
53
-#include "HAL.h"
54
 
53
 
55
 /* Exported constants --------------------------------------------------------*/
54
 /* Exported constants --------------------------------------------------------*/
56
 /* EEPROM emulation firmware error codes */
55
 /* EEPROM emulation firmware error codes */

Marlin/src/HAL/STM32_F4_F7/eeprom_if_flash.cpp → Marlin/src/HAL/STM32_F4_F7/eeprom_flash.cpp View File

18
  */
18
  */
19
 #if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7))
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
 #include "../../inc/MarlinConfig.h"
21
 #include "../../inc/MarlinConfig.h"
27
 
22
 
28
 #if ENABLED(FLASH_EEPROM_EMULATION)
23
 #if ENABLED(FLASH_EEPROM_EMULATION)
29
 
24
 
30
-// ------------------------
31
-// Includes
32
-// ------------------------
33
-
34
-#include "HAL.h"
25
+#include "../shared/eeprom_api.h"
35
 #include "eeprom_emul.h"
26
 #include "eeprom_emul.h"
36
-#include "../shared/eeprom_if.h"
37
-
38
-// ------------------------
39
-// Local defines
40
-// ------------------------
41
 
27
 
42
 // FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
28
 // FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
43
 // FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
29
 // FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
48
   //#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
34
   //#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
49
 #endif
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
     HAL_FLASH_Unlock();
40
     HAL_FLASH_Unlock();
64
 
41
 
65
     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
42
     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
69
       for (;;) HAL_Delay(1); // Spin forever until watchdog reset
46
       for (;;) HAL_Delay(1); // Spin forever until watchdog reset
70
 
47
 
71
     HAL_FLASH_Lock();
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
   HAL_FLASH_Unlock();
56
   HAL_FLASH_Unlock();
80
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
57
   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
86
   HAL_FLASH_Lock();
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
   uint16_t data = 0xFF;
69
   uint16_t data = 0xFF;
93
   const unsigned eeprom_address = (unsigned)pos;
70
   const unsigned eeprom_address = (unsigned)pos;
96
   return uint8_t(data);
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
 #endif // FLASH_EEPROM_EMULATION
111
 #endif // FLASH_EEPROM_EMULATION

Loading…
Cancel
Save