-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSD_routines.c
executable file
·273 lines (213 loc) · 7.96 KB
/
SD_routines.c
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
/*
SD_routines.c
SD Routines in the PETdisk storage device
Copyright (C) 2011 Michael Hill
This program 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 program 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 this program. If not, see <http://www.gnu.org/licenses/>.
Contact the author at bitfixer@bitfixer.com
http://bitfixer.com
SD routines inspired by CC Dharmani's microcontroller blog
http://dharmanitech.com
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "SPI_routines.h"
#include "SD_routines.h"
#include "UART_routines.h"
//******************************************************************
//Function : to initialize the SD/SDHC card in SPI mode
//Arguments : none
//return : unsigned char; will be 0 if no error,
// otherwise the response byte will be sent
//******************************************************************
unsigned char SD_init(void)
{
unsigned char i, response, SD_version;
unsigned int retry = 0 ;
for(i = 0; i < 10; i++)
SPI_transmit(0xff); //80 clock pulses spent before sending the first command
SD_CS_ASSERT;
do
{
response = SD_sendCommand(GO_IDLE_STATE, 0); //send 'reset & go idle' command
retry++;
if(retry>0x20)
return 1; //time out, card not detected
//sprintf(test, "** %02X", response);
//transmitString(test);
} while(response != 0x01);
SD_CS_DEASSERT;
SPI_transmit (0xff);
SPI_transmit (0xff);
retry = 0;
SD_version = 2; //default set to SD compliance with ver2.x;
//this may change after checking the next command
do
{
response = SD_sendCommand(SEND_IF_COND,0x000001AA); //Check power supply status, mendatory for SDHC card
retry++;
if(retry>0xfe)
{
TX_NEWLINE;
SD_version = 1;
_cardType = 1;
break;
} //time out
}while(response != 0x01);
retry = 0;
do
{
response = SD_sendCommand(APP_CMD,0); //CMD55, must be sent before sending any ACMD command
response = SD_sendCommand(SD_SEND_OP_COND,0x40000000); //ACMD41
retry++;
if(retry>0xfe)
{
TX_NEWLINE;
return 2; //time out, card initialization failed
}
}while(response != 0x00);
retry = 0;
_SDHC_flag = 0;
if (SD_version == 2)
{
do
{
response = SD_sendCommand(READ_OCR,0);
retry++;
if(retry>0xfe)
{
TX_NEWLINE;
_cardType = 0;
break;
} //time out
}while(response != 0x00);
if(_SDHC_flag == 1) _cardType = 2;
else _cardType = 3;
}
//SD_sendCommand(CRC_ON_OFF, OFF); //disable CRC; deafault - CRC disabled in SPI mode
//SD_sendCommand(SET_BLOCK_LEN, 512); //set block size to 512; default size is 512
return 0; //successful return
}
//******************************************************************
//Function : to send a command to SD card
//Arguments : unsigned char (8-bit command value)
// & unsigned long (32-bit command argument)
//return : unsigned char; response byte
//******************************************************************
unsigned char SD_sendCommand(unsigned char cmd, unsigned long arg)
{
unsigned char response, retry=0, status;
//SD card accepts byte address while SDHC accepts block address in multiples of 512
//so, if it's SD card we need to convert block address into corresponding byte address by
//multipying it with 512. which is equivalent to shifting it left 9 times
//following 'if' loop does that
if(_SDHC_flag == 0)
if(cmd == READ_SINGLE_BLOCK ||
cmd == READ_MULTIPLE_BLOCKS ||
cmd == WRITE_SINGLE_BLOCK ||
cmd == WRITE_MULTIPLE_BLOCKS ||
cmd == ERASE_BLOCK_START_ADDR||
cmd == ERASE_BLOCK_END_ADDR )
{
arg = arg << 9;
}
SD_CS_ASSERT;
SPI_transmit(cmd | 0x40); //send command, first two bits always '01'
SPI_transmit(arg>>24);
SPI_transmit(arg>>16);
SPI_transmit(arg>>8);
SPI_transmit(arg);
if(cmd == SEND_IF_COND) //it is compulsory to send correct CRC for CMD8 (CRC=0x87) & CMD0 (CRC=0x95)
SPI_transmit(0x87); //for remaining commands, CRC is ignored in SPI mode
else
SPI_transmit(0x95);
while((response = SPI_receive()) == 0xff) //wait response
if(retry++ > 0xfe) break; //time out error
if(response == 0x00 && cmd == 58) //checking response of CMD58
{
status = SPI_receive() & 0x40; //first byte of the OCR register (bit 31:24)
if(status == 0x40) _SDHC_flag = 1; //we need it to verify SDHC card
else _SDHC_flag = 0;
SPI_receive(); //remaining 3 bytes of the OCR register are ignored here
SPI_receive(); //one can use these bytes to check power supply limits of SD
SPI_receive();
}
SPI_receive(); //extra 8 CLK
SD_CS_DEASSERT;
return response; //return state
}
//******************************************************************
//Function : to read a single block from SD card
//Arguments : none
//return : unsigned char; will be 0 if no error,
// otherwise the response byte will be sent
//******************************************************************
unsigned char SD_readSingleBlock(unsigned long startBlock)
{
unsigned char response;
unsigned int i, retry=0;
response = SD_sendCommand(READ_SINGLE_BLOCK, startBlock); //read a Block command
if(response != 0x00) return response; //check for SD status: 0x00 - OK (No flags set)
SD_CS_ASSERT;
retry = 0;
while(SPI_receive() != 0xfe) //wait for start block token 0xfe (0x11111110)
if(retry++ > 0xfffe){SD_CS_DEASSERT; return 1;} //return if time-out
for(i=0; i<512; i++) //read 512 bytes
_buffer[i] = SPI_receive();
SPI_receive(); //receive incoming CRC (16-bit), CRC is ignored here
SPI_receive();
SPI_receive(); //extra 8 clock pulses
SD_CS_DEASSERT;
return 0;
}
//******************************************************************
//Function : to write to a single block of SD card
//Arguments : none
//return : unsigned char; will be 0 if no error,
// otherwise the response byte will be sent
//******************************************************************
unsigned char SD_writeSingleBlock(unsigned long startBlock)
{
unsigned char response;
unsigned int i, retry=0;
response = SD_sendCommand(WRITE_SINGLE_BLOCK, startBlock); //write a Block command
if(response != 0x00)
return response; //check for SD status: 0x00 - OK (No flags set)
SD_CS_ASSERT;
SPI_transmit(0xfe); //Send start block token 0xfe (0x11111110)
for(i=0; i<512; i++) //send 512 bytes data
SPI_transmit(_buffer[i]);
SPI_transmit(0xff); //transmit dummy CRC (16-bit), CRC is ignored here
SPI_transmit(0xff);
response = SPI_receive();
if( (response & 0x1f) != 0x05) //response= 0xXXX0AAA1 ; AAA='010' - data accepted
{ //AAA='101'-data rejected due to CRC error
SD_CS_DEASSERT; //AAA='110'-data rejected due to write error
return response;
}
while(!SPI_receive()) //wait for SD card to complete writing and get idle
if(retry++ > 0xfffe)
{
SD_CS_DEASSERT;
return 1;
}
SD_CS_DEASSERT;
SPI_transmit(0xff); //just spend 8 clock cycle delay before reasserting the CS line
SD_CS_ASSERT; //re-asserting the CS line to verify if card is still busy
while(!SPI_receive()) //wait for SD card to complete writing and get idle
if(retry++ > 0xfffe)
{
SD_CS_DEASSERT;
return 1;
}
SD_CS_DEASSERT;
return 0;
}