Browse Source

W25QXX SPI Flash support (#18897)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
Victor Oliveira 4 years ago
parent
commit
0a1b865987
No account linked to committer's email address

+ 92
- 0
Marlin/src/gcode/control/M993_M994.cpp View File

@@ -0,0 +1,92 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#include "../../inc/MarlinConfig.h"
24
+
25
+#if ALL(HAS_SPI_FLASH, SDSUPPORT, MARLIN_DEV_MODE)
26
+
27
+#include "../gcode.h"
28
+#include "../../sd/cardreader.h"
29
+#include "../../libs/W25Qxx.h"
30
+
31
+/**
32
+ * M993: Backup SPI Flash to SD
33
+ */
34
+void GcodeSuite::M993() {
35
+  if (!card.isMounted()) card.mount();
36
+
37
+  char fname[] = "spiflash.bin";
38
+  card.openFileWrite(fname);
39
+  if (!card.isFileOpen()) {
40
+    SERIAL_ECHOLNPAIR("Failed to open ", fname, " to write.");
41
+    return;
42
+  }
43
+
44
+  W25QXXFlash W25QXX;
45
+
46
+  uint8_t buf[1024];
47
+  uint32_t addr = 0;
48
+  W25QXX.init(SPI_QUARTER_SPEED);
49
+  SERIAL_ECHOPGM("Save SPI Flash");
50
+  while (addr < SPI_FLASH_SIZE) {
51
+    W25QXX.SPI_FLASH_BufferRead(buf, addr, COUNT(buf));
52
+    addr += COUNT(buf);
53
+    card.write(buf, COUNT(buf));
54
+    if (addr % (COUNT(buf) * 10) == 0) SERIAL_CHAR('.');
55
+  }
56
+  SERIAL_ECHOLNPGM(" done");
57
+
58
+  card.closefile();
59
+}
60
+
61
+/**
62
+ * M994: Load a backup from SD to SPI Flash
63
+ */
64
+void GcodeSuite::M994() {
65
+  if (!card.isMounted()) card.mount();
66
+
67
+  char fname[] = "spiflash.bin";
68
+  card.openFileRead(fname);
69
+  if (!card.isFileOpen()) {
70
+    SERIAL_ECHOLNPAIR("Failed to open ", fname, " to read.");
71
+    return;
72
+  }
73
+
74
+  W25QXXFlash W25QXX;
75
+
76
+  uint8_t buf[1024];
77
+  uint32_t addr = 0;
78
+  W25QXX.init(SPI_QUARTER_SPEED);
79
+  W25QXX.SPI_FLASH_BulkErase();
80
+  SERIAL_ECHOPGM("Load SPI Flash");
81
+  while(addr < SPI_FLASH_SIZE) {
82
+    card.read(buf, COUNT(buf));
83
+    W25QXX.SPI_FLASH_BufferWrite(buf, addr, COUNT(buf));
84
+    addr += COUNT(buf);
85
+    if (addr % (COUNT(buf) * 10) == 0) SERIAL_CHAR('.');
86
+  }
87
+  SERIAL_ECHOLNPGM(" done");
88
+
89
+  card.closefile();
90
+}
91
+
92
+#endif // HAS_SPI_FLASH && SDSUPPORT && MARLIN_DEV_MODE

+ 5
- 0
Marlin/src/gcode/gcode.cpp View File

@@ -871,6 +871,11 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
871 871
         case 422: M422(); break;                                  // M422: Set Z Stepper automatic alignment position using probe
872 872
       #endif
873 873
 
874
+      #if BOTH(HAS_SPI_FLASH, SDSUPPORT)
875
+        case 993: M993(); break;                                  // M993: Backup SPI Flash to SD
876
+        case 994: M994(); break;                                  // M994: Load a Backup from SD to SPI Flash
877
+      #endif
878
+
874 879
       #if ENABLED(TOUCH_SCREEN_CALIBRATION)
875 880
         case 995: M995(); break;                                  // M995: Touch screen calibration for TFT display
876 881
       #endif

+ 7
- 0
Marlin/src/gcode/gcode.h View File

@@ -276,6 +276,8 @@
276 276
  * ************ Custom codes - This can change to suit future G-code regulations
277 277
  * G425 - Calibrate using a conductive object. (Requires CALIBRATION_GCODE)
278 278
  * M928 - Start SD logging: "M928 filename.gco". Stop with M29. (Requires SDSUPPORT)
279
+ * M993 - Backup SPI Flash to SD
280
+ * M994 - Load a Backup from SD to SPI Flash
279 281
  * M995 - Touch screen calibration for TFT display
280 282
  * M997 - Perform in-application firmware update
281 283
  * M999 - Restart after being stopped by error
@@ -846,6 +848,11 @@ private:
846 848
 
847 849
   TERN_(TOUCH_SCREEN_CALIBRATION, static void M995());
848 850
 
851
+  #if BOTH(HAS_SPI_FLASH, SDSUPPORT)
852
+    static void M993();
853
+    static void M994();
854
+  #endif
855
+
849 856
   TERN_(PLATFORM_M997_SUPPORT, static void M997());
850 857
 
851 858
   static void M999();

+ 2
- 0
Marlin/src/lcd/extui/lib/mks_ui/SPIFlashStorage.cpp View File

@@ -27,6 +27,8 @@
27 27
 #include "../../../../inc/MarlinConfig.h"
28 28
 #include "SPIFlashStorage.h"
29 29
 
30
+extern W25QXXFlash W25QXX;
31
+
30 32
 uint8_t SPIFlashStorage::m_pageData[SPI_FLASH_PageSize];
31 33
 uint32_t SPIFlashStorage::m_currentPage;
32 34
 uint16_t SPIFlashStorage::m_pageDataUsed;

+ 1
- 1
Marlin/src/lcd/extui/lib/mks_ui/SPIFlashStorage.h View File

@@ -21,7 +21,7 @@
21 21
  */
22 22
 #pragma once
23 23
 
24
-#include "W25Qxx.h"
24
+#include "../../../../libs/W25Qxx.h"
25 25
 
26 26
 #define HAS_SPI_FLASH_COMPRESSION 1
27 27
 

+ 1
- 1
Marlin/src/lcd/extui/lib/mks_ui/draw_ui.cpp View File

@@ -27,7 +27,6 @@
27 27
   #include "SPI_TFT.h"
28 28
 #endif
29 29
 
30
-#include "W25Qxx.h"
31 30
 #include "tft_lvgl_configuration.h"
32 31
 
33 32
 #include "pic_manager.h"
@@ -50,6 +49,7 @@
50 49
   #include "../../../../feature/pause.h"
51 50
 #endif
52 51
 
52
+W25QXXFlash W25QXX;
53 53
 CFG_ITMES gCfgItems;
54 54
 UI_CFG uiCfg;
55 55
 DISP_STATE_STACK disp_state_stack;

+ 0
- 1
Marlin/src/lcd/extui/lib/mks_ui/mks_hardware_test.cpp View File

@@ -29,7 +29,6 @@
29 29
 
30 30
 #include "tft_lvgl_configuration.h"
31 31
 #include "draw_ready_print.h"
32
-#include "W25Qxx.h"
33 32
 #include "mks_hardware_test.h"
34 33
 #include "draw_ui.h"
35 34
 #include "pic_manager.h"

+ 2
- 2
Marlin/src/lcd/extui/lib/mks_ui/pic_manager.cpp View File

@@ -29,10 +29,10 @@
29 29
 #include "mks_hardware_test.h"
30 30
 
31 31
 #include "SPIFlashStorage.h"
32
-#include "W25Qxx.h"
32
+#include "../../../../libs/W25Qxx.h"
33 33
 
34
-#include "../../../../MarlinCore.h"
35 34
 #include "../../../../sd/cardreader.h"
35
+#include "../../../../MarlinCore.h"
36 36
 
37 37
 extern uint16_t DeviceCode;
38 38
 extern unsigned char bmp_public_buf[17 * 1024];

+ 4
- 0
Marlin/src/lcd/extui/lib/mks_ui/pic_manager.h View File

@@ -23,6 +23,8 @@
23 23
 
24 24
 #include "../../../../inc/MarlinConfig.h"
25 25
 
26
+#include "../../../../libs/W25Qxx.h"
27
+
26 28
 #include <lvgl.h>
27 29
 
28 30
 #include <stdint.h>
@@ -154,6 +156,8 @@ extern void spi_flash_read_test();
154 156
 extern void default_view_Read(uint8_t *default_view_Rbuff, uint32_t default_view_Readsize);
155 157
 extern void flash_view_Read(uint8_t *flash_view_Rbuff, uint32_t flash_view_Readsize);
156 158
 
159
+extern W25QXXFlash W25QXX;
160
+
157 161
 #ifdef __cplusplus
158 162
 } /* C-declarations for C++ */
