1 /*------------------------------------------------------------------------/
\r
2 / Bitbanging MMCv3/SDv1/SDv2 (in SPI mode) control module for PFF
\r
3 /-------------------------------------------------------------------------/
\r
5 / Copyright (C) 2010, ChaN, all right reserved.
\r
7 / * This software is a free software and there is NO WARRANTY.
\r
8 / * No restriction on use. You can use, modify and redistribute it for
\r
9 / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
\r
10 / * Redistributions of source code must retain the above copyright notice.
\r
12 /--------------------------------------------------------------------------/
\r
16 It uses only 4-6 bit of GPIO port. No interrupt, no SPI port is used.
\r
18 * Platform Independent
\r
19 You need to modify only a few macros to control GPIO ports.
\r
21 /-------------------------------------------------------------------------*/
\r
26 #define H8_3069F_P4DR ((volatile uint8 *)0xFFFFD3)
\r
27 #define H8_3069F_PBDR ((volatile uint8 *)0xFFFFDA)
\r
29 #define PB_BIT_SCLK (1 << 5)
\r
30 #define PB_BIT_MOSI (1 << 6)
\r
31 #define PB_BIT_MISO (1 << 7)
\r
32 #define P4_BIT_CS (1 << 0)
\r
34 /*-------------------------------------------------------------------------*/
\r
35 /* Platform dependent macros and functions needed to be modified */
\r
36 /*-------------------------------------------------------------------------*/
\r
38 #define INIT_PORT() do { } while (0) /* Initialize MMC control port (CS/CLK/DI:output, DO:input) */
\r
39 #define DLY_US(n) do { volatile uint32 dlycnt; for (dlycnt = 0; dlycnt < (n) * 10; dlycnt++) {} } while (0) /* Delay n microseconds */
\r
40 #define FORWARD(d) do { } while (0) /* Data in-time processing function (depends on the project) */
\r
42 #define CS_H() do { *H8_3069F_P4DR |= P4_BIT_CS; } while (0) /* Set MMC CS "high" */
\r
43 #define CS_L() do { *H8_3069F_P4DR &= ~P4_BIT_CS; } while (0) /* Set MMC CS "low" */
\r
44 #define CK_H() do { *H8_3069F_PBDR |= PB_BIT_SCLK; } while (0) /* Set MMC SCLK "high" */
\r
45 #define CK_L() do { *H8_3069F_PBDR &= ~PB_BIT_SCLK; } while (0) /* Set MMC SCLK "low" */
\r
46 #define DI_H() do { *H8_3069F_PBDR |= PB_BIT_MOSI; } while (0) /* Set MMC DI "high" */
\r
47 #define DI_L() do { *H8_3069F_PBDR &= ~PB_BIT_MOSI; } while (0) /* Set MMC DI "low" */
\r
48 #define DO ((*H8_3069F_PBDR & PB_BIT_MISO) ? 1 : 0) /* Get MMC DO value (high:true, low:false) */
\r
51 /*--------------------------------------------------------------------------
\r
53 Module Private Functions
\r
55 ---------------------------------------------------------------------------*/
\r
57 /* Definitions for MMC/SDC command */
\r
58 #define CMD0 (0x40+0) /* GO_IDLE_STATE */
\r
59 #define CMD1 (0x40+1) /* SEND_OP_COND (MMC) */
\r
60 #define ACMD41 (0xC0+41) /* SEND_OP_COND (SDC) */
\r
61 #define CMD8 (0x40+8) /* SEND_IF_COND */
\r
62 #define CMD16 (0x40+16) /* SET_BLOCKLEN */
\r
63 #define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */
\r
64 #define CMD24 (0x40+24) /* WRITE_BLOCK */
\r
65 #define CMD55 (0x40+55) /* APP_CMD */
\r
66 #define CMD58 (0x40+58) /* READ_OCR */
\r
68 /* Card type flags (CardType) */
\r
69 #define CT_MMC 0x01 /* MMC ver 3 */
\r
70 #define CT_SD1 0x02 /* SD ver 1 */
\r
71 #define CT_SD2 0x04 /* SD ver 2 */
\r
72 #define CT_SDC (CT_SD1|CT_SD2) /* SD */
\r
73 #define CT_BLOCK 0x08 /* Block addressing */
\r
78 BYTE CardType; /* b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing */
\r
82 /*-----------------------------------------------------------------------*/
\r
83 /* Transmit a byte to the MMC (bitbanging) */
\r
84 /*-----------------------------------------------------------------------*/
\r
88 BYTE d /* Data to be sent */
\r
91 if (d & 0x80) DI_H(); else DI_L(); /* bit7 */
\r
93 if (d & 0x40) DI_H(); else DI_L(); /* bit6 */
\r
95 if (d & 0x20) DI_H(); else DI_L(); /* bit5 */
\r
97 if (d & 0x10) DI_H(); else DI_L(); /* bit4 */
\r
99 if (d & 0x08) DI_H(); else DI_L(); /* bit3 */
\r
101 if (d & 0x04) DI_H(); else DI_L(); /* bit2 */
\r
103 if (d & 0x02) DI_H(); else DI_L(); /* bit1 */
\r
105 if (d & 0x01) DI_H(); else DI_L(); /* bit0 */
\r
111 /*-----------------------------------------------------------------------*/
\r
112 /* Receive a byte from the MMC (bitbanging) */
\r
113 /*-----------------------------------------------------------------------*/
\r
116 BYTE rcvr_mmc (void)
\r
121 DI_H(); /* Send 0xFF */
\r
123 r = 0; if (DO) r++; /* bit7 */
\r
125 r <<= 1; if (DO) r++; /* bit6 */
\r
127 r <<= 1; if (DO) r++; /* bit5 */
\r
129 r <<= 1; if (DO) r++; /* bit4 */
\r
131 r <<= 1; if (DO) r++; /* bit3 */
\r
133 r <<= 1; if (DO) r++; /* bit2 */
\r
135 r <<= 1; if (DO) r++; /* bit1 */
\r
137 r <<= 1; if (DO) r++; /* bit0 */
\r
145 /*-----------------------------------------------------------------------*/
\r
146 /* Skip bytes on the MMC (bitbanging) */
\r
147 /*-----------------------------------------------------------------------*/
\r
151 WORD n /* Number of bytes to skip */
\r
154 DI_H(); /* Send 0xFF */
\r
170 /*-----------------------------------------------------------------------*/
\r
171 /* Deselect the card and release SPI bus */
\r
172 /*-----------------------------------------------------------------------*/
\r
175 void release_spi (void)
\r
182 /*-----------------------------------------------------------------------*/
\r
183 /* Send a command packet to MMC */
\r
184 /*-----------------------------------------------------------------------*/
\r
188 BYTE cmd, /* Command byte */
\r
189 DWORD arg /* Argument */
\r
195 if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
\r
197 res = send_cmd(CMD55, 0);
\r
198 if (res > 1) return res;
\r
201 /* Select the card */
\r
202 CS_H(); rcvr_mmc();
\r
203 CS_L(); rcvr_mmc();
\r
205 /* Send a command packet */
\r
206 xmit_mmc(cmd); /* Start + Command index */
\r
207 xmit_mmc((BYTE)(arg >> 24)); /* Argument[31..24] */
\r
208 xmit_mmc((BYTE)(arg >> 16)); /* Argument[23..16] */
\r
209 xmit_mmc((BYTE)(arg >> 8)); /* Argument[15..8] */
\r
210 xmit_mmc((BYTE)arg); /* Argument[7..0] */
\r
211 n = 0x01; /* Dummy CRC + Stop */
\r
212 if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
\r
213 if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
\r
216 /* Receive a command response */
\r
217 n = 10; /* Wait for a valid response in timeout of 10 attempts */
\r
220 } while ((res & 0x80) && --n);
\r
222 return res; /* Return with the response value */
\r
227 /*--------------------------------------------------------------------------
\r
231 ---------------------------------------------------------------------------*/
\r
234 /*-----------------------------------------------------------------------*/
\r
235 /* Initialize Disk Drive */
\r
236 /*-----------------------------------------------------------------------*/
\r
238 DSTATUS disk_initialize (void)
\r
240 BYTE n, cmd, ty, buf[4];
\r
247 skip_mmc(10); /* Dummy clocks */
\r
250 if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
\r
251 if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2 */
\r
252 for (n = 0; n < 4; n++) buf[n] = rcvr_mmc(); /* Get trailing return value of R7 resp */
\r
253 if (buf[2] == 0x01 && buf[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
\r
254 for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state (ACMD41 with HCS bit) */
\r
255 if (send_cmd(ACMD41, 1UL << 30) == 0) break;
\r
258 if (tmr && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
\r
259 for (n = 0; n < 4; n++) buf[n] = rcvr_mmc();
\r
260 ty = (buf[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 (HC or SC) */
\r
263 } else { /* SDv1 or MMCv3 */
\r
264 if (send_cmd(ACMD41, 0) <= 1) {
\r
265 ty = CT_SD1; cmd = ACMD41; /* SDv1 */
\r
267 ty = CT_MMC; cmd = CMD1; /* MMCv3 */
\r
269 for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state */
\r
270 if (send_cmd(ACMD41, 0) == 0) break;
\r
273 if (!tmr || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
\r
280 return ty ? 0 : STA_NOINIT;
\r
285 /*-----------------------------------------------------------------------*/
\r
286 /* Read partial sector */
\r
287 /*-----------------------------------------------------------------------*/
\r
289 DRESULT disk_readp (
\r
290 BYTE *buff, /* Pointer to the read buffer (NULL:Read bytes are forwarded to the stream) */
\r
291 DWORD lba, /* Sector number (LBA) */
\r
292 WORD ofs, /* Byte offset to read from (0..511) */
\r
293 WORD cnt /* Number of bytes to read (ofs + cnt mus be <= 512) */
\r
301 if (!(CardType & CT_BLOCK)) lba *= 512; /* Convert to byte address if needed */
\r
304 if (send_cmd(CMD17, lba) == 0) { /* READ_SINGLE_BLOCK */
\r
307 do { /* Wait for data packet in timeout of 100ms */
\r
310 } while (d == 0xFF && --tmr);
\r
312 if (d == 0xFE) { /* A data packet arrived */
\r
313 bc = 514 - ofs - cnt;
\r
315 /* Skip leading bytes */
\r
316 if (ofs) skip_mmc(ofs);
\r
318 /* Receive a part of the sector */
\r
319 if (buff) { /* Store data to the memory */
\r
321 *buff++ = rcvr_mmc();
\r
323 } else { /* Forward data to the outgoing stream */
\r
330 /* Skip trailing bytes and CRC */
\r
344 /*-----------------------------------------------------------------------*/
\r
345 /* Write partial sector */
\r
346 /*-----------------------------------------------------------------------*/
\r
349 DRESULT disk_writep (
\r
350 const BYTE *buff, /* Pointer to the bytes to be written (NULL:Initiate/Finalize sector write) */
\r
351 DWORD sa /* Number of bytes to send, Sector number (LBA) or zero */
\r
361 if (buff) { /* Send data bytes */
\r
363 while (bc && wc) { /* Send data bytes to the card */
\r
369 if (sa) { /* Initiate sector write process */
\r
370 if (!(CardType & CT_BLOCK)) sa *= 512; /* Convert to byte address if needed */
\r
371 if (send_cmd(CMD24, sa) == 0) { /* WRITE_SINGLE_BLOCK */
\r
372 xmit_mmc(0xFF); xmit_mmc(0xFE); /* Data block header */
\r
373 wc = 512; /* Set byte counter */
\r
376 } else { /* Finalize sector write process */
\r
378 while (bc--) xmit_mmc(0); /* Fill left bytes and CRC with zeros */
\r
379 if ((rcvr_mmc() & 0x1F) == 0x05) { /* Receive data resp and wait for end of write process in timeout of 300ms */
\r
380 for (tmr = 10000; rcvr_mmc() != 0xFF && tmr; tmr--) /* Wait for ready (max 1000ms) */
\r
382 if (tmr) res = RES_OK;
\r