1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "../inc/MarlinConfig.h"
#if HAS_SPI_FLASH
#include "W25Qxx.h"
W25QXXFlash W25QXX;
#ifndef SPI_FLASH_MISO_PIN
#define SPI_FLASH_MISO_PIN W25QXX_MISO_PIN
#endif
#ifndef SPI_FLASH_MOSI_PIN
#define SPI_FLASH_MOSI_PIN W25QXX_MOSI_PIN
#endif
#ifndef SPI_FLASH_SCK_PIN
#define SPI_FLASH_SCK_PIN W25QXX_SCK_PIN
#endif
#ifndef SPI_FLASH_CS_PIN
#define SPI_FLASH_CS_PIN W25QXX_CS_PIN
#endif
#ifndef NC
#define NC -1
#endif
MarlinSPI W25QXXFlash::mySPI(SPI_FLASH_MOSI_PIN, SPI_FLASH_MISO_PIN, SPI_FLASH_SCK_PIN, NC);
#define W25QXX_CS_H OUT_WRITE(SPI_FLASH_CS_PIN, HIGH)
#define W25QXX_CS_L OUT_WRITE(SPI_FLASH_CS_PIN, LOW)
bool flash_dma_mode = true;
void W25QXXFlash::init(uint8_t spiRate) {
OUT_WRITE(SPI_FLASH_CS_PIN, HIGH);
/**
* STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz
* STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1
* so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2
*/
#if SPI_DEVICE == 1
#define SPI_CLOCK_MAX SPI_CLOCK_DIV4
#else
#define SPI_CLOCK_MAX SPI_CLOCK_DIV2
#endif
uint8_t clock;
switch (spiRate) {
case SPI_FULL_SPEED: clock = SPI_CLOCK_MAX; break;
case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4; break;
case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8; break;
case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
default: clock = SPI_CLOCK_DIV2;// Default from the SPI library
}
mySPI.setClockDivider(clock);
mySPI.setBitOrder(MSBFIRST);
mySPI.setDataMode(SPI_MODE0);
mySPI.begin();
}
/**
* @brief Receive a single byte from the SPI port.
*
* @return Byte received
*/
uint8_t W25QXXFlash::spi_flash_Rec() {
const uint8_t returnByte = mySPI.transfer(0xFF);
return returnByte;
}
uint8_t W25QXXFlash::spi_flash_read_write_byte(uint8_t data) {
const uint8_t returnByte = mySPI.transfer(data);
return returnByte;
}
/**
* @brief Receive a number of bytes from the SPI port to a buffer
*
* @param buf Pointer to starting address of buffer to write to.
* @param nbyte Number of bytes to receive.
* @return Nothing
*
* @details Uses DMA
*/
void W25QXXFlash::spi_flash_Read(uint8_t* buf, uint16_t nbyte) {
mySPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);
}
/**
* @brief Send a single byte on SPI port
*
* @param b Byte to send
*
* @details
*/
void W25QXXFlash::spi_flash_Send(uint8_t b) { mySPI.transfer(b); }
/**
* @brief Write token and then write from 512 byte buffer to SPI (for SD card)
*
* @param buf Pointer with buffer start address
* @return Nothing
*
* @details Use DMA
*/
void W25QXXFlash::spi_flash_SendBlock(uint8_t token, const uint8_t* buf) {
mySPI.transfer(token);
mySPI.dmaSend(const_cast<uint8_t*>(buf), 512);
}
uint16_t W25QXXFlash::W25QXX_ReadID(void) {
uint16_t Temp = 0;
W25QXX_CS_L;
spi_flash_Send(0x90);
spi_flash_Send(0x00);
spi_flash_Send(0x00);
spi_flash_Send(0x00);
Temp |= spi_flash_Rec() << 8;
Temp |= spi_flash_Rec();
W25QXX_CS_H;
return Temp;
}
void W25QXXFlash::SPI_FLASH_WriteEnable(void) {
// Select the FLASH: Chip Select low
W25QXX_CS_L;
// Send "Write Enable" instruction
spi_flash_Send(W25X_WriteEnable);
// Deselect the FLASH: Chip Select high
W25QXX_CS_H;
}
/*******************************************************************************
* Function Name : SPI_FLASH_WaitForWriteEnd
* Description : Polls the status of the Write In Progress (WIP) flag in the
* FLASH's status register and loop until write opertaion
* has completed.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void W25QXXFlash::SPI_FLASH_WaitForWriteEnd(void) {
uint8_t FLASH_Status = 0;
// Select the FLASH: Chip Select low
W25QXX_CS_L;
// Send "Read Status Register" instruction
spi_flash_Send(W25X_ReadStatusReg);
// Loop as long as the memory is busy with a write cycle
do
/* Send a dummy byte to generate the clock needed by the FLASH
and put the value of the status register in FLASH_Status variable */
FLASH_Status = spi_flash_Rec();
while ((FLASH_Status & WIP_Flag) == 0x01); // Write in progress
// Deselect the FLASH: Chip Select high
W25QXX_CS_H;
}
void W25QXXFlash::SPI_FLASH_SectorErase(uint32_t SectorAddr) {
// Send write enable instruction
SPI_FLASH_WriteEnable();
// Sector Erase
// Select the FLASH: Chip Select low
W25QXX_CS_L;
// Send Sector Erase instruction
spi_flash_Send(W25X_SectorErase);
// Send SectorAddr high nibble address byte
spi_flash_Send((SectorAddr & 0xFF0000) >> 16);
// Send SectorAddr medium nibble address byte
spi_flash_Send((SectorAddr & 0xFF00) >> 8);
// Send SectorAddr low nibble address byte
spi_flash_Send(SectorAddr & 0xFF);
// Deselect the FLASH: Chip Select high
W25QXX_CS_H;
// Wait the end of Flash writing
SPI_FLASH_WaitForWriteEnd();
}
void W25QXXFlash::SPI_FLASH_BlockErase(uint32_t BlockAddr) {
SPI_FLASH_WriteEnable();
W25QXX_CS_L;
// Send Sector Erase instruction
spi_flash_Send(W25X_BlockErase);
// Send SectorAddr high nibble address byte
spi_flash_Send((BlockAddr & 0xFF0000) >> 16);
// Send SectorAddr medium nibble address byte
spi_flash_Send((BlockAddr & 0xFF00) >> 8);
// Send SectorAddr low nibble address byte
spi_flash_Send(BlockAddr & 0xFF);
W25QXX_CS_H;
SPI_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SPI_FLASH_BulkErase
* Description : Erases the entire FLASH.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void W25QXXFlash::SPI_FLASH_BulkErase(void) {
// Send write enable instruction
SPI_FLASH_WriteEnable();
// Bulk Erase
// Select the FLASH: Chip Select low
W25QXX_CS_L;
// Send Bulk Erase instruction
spi_flash_Send(W25X_ChipErase);
// Deselect the FLASH: Chip Select high
W25QXX_CS_H;
// Wait the end of Flash writing
SPI_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SPI_FLASH_PageWrite
* Description : Writes more than one byte to the FLASH with a single WRITE
* cycle(Page WRITE sequence). The number of byte can't exceed
* the FLASH page size.
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the FLASH.
* - WriteAddr : FLASH's internal address to write to.
* - NumByteToWrite : number of bytes to write to the FLASH,
* must be equal or less than "SPI_FLASH_PageSize" value.
* Output : None
* Return : None
*******************************************************************************/
void W25QXXFlash::SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) {
// Enable the write access to the FLASH
SPI_FLASH_WriteEnable();
// Select the FLASH: Chip Select low
W25QXX_CS_L;
// Send "Write to Memory " instruction
spi_flash_Send(W25X_PageProgram);
// Send WriteAddr high nibble address byte to write to
spi_flash_Send((WriteAddr & 0xFF0000) >> 16);
// Send WriteAddr medium nibble address byte to write to
spi_flash_Send((WriteAddr & 0xFF00) >> 8);
// Send WriteAddr low nibble address byte to write to
spi_flash_Send(WriteAddr & 0xFF);
NOMORE(NumByteToWrite, SPI_FLASH_PerWritePageSize);
// While there is data to be written on the FLASH
while (NumByteToWrite--) {
// Send the current byte
spi_flash_Send(*pBuffer);
// Point on the next byte to be written
pBuffer++;
}
// Deselect the FLASH: Chip Select high
W25QXX_CS_H;
// Wait the end of Flash writing
SPI_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SPI_FLASH_BufferWrite
* Description : Writes block of data to the FLASH. In this function, the
* number of WRITE cycles are reduced, using Page WRITE sequence.
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the FLASH.
* - WriteAddr : FLASH's internal address to write to.
* - NumByteToWrite : number of bytes to write to the FLASH.
* Output : None
* Return : None
*******************************************************************************/
void W25QXXFlash::SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) {
uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
Addr = WriteAddr % SPI_FLASH_PageSize;
count = SPI_FLASH_PageSize - Addr;
NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
if (Addr == 0) { // WriteAddr is SPI_FLASH_PageSize aligned
if (NumOfPage == 0) { // NumByteToWrite < SPI_FLASH_PageSize
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
}
else { // NumByteToWrite > SPI_FLASH_PageSize
while (NumOfPage--) {
SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
WriteAddr += SPI_FLASH_PageSize;
pBuffer += SPI_FLASH_PageSize;
}
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
}
}
else { // WriteAddr is not SPI_FLASH_PageSize aligned
if (NumOfPage == 0) { // NumByteToWrite < SPI_FLASH_PageSize
if (NumOfSingle > count) { // (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize
temp = NumOfSingle - count;
SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
WriteAddr += count;
pBuffer += count;
SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
}
else
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
}
else { // NumByteToWrite > SPI_FLASH_PageSize
NumByteToWrite -= count;
NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
WriteAddr += count;
pBuffer += count;
while (NumOfPage--) {
SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
WriteAddr += SPI_FLASH_PageSize;
pBuffer += SPI_FLASH_PageSize;
}
if (NumOfSingle != 0)
SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
}
}
}
/*******************************************************************************
* Function Name : SPI_FLASH_BufferRead
* Description : Reads a block of data from the FLASH.
* Input : - pBuffer : pointer to the buffer that receives the data read
* from the FLASH.
* - ReadAddr : FLASH's internal address to read from.
* - NumByteToRead : number of bytes to read from the FLASH.
* Output : None
* Return : None
*******************************************************************************/
void W25QXXFlash::SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) {
// Select the FLASH: Chip Select low
W25QXX_CS_L;
// Send "Read from Memory " instruction
spi_flash_Send(W25X_ReadData);
// Send ReadAddr high nibble address byte to read from
spi_flash_Send((ReadAddr & 0xFF0000) >> 16);
// Send ReadAddr medium nibble address byte to read from
spi_flash_Send((ReadAddr & 0xFF00) >> 8);
// Send ReadAddr low nibble address byte to read from
spi_flash_Send(ReadAddr & 0xFF);
if (NumByteToRead <= 32 || !flash_dma_mode) {
while (NumByteToRead--) { // While there is data to be read
// Read a byte from the FLASH
*pBuffer = spi_flash_Rec();
// Point to the next location where the byte read will be saved
pBuffer++;
}
}
else
spi_flash_Read(pBuffer, NumByteToRead);
W25QXX_CS_H;
}
#endif // HAS_SPI_FLASH
|