159 163
 #endif

+ 0
- 1
Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp View File

@@ -35,7 +35,6 @@
35 35
 
36 36
 #include "tft_lvgl_configuration.h"
37 37
 #include "draw_ready_print.h"
38
-#include "W25Qxx.h"
39 38
 #include "pic_manager.h"
40 39
 #include "mks_hardware_test.h"
41 40
 #include "draw_ui.h"

Marlin/src/lcd/extui/lib/mks_ui/W25Qxx.cpp → Marlin/src/libs/W25Qxx.cpp View File

@@ -19,13 +19,12 @@
19 19
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 20
  *
21 21
  */
22
-#include "../../../../inc/MarlinConfigPre.h"
23 22
 
24
-#if 1 // ENABLED(SPI_FLASH)
25
-#if HAS_TFT_LVGL_UI
23
+#include "../inc/MarlinConfig.h"
24
+
25
+#if HAS_SPI_FLASH
26 26
 
27 27
 #include <SPI.h>
28
-#include "../../../../inc/MarlinConfig.h"
29 28
 
30 29
 #include "W25Qxx.h"
31 30
 
@@ -45,9 +44,7 @@
45 44
 #define W25QXX_CS_H OUT_WRITE(SPI_FLASH_CS_PIN, HIGH)
46 45
 #define W25QXX_CS_L OUT_WRITE(SPI_FLASH_CS_PIN, LOW)
