Parcourir la source

Add SPI EEPROM to STM32F1 (#14239)

Tanguy Pruvot il y a 6 ans
Parent
révision
c3e5225531

+ 17
- 1
Marlin/src/HAL/HAL_STM32F1/HAL_spi_STM32F1.cpp Voir le fichier

@@ -40,9 +40,10 @@
40 40
 #include "../shared/HAL_SPI.h"
41 41
 #include "pins_arduino.h"
42 42
 #include "spi_pins.h"
43
-#include "../../core/macros.h"
44 43
 #include <SPI.h>
45 44
 
45
+#include "../../inc/MarlinConfigPre.h"
46
+
46 47
 // --------------------------------------------------------------------------
47 48
 // Public Variables
48 49
 // --------------------------------------------------------------------------
@@ -180,6 +181,21 @@ void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode)
180 181
   SPI.beginTransaction(spiConfig);
181 182
 }
182 183
 
184
+#if ENABLED(SPI_EEPROM)
185
+
186
+// Read single byte from specified SPI channel
187
+uint8_t spiRec(uint32_t chan) { return spiRec(); }
188
+
189
+// Write single byte to specified SPI channel
190
+void spiSend(uint32_t chan, byte b) { spiSend(b); }
191
+
192
+// Write buffer to specified SPI channel
193
+void spiSend(uint32_t chan, const uint8_t* buf, size_t n) {
194
+  for (size_t p = 0; p < n; p++) spiSend(buf[p]);
195
+}
196
+
197
+#endif // SPI_EEPROM
198
+
183 199
 #endif // SOFTWARE_SPI
184 200
 
185 201
 #endif // __STM32F1__

+ 66
- 0
Marlin/src/HAL/HAL_STM32F1/persistent_store_eeprom.cpp Voir le fichier

@@ -0,0 +1,66 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ *
4
+ * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+#ifdef __STM32F1__
22
+
23
+#include "../../inc/MarlinConfig.h"
24
+
25
+#if ENABLED(EEPROM_SETTINGS) && EITHER(SPI_EEPROM, I2C_EEPROM)
26
+
27
+#include "../shared/persistent_store_api.h"
28
+
29
+bool PersistentStore::access_start() { return true; }
30
+bool PersistentStore::access_finish() { return true; }
31
+
32
+bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
33
+  while (size--) {
34
+    uint8_t * const p = (uint8_t * const)pos;
35
+    uint8_t v = *value;
36
+    // EEPROM has only ~100,000 write cycles,
37
+    // so only write bytes that have changed!
38
+    if (v != eeprom_read_byte(p)) {
39
+      eeprom_write_byte(p, v);
40
+      if (eeprom_read_byte(p) != v) {
41
+        SERIAL_ECHO_MSG(MSG_ERR_EEPROM_WRITE);
42
+        return true;
43
+      }
44
+    }
45
+    crc16(crc, &v, 1);
46
+    pos++;
47
+    value++;
48
+  };
49
+  return false;
50
+}
51
+
52
+bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool set/*=true*/) {
53
+  do {
54
+    uint8_t c = eeprom_read_byte((uint8_t*)pos);
55
+    if (set && value) *value = c;
56
+    crc16(crc, &c, 1);
57
+    pos++;
58
+    value++;
59
+  } while (--size);
60
+  return false;
61
+}
62
+
63
+size_t PersistentStore::capacity() { return E2END + 1; }
64
+
65
+#endif // EEPROM_SETTINGS && EITHER(SPI_EEPROM, I2C_EEPROM)
66
+#endif // __STM32F1__

+ 1
- 1
Marlin/src/HAL/HAL_STM32F1/persistent_store_sdcard.cpp Voir le fichier

@@ -28,7 +28,7 @@
28 28
 
29 29
 #include "../../inc/MarlinConfig.h"
30 30
 
31
-#if ENABLED(EEPROM_SETTINGS) && DISABLED(FLASH_EEPROM_EMULATION)
31
+#if ENABLED(EEPROM_SETTINGS) && NONE(FLASH_EEPROM_EMULATION, SPI_EEPROM, I2C_EEPROM)
32 32
 
33 33
 #include "../shared/persistent_store_api.h"
34 34
 

Chargement…
Annuler
Enregistrer