annotate mmc.c @ 92:c3eb67cd8b61

handle rem from btoff
author Matt Johnston <matt@ucc.asn.au>
date Mon, 16 Jul 2012 21:28:49 +0800
parents 888be1b234b6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 /* MMCv3/SDv1/SDv2 (in SPI mode) control module (C)ChaN, 2010 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 /* Only rcvr_spi(), xmit_spi(), disk_timerproc() and some macros */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 /* are platform dependent. */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 #include <avr/io.h>
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 #include "diskio.h"
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 /*--------------------------------------------------------------------------
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 Module Private Functions
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 ---------------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 /* Port Controls (Platform dependent) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 #define CS_LOW() PORTB &= ~1 /* MMC CS = L */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 #define CS_HIGH() PORTB |= 1 /* MMC CS = H */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 #define SOCKWP (PINB & 0x20) /* Write protected. yes:true, no:false, default:false */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 #define SOCKINS (!(PINB & 0x10)) /* Card detected. yes:true, no:false, default:true */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #define FCLK_SLOW() SPCR = 0x52 /* Set slow clock (100k-400k) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #define FCLK_FAST() SPCR = 0x50 /* Set fast clock (depends on the CSD) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 /* Definitions for MMC/SDC command */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #define CMD0 (0) /* GO_IDLE_STATE */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 #define CMD1 (1) /* SEND_OP_COND (MMC) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 #define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #define CMD8 (8) /* SEND_IF_COND */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #define CMD9 (9) /* SEND_CSD */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 #define CMD10 (10) /* SEND_CID */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #define CMD12 (12) /* STOP_TRANSMISSION */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #define ACMD13 (0x80+13) /* SD_STATUS (SDC) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 #define CMD16 (16) /* SET_BLOCKLEN */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 #define CMD17 (17) /* READ_SINGLE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 #define CMD18 (18) /* READ_MULTIPLE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #define CMD23 (23) /* SET_BLOCK_COUNT (MMC) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 #define CMD24 (24) /* WRITE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #define CMD55 (55) /* APP_CMD */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 #define CMD58 (58) /* READ_OCR */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 /* Card type flags (CardType) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #define CT_MMC 0x01 /* MMC ver 3 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 #define CT_SD1 0x02 /* SD ver 1 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 #define CT_SD2 0x04 /* SD ver 2 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 #define CT_SDC (CT_SD1|CT_SD2) /* SD */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 #define CT_BLOCK 0x08 /* Block addressing */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 static volatile
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 DSTATUS Stat = STA_NOINIT; /* Disk status */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 static volatile
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 BYTE Timer1, Timer2; /* 100Hz decrement timer */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 BYTE CardType; /* Card type flags */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 /* Transmit a byte to MMC via SPI (Platform dependent) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #define xmit_spi(dat) SPDR=(dat); loop_until_bit_is_set(SPSR,SPIF)
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 /* Receive a byte from MMC via SPI (Platform dependent) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 BYTE rcvr_spi (void)
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 SPDR = 0xFF;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 loop_until_bit_is_set(SPSR, SPIF);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 return SPDR;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 /* Alternative macro to receive data fast */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #define rcvr_spi_m(dst) SPDR=0xFF; loop_until_bit_is_set(SPSR,SPIF); *(dst)=SPDR
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 /* Wait for card ready */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 int wait_ready (void) /* 1:OK, 0:Timeout */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 BYTE d;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 Timer2 = 50; /* Wait for ready in timeout of 500ms */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 do
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 d = rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 while (d != 0xFF && Timer2);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 return (d == 0xFF) ? 1 : 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 /* Deselect the card and release SPI bus */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 void deselect (void)
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 CS_HIGH();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 rcvr_spi(); /* Dummy clock (force DO hi-z for multiple slave SPI) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 /* Select the card and wait for ready */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 int select (void) /* 1:Successful, 0:Timeout */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 CS_LOW();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 rcvr_spi(); /* Dummy clock (force DO enabled) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 if (wait_ready()) return 1; /* OK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 deselect();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 return 0; /* Timeout */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 /* Power Control (Platform dependent) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 /* When the target system does not support socket power control, there */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 /* is nothing to do in these functions and chk_power always returns 1. */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 int power_status(void) /* Socket power state: 0=off, 1=on */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 return 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 // matt
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 //return (PORTE & 0x80) ? 0 : 1;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 void power_on (void)
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 // matt
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 #if 0
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 PORTE &= ~0x80; /* Socket power on */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 for (Timer1 = 2; Timer1; ); /* Wait for 20ms */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 PORTB = 0b10110101; /* Enable drivers */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 DDRB = 0b11000111;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 #endif
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 SPCR = 0x52; /* Enable SPI function in mode 0 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 SPSR = 0x01; /* SPI 2x mode */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 void power_off (void)
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 SPCR = 0; /* Disable SPI function */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 DDRB = 0b11000000; /* Disable drivers */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 PORTB = 0b10110000;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 // matt
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 #if 0
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 PORTE |= 0x80; /* Socket power off */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 #endif
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 Stat |= STA_NOINIT;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189 /* Receive a data packet from MMC */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 int rcvr_datablock (
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 BYTE *buff, /* Data buffer to store received data */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 UINT btr /* Byte count (must be multiple of 4) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 BYTE token;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 Timer1 = 20;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 do { /* Wait for data packet in timeout of 200ms */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 token = rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204 } while ((token == 0xFF) && Timer1);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 if(token != 0xFE) return 0; /* If not valid data token, retutn with error */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 do { /* Receive the data block into buffer */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208 rcvr_spi_m(buff++);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 rcvr_spi_m(buff++);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 rcvr_spi_m(buff++);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 rcvr_spi_m(buff++);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 } while (btr -= 4);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 rcvr_spi(); /* Discard CRC */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 return 1; /* Return with success */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 /* Send a data packet to MMC */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 int xmit_datablock (
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 const BYTE *buff, /* 512 byte data block to be transmitted */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 BYTE token /* Data/Stop token */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
231 BYTE resp, wc;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
232
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
233
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 if (!wait_ready()) return 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
236 xmit_spi(token); /* Xmit data token */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
237 if (token != 0xFD) { /* Is data token */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 wc = 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239 do { /* Xmit the 512 byte data block to MMC */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 xmit_spi(*buff++);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 xmit_spi(*buff++);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242 } while (--wc);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 xmit_spi(0xFF); /* CRC (Dummy) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 xmit_spi(0xFF);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 resp = rcvr_spi(); /* Reveive data response */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 if ((resp & 0x1F) != 0x05) /* If not accepted, return with error */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 return 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 return 1;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 /* Send a command packet to MMC */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 static
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 BYTE send_cmd ( /* Returns R1 resp (bit7==1:Send failed) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 BYTE cmd, /* Command index */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 DWORD arg /* Argument */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 BYTE n, res;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 cmd &= 0x7F;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 res = send_cmd(CMD55, 0);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 if (res > 1) return res;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 /* Select the card and wait for ready */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 deselect();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 if (!select()) return 0xFF;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 /* Send command packet */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 xmit_spi(0x40 | cmd); /* Start + Command index */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283 xmit_spi((BYTE)arg); /* Argument[7..0] */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 n = 0x01; /* Dummy CRC + Stop */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 xmit_spi(n);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 /* Receive command response */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 n = 10; /* Wait for a valid response in timeout of 10 attempts */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 do
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 res = rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 while ((res & 0x80) && --n);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 return res; /* Return with the response value */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 /*--------------------------------------------------------------------------
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 Public Functions
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 ---------------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 /* Initialize Disk Drive */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 DSTATUS disk_initialize (
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 BYTE drv /* Physical drive nmuber (0) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 BYTE n, cmd, ty, ocr[4];
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 if (drv) return STA_NOINIT; /* Supports only single drive */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 if (Stat & STA_NODISK) return Stat; /* No card in the socket */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 power_on(); /* Force socket power on */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 FCLK_SLOW();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 for (n = 10; n; n--) rcvr_spi(); /* 80 dummy clocks */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326 ty = 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328 Timer1 = 100; /* Initialization timeout of 1000 msec */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 for (n = 0; n < 4; n++) ocr[n] = rcvr_spi(); /* Get trailing return value of R7 resp */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 while (Timer1 && send_cmd(ACMD41, 1UL << 30)); /* Wait for leaving idle state (ACMD41 with HCS bit) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 if (Timer1 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334 for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 } else { /* SDv1 or MMCv3 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339 if (send_cmd(ACMD41, 0) <= 1) {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 ty = CT_SD1; cmd = ACMD41; /* SDv1 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 } else {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 ty = CT_MMC; cmd = CMD1; /* MMCv3 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 while (Timer1 && send_cmd(cmd, 0)); /* Wait for leaving idle state */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 if (!Timer1 || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 ty = 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 CardType = ty;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 deselect();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 if (ty) { /* Initialization succeded */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 FCLK_FAST();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 } else { /* Initialization failed */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 power_off();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 return Stat;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 /* Get Disk Status */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 DSTATUS disk_status (
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369 BYTE drv /* Physical drive nmuber (0) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372 if (drv) return STA_NOINIT; /* Supports only single drive */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 return Stat;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 /* Read Sector(s) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 DRESULT disk_read (
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383 BYTE drv, /* Physical drive nmuber (0) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384 BYTE *buff, /* Pointer to the data buffer to store read data */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 DWORD sector, /* Start sector number (LBA) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 BYTE count /* Sector count (1..255) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 if (drv || !count) return RES_PARERR;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390 if (Stat & STA_NOINIT) return RES_NOTRDY;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
392 if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394 if (count == 1) { /* Single block read */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 if ((send_cmd(CMD17, sector) == 0) /* READ_SINGLE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396 && rcvr_datablock(buff, 512))
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 count = 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 else { /* Multiple block read */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 do {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402 if (!rcvr_datablock(buff, 512)) break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403 buff += 512;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 } while (--count);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405 send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 deselect();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 return count ? RES_ERROR : RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 /* Write Sector(s) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 DRESULT disk_write (
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 BYTE drv, /* Physical drive nmuber (0) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 const BYTE *buff, /* Pointer to the data to be written */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 DWORD sector, /* Start sector number (LBA) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 BYTE count /* Sector count (1..255) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 if (drv || !count) return RES_PARERR;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427 if (Stat & STA_NOINIT) return RES_NOTRDY;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 if (Stat & STA_PROTECT) return RES_WRPRT;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432 if (count == 1) { /* Single block write */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434 && xmit_datablock(buff, 0xFE))
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 count = 0;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 else { /* Multiple block write */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 if (CardType & CT_SDC) send_cmd(ACMD23, count);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 do {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441 if (!xmit_datablock(buff, 0xFC)) break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 buff += 512;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443 } while (--count);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 if (!xmit_datablock(0, 0xFD)) /* STOP_TRAN token */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 count = 1;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 deselect();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 return count ? RES_ERROR : RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 /* Miscellaneous Functions */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459 DRESULT disk_ioctl (
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 BYTE drv, /* Physical drive nmuber (0) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 BYTE ctrl, /* Control code */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462 void *buff /* Buffer to send/receive control data */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 )
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465 DRESULT res;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 BYTE n, csd[16], *ptr = buff;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467 WORD csize;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 if (drv) return RES_PARERR;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472 res = RES_ERROR;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 if (ctrl == CTRL_POWER) {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 switch (ptr[0]) {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476 case 0: /* Sub control code (POWER_OFF) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477 power_off(); /* Power off */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480 case 1: /* Sub control code (POWER_GET) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 ptr[1] = (BYTE)power_status();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484 default :
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 res = RES_PARERR;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 else {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 if (Stat & STA_NOINIT) return RES_NOTRDY;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 switch (ctrl) {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492 case CTRL_SYNC : /* Make sure that no pending write process. Do not remove this or written sector might not left updated. */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 if (select()) {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494 deselect();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501 if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 csize = csd[9] + ((WORD)csd[8] << 8) + 1;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503 *(DWORD*)buff = (DWORD)csize << 10;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 } else { /* SDC ver 1.XX or MMC*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506 csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 *(DWORD*)buff = (DWORD)csize << (n - 9);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 case GET_SECTOR_SIZE : /* Get R/W sector size (WORD) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514 *(WORD*)buff = 512;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519 if (CardType & CT_SD2) { /* SDv2? */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522 if (rcvr_datablock(csd, 16)) { /* Read partial block */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 for (n = 64 - 16; n; n--) rcvr_spi(); /* Purge trailing data */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 *(DWORD*)buff = 16UL << (csd[10] >> 4);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528 } else { /* SDv1 or MMCv3 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { /* Read CSD */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 if (CardType & CT_SD1) { /* SDv1 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532 } else { /* MMCv3 */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
537 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 case MMC_GET_TYPE : /* Get card type flags (1 byte) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541 *ptr = CardType;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 case MMC_GET_CSD : /* Receive CSD as a data block (16 bytes) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 if (send_cmd(CMD9, 0) == 0 /* READ_CSD */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547 && rcvr_datablock(ptr, 16))
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 case MMC_GET_CID : /* Receive CID as a data block (16 bytes) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552 if (send_cmd(CMD10, 0) == 0 /* READ_CID */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 && rcvr_datablock(ptr, 16))
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557 case MMC_GET_OCR : /* Receive OCR as an R3 resp (4 bytes) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 if (send_cmd(CMD58, 0) == 0) { /* READ_OCR */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 for (n = 4; n; n--) *ptr++ = rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 case MMC_GET_SDSTAT : /* Receive SD statsu as a data block (64 bytes) */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 if (send_cmd(ACMD13, 0) == 0) { /* SD_STATUS */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 rcvr_spi();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 if (rcvr_datablock(ptr, 64))
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 res = RES_OK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 break;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 default:
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 res = RES_PARERR;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 deselect();
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 return res;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580 }
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 /* Device Timer Interrupt Procedure */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 /*-----------------------------------------------------------------------*/
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 /* This function must be called in period of 10ms */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 void disk_timerproc (void)
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590 {
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 BYTE n, s;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 n = Timer1; /* 100Hz decrement timer */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 if (n) Timer1 = --n;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596 n = Timer2;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 if (n) Timer2 = --n;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 s = Stat;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601 if (SOCKWP) /* Write protected */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 s |= STA_PROTECT;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603 else /* Write enabled */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604 s &= ~STA_PROTECT;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 if (SOCKINS) /* Card inserted */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 s &= ~STA_NODISK;
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608 else /* Socket empty */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 s |= (STA_NODISK | STA_NOINIT);
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 Stat = s; /* Update MMC status */
888be1b234b6 Add FatFS
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 }