47 46
 
48
-ext_FLASH W25QXX;
49
-
50
-void ext_FLASH::init(uint8_t spiRate) {
47
+void W25QXXFlash::init(uint8_t spiRate) {
51 48
 
52 49
   OUT_WRITE(SPI_FLASH_CS_PIN, HIGH);
53 50
 
@@ -85,12 +82,12 @@ void ext_FLASH::init(uint8_t spiRate) {
85 82
  *
86 83
  * @details
87 84
  */
88
-uint8_t ext_FLASH::spi_flash_Rec() {
85
+uint8_t W25QXXFlash::spi_flash_Rec() {
89 86
   uint8_t returnByte = SPI.transfer(ff);
90 87
   return returnByte;
91 88
 }
92 89
 
93
-uint8_t ext_FLASH::spi_flash_read_write_byte(uint8_t data) {
90
+uint8_t W25QXXFlash::spi_flash_read_write_byte(uint8_t data) {
94 91
   uint8_t returnByte = SPI.transfer(data);
95 92
   return returnByte;
96 93
 }
@@ -104,7 +101,7 @@ uint8_t ext_FLASH::spi_flash_read_write_byte(uint8_t data) {
104 101
  *
105 102
  * @details Uses DMA
106 103
  */
107
-void ext_FLASH::spi_flash_Read(uint8_t* buf, uint16_t nbyte) { SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte); }
104
+void W25QXXFlash::spi_flash_Read(uint8_t* buf, uint16_t nbyte) { SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte); }
108 105
 
109 106
 /**
110 107
  * @brief  Send a single byte on SPI port
@@ -113,7 +110,7 @@ void ext_FLASH::spi_flash_Read(uint8_t* buf, uint16_t nbyte) { SPI.dmaTransfer(0
113 110
  *
114 111
  * @details
115 112
  */
116
-void ext_FLASH::spi_flash_Send(uint8_t b) { SPI.send(b); }
113
+void W25QXXFlash::spi_flash_Send(uint8_t b) { SPI.send(b); }
117 114
 
118 115
 /**
119 116
  * @brief  Write token and then write from 512 byte buffer to SPI (for SD card)
@@ -123,12 +120,12 @@ void ext_FLASH::spi_flash_Send(uint8_t b) { SPI.send(b); }
123 120
  *
124 121
  * @details Use DMA
125 122
  */
126
-void ext_FLASH::spi_flash_SendBlock(uint8_t token, const uint8_t* buf) {
123
+void W25QXXFlash::spi_flash_SendBlock(uint8_t token, const uint8_t* buf) {
127 124
   SPI.send(token);
128 125
   SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
129 126
 }
130 127
 
131
-uint16_t ext_FLASH::W25QXX_ReadID(void) {
128
+uint16_t W25QXXFlash::W25QXX_ReadID(void) {
132 129
   uint16_t Temp = 0;
133 130
   W25QXX_CS_L;
134 131
   spi_flash_Send(0x90);
@@ -141,7 +138,7 @@ uint16_t ext_FLASH::W25QXX_ReadID(void) {
141 138
   return Temp;
142 139
 }
143 140
 
144
-void ext_FLASH::SPI_FLASH_WriteEnable(void) {
141
+void W25QXXFlash::SPI_FLASH_WriteEnable(void) {
145 142
   /* Select the FLASH: Chip Select low */
146 143
   W25QXX_CS_L;
147 144
   /* Send "Write Enable" instruction */
@@ -159,7 +156,7 @@ void ext_FLASH::SPI_FLASH_WriteEnable(void) {
159 156
 * Output         : None
160 157
 * Return         : None
161 158
 *******************************************************************************/
162
-void ext_FLASH::SPI_FLASH_WaitForWriteEnd(void) {
159
+void W25QXXFlash::SPI_FLASH_WaitForWriteEnd(void) {
163 160
   uint8_t FLASH_Status = 0;
164 161
 
165 162
   /* Select the FLASH: Chip Select low */
@@ -178,7 +175,7 @@ void ext_FLASH::SPI_FLASH_WaitForWriteEnd(void) {
178 175
   W25QXX_CS_H;
179 176
 }
180 177
 
181
-void ext_FLASH::SPI_FLASH_SectorErase(uint32_t SectorAddr) {
178
+void W25QXXFlash::SPI_FLASH_SectorErase(uint32_t SectorAddr) {
182 179
   /* Send write enable instruction */
183 180
   SPI_FLASH_WriteEnable();
184 181
 
@@ -200,7 +197,7 @@ void ext_FLASH::SPI_FLASH_SectorErase(uint32_t SectorAddr) {
200 197
   SPI_FLASH_WaitForWriteEnd();
201 198
 }
202 199
 
203
-void ext_FLASH::SPI_FLASH_BlockErase(uint32_t BlockAddr) {
200
+void W25QXXFlash::SPI_FLASH_BlockErase(uint32_t BlockAddr) {
204 201
   SPI_FLASH_WriteEnable();
205 202
   W25QXX_CS_L;
206 203
   /* Send Sector Erase instruction */
@@ -224,7 +221,7 @@ void ext_FLASH::SPI_FLASH_BlockErase(uint32_t BlockAddr) {
224 221
 * Output         : None
225 222
 * Return         : None
226 223
 *******************************************************************************/
227
-void ext_FLASH::SPI_FLASH_BulkErase(void) {
224
+void W25QXXFlash::SPI_FLASH_BulkErase(void) {
228 225
   /* Send write enable instruction */
229 226
   SPI_FLASH_WriteEnable();
230 227
 
@@ -253,7 +250,7 @@ void ext_FLASH::SPI_FLASH_BulkErase(void) {
253 250
 * Output         : None
254 251
 * Return         : None
255 252
 *******************************************************************************/
256
-void ext_FLASH::SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) {
253
+void W25QXXFlash::SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) {
257 254
   /* Enable the write access to the FLASH */
258 255
   SPI_FLASH_WriteEnable();
259 256
 
@@ -296,7 +293,7 @@ void ext_FLASH::SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16
296 293
 * Output         : None
297 294
 * Return         : None
298 295
 *******************************************************************************/
299
-void ext_FLASH::SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) {
296
+void W25QXXFlash::SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) {
300 297
   uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
301 298
 
302 299
   Addr = WriteAddr % SPI_FLASH_PageSize;
@@ -361,7 +358,7 @@ void ext_FLASH::SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint
361 358
 * Output         : None
362 359
 * Return         : None
363 360
 *******************************************************************************/
364
-void ext_FLASH::SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) {
361
+void W25QXXFlash::SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) {
365 362
   /* Select the FLASH: Chip Select low */
366 363
   W25QXX_CS_L;
367 364
 
@@ -389,7 +386,4 @@ void ext_FLASH::SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16
389 386
   W25QXX_CS_H;
390 387
 }
391 388
 
392
-void ext_FLASH::lv_pic_read(uint8_t *P_Rbuff, uint32_t addr, uint32_t size) {SPI_FLASH_BufferRead((uint8_t *)P_Rbuff, addr, size);}
393
-
394
-#endif // HAS_TFT_LVGL_UI
395
-#endif // 1 ... SPI_FLASH
389
+#endif // HAS_SPI_FLASH

Marlin/src/lcd/extui/lib/mks_ui/W25Qxx.h → Marlin/src/libs/W25Qxx.h View File

@@ -52,49 +52,7 @@
52 52
 #define SPI_FLASH_PageSize           256
53 53
 #define SPI_FLASH_PerWritePageSize   256
54 54
 
55
-#if 0
56
-
57
-  #define PIC_NAME_MAX_LEN        50
58
-
59
-  #define LOGO_MAX_SIZE           (300*1024)
60
-  #define TITLELOGO_MAX_SIZE      (150*1024)
61
-  #define DEFAULT_VIEW_MAX_SIZE   (200*200*2)
62
-  #define FLASH_VIEW_MAX_SIZE     (200*200*2)
63
-
64
-  //Robin 2
65
-  #define PIC_NAME_ADDR           0x003000
66
-  #define PIC_SIZE_ADDR           0x007000
67
-  #define PIC_COUNTER_ADDR        0x008000
68
-  #define PIC_LOGO_ADDR           0x009000
69
-  //#define PIC_DATA_ADDR         0x02f000
70
-
71
-  #define DEFAULT_VIEW_ADDR       0XC5800
72
-  #define BAK_VIEW_ADDR           (DEFAULT_VIEW_ADDR+90*1024)
73
-  #define PIC_ICON_LOGO_ADDR      (BAK_VIEW_ADDR+80*1024)
74
-
75
-  #define PIC_DATA_ADDR           (PIC_ICON_LOGO_ADDR+350*1024)
76
-
77
-  #define FONTINFOADDR            0x600000
78
-  #define UNIGBK_FLASH_ADDR       (FONTINFOADDR+4096) // 4*1024
79
-  #define GBK_FLASH_ADDR          (UNIGBK_FLASH_ADDR+180224) // 176*1024
80
-
81
-  #define PER_PIC_MAX_SPACE       (32*1024)
82
-
83
-  union union32 {
84
-    uint8_t bytes[4];
85
-    uint32_t dwords;
86
-  };
87
-
88
-  struct pic_msg {
89
-    uint8_t name[PIC_NAME_MAX_LEN];
90
-    union union32 size;
91
-  };
92
-
93
-  typedef struct pic_msg PIC_MSG;
94
-
95
-#endif // if 0
96
-
97
-class ext_FLASH {
55
+class W25QXXFlash {
98 56
 public:
99 57
   void init(uint8_t spiRate);
100 58
   static uint8_t spi_flash_Rec();
@@ -111,14 +69,8 @@ public:
111 69
   static void SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
112 70
   static void SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
113 71
   static void SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead);
114
-  //uint32_t lv_get_pic_addr(uint8_t *Pname);
115
-  void lv_pic_read(uint8_t *P_Rbuff, uint32_t addr, uint32_t size);
116 72
 };
117 73
 
118
-extern ext_FLASH W25QXX;
119
-
120
-//extern uint32_t lv_get_pic_addr(uint8_t *Pname);
121
-
122 74
 //#ifdef __cplusplus
123 75
 //} /* C-declarations for C++ */
124 76
 //#endif

+ 3
- 2
Marlin/src/pins/stm32f1/pins_MKS_ROBIN_NANO.h View File

@@ -240,8 +240,9 @@
240 240
   #define ILI9488_ORIENTATION               ILI9488_MADCTL_MX | ILI9488_MADCTL_MV
241 241
 #endif
242 242
 
243
-#define SPI_FLASH
244
-#if ENABLED(SPI_FLASH)
243
+#define HAS_SPI_FLASH                       1
244
+#define SPI_FLASH_SIZE                      0x1000000 // 16MB
245
+#if HAS_SPI_FLASH
245 246
   #define W25QXX_CS_PIN                     PB12
246 247
   #define W25QXX_MOSI_PIN                   PB15
247 248
   #define W25QXX_MISO_PIN                   PB14

+ 3
- 2
Marlin/src/pins/stm32f1/pins_MKS_ROBIN_NANO_V2.h View File

@@ -398,8 +398,9 @@
398 398
 
399 399
 #endif // HAS_SPI_LCD
400 400
 
401
-#define SPI_FLASH
402
-#if ENABLED(SPI_FLASH)
401
+#define HAS_SPI_FLASH                       1
402
+#define SPI_FLASH_SIZE                      0x1000000 // 16MB
403
+#if HAS_SPI_FLASH
403 404
   #define W25QXX_CS_PIN                     PB12
404 405
   #define W25QXX_MOSI_PIN                   PB15
405 406
   #define W25QXX_MISO_PIN                   PB14

Loading…
Cancel
Save