This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SD.c
288 lines (266 loc) · 6.75 KB
/
SD.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "SD.h"
#include <avr/io.h>
#include <inttypes.h>
#include <stdio.h>
#include "types.h"
#define SD_CONFIG_SLOW() do{SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<SPR1);\
SPSR=(1<<SPI2X);\
}while(0);
//Enable SPI,Master,SPR0,SPR1
//Speedx2 with SPR0,SPR1 Fsck=Fosc/64
#define SD_CONFIG_FAST() do{SPCR=(1<<SPE)|(1<<MSTR);\
SPSR=(1<<SPI2X);\
}while(0);
//Enable SPI,Master
//Speedx2 without SPR0,SPR1 Fsck=Fosc/2
uint8_t CMD0[6]={0x40,0x00,0x00,0x00,0x00,0x95};
uint8_t CMD1[6]={0x41,0x00,0x00,0x00,0x00,0xFF};
uint8_t CMD9[6] = {0x40|9,0x00,0x00,0x00,0x00,0xFF};
uint8_t CMD24[6]={0x40|24,0x00,0x00,0x00,0x00,0xFF};
uint8_t CMD17[6] = {0x40|17,0x00,0x00,0x00,0x00,0xFF};
void spi_init(void)
{
DDRB|=1<<4;//CS片选
DDRB|=1<<5;//MOSI
DDRB&=~(1<<6);//MISO
DDRB|=1<<7;//SCK
SD_CONFIG_SLOW();
//Be slower when init
PORTB|=(1<<4)|(1<<5)|(1<<7);
}
uint8_t send(uint8_t dat)//发送数据
{
uint8_t dat1=0;
SPDR=dat;
while(!(SPSR&(1<<7)));//等待数据发送完成
dat1=SPDR;
return dat1;
}
uint8_t send_cmd(uint8_t *CMD)//发送命令
{
uint8_t i = 0,dat;
PORTB|=(1<<4);//CS=0;
send(0xff);//一定要有这一步,否则可能初始化不能成功
PORTB&=~(1<<4);
send(CMD[0]);
send(CMD[1]);
send(CMD[2]);
send(CMD[3]);
send(CMD[4]);
send(CMD[5]);
do
{
dat=send(0xff);//读回命令的响应
i++;
}while((i<200)&&(dat==0xff));
return dat;
}
UInt32 sdPrvGetBits(UInt8* data,UInt32 numBytesInArray,UInt32 startBit,UInt32 len){//for CID and CSD data..
UInt32 bitWrite = 0;
UInt32 numBitsInArray = numBytesInArray * 8;
UInt32 ret = 0;
do{
UInt32 bit,byte;
bit = numBitsInArray - startBit - 1;
byte = bit / 8;
bit = 7 - (bit % 8);
ret |= ((data[byte] >> bit) & 1) << (bitWrite++);
startBit++;
}while(--len);
return ret;
}
UInt32 sdPrvGetCardNumBlocks(UInt8* csd){
UInt32 ver = sdPrvGetBits(csd,16,126,2);
UInt32 mmcver = sdPrvGetBits(csd,16,122,4);
//mmcver == 0 SD (In SD,it's reserved)
//mmcver > 0 MMC
UInt32 cardSz = 0;
//printf("Ver:%lu MMCVer:%lu\n",ver,mmcver);
if(ver == 0||((mmcver > 0)&&ver<=3)){
UInt32 cSize = sdPrvGetBits(csd,16,62,12);
UInt32 cSizeMult = sdPrvGetBits(csd,16,47,3);
UInt32 readBlLen = sdPrvGetBits(csd,16,80,4);
UInt32 blockLen,blockNr;
UInt32 divTimes = 9; //from bytes to blocks division
blockLen = 1UL << readBlLen;
blockNr = (cSize + 1) * (1UL << (cSizeMult + 2));
/*
multiplying those two produces result in bytes, we need it in blocks
so we shift right 9 times. doing it after multiplication might fuck up
the 4GB card, so we do it before, but to avoid killing significant bits
we only cut the zero-valued bits, if at the end we end up with non-zero
"divTimes", divide after multiplication, and thus underuse the card a bit.
This will never happen in reality since 512 is 2^9, and we are
multiplying two numbers whose product is a multiple of 2^9, so they
togethr should have at least 9 lower zero bits.
*/
while(divTimes && !(blockLen & 1)){
blockLen = blockLen >> 1;
divTimes--;
}
while(divTimes && !(blockNr & 1)){
blockNr = blockNr >> 1;
divTimes--;
}
cardSz = (blockNr * blockLen) >> divTimes;
}
else if(ver == 1){
cardSz = sdPrvGetBits(csd,16,48,22)/*num 512K blocks*/ << 10;
}else{
printf("Card version do not support:\nCSD Version:0x%lx MMC Version:0x%lx",ver,mmcver);
}
return cardSz;
}
int64_t MMC_init(){
uint8_t n;//MMC卡的初始化可能要多次发送命令才会初始化成功
uint16_t m;
uint8_t csd[16];
PORTB&=~(1<<4);
for(m=0;m<0x0f;m++)
{
send(0xff);
}//上电发送大于74个时钟 16*8
do
{
n=send_cmd(CMD0);//发送CMD0复位,响应R1=0x01
m++;
}while((m<200)&&(n!=0x01));
if(m == 200){
PORTB|=(1<<4);
return -1;
}
m=0;
do
{
n=send_cmd(CMD1);//发送CMD1激活卡,响应R1=0x00
m++;
}while((m<2000)&&(n!=0x00));
PORTB|=(1<<4);
if(m == 2000){
return -2;
}
PORTB&=~(1<<4);
n = send_cmd(CMD9);
if(n != 0x00){
PORTB|=(1<<4);
return -3;
}
for(n = 0;n<16;n++){
csd[n] = send(0xFF);
}
PORTB|=(1<<4);
SD_CONFIG_FAST();
return sdPrvGetCardNumBlocks(csd);//初始化成功
}
/*
uint8_t write_byte(void)//
{
uint16_t i;
uint8_t dat;
CMD24[0]=0x58;
CMD24[1]=0x00;
CMD24[2]=0x00;
CMD24[3]=0x00;
CMD24[4]=0x00;
CMD24[5]=0xff;
send_cmd(CMD24);//命令只要发送一次就可了当初始化成功的时候
send(0xff);//填充时钟
send(0xfe);//发送起始令牌
for(i=0;i<512;i++)
{
send(buf[i]);
}
send(0xff);
send(0xff);//两个字节的校检字节
dat=send(0xff);
return dat;
}
void read_byte(void)//读数据块命令成功
{
uint16_t i;
CMD17[0]=0x40+17;
CMD17[1]=0x00;
CMD17[2]=0x00;
CMD17[3]=0x00;
CMD17[4]=0x00;
CMD17[5]=0xff;
send_cmd(CMD17);//发送读数据命令的时候,只要发一次,不能多发否则会出现各种问题
while(send(0xff)!=0xfe);//等待第一个数据令牌
for(i=0;i<512;i++)
{
buf[i]=send(0xff);
}
send(0xff);
send(0xff);//两个字节的校检字节
}
*/
Boolean sdInit(SD* sd){
int64_t mmc_init_return_value;
spi_init();
mmc_init_return_value = MMC_init();
if(mmc_init_return_value < 0){
sd->numSec = 0;
return false;
}
sd->numSec = mmc_init_return_value;
//sd->numSec = 512UL*1024*1024/512;//512Mb
sd->inited = 1;
printf("SD size:%lu\n",(sd->numSec)<<9);
return true;
#ifdef PRINT_DEBUG_TEXT
printf("Init Successfully\n");
#endif
return true;
}
uint32_t sdGetNumSec(SD* sd){
return sd->numSec;//512Mb
}
Boolean sdSecRead(SD* sd, UInt32 sec, void* buf){
if(sec >= (sd->numSec)){
printf("SD Read:Out of range Sector:%lu Sector number on SD card:%lu\n",sec,sd->numSec);
return false;
//Read sector is out of range
}
uint16_t i;
CMD17[1]=((uint64_t)sec<<9UL)>>24UL;
CMD17[2]=((uint64_t)sec<<9UL)>>16UL;
CMD17[3]=((uint64_t)sec<<9UL)>>8UL;
CMD17[4]=(uint64_t)sec<<9UL;//Sector to Address
send_cmd(CMD17);//发送读数据命令的时候,只要发一次,不能多发否则会出现各种问题
while(send(0xff)!=0xfe);//等待第一个数据令牌
for(i=0;i<512;i++)
{
*(uint8_t *)buf++=send(0xff);
}
send(0xFF);
send(0xFF);//两个字节的校检字节
return true;
}
Boolean sdSecWrite(SD* sd, UInt32 sec, void* buf){
if(sec >= (sd->numSec)){
printf("SD Write:Out of range Sector:%lu Sector number on SD card:%lu\n",sec,sd->numSec);
return false;
//Read sector is out of range
}
uint16_t i;
uint8_t dat;
CMD24[1]=((uint64_t)sec<<9UL)>>24UL;
CMD24[2]=((uint64_t)sec<<9UL)>>16UL;
CMD24[3]=((uint64_t)sec<<9UL)>>8UL;
CMD24[4]=(uint64_t)sec<<9UL;//Sector to Address
send_cmd(CMD24);//命令只要发送一次就可了当初始化成功的时候
send(0xFF);//填充时钟
send(0xFE);//发送起始令牌
for(i=0;i<512;i++)
{
send(*(uint8_t *)buf++);
}
send(0xff);
send(0xff);//两个字节的校检字节
dat=send(0xff);
if(dat == 0x05){
return true;
}else{
return false;
}
}