|
@@ -0,0 +1,104 @@
|
|
1
|
+/**
|
|
2
|
+ * Marlin 3D Printer Firmware
|
|
3
|
+ * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
4
|
+ *
|
|
5
|
+ * Based on Sprinter and grbl.
|
|
6
|
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
|
7
|
+ *
|
|
8
|
+ * This program is free software: you can redistribute it and/or modify
|
|
9
|
+ * it under the terms of the GNU General Public License as published by
|
|
10
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+ * (at your option) any later version.
|
|
12
|
+ *
|
|
13
|
+ * This program is distributed in the hope that it will be useful,
|
|
14
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+ * GNU General Public License for more details.
|
|
17
|
+ *
|
|
18
|
+ * You should have received a copy of the GNU General Public License
|
|
19
|
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+ *
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+/**
|
|
24
|
+ * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
|
|
25
|
+ * Implementation of EEPROM settings in SD Card
|
|
26
|
+ */
|
|
27
|
+
|
|
28
|
+#ifdef TARGET_STM32F4
|
|
29
|
+
|
|
30
|
+#include "../../inc/MarlinConfig.h"
|
|
31
|
+
|
|
32
|
+#if ENABLED(EEPROM_SETTINGS) && NONE(FLASH_EEPROM_EMULATION, SPI_EEPROM, I2C_EEPROM)
|
|
33
|
+
|
|
34
|
+#include "../shared/persistent_store_api.h"
|
|
35
|
+
|
|
36
|
+#ifndef E2END
|
|
37
|
+ #define E2END 0xFFF // 4KB
|
|
38
|
+#endif
|
|
39
|
+#define HAL_EEPROM_SIZE (E2END + 1) // 16KB
|
|
40
|
+
|
|
41
|
+#define _ALIGN(x) __attribute__ ((aligned(x))) // SDIO uint32_t* compat.
|
|
42
|
+static char _ALIGN(4) HAL_eeprom_data[HAL_EEPROM_SIZE];
|
|
43
|
+
|
|
44
|
+#if ENABLED(SDSUPPORT)
|
|
45
|
+
|
|
46
|
+ #include "../../sd/cardreader.h"
|
|
47
|
+
|
|
48
|
+ #define EEPROM_FILENAME "eeprom.dat"
|
|
49
|
+
|
|
50
|
+ bool PersistentStore::access_start() {
|
|
51
|
+ if (!card.isDetected()) return false;
|
|
52
|
+
|
|
53
|
+ SdFile file, root = card.getroot();
|
|
54
|
+ if (!file.open(&root, EEPROM_FILENAME, O_RDONLY))
|
|
55
|
+ return false;
|
|
56
|
+
|
|
57
|
+ int16_t bytes_read = file.read(HAL_eeprom_data, HAL_STM32F4_EEPROM_SIZE);
|
|
58
|
+ if (bytes_read < 0) return false;
|
|
59
|
+ for (; bytes_read < HAL_STM32F4_EEPROM_SIZE; bytes_read++)
|
|
60
|
+ HAL_eeprom_data[bytes_read] = 0xFF;
|
|
61
|
+ file.close();
|
|
62
|
+ return true;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ bool PersistentStore::access_finish() {
|
|
66
|
+ if (!card.isDetected()) return false;
|
|
67
|
+
|
|
68
|
+ SdFile file, root = card.getroot();
|
|
69
|
+ int16_t bytes_written = 0;
|
|
70
|
+ if (file.open(&root, EEPROM_FILENAME, O_CREAT | O_WRITE | O_TRUNC)) {
|
|
71
|
+ bytes_written = file.write(HAL_eeprom_data, HAL_STM32F4_EEPROM_SIZE);
|
|
72
|
+ file.close();
|
|
73
|
+ }
|
|
74
|
+ return (bytes_written == HAL_STM32F4_EEPROM_SIZE);
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+#else // !SDSUPPORT
|
|
78
|
+
|
|
79
|
+ #error "Please define SPI_EEPROM (in Configuration.h) or disable EEPROM_SETTINGS."
|
|
80
|
+
|
|
81
|
+#endif // !SDSUPPORT
|
|
82
|
+
|
|
83
|
+bool PersistentStore::write_data(int &pos, const uint8_t *value, const size_t size, uint16_t *crc) {
|
|
84
|
+ for (size_t i = 0; i < size; i++)
|
|
85
|
+ HAL_eeprom_data[pos + i] = value[i];
|
|
86
|
+ crc16(crc, value, size);
|
|
87
|
+ pos += size;
|
|
88
|
+ return false;
|
|
89
|
+}
|
|
90
|
+
|
|
91
|
+bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
|
|
92
|
+ for (size_t i = 0; i < size; i++) {
|
|
93
|
+ uint8_t c = HAL_eeprom_data[pos + i];
|
|
94
|
+ if (writing) value[i] = c;
|
|
95
|
+ crc16(crc, &c, 1);
|
|
96
|
+ }
|
|
97
|
+ pos += size;
|
|
98
|
+ return false;
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+size_t PersistentStore::capacity() { return HAL_STM32F4_EEPROM_SIZE; }
|
|
102
|
+
|
|
103
|
+#endif // EEPROM_SETTINGS
|
|
104
|
+#endif // __STM32F4__
|