annotate mmc.c @ 381:83c83014e5e3

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