📄 sd.lst
字号:
95 2 DeviceInfo.bFatType = 2; //fat16
96 2
97 2 }
98 1 else
99 1 {
100 2 DeviceInfo.bFatType = 3; //fat32
101 2 DeviceInfo.TotCluster =
102 2 (DeviceInfo.BPB_TotSec32-DeviceInfo.FirstDataSector+1)/DeviceInfo.BPB_SecPerClus+1;
103 2
104 2 }
105 1 //End Judge the FAT type
106 1 }
107 //######################################################
108 unsigned char SDCommand(unsigned char command, unsigned char *pAddr)
109 //######################################################
110 {
111 1 unsigned char i, addr[4];
112 1
113 1 for (i=0;i<4;i++)
114 1 {
115 2 addr[i] = *pAddr;
116 2 pAddr++;
117 2 }
118 1
119 1 SPI_WRITE(0xFF); //Dummy write
120 1 SPI_WRITE(command);
121 1 SPI_WRITE(addr[0]); //MSB of address
122 1 SPI_WRITE(addr[1]);
123 1 SPI_WRITE(addr[2]);
124 1 SPI_WRITE(addr[3]); //LSB of address
125 1 SPI_WRITE(0xFF); //dummy checksum
126 1
127 1 // SPI_WRITE(0xFF);
128 1
129 1 return (1);
130 1 }
131 //
132 unsigned char SDMp3Read(unsigned long sector)
133 //######################################################
134 {
135 1 unsigned int i;
136 1 unsigned long startadr;
137 1
138 1 // SPI_CTL1 = SPI_3M; // set high speed
139 1
140 1 // if(sector>=maxsect) return 0; //sectornumber too big
141 1 BufRst = 0x3; // reset peripheral fifo
142 1 BufRst = 0x0;
143 1
144 1 //calculate startadress of the sector
145 1
146 1 startadr=sector * (unsigned long)BYTE_PER_SEC;
147 1
148 1 SDCommand(SD_READ_BLOCK, (unsigned char *)&startadr);
149 1 do
150 1 {
151 2 SPI_WRITE(0xFF);
152 2 }while(!(SPI_STA0 & SPI_RRDY)); // wait for card response
153 1
154 1 do
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 22
155 1 {
156 2 SPI_WRITE(0xFF);
157 2 }while (SPI_DATA != 0x0); // wait for response token
158 1
159 1 do
160 1 {
161 2 SPI_WRITE(0xFF);
162 2 }while (SPI_DATA & 0x1); // wait for data start
163 1
164 1 for(i=0; i<BYTE_PER_SEC; i++)
165 1 {
166 2 SPI_WRITE(0xFF); // shift out a byte into SPI_DATA
167 2 BufAddr=SPI_DATA; // store byte in buffer
168 2 }
169 1
170 1 SPI_WRITE(0xFF); // 16 bit crc follows data
171 1 SPI_WRITE(0xFF);
172 1 // SPI_CTL1 = SPI_1P5M; // set high speed
173 1 return 1;
174 1 }
175 //######################################################
176 unsigned char SDReadSector(unsigned long sector)
177 //######################################################
178 {
179 1 unsigned int i;
180 1 unsigned long startadr;
181 1
182 1 if(sector>=maxsect) return 1; //sectornumber too big
183 1
184 1 //calculate startadress of the sector
185 1 startadr=sector * (unsigned long)BYTE_PER_SEC;
186 1
187 1 //SendUart((unsigned char *)&startadr, 4);
188 1 SDCommand(SD_READ_BLOCK, (unsigned char *)&startadr);
189 1
190 1 do
191 1 {
192 2 SPI_WRITE(0xFF);
193 2 }while(!(SPI_STA0 & SPI_RRDY)); // wait for card response
194 1
195 1 do
196 1 {
197 2 SPI_WRITE(0xFF);
198 2 }while (SPI_DATA != 0x0); // wait for response token
199 1
200 1 do
201 1 {
202 2 SPI_WRITE(0xFF);
203 2 }while (SPI_DATA & 0x1); // wait for data start
204 1
205 1 for(i=0; i<BYTE_PER_SEC; i++)
206 1 {
207 2 SPI_WRITE(0xFF); // shift out a byte into SPI_DATA
208 2 DBUF[i]=SPI_DATA; // store byte in buffer
209 2 }
210 1
211 1 SPI_WRITE(0xFF); // 16 bit crc follows data
212 1 SPI_WRITE(0xFF);
213 1
214 1 return 1;
215 1 }
216
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 23
217 //######################################################
218 unsigned char SDReadFat(unsigned long sector)
219 //######################################################
220 {
221 1 unsigned int i;
222 1 unsigned long startadr;
223 1
224 1 if(sector>=maxsect) return 1; //sectornumber too big
225 1
226 1 //calculate startadress of the sector
227 1 startadr=sector * (unsigned long)BYTE_PER_SEC;
228 1
229 1 //SendUart((unsigned char *)&startadr, 4);
230 1 SDCommand(SD_READ_BLOCK, (unsigned char *)&startadr);
231 1
232 1 do
233 1 {
234 2 SPI_WRITE(0xFF);
235 2 }while(!(SPI_STA0 & SPI_RRDY)); // wait for card response
236 1
237 1 do
238 1 {
239 2 SPI_WRITE(0xFF);
240 2 }while (SPI_DATA != 0x0); // wait for response token
241 1
242 1 do
243 1 {
244 2 SPI_WRITE(0xFF);
245 2 }while (SPI_DATA & 0x1); // wait for data start
246 1
247 1 for(i=0; i<BYTE_PER_SEC; i++)
248 1 {
249 2 SPI_WRITE(0xFF); // shift out a byte into SPI_DATA
250 2 FATBUF[i]=SPI_DATA; // store byte in buffer
251 2 }
252 1
253 1 SPI_WRITE(0xFF); // 16 bit crc follows data
254 1 SPI_WRITE(0xFF);
255 1
256 1 return 1;
257 1 }
258
259 unsigned char SDReadRdir(unsigned long sector)
260 //######################################################
261 {
262 1 unsigned int i;
263 1 unsigned long startadr;
264 1
265 1 if(sector>=maxsect) return 1; //sectornumber too big
266 1
267 1 //calculate startadress of the sector
268 1 startadr=sector * (unsigned long)BYTE_PER_SEC;
269 1
270 1 //SendUart((unsigned char *)&startadr, 4);
271 1 SDCommand(SD_READ_BLOCK, (unsigned char *)&startadr);
272 1
273 1 do
274 1 {
275 2 SPI_WRITE(0xFF);
276 2 }while(!(SPI_STA0 & SPI_RRDY)); // wait for card response
277 1
278 1 do
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 24
279 1 {
280 2 SPI_WRITE(0xFF);
281 2 }while (SPI_DATA != 0x0); // wait for response token
282 1
283 1 do
284 1 {
285 2 SPI_WRITE(0xFF);
286 2 }while (SPI_DATA & 0x1); // wait for data start
287 1
288 1 for(i=0; i<BYTE_PER_SEC; i++)
289 1 {
290 2 SPI_WRITE(0xFF); // shift out a byte into SPI_DATA
291 2 RDIRBUF[i]=SPI_DATA; // store byte in buffer
292 2 }
293 1
294 1 SPI_WRITE(0xFF); // 16 bit crc follows data
295 1 SPI_WRITE(0xFF);
296 1
297 1 return 1;
298 1 }
299 //######################################################
300 unsigned char SDWriteSector(unsigned long sector, unsigned char *pDBUF)
301 //######################################################
302 {
303 1 unsigned int i;
304 1 unsigned char by;
305 1 unsigned long startadr;
306 1
307 1 if(sector>=maxsect) return 1; //sectornumber too big
308 1
309 1 //calculate startadress
310 1 // startadr=sector * (unsigned long)BYTE_PER_SEC;
311 1 startadr=sector<<9;
312 1
313 1 SDCommand(SD_WRITE_BLOCK,(unsigned char *)&startadr);
314 1
315 1 SPI_WRITE(0xFF); // do we need this TWO dummy writes ?
316 1 SPI_WRITE(0xFF);
317 1
318 1 SPI_WRITE(0xFE); // start block token
319 1
320 1 for(i=0; i<BYTE_PER_SEC; i++)
321 1 {
322 2 SPI_WRITE(*pDBUF++);
323 2 }
324 1
325 1 SPI_WRITE(0xFF); // 16 bit crc follows data
326 1 SPI_WRITE(0xFF);
327 1
328 1 SPI_WRITE(0xFF); // read response
329 1 by=SPI_DATA & 0x1F;
330 1 if(by != 0x05) // data block accepted ?
331 1 {
332 2 return 1;
333 2 }
334 1
335 1 do
336 1 {
337 2 SPI_WRITE(0xFF);
338 2 }while(SPI_DATA !=0xFF); // wait til busy is gone
339 1
340 1 return 0;
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 25
341 1 }
342
343 //######################################################
344 unsigned char SDIdentify(void)
345 //######################################################
346 {
347 1 unsigned char by;
348 1 unsigned int i;
349 1 unsigned long addr;
350 1
351 1 // pCSD_BLOCK pCSD;
352 1
353 1 // SPI_CTL0
354 1 // SPI_LSB=0 Send MSB first
355 1 // SPI_CPOL=1 SCK high if IDLE
356 1 // SPI_CPHA=1 SPI Mode 1
357 1 // SPI_MST=1 I am the master
358 1 // SPI_TIE=0 No SPI TX Interrupt
359 1 // SPI_RIE=0 No SPI RX Interrupt
360 1 // SPI_EN=1 SPI Enable
361 1 // SPI_SSOEN=0 Output enable for select
362 1 SPI_CTL0 = 0x4E;
363 1
364 1 // SPI_CTL1
365 1 SPI_CTL1 = SPI_750K; // set speed
366 1
367 1 for(i=0; i<10; i++) SPI_WRITE(0xFF); // give min 74 clock pulses before
368 1 // sending commands
369 1
370 1 //send CMD0 for RESET
371 1 SPI_WRITE(SD_RESET); //command code CMD0
372 1 SPI_WRITE(0x00);
373 1 SPI_WRITE(0x00);
374 1 SPI_WRITE(0x00);
375 1 SPI_WRITE(0x00);
376 1 SPI_WRITE(0x95); // CMD0 needs a checksum !
377 1 SPI_WRITE(0xFF); // get 16 bit response high
378 1
379 1 while(!(SPI_STA0&SPI_RRDY));
380 1 respBuf[0]=SPI_DATA;
381 1 SPI_WRITE(0xFF);
382 1 while(!(SPI_STA0&SPI_RRDY));
383 1 respBuf[1]=SPI_DATA;
384 1 SPI_WRITE(0xFF);
385 1 while(!(SPI_STA0&SPI_RRDY));
386 1 respBuf[2]=SPI_DATA;
387 1 SPI_WRITE(0xFF);
388 1 while(!(SPI_STA0&SPI_RRDY));
389 1 respBuf[3]=SPI_DATA;
390 1 SPI_WRITE(0xFF);
391 1 while(!(SPI_STA0&SPI_RRDY));
392 1 respBuf[4]=SPI_DATA;
393 1 SPI_WRITE(0xFF);
394 1 while(!(SPI_STA0&SPI_RRDY));
395 1 respBuf[5]=SPI_DATA;
396 1 SPI_WRITE(0xFF);
397 1 while(!(SPI_STA0&SPI_RRDY));
398 1 respBuf[6]=SPI_DATA;
399 1 SPI_WRITE(0xFF);
400 1 while(!(SPI_STA0&SPI_RRDY));
401 1 respBuf[7]=SPI_DATA;
402 1 SPI_WRITE(0xFF);
C51 COMPILER V8.02 SD 09/19/2006 15:01:24 PAGE 26
403 1
404 1 SendUart(respBuf, 8);
405 1 Dela
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -