|
@@ -0,0 +1,558 @@
|
|
1
|
+/*------------------------------------------------------------------------*/
|
|
2
|
+/* STM32F1: MMCv3/SDv1/SDv2 (SPI mode) control module */
|
|
3
|
+/*------------------------------------------------------------------------*/
|
|
4
|
+/*
|
|
5
|
+/ * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
6
|
+/ * Copyright (c) 2019 BigTreeTech [https://github.com/bigtreetech]
|
|
7
|
+/ * Copyright (C) 2015, ChaN, all right reserved.
|
|
8
|
+/
|
|
9
|
+/ * This software is a free software and there is NO WARRANTY.
|
|
10
|
+/ * No restriction on use. You can use, modify and redistribute it for
|
|
11
|
+/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
|
|
12
|
+/ * Redistributions of source code must retain the above copyright notice.
|
|
13
|
+/
|
|
14
|
+/-------------------------------------------------------------------------*/
|
|
15
|
+#include "../../inc/MarlinConfig.h"
|
|
16
|
+
|
|
17
|
+#ifdef HAS_ONBOARD_SD
|
|
18
|
+
|
|
19
|
+#include "onboard_sd.h"
|
|
20
|
+#include "spi.h"
|
|
21
|
+#include "fastio.h"
|
|
22
|
+
|
|
23
|
+#ifdef SHARED_SD_CARD
|
|
24
|
+ #ifndef ON_BOARD_SPI_DEVICE
|
|
25
|
+ #define ON_BOARD_SPI_DEVICE SPI_DEVICE
|
|
26
|
+ #endif
|
|
27
|
+ #define ONBOARD_SD_SPI SPI
|
|
28
|
+#else
|
|
29
|
+ SPIClass OnBoardSPI(ON_BOARD_SPI_DEVICE)
|
|
30
|
+ #define ONBOARD_SD_SPI OnBoardSPI
|
|
31
|
+#endif
|
|
32
|
+
|
|
33
|
+#if ON_BOARD_SPI_DEVICE == 1
|
|
34
|
+ #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_4
|
|
35
|
+#else
|
|
36
|
+ #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_2
|
|
37
|
+#endif
|
|
38
|
+
|
|
39
|
+#define CS_LOW() {WRITE(ONBOARD_SD_CS_PIN, LOW);} /* Set OnBoardSPI cs low */
|
|
40
|
+#define CS_HIGH() {WRITE(ONBOARD_SD_CS_PIN, HIGH);} /* Set OnBoardSPI cs high */
|
|
41
|
+
|
|
42
|
+#define FCLK_FAST() ONBOARD_SD_SPI.setClockDivider(SPI_CLOCK_MAX)
|
|
43
|
+#define FCLK_SLOW() ONBOARD_SD_SPI.setClockDivider(SPI_BAUD_PCLK_DIV_256)
|
|
44
|
+
|
|
45
|
+/*--------------------------------------------------------------------------
|
|
46
|
+ Module Private Functions
|
|
47
|
+---------------------------------------------------------------------------*/
|
|
48
|
+
|
|
49
|
+#include "onboard_sd.h"
|
|
50
|
+
|
|
51
|
+/* MMC/SD command */
|
|
52
|
+#define CMD0 (0) /* GO_IDLE_STATE */
|
|
53
|
+#define CMD1 (1) /* SEND_OP_COND (MMC) */
|
|
54
|
+#define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */
|
|
55
|
+#define CMD8 (8) /* SEND_IF_COND */
|
|
56
|
+#define CMD9 (9) /* SEND_CSD */
|
|
57
|
+#define CMD10 (10) /* SEND_CID */
|
|
58
|
+#define CMD12 (12) /* STOP_TRANSMISSION */
|
|
59
|
+#define ACMD13 (0x80+13) /* SD_STATUS (SDC) */
|
|
60
|
+#define CMD16 (16) /* SET_BLOCKLEN */
|
|
61
|
+#define CMD17 (17) /* READ_SINGLE_BLOCK */
|
|
62
|
+#define CMD18 (18) /* READ_MULTIPLE_BLOCK */
|
|
63
|
+#define CMD23 (23) /* SET_BLOCK_COUNT (MMC) */
|
|
64
|
+#define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
|
|
65
|
+#define CMD24 (24) /* WRITE_BLOCK */
|
|
66
|
+#define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
|
|
67
|
+#define CMD32 (32) /* ERASE_ER_BLK_START */
|
|
68
|
+#define CMD33 (33) /* ERASE_ER_BLK_END */
|
|
69
|
+#define CMD38 (38) /* ERASE */
|
|
70
|
+#define CMD48 (48) /* READ_EXTR_SINGLE */
|
|
71
|
+#define CMD49 (49) /* WRITE_EXTR_SINGLE */
|
|
72
|
+#define CMD55 (55) /* APP_CMD */
|
|
73
|
+#define CMD58 (58) /* READ_OCR */
|
|
74
|
+
|
|
75
|
+static volatile DSTATUS Stat = STA_NOINIT; /* Physical drive status */
|
|
76
|
+static volatile UINT timeout;
|
|
77
|
+static BYTE CardType; /* Card type flags */
|
|
78
|
+
|
|
79
|
+/*-----------------------------------------------------------------------*/
|
|
80
|
+/* Send/Receive data to the MMC (Platform dependent) */
|
|
81
|
+/*-----------------------------------------------------------------------*/
|
|
82
|
+
|
|
83
|
+/* Exchange a byte */
|
|
84
|
+static BYTE xchg_spi (
|
|
85
|
+ BYTE dat /* Data to send */
|
|
86
|
+) {
|
|
87
|
+ BYTE returnByte = ONBOARD_SD_SPI.transfer(dat);
|
|
88
|
+ return returnByte;
|
|
89
|
+}
|
|
90
|
+
|
|
91
|
+/* Receive multiple byte */
|
|
92
|
+static void rcvr_spi_multi (
|
|
93
|
+ BYTE *buff, /* Pointer to data buffer */
|
|
94
|
+ UINT btr /* Number of bytes to receive (16, 64 or 512) */
|
|
95
|
+) {
|
|
96
|
+ ONBOARD_SD_SPI.dmaTransfer(0, const_cast<uint8_t*>(buff), btr);
|
|
97
|
+}
|
|
98
|
+
|
|
99
|
+#if _DISKIO_WRITE
|
|
100
|
+
|
|
101
|
+ /* Send multiple bytes */
|
|
102
|
+ static void xmit_spi_multi (
|
|
103
|
+ const BYTE *buff, /* Pointer to the data */
|
|
104
|
+ UINT btx /* Number of bytes to send (multiple of 16) */
|
|
105
|
+ ) {
|
|
106
|
+ ONBOARD_SD_SPI.dmaSend(const_cast<uint8_t*>(buff), btx);
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+#endif // _DISKIO_WRITE
|
|
110
|
+
|
|
111
|
+/*-----------------------------------------------------------------------*/
|
|
112
|
+/* Wait for card ready */
|
|
113
|
+/*-----------------------------------------------------------------------*/
|
|
114
|
+
|
|
115
|
+static int wait_ready ( /* 1:Ready, 0:Timeout */
|
|
116
|
+ UINT wt /* Timeout [ms] */
|
|
117
|
+) {
|
|
118
|
+ BYTE d;
|
|
119
|
+
|
|
120
|
+ timeout = millis() + wt;
|
|
121
|
+ do {
|
|
122
|
+ d = xchg_spi(0xFF);
|
|
123
|
+ /* This loop takes a while. Insert rot_rdq() here for multitask environment. */
|
|
124
|
+ } while (d != 0xFF && (timeout > millis())); /* Wait for card goes ready or timeout */
|
|
125
|
+
|
|
126
|
+ return (d == 0xFF) ? 1 : 0;
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+/*-----------------------------------------------------------------------*/
|
|
130
|
+/* Deselect card and release SPI */
|
|
131
|
+/*-----------------------------------------------------------------------*/
|
|
132
|
+
|
|
133
|
+static void deselect(void) {
|
|
134
|
+ CS_HIGH(); /* CS = H */
|
|
135
|
+ xchg_spi(0xFF); /* Dummy clock (force DO hi-z for multiple slave SPI) */
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+/*-----------------------------------------------------------------------*/
|
|
139
|
+/* Select card and wait for ready */
|
|
140
|
+/*-----------------------------------------------------------------------*/
|
|
141
|
+
|
|
142
|
+static int select(void) { /* 1:OK, 0:Timeout */
|
|
143
|
+ CS_LOW(); /* CS = L */
|
|
144
|
+ xchg_spi(0xFF); /* Dummy clock (force DO enabled) */
|
|
145
|
+
|
|
146
|
+ if (wait_ready(500)) return 1; /* Leading busy check: Wait for card ready */
|
|
147
|
+
|
|
148
|
+ deselect(); /* Timeout */
|
|
149
|
+ return 0;
|
|
150
|
+}
|
|
151
|
+
|
|
152
|
+/*-----------------------------------------------------------------------*/
|
|
153
|
+/* Control SPI module (Platform dependent) */
|
|
154
|
+/*-----------------------------------------------------------------------*/
|
|
155
|
+
|
|
156
|
+static void power_on(void) { /* Enable SSP module and attach it to I/O pads */
|
|
157
|
+ ONBOARD_SD_SPI.setModule(ON_BOARD_SPI_DEVICE);
|
|
158
|
+ ONBOARD_SD_SPI.begin();
|
|
159
|
+ ONBOARD_SD_SPI.setBitOrder(MSBFIRST);
|
|
160
|
+ ONBOARD_SD_SPI.setDataMode(SPI_MODE0);
|
|
161
|
+ OUT_WRITE(ONBOARD_SD_CS_PIN, HIGH); /* Set CS# high */
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+static void power_off(void) { /* Disable SPI function */
|
|
165
|
+ select(); /* Wait for card ready */
|
|
166
|
+ deselect();
|
|
167
|
+}
|
|
168
|
+
|
|
169
|
+/*-----------------------------------------------------------------------*/
|
|
170
|
+/* Receive a data packet from the MMC */
|
|
171
|
+/*-----------------------------------------------------------------------*/
|
|
172
|
+
|
|
173
|
+static int rcvr_datablock ( /* 1:OK, 0:Error */
|
|
174
|
+ BYTE *buff, /* Data buffer */
|
|
175
|
+ UINT btr /* Data block length (byte) */
|
|
176
|
+) {
|
|
177
|
+ BYTE token;
|
|
178
|
+
|
|
179
|
+ timeout = millis() + 200;
|
|
180
|
+ do { /* Wait for DataStart token in timeout of 200ms */
|
|
181
|
+ token = xchg_spi(0xFF);
|
|
182
|
+ /* This loop will take a while. Insert rot_rdq() here for multitask environment. */
|
|
183
|
+ } while ((token == 0xFF) && (timeout > millis()));
|
|
184
|
+ if (token != 0xFE) return 0; /* Function fails if invalid DataStart token or timeout */
|
|
185
|
+
|
|
186
|
+ rcvr_spi_multi(buff, btr); /* Store trailing data to the buffer */
|
|
187
|
+ xchg_spi(0xFF); xchg_spi(0xFF); /* Discard CRC */
|
|
188
|
+
|
|
189
|
+ return 1; /* Function succeeded */
|
|
190
|
+}
|
|
191
|
+
|
|
192
|
+/*-----------------------------------------------------------------------*/
|
|
193
|
+/* Send a data packet to the MMC */
|
|
194
|
+/*-----------------------------------------------------------------------*/
|
|
195
|
+
|
|
196
|
+#if _DISKIO_WRITE
|
|
197
|
+
|
|
198
|
+ static int xmit_datablock ( /* 1:OK, 0:Failed */
|
|
199
|
+ const BYTE *buff, /* Ponter to 512 byte data to be sent */
|
|
200
|
+ BYTE token /* Token */
|
|
201
|
+ ) {
|
|
202
|
+ BYTE resp;
|
|
203
|
+
|
|
204
|
+ if (!wait_ready(500)) return 0; /* Leading busy check: Wait for card ready to accept data block */
|
|
205
|
+
|
|
206
|
+ xchg_spi(token); /* Send token */
|
|
207
|
+ if (token == 0xFD) return 1; /* Do not send data if token is StopTran */
|
|
208
|
+
|
|
209
|
+ xmit_spi_multi(buff, 512); /* Data */
|
|
210
|
+ xchg_spi(0xFF); xchg_spi(0xFF); /* Dummy CRC */
|
|
211
|
+
|
|
212
|
+ resp = xchg_spi(0xFF); /* Receive data resp */
|
|
213
|
+
|
|
214
|
+ return (resp & 0x1F) == 0x05 ? 1 : 0; /* Data was accepted or not */
|
|
215
|
+
|
|
216
|
+ /* Busy check is done at next transmission */
|
|
217
|
+ }
|
|
218
|
+
|
|
219
|
+#endif // _DISKIO_WRITE
|
|
220
|
+
|
|
221
|
+/*-----------------------------------------------------------------------*/
|
|
222
|
+/* Send a command packet to the MMC */
|
|
223
|
+/*-----------------------------------------------------------------------*/
|
|
224
|
+
|
|
225
|
+static BYTE send_cmd ( /* Return value: R1 resp (bit7==1:Failed to send) */
|
|
226
|
+ BYTE cmd, /* Command index */
|
|
227
|
+ DWORD arg /* Argument */
|
|
228
|
+) {
|
|
229
|
+ BYTE n, res;
|
|
230
|
+
|
|
231
|
+ if (cmd & 0x80) { /* Send a CMD55 prior to ACMD<n> */
|
|
232
|
+ cmd &= 0x7F;
|
|
233
|
+ res = send_cmd(CMD55, 0);
|
|
234
|
+ if (res > 1) return res;
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ /* Select the card and wait for ready except to stop multiple block read */
|
|
238
|
+ if (cmd != CMD12) {
|
|
239
|
+ deselect();
|
|
240
|
+ if (!select()) return 0xFF;
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ /* Send command packet */
|
|
244
|
+ xchg_spi(0x40 | cmd); /* Start + command index */
|
|
245
|
+ xchg_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
|
|
246
|
+ xchg_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
|
|
247
|
+ xchg_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
|
|
248
|
+ xchg_spi((BYTE)arg); /* Argument[7..0] */
|
|
249
|
+ n = 0x01; /* Dummy CRC + Stop */
|
|
250
|
+ if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
|
|
251
|
+ if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
|
|
252
|
+ xchg_spi(n);
|
|
253
|
+
|
|
254
|
+ /* Receive command resp */
|
|
255
|
+ if (cmd == CMD12) xchg_spi(0xFF); /* Diacard following one byte when CMD12 */
|
|
256
|
+ n = 10; /* Wait for response (10 bytes max) */
|
|
257
|
+ do
|
|
258
|
+ res = xchg_spi(0xFF);
|
|
259
|
+ while ((res & 0x80) && --n);
|
|
260
|
+
|
|
261
|
+ return res; /* Return received response */
|
|
262
|
+}
|
|
263
|
+
|
|
264
|
+/*--------------------------------------------------------------------------
|
|
265
|
+ Public Functions
|
|
266
|
+---------------------------------------------------------------------------*/
|
|
267
|
+
|
|
268
|
+/*-----------------------------------------------------------------------*/
|
|
269
|
+/* Initialize disk drive */
|
|
270
|
+/*-----------------------------------------------------------------------*/
|
|
271
|
+
|
|
272
|
+DSTATUS disk_initialize (
|
|
273
|
+ BYTE drv /* Physical drive number (0) */
|
|
274
|
+) {
|
|
275
|
+ BYTE n, cmd, ty, ocr[4];
|
|
276
|
+
|
|
277
|
+ if (drv) return STA_NOINIT; /* Supports only drive 0 */
|
|
278
|
+ power_on(); /* Initialize SPI */
|
|
279
|
+
|
|
280
|
+ if (Stat & STA_NODISK) return Stat; /* Is a card existing in the soket? */
|
|
281
|
+
|
|
282
|
+ FCLK_SLOW();
|
|
283
|
+ for (n = 10; n; n--) xchg_spi(0xFF); /* Send 80 dummy clocks */
|
|
284
|
+
|
|
285
|
+ ty = 0;
|
|
286
|
+ if (send_cmd(CMD0, 0) == 1) { /* Put the card SPI state */
|
|
287
|
+ timeout = millis() + 1000; /* Initialization timeout = 1 sec */
|
|
288
|
+ if (send_cmd(CMD8, 0x1AA) == 1) { /* Is the catd SDv2? */
|
|
289
|
+ for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF); /* Get 32 bit return value of R7 resp */
|
|
290
|
+ if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* Does the card support 2.7-3.6V? */
|
|
291
|
+ while ((timeout > millis()) && send_cmd(ACMD41, 1UL << 30)) ; /* Wait for end of initialization with ACMD41(HCS) */
|
|
292
|
+ if ((timeout > millis()) && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
|
|
293
|
+ for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
|
|
294
|
+ ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Check if the card is SDv2 */
|
|
295
|
+ }
|
|
296
|
+ }
|
|
297
|
+ } else { /* Not an SDv2 card */
|
|
298
|
+ if (send_cmd(ACMD41, 0) <= 1) { /* SDv1 or MMCv3? */
|
|
299
|
+ ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */
|
|
300
|
+ } else {
|
|
301
|
+ ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */
|
|
302
|
+ }
|
|
303
|
+ while ((timeout > millis()) && send_cmd(cmd, 0)) ; /* Wait for the card leaves idle state */
|
|
304
|
+ if (!(timeout > millis()) || send_cmd(CMD16, 512) != 0) /* Set block length: 512 */
|
|
305
|
+ ty = 0;
|
|
306
|
+ }
|
|
307
|
+ }
|
|
308
|
+ CardType = ty; /* Card type */
|
|
309
|
+ deselect();
|
|
310
|
+
|
|
311
|
+ if (ty) { /* OK */
|
|
312
|
+ FCLK_FAST(); /* Set fast clock */
|
|
313
|
+ Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */
|
|
314
|
+ } else { /* Failed */
|
|
315
|
+ power_off();
|
|
316
|
+ Stat = STA_NOINIT;
|
|
317
|
+ }
|
|
318
|
+
|
|
319
|
+ return Stat;
|
|
320
|
+}
|
|
321
|
+
|
|
322
|
+/*-----------------------------------------------------------------------*/
|
|
323
|
+/* Get disk status */
|
|
324
|
+/*-----------------------------------------------------------------------*/
|
|
325
|
+
|
|
326
|
+DSTATUS disk_status (
|
|
327
|
+ BYTE drv /* Physical drive number (0) */
|
|
328
|
+) {
|
|
329
|
+ if (drv) return STA_NOINIT; /* Supports only drive 0 */
|
|
330
|
+ return Stat; /* Return disk status */
|
|
331
|
+}
|
|
332
|
+
|
|
333
|
+/*-----------------------------------------------------------------------*/
|
|
334
|
+/* Read sector(s) */
|
|
335
|
+/*-----------------------------------------------------------------------*/
|
|
336
|
+
|
|
337
|
+DRESULT disk_read (
|
|
338
|
+ BYTE drv, /* Physical drive number (0) */
|
|
339
|
+ BYTE *buff, /* Pointer to the data buffer to store read data */
|
|
340
|
+ DWORD sector, /* Start sector number (LBA) */
|
|
341
|
+ UINT count /* Number of sectors to read (1..128) */
|
|
342
|
+) {
|
|
343
|
+ BYTE cmd;
|
|
344
|
+
|
|
345
|
+ if (drv || !count) return RES_PARERR; /* Check parameter */
|
|
346
|
+ if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */
|
|
347
|
+ if (!(CardType & CT_BLOCK)) sector *= 512; /* LBA ot BA conversion (byte addressing cards) */
|
|
348
|
+ FCLK_FAST();
|
|
349
|
+ cmd = count > 1 ? CMD18 : CMD17; /* READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */
|
|
350
|
+ if (send_cmd(cmd, sector) == 0) {
|
|
351
|
+ do {
|
|
352
|
+ if (!rcvr_datablock(buff, 512)) break;
|
|
353
|
+ buff += 512;
|
|
354
|
+ } while (--count);
|
|
355
|
+ if (cmd == CMD18) send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
|
|
356
|
+ }
|
|
357
|
+ deselect();
|
|
358
|
+
|
|
359
|
+ return count ? RES_ERROR : RES_OK; /* Return result */
|
|
360
|
+}
|
|
361
|
+
|
|
362
|
+/*-----------------------------------------------------------------------*/
|
|
363
|
+/* Write sector(s) */
|
|
364
|
+/*-----------------------------------------------------------------------*/
|
|
365
|
+
|
|
366
|
+#if _DISKIO_WRITE
|
|
367
|
+
|
|
368
|
+ DRESULT disk_write(
|
|
369
|
+ BYTE drv, /* Physical drive number (0) */
|
|
370
|
+ const BYTE *buff, /* Ponter to the data to write */
|
|
371
|
+ DWORD sector, /* Start sector number (LBA) */
|
|
372
|
+ UINT count /* Number of sectors to write (1..128) */
|
|
373
|
+ ) {
|
|
374
|
+ if (drv || !count) return RES_PARERR; /* Check parameter */
|
|
375
|
+ if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check drive status */
|
|
376
|
+ if (Stat & STA_PROTECT) return RES_WRPRT; /* Check write protect */
|
|
377
|
+ FCLK_FAST();
|
|
378
|
+ if (!(CardType & CT_BLOCK)) sector *= 512; /* LBA ==> BA conversion (byte addressing cards) */
|
|
379
|
+
|
|
380
|
+ if (count == 1) { /* Single sector write */
|
|
381
|
+ if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
|
|
382
|
+ && xmit_datablock(buff, 0xFE)) {
|
|
383
|
+ count = 0;
|
|
384
|
+ }
|
|
385
|
+ }
|
|
386
|
+ else { /* Multiple sector write */
|
|
387
|
+ if (CardType & CT_SDC) send_cmd(ACMD23, count); /* Predefine number of sectors */
|
|
388
|
+ if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
|
|
389
|
+ do {
|
|
390
|
+ if (!xmit_datablock(buff, 0xFC)) break;
|
|
391
|
+ buff += 512;
|
|
392
|
+ } while (--count);
|
|
393
|
+ if (!xmit_datablock(0, 0xFD)) count = 1; /* STOP_TRAN token */
|
|
394
|
+ }
|
|
395
|
+ }
|
|
396
|
+ deselect();
|
|
397
|
+
|
|
398
|
+ return count ? RES_ERROR : RES_OK; /* Return result */
|
|
399
|
+ }
|
|
400
|
+
|
|
401
|
+#endif // _DISKIO_WRITE
|
|
402
|
+
|
|
403
|
+/*-----------------------------------------------------------------------*/
|
|
404
|
+/* Miscellaneous drive controls other than data read/write */
|
|
405
|
+/*-----------------------------------------------------------------------*/
|
|
406
|
+
|
|
407
|
+#if _DISKIO_IOCTL
|
|
408
|
+
|
|
409
|
+ DRESULT disk_ioctl (
|
|
410
|
+ BYTE drv, /* Physical drive number (0) */
|
|
411
|
+ BYTE cmd, /* Control command code */
|
|
412
|
+ void *buff /* Pointer to the conrtol data */
|
|
413
|
+ ) {
|
|
414
|
+ DRESULT res;
|
|
415
|
+ BYTE n, csd[16], *ptr = (BYTE *)buff;
|
|
416
|
+ DWORD *dp, st, ed, csize;
|
|
417
|
+ #if _DISKIO_ISDIO
|
|
418
|
+ SDIO_CMD *sdio = buff;
|
|
419
|
+ BYTE rc, *buf;
|
|
420
|
+ UINT dc;
|
|
421
|
+ #endif
|
|
422
|
+
|
|
423
|
+ if (drv) return RES_PARERR; /* Check parameter */
|
|
424
|
+ if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */
|
|
425
|
+
|
|
426
|
+ res = RES_ERROR;
|
|
427
|
+ FCLK_FAST();
|
|
428
|
+ switch (cmd) {
|
|
429
|
+ case CTRL_SYNC: /* Wait for end of internal write process of the drive */
|
|
430
|
+ if (select()) res = RES_OK;
|
|
431
|
+ break;
|
|
432
|
+
|
|
433
|
+ case GET_SECTOR_COUNT: /* Get drive capacity in unit of sector (DWORD) */
|
|
434
|
+ if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
|
|
435
|
+ if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
|
|
436
|
+ csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
|
|
437
|
+ *(DWORD*)buff = csize << 10;
|
|
438
|
+ } else { /* SDC ver 1.XX or MMC ver 3 */
|
|
439
|
+ n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
|
|
440
|
+ csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
|
|
441
|
+ *(DWORD*)buff = csize << (n - 9);
|
|
442
|
+ }
|
|
443
|
+ res = RES_OK;
|
|
444
|
+ }
|
|
445
|
+ break;
|
|
446
|
+
|
|
447
|
+ case GET_BLOCK_SIZE: /* Get erase block size in unit of sector (DWORD) */
|
|
448
|
+ if (CardType & CT_SD2) { /* SDC ver 2.00 */
|
|
449
|
+ if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */
|
|
450
|
+ xchg_spi(0xFF);
|
|
451
|
+ if (rcvr_datablock(csd, 16)) { /* Read partial block */
|
|
452
|
+ for (n = 64 - 16; n; n--) xchg_spi(0xFF); /* Purge trailing data */
|
|
453
|
+ *(DWORD*)buff = 16UL << (csd[10] >> 4);
|
|
454
|
+ res = RES_OK;
|
|
455
|
+ }
|
|
456
|
+ }
|
|
457
|
+ } else { /* SDC ver 1.XX or MMC */
|
|
458
|
+ if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { /* Read CSD */
|
|
459
|
+ if (CardType & CT_SD1) { /* SDC ver 1.XX */
|
|
460
|
+ *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
|
|
461
|
+ } else { /* MMC */
|
|
462
|
+ *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
|
|
463
|
+ }
|
|
464
|
+ res = RES_OK;
|
|
465
|
+ }
|
|
466
|
+ }
|
|
467
|
+ break;
|
|
468
|
+
|
|
469
|
+ case CTRL_TRIM: /* Erase a block of sectors (used when _USE_TRIM in ffconf.h is 1) */
|
|
470
|
+ if (!(CardType & CT_SDC)) break; /* Check if the card is SDC */
|
|
471
|
+ if (disk_ioctl(drv, MMC_GET_CSD, csd)) break; /* Get CSD */
|
|
472
|
+ if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */
|
|
473
|
+ dp = (DWORD *)buff; st = dp[0]; ed = dp[1]; /* Load sector block */
|
|
474
|
+ if (!(CardType & CT_BLOCK)) {
|
|
475
|
+ st *= 512; ed *= 512;
|
|
476
|
+ }
|
|
477
|
+ if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 && send_cmd(CMD38, 0) == 0 && wait_ready(30000)) { /* Erase sector block */
|
|
478
|
+ res = RES_OK; /* FatFs does not check result of this command */
|
|
479
|
+ }
|
|
480
|
+ break;
|
|
481
|
+
|
|
482
|
+ /* Following commands are never used by FatFs module */
|
|
483
|
+
|
|
484
|
+ case MMC_GET_TYPE: /* Get MMC/SDC type (BYTE) */
|
|
485
|
+ *ptr = CardType;
|
|
486
|
+ res = RES_OK;
|
|
487
|
+ break;
|
|
488
|
+
|
|
489
|
+ case MMC_GET_CSD: /* Read CSD (16 bytes) */
|
|
490
|
+ if (send_cmd(CMD9, 0) == 0 && rcvr_datablock(ptr, 16)) { /* READ_CSD */
|
|
491
|
+ res = RES_OK;
|
|
492
|
+ }
|
|
493
|
+ break;
|
|
494
|
+
|
|
495
|
+ case MMC_GET_CID: /* Read CID (16 bytes) */
|
|
496
|
+ if (send_cmd(CMD10, 0) == 0 && rcvr_datablock(ptr, 16)) { /* READ_CID */
|
|
497
|
+ res = RES_OK;
|
|
498
|
+ }
|
|
499
|
+ break;
|
|
500
|
+
|
|
501
|
+ case MMC_GET_OCR: /* Read OCR (4 bytes) */
|
|
502
|
+ if (send_cmd(CMD58, 0) == 0) { /* READ_OCR */
|
|
503
|
+ for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF);
|
|
504
|
+ res = RES_OK;
|
|
505
|
+ }
|
|
506
|
+ break;
|
|
507
|
+
|
|
508
|
+ case MMC_GET_SDSTAT: /* Read SD status (64 bytes) */
|
|
509
|
+ if (send_cmd(ACMD13, 0) == 0) { /* SD_STATUS */
|
|
510
|
+ xchg_spi(0xFF);
|
|
511
|
+ if (rcvr_datablock(ptr, 64)) res = RES_OK;
|
|
512
|
+ }
|
|
513
|
+ break;
|
|
514
|
+
|
|
515
|
+ #if _DISKIO_ISDIO
|
|
516
|
+
|
|
517
|
+ case ISDIO_READ:
|
|
518
|
+ sdio = buff;
|
|
519
|
+ if (send_cmd(CMD48, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) {
|
|
520
|
+ for (Timer1 = 1000; (rc = xchg_spi(0xFF)) == 0xFF && Timer1; ) ;
|
|
521
|
+ if (rc == 0xFE) {
|
|
522
|
+ for (buf = sdio->data, dc = sdio->ndata; dc; dc--) *buf++ = xchg_spi(0xFF);
|
|
523
|
+ for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF);
|
|
524
|
+ res = RES_OK;
|
|
525
|
+ }
|
|
526
|
+ }
|
|
527
|
+ break;
|
|
528
|
+ case ISDIO_WRITE:
|
|
529
|
+ sdio = buff;
|
|
530
|
+ if (send_cmd(CMD49, 0x80000000 | sdio->func << 28 | sdio->addr << 9 | ((sdio->ndata - 1) & 0x1FF)) == 0) {
|
|
531
|
+ xchg_spi(0xFF); xchg_spi(0xFE);
|
|
532
|
+ for (buf = sdio->data, dc = sdio->ndata; dc; dc--) xchg_spi(*buf++);
|
|
533
|
+ for (dc = 514 - sdio->ndata; dc; dc--) xchg_spi(0xFF);
|
|
534
|
+ if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK;
|
|
535
|
+ }
|
|
536
|
+ break;
|
|
537
|
+ case ISDIO_MRITE:
|
|
538
|
+ sdio = buff;
|
|
539
|
+ if (send_cmd(CMD49, 0x84000000 | sdio->func << 28 | sdio->addr << 9 | sdio->ndata >> 8) == 0) {
|
|
540
|
+ xchg_spi(0xFF); xchg_spi(0xFE);
|
|
541
|
+ xchg_spi(sdio->ndata);
|
|
542
|
+ for (dc = 513; dc; dc--) xchg_spi(0xFF);
|
|
543
|
+ if ((xchg_spi(0xFF) & 0x1F) == 0x05) res = RES_OK;
|
|
544
|
+ }
|
|
545
|
+ break;
|
|
546
|
+
|
|
547
|
+ #endif // _DISKIO_ISDIO
|
|
548
|
+
|
|
549
|
+ default: res = RES_PARERR;
|
|
550
|
+ }
|
|
551
|
+
|
|
552
|
+ deselect();
|
|
553
|
+ return res;
|
|
554
|
+ }
|
|
555
|
+
|
|
556
|
+#endif // _DISKIO_IOCTL
|
|
557
|
+
|
|
558
|
+#endif // HAS_ONBOARD_SD
|