User | Revision | Line number | New contents of line |
apm_litoral |
0:b65664229800
|
1
|
#include "SDFileSystem.h"
|
apm_litoral |
0:b65664229800
|
2
|
|
apm_litoral |
0:b65664229800
|
3
|
#define SD_COMMAND_TIMEOUT 5000
|
apm_litoral |
0:b65664229800
|
4
|
|
apm_litoral |
0:b65664229800
|
5
|
SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
|
apm_litoral |
0:b65664229800
|
6
|
FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {
|
apm_litoral |
0:b65664229800
|
7
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
8
|
}
|
apm_litoral |
0:b65664229800
|
9
|
|
apm_litoral |
0:b65664229800
|
10
|
#define R1_IDLE_STATE (1 << 0)
|
apm_litoral |
0:b65664229800
|
11
|
#define R1_ERASE_RESET (1 << 1)
|
apm_litoral |
0:b65664229800
|
12
|
#define R1_ILLEGAL_COMMAND (1 << 2)
|
apm_litoral |
0:b65664229800
|
13
|
#define R1_COM_CRC_ERROR (1 << 3)
|
apm_litoral |
0:b65664229800
|
14
|
#define R1_ERASE_SEQUENCE_ERROR (1 << 4)
|
apm_litoral |
0:b65664229800
|
15
|
#define R1_ADDRESS_ERROR (1 << 5)
|
apm_litoral |
0:b65664229800
|
16
|
#define R1_PARAMETER_ERROR (1 << 6)
|
apm_litoral |
0:b65664229800
|
17
|
|
apm_litoral |
0:b65664229800
|
18
|
// Types
|
apm_litoral |
0:b65664229800
|
19
|
// - v1.x Standard Capacity
|
apm_litoral |
0:b65664229800
|
20
|
// - v2.x Standard Capacity
|
apm_litoral |
0:b65664229800
|
21
|
// - v2.x High Capacity
|
apm_litoral |
0:b65664229800
|
22
|
// - Not recognised as an SD Card
|
apm_litoral |
0:b65664229800
|
23
|
|
apm_litoral |
0:b65664229800
|
24
|
#define SDCARD_FAIL 0
|
apm_litoral |
0:b65664229800
|
25
|
#define SDCARD_V1 1
|
apm_litoral |
0:b65664229800
|
26
|
#define SDCARD_V2 2
|
apm_litoral |
0:b65664229800
|
27
|
#define SDCARD_V2HC 3
|
apm_litoral |
0:b65664229800
|
28
|
|
apm_litoral |
0:b65664229800
|
29
|
int SDFileSystem::initialise_card() {
|
apm_litoral |
0:b65664229800
|
30
|
// Set to 100kHz for initialisation, and clock card with cs = 1
|
apm_litoral |
0:b65664229800
|
31
|
_spi.frequency(100000);
|
apm_litoral |
0:b65664229800
|
32
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
33
|
for(int i=0; i<16; i++) {
|
apm_litoral |
0:b65664229800
|
34
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
35
|
}
|
apm_litoral |
0:b65664229800
|
36
|
|
apm_litoral |
0:b65664229800
|
37
|
// send CMD0, should return with all zeros except IDLE STATE set (bit 0)
|
apm_litoral |
0:b65664229800
|
38
|
if(_cmd(0, 0) != R1_IDLE_STATE) {
|
apm_litoral |
0:b65664229800
|
39
|
fprintf(stderr, "No disk, or could not put SD card in to SPI idle state\n");
|
apm_litoral |
0:b65664229800
|
40
|
return SDCARD_FAIL;
|
apm_litoral |
0:b65664229800
|
41
|
}
|
apm_litoral |
0:b65664229800
|
42
|
|
apm_litoral |
0:b65664229800
|
43
|
// send CMD8 to determine whther it is ver 2.x
|
apm_litoral |
0:b65664229800
|
44
|
int r = _cmd8();
|
apm_litoral |
0:b65664229800
|
45
|
if(r == R1_IDLE_STATE) {
|
apm_litoral |
0:b65664229800
|
46
|
return initialise_card_v2();
|
apm_litoral |
0:b65664229800
|
47
|
} else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
|
apm_litoral |
0:b65664229800
|
48
|
return initialise_card_v1();
|
apm_litoral |
0:b65664229800
|
49
|
} else {
|
apm_litoral |
0:b65664229800
|
50
|
fprintf(stderr, "Not in idle state after sending CMD8 (not an SD card?)\n");
|
apm_litoral |
0:b65664229800
|
51
|
return SDCARD_FAIL;
|
apm_litoral |
0:b65664229800
|
52
|
}
|
apm_litoral |
0:b65664229800
|
53
|
}
|
apm_litoral |
0:b65664229800
|
54
|
|
apm_litoral |
0:b65664229800
|
55
|
int SDFileSystem::initialise_card_v1() {
|
apm_litoral |
0:b65664229800
|
56
|
for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
|
apm_litoral |
0:b65664229800
|
57
|
_cmd(55, 0);
|
apm_litoral |
0:b65664229800
|
58
|
if(_cmd(41, 0) == 0) {
|
apm_litoral |
0:b65664229800
|
59
|
return SDCARD_V1;
|
apm_litoral |
0:b65664229800
|
60
|
}
|
apm_litoral |
0:b65664229800
|
61
|
}
|
apm_litoral |
0:b65664229800
|
62
|
|
apm_litoral |
0:b65664229800
|
63
|
fprintf(stderr, "Timeout waiting for v1.x card\n");
|
apm_litoral |
0:b65664229800
|
64
|
return SDCARD_FAIL;
|
apm_litoral |
0:b65664229800
|
65
|
}
|
apm_litoral |
0:b65664229800
|
66
|
|
apm_litoral |
0:b65664229800
|
67
|
|
apm_litoral |
0:b65664229800
|
68
|
void SDFileSystem::init_bios() {
|
apm_litoral |
0:b65664229800
|
69
|
int i=0;
|
apm_litoral |
0:b65664229800
|
70
|
const unsigned short data[229] = {
|
apm_litoral |
0:b65664229800
|
71
|
0x3C,0x21,0x2D,0x2D,0x20,0x41,0x6C,0x6C,0x20,0x50,0x6F,0x77,0x65,0x72,0x20,0x4D,
|
apm_litoral |
0:b65664229800
|
72
|
0x69,0x63,0x72,0x6F,0x63,0x6F,0x6E,0x74,0x72,0x6F,0x6C,0x6C,0x65,0x72,0x20,0x57,
|
apm_litoral |
0:b65664229800
|
73
|
0x65,0x62,0x73,0x69,0x74,0x65,0x20,0x61,0x6E,0x64,0x20,0x41,0x75,0x74,0x68,0x65,
|
apm_litoral |
0:b65664229800
|
74
|
0x6E,0x74,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x53,0x68,0x6F,0x72,0x74,0x63,
|
apm_litoral |
0:b65664229800
|
75
|
0x75,0x74,0x20,0x2D,0x2D,0x3E,0x0D,0x0A,0x3C,0x68,0x74,0x6D,0x6C,0x3E,0x0D,0x0A,
|
apm_litoral |
0:b65664229800
|
76
|
0x3C,0x68,0x65,0x61,0x64,0x3E,0x0D,0x0A,0x3C,0x6D,0x65,0x74,0x61,0x20,0x68,0x74,
|
apm_litoral |
0:b65664229800
|
77
|
0x74,0x70,0x2D,0x65,0x71,0x75,0x69,0x76,0x3D,0x22,0x72,0x65,0x66,0x72,0x65,0x73,
|
apm_litoral |
0:b65664229800
|
78
|
0x68,0x22,0x20,0x63,0x6F,0x6E,0x74,0x65,0x6E,0x74,0x3D,0x22,0x30,0x3B,0x20,0x75,
|
apm_litoral |
0:b65664229800
|
79
|
0x72,0x6C,0x3D,0x68,0x74,0x74,0x70,0x3A,0x2F,0x2F,0x77,0x77,0x77,0x2E,0x61,0x70,
|
apm_litoral |
0:b65664229800
|
80
|
0x6D,0x6D,0x69,0x63,0x72,0x6F,0x2E,0x63,0x6F,0x6D,0x22,0x2F,0x3E,0x0D,0x0A,0x3C,
|
apm_litoral |
0:b65664229800
|
81
|
0x74,0x69,0x74,0x6C,0x65,0x3E,0x41,0x50,0x4D,0x20,0x57,0x65,0x62,0x73,0x69,0x74,
|
apm_litoral |
0:b65664229800
|
82
|
0x65,0x20,0x53,0x68,0x6F,0x72,0x74,0x63,0x75,0x74,0x3C,0x2F,0x74,0x69,0x74,0x6C,
|
apm_litoral |
0:b65664229800
|
83
|
0x65,0x3E,0x0D,0x0A,0x3C,0x2F,0x68,0x65,0x61,0x64,0x3E,0x0D,0x0A,0x3C,0x62,0x6F,
|
apm_litoral |
0:b65664229800
|
84
|
0x64,0x79,0x3E,0x3C,0x2F,0x62,0x6F,0x64,0x79,0x3E,0x0D,0x0A,0x3C,0x2F,0x68,0x74,
|
apm_litoral |
0:b65664229800
|
85
|
0x6D,0x6C,0x3E,0x0D,0x0A
|
apm_litoral |
0:b65664229800
|
86
|
|
apm_litoral |
0:b65664229800
|
87
|
};
|
apm_litoral |
0:b65664229800
|
88
|
|
apm_litoral |
0:b65664229800
|
89
|
const char stroop[12] = {
|
apm_litoral |
0:b65664229800
|
90
|
0x2F,0x73,0x64,0x2F,0x41,0x50,0x4D,0x2E,0x48,0x54,0x4D,0x00
|
apm_litoral |
0:b65664229800
|
91
|
};
|
apm_litoral |
0:b65664229800
|
92
|
|
apm_litoral |
0:b65664229800
|
93
|
FILE *binary = fopen((const char *)&stroop[0], "w");
|
apm_litoral |
0:b65664229800
|
94
|
while (i<=229) {
|
apm_litoral |
0:b65664229800
|
95
|
fprintf(binary,(const char *)&data[i]);
|
apm_litoral |
0:b65664229800
|
96
|
i++;
|
apm_litoral |
0:b65664229800
|
97
|
}
|
apm_litoral |
0:b65664229800
|
98
|
fclose(binary);
|
apm_litoral |
0:b65664229800
|
99
|
}
|
apm_litoral |
0:b65664229800
|
100
|
|
apm_litoral |
0:b65664229800
|
101
|
|
apm_litoral |
0:b65664229800
|
102
|
|
apm_litoral |
0:b65664229800
|
103
|
|
apm_litoral |
0:b65664229800
|
104
|
int SDFileSystem::initialise_card_v2() {
|
apm_litoral |
0:b65664229800
|
105
|
|
apm_litoral |
0:b65664229800
|
106
|
for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
|
apm_litoral |
0:b65664229800
|
107
|
_cmd(55, 0);
|
apm_litoral |
0:b65664229800
|
108
|
if(_cmd(41, 0) == 0) {
|
apm_litoral |
0:b65664229800
|
109
|
_cmd58();
|
apm_litoral |
0:b65664229800
|
110
|
return SDCARD_V2;
|
apm_litoral |
0:b65664229800
|
111
|
}
|
apm_litoral |
0:b65664229800
|
112
|
}
|
apm_litoral |
0:b65664229800
|
113
|
|
apm_litoral |
0:b65664229800
|
114
|
fprintf(stderr, "Timeout waiting for v2.x card\n");
|
apm_litoral |
0:b65664229800
|
115
|
return SDCARD_FAIL;
|
apm_litoral |
0:b65664229800
|
116
|
}
|
apm_litoral |
0:b65664229800
|
117
|
|
apm_litoral |
0:b65664229800
|
118
|
int SDFileSystem::disk_initialize() {
|
apm_litoral |
0:b65664229800
|
119
|
|
apm_litoral |
0:b65664229800
|
120
|
int i = initialise_card();
|
apm_litoral |
0:b65664229800
|
121
|
// printf("init card = %d\n", i);
|
apm_litoral |
0:b65664229800
|
122
|
// printf("OK\n");
|
apm_litoral |
0:b65664229800
|
123
|
|
apm_litoral |
0:b65664229800
|
124
|
_sectors = _sd_sectors();
|
apm_litoral |
0:b65664229800
|
125
|
|
apm_litoral |
0:b65664229800
|
126
|
// Set block length to 512 (CMD16)
|
apm_litoral |
0:b65664229800
|
127
|
if(_cmd(16, 512) != 0) {
|
apm_litoral |
0:b65664229800
|
128
|
fprintf(stderr, "Set 512-byte block timed out\n");
|
apm_litoral |
0:b65664229800
|
129
|
return 1;
|
apm_litoral |
0:b65664229800
|
130
|
}
|
apm_litoral |
0:b65664229800
|
131
|
|
apm_litoral |
0:b65664229800
|
132
|
_spi.frequency(1000000); // Set to 1MHz for data transfer
|
apm_litoral |
0:b65664229800
|
133
|
return 0;
|
apm_litoral |
0:b65664229800
|
134
|
}
|
apm_litoral |
0:b65664229800
|
135
|
|
apm_litoral |
0:b65664229800
|
136
|
int SDFileSystem::disk_write(const char *buffer, int block_number) {
|
apm_litoral |
0:b65664229800
|
137
|
// set write address for single block (CMD24)
|
apm_litoral |
0:b65664229800
|
138
|
if(_cmd(24, block_number * 512) != 0) {
|
apm_litoral |
0:b65664229800
|
139
|
return 1;
|
apm_litoral |
0:b65664229800
|
140
|
}
|
apm_litoral |
0:b65664229800
|
141
|
|
apm_litoral |
0:b65664229800
|
142
|
// send the data block
|
apm_litoral |
0:b65664229800
|
143
|
_write(buffer, 512);
|
apm_litoral |
0:b65664229800
|
144
|
return 0;
|
apm_litoral |
0:b65664229800
|
145
|
}
|
apm_litoral |
0:b65664229800
|
146
|
|
apm_litoral |
0:b65664229800
|
147
|
int SDFileSystem::disk_read(char *buffer, int block_number) {
|
apm_litoral |
0:b65664229800
|
148
|
// set read address for single block (CMD17)
|
apm_litoral |
0:b65664229800
|
149
|
if(_cmd(17, block_number * 512) != 0) {
|
apm_litoral |
0:b65664229800
|
150
|
return 1;
|
apm_litoral |
0:b65664229800
|
151
|
}
|
apm_litoral |
0:b65664229800
|
152
|
|
apm_litoral |
0:b65664229800
|
153
|
// receive the data
|
apm_litoral |
0:b65664229800
|
154
|
_read(buffer, 512);
|
apm_litoral |
0:b65664229800
|
155
|
return 0;
|
apm_litoral |
0:b65664229800
|
156
|
}
|
apm_litoral |
0:b65664229800
|
157
|
|
apm_litoral |
0:b65664229800
|
158
|
int SDFileSystem::disk_status() { return 0; }
|
apm_litoral |
0:b65664229800
|
159
|
int SDFileSystem::disk_sync() { return 0; }
|
apm_litoral |
0:b65664229800
|
160
|
int SDFileSystem::disk_sectors() { return _sectors; }
|
apm_litoral |
0:b65664229800
|
161
|
|
apm_litoral |
0:b65664229800
|
162
|
// PRIVATE FUNCTIONS
|
apm_litoral |
0:b65664229800
|
163
|
|
apm_litoral |
0:b65664229800
|
164
|
int SDFileSystem::_cmd(int cmd, int arg) {
|
apm_litoral |
0:b65664229800
|
165
|
_cs = 0;
|
apm_litoral |
0:b65664229800
|
166
|
|
apm_litoral |
0:b65664229800
|
167
|
// send a command
|
apm_litoral |
0:b65664229800
|
168
|
_spi.write(0x40 | cmd);
|
apm_litoral |
0:b65664229800
|
169
|
_spi.write(arg >> 24);
|
apm_litoral |
0:b65664229800
|
170
|
_spi.write(arg >> 16);
|
apm_litoral |
0:b65664229800
|
171
|
_spi.write(arg >> 8);
|
apm_litoral |
0:b65664229800
|
172
|
_spi.write(arg >> 0);
|
apm_litoral |
0:b65664229800
|
173
|
_spi.write(0x95);
|
apm_litoral |
0:b65664229800
|
174
|
|
apm_litoral |
0:b65664229800
|
175
|
// wait for the repsonse (response[7] == 0)
|
apm_litoral |
0:b65664229800
|
176
|
for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
|
apm_litoral |
0:b65664229800
|
177
|
int response = _spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
178
|
if(!(response & 0x80)) {
|
apm_litoral |
0:b65664229800
|
179
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
180
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
181
|
return response;
|
apm_litoral |
0:b65664229800
|
182
|
}
|
apm_litoral |
0:b65664229800
|
183
|
}
|
apm_litoral |
0:b65664229800
|
184
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
185
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
186
|
return -1; // timeout
|
apm_litoral |
0:b65664229800
|
187
|
}
|
apm_litoral |
0:b65664229800
|
188
|
int SDFileSystem::_cmdx(int cmd, int arg) {
|
apm_litoral |
0:b65664229800
|
189
|
_cs = 0;
|
apm_litoral |
0:b65664229800
|
190
|
|
apm_litoral |
0:b65664229800
|
191
|
// send a command
|
apm_litoral |
0:b65664229800
|
192
|
_spi.write(0x40 | cmd);
|
apm_litoral |
0:b65664229800
|
193
|
_spi.write(arg >> 24);
|
apm_litoral |
0:b65664229800
|
194
|
_spi.write(arg >> 16);
|
apm_litoral |
0:b65664229800
|
195
|
_spi.write(arg >> 8);
|
apm_litoral |
0:b65664229800
|
196
|
_spi.write(arg >> 0);
|
apm_litoral |
0:b65664229800
|
197
|
_spi.write(0x95);
|
apm_litoral |
0:b65664229800
|
198
|
|
apm_litoral |
0:b65664229800
|
199
|
// wait for the repsonse (response[7] == 0)
|
apm_litoral |
0:b65664229800
|
200
|
for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
|
apm_litoral |
0:b65664229800
|
201
|
int response = _spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
202
|
if(!(response & 0x80)) {
|
apm_litoral |
0:b65664229800
|
203
|
return response;
|
apm_litoral |
0:b65664229800
|
204
|
}
|
apm_litoral |
0:b65664229800
|
205
|
}
|
apm_litoral |
0:b65664229800
|
206
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
207
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
208
|
return -1; // timeout
|
apm_litoral |
0:b65664229800
|
209
|
}
|
apm_litoral |
0:b65664229800
|
210
|
|
apm_litoral |
0:b65664229800
|
211
|
|
apm_litoral |
0:b65664229800
|
212
|
int SDFileSystem::_cmd58() {
|
apm_litoral |
0:b65664229800
|
213
|
_cs = 0;
|
apm_litoral |
0:b65664229800
|
214
|
int arg = 0;
|
apm_litoral |
0:b65664229800
|
215
|
|
apm_litoral |
0:b65664229800
|
216
|
// send a command
|
apm_litoral |
0:b65664229800
|
217
|
_spi.write(0x40 | 58);
|
apm_litoral |
0:b65664229800
|
218
|
_spi.write(arg >> 24);
|
apm_litoral |
0:b65664229800
|
219
|
_spi.write(arg >> 16);
|
apm_litoral |
0:b65664229800
|
220
|
_spi.write(arg >> 8);
|
apm_litoral |
0:b65664229800
|
221
|
_spi.write(arg >> 0);
|
apm_litoral |
0:b65664229800
|
222
|
_spi.write(0x95);
|
apm_litoral |
0:b65664229800
|
223
|
|
apm_litoral |
0:b65664229800
|
224
|
// wait for the repsonse (response[7] == 0)
|
apm_litoral |
0:b65664229800
|
225
|
for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
|
apm_litoral |
0:b65664229800
|
226
|
int response = _spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
227
|
if(!(response & 0x80)) {
|
apm_litoral |
0:b65664229800
|
228
|
int ocr = _spi.write(0xFF) << 24;
|
apm_litoral |
0:b65664229800
|
229
|
ocr |= _spi.write(0xFF) << 16;
|
apm_litoral |
0:b65664229800
|
230
|
ocr |= _spi.write(0xFF) << 8;
|
apm_litoral |
0:b65664229800
|
231
|
ocr |= _spi.write(0xFF) << 0;
|
apm_litoral |
0:b65664229800
|
232
|
// printf("OCR = 0x%08X\n", ocr);
|
apm_litoral |
0:b65664229800
|
233
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
234
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
235
|
return response;
|
apm_litoral |
0:b65664229800
|
236
|
}
|
apm_litoral |
0:b65664229800
|
237
|
}
|
apm_litoral |
0:b65664229800
|
238
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
239
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
240
|
return -1; // timeout
|
apm_litoral |
0:b65664229800
|
241
|
}
|
apm_litoral |
0:b65664229800
|
242
|
|
apm_litoral |
0:b65664229800
|
243
|
int SDFileSystem::_cmd8() {
|
apm_litoral |
0:b65664229800
|
244
|
_cs = 0;
|
apm_litoral |
0:b65664229800
|
245
|
|
apm_litoral |
0:b65664229800
|
246
|
// send a command
|
apm_litoral |
0:b65664229800
|
247
|
_spi.write(0x40 | 8); // CMD8
|
apm_litoral |
0:b65664229800
|
248
|
_spi.write(0x00); // reserved
|
apm_litoral |
0:b65664229800
|
249
|
_spi.write(0x00); // reserved
|
apm_litoral |
0:b65664229800
|
250
|
_spi.write(0x01); // 3.3v
|
apm_litoral |
0:b65664229800
|
251
|
_spi.write(0xAA); // check pattern
|
apm_litoral |
0:b65664229800
|
252
|
_spi.write(0x87); // crc
|
apm_litoral |
0:b65664229800
|
253
|
|
apm_litoral |
0:b65664229800
|
254
|
// wait for the repsonse (response[7] == 0)
|
apm_litoral |
0:b65664229800
|
255
|
for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) {
|
apm_litoral |
0:b65664229800
|
256
|
char response[5];
|
apm_litoral |
0:b65664229800
|
257
|
response[0] = _spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
258
|
if(!(response[0] & 0x80)) {
|
apm_litoral |
0:b65664229800
|
259
|
for(int j=1; j<5; j++) {
|
apm_litoral |
0:b65664229800
|
260
|
response[i] = _spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
261
|
}
|
apm_litoral |
0:b65664229800
|
262
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
263
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
264
|
return response[0];
|
apm_litoral |
0:b65664229800
|
265
|
}
|
apm_litoral |
0:b65664229800
|
266
|
}
|
apm_litoral |
0:b65664229800
|
267
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
268
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
269
|
return -1; // timeout
|
apm_litoral |
0:b65664229800
|
270
|
}
|
apm_litoral |
0:b65664229800
|
271
|
|
apm_litoral |
0:b65664229800
|
272
|
int SDFileSystem::_read(char *buffer, int length) {
|
apm_litoral |
0:b65664229800
|
273
|
_cs = 0;
|
apm_litoral |
0:b65664229800
|
274
|
|
apm_litoral |
0:b65664229800
|
275
|
// read until start byte (0xFF)
|
apm_litoral |
0:b65664229800
|
276
|
while(_spi.write(0xFF) != 0xFE);
|
apm_litoral |
0:b65664229800
|
277
|
|
apm_litoral |
0:b65664229800
|
278
|
// read data
|
apm_litoral |
0:b65664229800
|
279
|
for(int i=0; i<length; i++) {
|
apm_litoral |
0:b65664229800
|
280
|
buffer[i] = _spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
281
|
}
|
apm_litoral |
0:b65664229800
|
282
|
_spi.write(0xFF); // checksum
|
apm_litoral |
0:b65664229800
|
283
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
284
|
|
apm_litoral |
0:b65664229800
|
285
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
286
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
287
|
return 0;
|
apm_litoral |
0:b65664229800
|
288
|
}
|
apm_litoral |
0:b65664229800
|
289
|
|
apm_litoral |
0:b65664229800
|
290
|
int SDFileSystem::_write(const char *buffer, int length) {
|
apm_litoral |
0:b65664229800
|
291
|
_cs = 0;
|
apm_litoral |
0:b65664229800
|
292
|
|
apm_litoral |
0:b65664229800
|
293
|
// indicate start of block
|
apm_litoral |
0:b65664229800
|
294
|
_spi.write(0xFE);
|
apm_litoral |
0:b65664229800
|
295
|
|
apm_litoral |
0:b65664229800
|
296
|
// write the data
|
apm_litoral |
0:b65664229800
|
297
|
for(int i=0; i<length; i++) {
|
apm_litoral |
0:b65664229800
|
298
|
_spi.write(buffer[i]);
|
apm_litoral |
0:b65664229800
|
299
|
}
|
apm_litoral |
0:b65664229800
|
300
|
|
apm_litoral |
0:b65664229800
|
301
|
// write the checksum
|
apm_litoral |
0:b65664229800
|
302
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
303
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
304
|
|
apm_litoral |
0:b65664229800
|
305
|
// check the repsonse token
|
apm_litoral |
0:b65664229800
|
306
|
if((_spi.write(0xFF) & 0x1F) != 0x05) {
|
apm_litoral |
0:b65664229800
|
307
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
308
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
309
|
return 1;
|
apm_litoral |
0:b65664229800
|
310
|
}
|
apm_litoral |
0:b65664229800
|
311
|
|
apm_litoral |
0:b65664229800
|
312
|
// wait for write to finish
|
apm_litoral |
0:b65664229800
|
313
|
while(_spi.write(0xFF) == 0);
|
apm_litoral |
0:b65664229800
|
314
|
|
apm_litoral |
0:b65664229800
|
315
|
_cs = 1;
|
apm_litoral |
0:b65664229800
|
316
|
_spi.write(0xFF);
|
apm_litoral |
0:b65664229800
|
317
|
return 0;
|
apm_litoral |
0:b65664229800
|
318
|
}
|
apm_litoral |
0:b65664229800
|
319
|
|
apm_litoral |
0:b65664229800
|
320
|
static int ext_bits(char *data, int msb, int lsb) {
|
apm_litoral |
0:b65664229800
|
321
|
int bits = 0;
|
apm_litoral |
0:b65664229800
|
322
|
int size = 1 + msb - lsb;
|
apm_litoral |
0:b65664229800
|
323
|
for(int i=0; i<size; i++) {
|
apm_litoral |
0:b65664229800
|
324
|
int position = lsb + i;
|
apm_litoral |
0:b65664229800
|
325
|
int byte = 15 - (position >> 3);
|
apm_litoral |
0:b65664229800
|
326
|
int bit = position & 0x7;
|
apm_litoral |
0:b65664229800
|
327
|
int value = (data[byte] >> bit) & 1;
|
apm_litoral |
0:b65664229800
|
328
|
bits |= value << i;
|
apm_litoral |
0:b65664229800
|
329
|
}
|
apm_litoral |
0:b65664229800
|
330
|
return bits;
|
apm_litoral |
0:b65664229800
|
331
|
}
|
apm_litoral |
0:b65664229800
|
332
|
|
apm_litoral |
0:b65664229800
|
333
|
int SDFileSystem::_sd_sectors() {
|
apm_litoral |
0:b65664229800
|
334
|
|
apm_litoral |
0:b65664229800
|
335
|
// CMD9, Response R2 (R1 byte + 16-byte block read)
|
apm_litoral |
0:b65664229800
|
336
|
if(_cmdx(9, 0) != 0) {
|
apm_litoral |
0:b65664229800
|
337
|
fprintf(stderr, "Didn't get a response from the disk\n");
|
apm_litoral |
0:b65664229800
|
338
|
return 0;
|
apm_litoral |
0:b65664229800
|
339
|
}
|
apm_litoral |
0:b65664229800
|
340
|
|
apm_litoral |
0:b65664229800
|
341
|
char csd[16];
|
apm_litoral |
0:b65664229800
|
342
|
if(_read(csd, 16) != 0) {
|
apm_litoral |
0:b65664229800
|
343
|
fprintf(stderr, "Couldn't read csd response from disk\n");
|
apm_litoral |
0:b65664229800
|
344
|
return 0;
|
apm_litoral |
0:b65664229800
|
345
|
}
|
apm_litoral |
0:b65664229800
|
346
|
|
apm_litoral |
0:b65664229800
|
347
|
// csd_structure : csd[127:126]
|
apm_litoral |
0:b65664229800
|
348
|
// c_size : csd[73:62]
|
apm_litoral |
0:b65664229800
|
349
|
// c_size_mult : csd[49:47]
|
apm_litoral |
0:b65664229800
|
350
|
// read_bl_len : csd[83:80] - the *maximum* read block length
|
apm_litoral |
0:b65664229800
|
351
|
|
apm_litoral |
0:b65664229800
|
352
|
int csd_structure = ext_bits(csd, 127, 126);
|
apm_litoral |
0:b65664229800
|
353
|
int c_size = ext_bits(csd, 73, 62);
|
apm_litoral |
0:b65664229800
|
354
|
int c_size_mult = ext_bits(csd, 49, 47);
|
apm_litoral |
0:b65664229800
|
355
|
int read_bl_len = ext_bits(csd, 83, 80);
|
apm_litoral |
0:b65664229800
|
356
|
|
apm_litoral |
0:b65664229800
|
357
|
// printf("CSD_STRUCT = %d\n", csd_structure);
|
apm_litoral |
0:b65664229800
|
358
|
|
apm_litoral |
0:b65664229800
|
359
|
if(csd_structure != 0) {
|
apm_litoral |
0:b65664229800
|
360
|
fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures\n");
|
apm_litoral |
0:b65664229800
|
361
|
return 0;
|
apm_litoral |
0:b65664229800
|
362
|
}
|
apm_litoral |
0:b65664229800
|
363
|
|
apm_litoral |
0:b65664229800
|
364
|
// memory capacity = BLOCKNR * BLOCK_LEN
|
apm_litoral |
0:b65664229800
|
365
|
// where
|
apm_litoral |
0:b65664229800
|
366
|
// BLOCKNR = (C_SIZE+1) * MULT
|
apm_litoral |
0:b65664229800
|
367
|
// MULT = 2^(C_SIZE_MULT+2) (C_SIZE_MULT < 8)
|
apm_litoral |
0:b65664229800
|
368
|
// BLOCK_LEN = 2^READ_BL_LEN, (READ_BL_LEN < 12)
|
apm_litoral |
0:b65664229800
|
369
|
|
apm_litoral |
0:b65664229800
|
370
|
int block_len = 1 << read_bl_len;
|
apm_litoral |
0:b65664229800
|
371
|
int mult = 1 << (c_size_mult + 2);
|
apm_litoral |
0:b65664229800
|
372
|
int blocknr = (c_size + 1) * mult;
|
apm_litoral |
0:b65664229800
|
373
|
int capacity = blocknr * block_len;
|
apm_litoral |
0:b65664229800
|
374
|
|
apm_litoral |
0:b65664229800
|
375
|
int blocks = capacity / 512;
|
apm_litoral |
0:b65664229800
|
376
|
|
apm_litoral |
0:b65664229800
|
377
|
return blocks;
|
apm_litoral |
0:b65664229800
|
378
|
} |