-
Notifications
You must be signed in to change notification settings - Fork 2
/
SdCard.cpp
337 lines (277 loc) · 7.82 KB
/
SdCard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Arduino FAT16 Library
* Copyright (C) 2008 by William Greiman
*
* This file is part of the Arduino FAT16 Library
*
* This Library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with the Arduino Fat16 Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "SdCard.h"
//#include <inttypes.h>
#include <stdint.h>
#include <avr/io.h>
#include "Fat16Config.h"
#include "mSPI.h"
/*
* RAMSTART should be self-explanatory. It's bigger on parts with a
* lot of peripheral registers. Let 0x100 be the default
* Note that RAMSTART (for optiboot) need not be exactly at the start of RAM.
*/
#if !defined(RAMSTART) // newer versions of gcc avr-libc define RAMSTART
#define RAMSTART 0x100
#if defined (__AVR_ATmega644P__)
// correct for a bug in avr-libc
#undef SIGNATURE_2
#define SIGNATURE_2 0x0A
#elif defined(__AVR_ATmega1280__)
#undef RAMSTART
#define RAMSTART (0x200)
#endif
#endif
/* C zero initialises all global variables. However, that requires */
/* These definitions are NOT zero initialised, but that doesn't matter */
/* This allows us to drop the zero init code, saving us memory */
#define buff ((uint8_t*)(RAMSTART))
//------------------------------------------------------------------------------
#if 0//////////////////////////////////////////////////////////////////////////////////////////////////
// r1 status values
uint8_t const R1_READY_STATE = 0;
uint8_t const R1_IDLE_STATE = 1;
// start data token for read or write
uint8_t const DATA_START_BLOCK = 0XFE;
// data response tokens for write block
uint8_t const DATA_RES_MASK = 0X1F;
uint8_t const DATA_RES_ACCEPTED = 0X05;
uint8_t const DATA_RES_CRC_ERROR = 0X0B;
uint8_t const DATA_RES_WRITE_ERROR = 0X0D;
#endif////////////////////////////////////////////////////////////////////////////////////////////////////
//
// stop compiler from inlining where speed optimization is not required
//#define STATIC_NOINLINE static __attribute__((noinline))
uint8_t spiReceive(void) {
return SPI_transfer(0xff);
}
bool waitNotBusy(void) {
uint16_t count = 0;
while (spiReceive() != 0xff) {
if ( count >= SD_MAX_TRANSFERTS ) return false;
}
return true;
}
void chipSelectHigh(void) {
SD_SS_PORT_REG |= _BV(SD_SS_PIN);
}
void chipSelectLow(void) {
SD_SS_PORT_REG &= ! _BV(SD_SS_PIN);
}
uint8_t cardCommand(uint8_t cmd, uint32_t arg) {
/* wait not busy */
if (cmd != CMD0) {
waitNotBusy();
}
// send command
SPI_transfer(cmd | 0x40);
// send argument
uint8_t *pa = reinterpret_cast<uint8_t *>(&arg);
for (int8_t i = 3; i >= 0; i--) {
SPI_transfer(pa[i]);
}
// send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA
SPI_transfer(cmd == CMD0 ? 0X95 : 0X87);
// discard first fill byte to avoid MISO pull-up problem.
spiReceive();
// there are 1-8 fill bytes before response. fill bytes should be 0XFF.
uint8_t status;
for (uint8_t i = 0; ((status = spiReceive()) & 0X80) && i < 10; i++) {
}
return status;
}
uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
cardCommand(CMD55, 0);
return cardCommand(cmd, arg);
}
uint8_t SdCard_begin(void) {
uint8_t status;
// 16-bit init start time allows over a minute
uint16_t count = 0;
uint32_t arg;
uint8_t cardType;
// initialize SPI bus and chip select pin.
DDRB = _BV(2) | _BV(3) | _BV(5); //SS, MOSI, SCK
SD_SS_DDR_REG |= _BV(SD_SS_PIN); //SD SS as OUTPUT
chipSelectHigh();
// set SCK rate for initialization commands.
SPCR = SPI_SPCR_CONFIG | SPI_SCK_INIT_DIVISOR;
SPSR = 0x01; //enable SPI2X
chipSelectLow();
// must supply min of 74 clock cycles with CS high.
chipSelectHigh();
for (uint8_t i = 0; i < 10; i++) {
spiReceive();
}
// command to go idle in SPI mode
chipSelectLow();
while (cardCommand(CMD0, 0) != R1_IDLE_STATE) {
count++;
if( count > SD_MAX_CMD0_TRIES ) {
goto fail;
}
}
// check SD version
if (cardCommand(CMD8, 0x1AA) == (R1_ILLEGAL_COMMAND | R1_IDLE_STATE)) {
cardType = CARD_TYPE_SDV1;
} else {
for (uint8_t i = 0; i < 4; i++) {
status = spiReceive();
}
if (status == 0XAA) {
cardType = CARD_TYPE_SDV2;
} else {
goto fail;
}
}
// initialize card and send host supports SDHC if SD2
arg = cardType == CARD_TYPE_SDV2 ? 0X40000000 : 0;
count = 0;
while (cardAcmd(ACMD41, arg) != R1_READY_STATE) {
count++;
if( count > SD_MAX_COMMANDS ) {
goto fail;
}
}
// if SD2 read OCR register to check for SDHC card
if (cardType == CARD_TYPE_SDV2) {
if (cardCommand(CMD58, 0)) {
goto fail;
}
if ((spiReceive() & 0XC0) == 0XC0) {
cardType = CARD_TYPE_SDHC;
}
// Discard rest of ocr - contains allowed voltage range.
for (uint8_t i = 0; i < 3; i++) {
spiReceive();
}
}
/* reset SPI clock */
SPCR = SPI_SPCR_CONFIG;
chipSelectHigh();
spiReceive();
return cardType;
fail:
chipSelectHigh();
spiReceive();
return 0;
}
//------------------------------------------------------------------------------
/**
* Reads a 512 byte block from a storage device.
*
* \param[in] blockNumber Logical block to be read.
* \param[out] dst Pointer to the location that will receive the data.
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
bool SdCard_readBlock(uint32_t blockNumber, uint8_t cardType) {
uint8_t status;
uint16_t count;
/* start SPI */
chipSelectLow();
/* get block number */
if (cardType != CARD_TYPE_SDHC) {
blockNumber <<= 9;
}
if (cardCommand(CMD17, blockNumber)) {
goto fail;
}
/********/
/* read */
/********/
// wait for start block token
count = 0;
while ((status = spiReceive()) == 0XFF) {
count++;
if (count > SD_MAX_TRANSFERTS) {
goto fail;
}
}
if (status != DATA_START_BLOCK) {
goto fail;
}
// transfer data
for (int i = 0; i < 512; i++) {
buff[i] = spiReceive();
}
// discard crc
spiReceive();
spiReceive();
// ok
chipSelectHigh();
spiReceive();
return true;
fail:
chipSelectHigh();
spiReceive();
return false;
}
//------------------------------------------------------------------------------
/**
* Writes a 512 byte block to a storage device.
*
* \param[in] blockNumber Logical block to be written.
* \param[in] src Pointer to the location of the data to be written.
* \return The value one, true, is returned for success and
* the value zero, false, is returned for failure.
*/
bool SdCard_writeBlock(uint32_t blockNumber, uint8_t cardType) {
uint8_t status;
/* start SPI */
chipSelectLow();
/* set block number */
if (cardType != CARD_TYPE_SDHC) {
blockNumber <<= 9;
}
if (cardCommand(CMD24, blockNumber)) {
goto fail;
}
/*********/
/* write */
/*********/
SPI_transfer(DATA_START_BLOCK);
for (int i = 0; i < 512; i++) {
SPI_transfer(buff[i]);
}
spiReceive();
spiReceive();
status = spiReceive();
if ((status & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
goto fail;
}
/**********************/
/* flush cache buffer */
/**********************/
if ( !waitNotBusy() ) {
goto fail;
}
// response is r2 so get and check two bytes for nonzero
if (cardCommand(CMD13, 0) || spiReceive()) {
goto fail;
}
// ok
chipSelectHigh();
spiReceive();
return true;
fail:
chipSelectHigh();
spiReceive();
return false;
}