The first thing to understand is that an SD card is made up from a series of sectors. Each sector is 512 bytes in size (almost always for SD cards) and the SD card likes to read and write entire sectors at a time.
not to scale ;-)
Reading data is a case of sending the appropriate "command packet" - 6 bytes which are:
- the command byte (CMD17)
- 4 parameter bytes (the address of the sector to read)
- two CRC bytes (an accurate crc value is not actually necessary in SPI mode, but two bytes still need to be sent)
It is possible to read multiple sectors of data, one after the other, and use the sd card like one massive serial eeprom, but for now, we're going to just work one sector at a time (this approach is much better suited to working with FAT16-based files, which we'll discuss later).
The command value to read a single sector of data is CMD17. After sending the command, we have to check for an "ok" response from the sd card then wait for the "not-busy" response (any non-0xFF value).
Boolean sdReadBlock(UInt32 sec){
UInt8 v;
UARTPrint("Starting to read block ");
UARTPrintNumber(sec);
UARTPrintLn(" ");
v = sdCommandAndResponse (17, ((UInt32)sec) << 9);
if(v & FLAG_TIMEOUT) {
UARTPrintLn("start read block command 17 timeout");
fatal(1);
return false;
}else{
do{
v = sdSpiByte(0xFF);
}while(v == 0xFF);
if(v != 0xFE) {
UARTLog("Read block response",v);
fatal(2);
return false;
}
curPos=0;
return true;
}
}
This function accepts a single 4-byte parameter, the sector address to open, and returns true or false to indicate whether the card responded correctly.
It also resets a counter curPos, which will keep track of the number of bytes we've read from the current sector on the disk. When we've had exactly 512 bytes back from the disk, we'll have to "close" the current sector by reading back the two bytes that make up the crc for the previous 512-byte data stream.
void sdSecReadStop(){
if(curPos<512){
UARTPrint("add stuff bytes to close sector - ");
UARTPrintNumber(curPos);
UARTPrintLn(" ");
}
while(curPos < 512){
sdSpiByte(0xFF);
curPos++;
}
// read back the two CRC bytes
UARTPrint("CRC ");
r=readByte();
UARTByte(r);
UARTPrint(" ");
r=readByte();
UARTByte(r);
UARTPrintLn(" ");
UARTPrintLn("End read sector");
UARTPrintLn(" ");
}
Now we can open a sector and close it again, the last piece of the puzzle is to stream the data back from the sd card and do something with it! When we come to make our audio player, we'll use the data immediately - sending it to the speaker - but for testing, or for more general purpose use, we'll put the data into a series of buffers and use it later.
Now - here's a thing.
So far, everything discussed can be ported to another platform (AVR/Ardunio for example) but when it comes to storing the data in our internal buffers, there's a peculiarity with the PIC microcontroller - or at least, with most compilers that support using arrays. It may be the same with Arduino and/or other compilers, but we've always found that large arrays are often difficult to use.
This is because of the way PICs store data in RAM - it uses "banks" to keep the data in memory, and one array can't usually span more than one bank of data. In practice, this means a limit of about 90 elements in an array.
To keep things simple, we're going to store an entire sector's worth of data (512 bytes) in 8 eight arrays of 64 elements.
unsigned char wavData1[64];
unsigned char wavData2[64];
unsigned char wavData3[64];
unsigned char wavData4[64];
unsigned char wavData5[64];
unsigned char wavData6[64];
unsigned char wavData7[64];
unsigned char wavData8[64];
unsigned char readSector(){
unsigned short cy;
unsigned short cv;
r=sdReadBlock(iSector);
for(cy=0; cy < 512; cy++){
r=readByte();
UARTByte(r); //write to serial for debugging
if(cy< 64){
cv=cy;
wavData1[cv]=r;
}else if(cy<128){
cv=cy- 64;
wavData2[cv]=r;
}else if(cy<192){
cv=cy-128;
wavData3[cv]=r;
}else if(cy<256){
cv=cy-192;
wavData4[cv]=r;
}else if(cy<320){
cv=cy-256;
wavData5[cv]=r;
}else if(cy<384){
cv=cy-320;
wavData6[cv]=r;
}else if(cy<448){
cv=cy-384;
wavData7[cv]=r;
}else if(cy<512){
cv=cy-448;
wavData8[cv]=r;
}
}
sdSecReadStop();
UARTPrintLn(" ");
return(1);
}
Now we can read a sector-ful of data, it's time to write some data back.
There are any number of ways you can get data into the PIC/microcontroller in order to write them to the sd card. We'll leave that side of things for another post - for now, here are some functions to write data to a specific sector on the card.
The command to write a single sector-ful of data to the card is CMD24
(you can write an entire stream across multiple sectors if you're using the card as a large serial eeprom chip, but this makes things difficult if we're going to work with FAT16 formatted cards in future).
When writing data, the 6-byte data packet consists of:
- single command byte
- four parameter bytes (sector to write to)
- single crc byte
- a single byte representing a token confirming the command request
Boolean sdWriteBlockStart(UInt32 sec){
UInt8 v;
v = sdCommandAndResponse(24, ((UInt32)sec) << 9);
if(v) {
UARTLog("write block start response",v);
fatal(1);
return false;
}else{
UARTPrintLn("write block started");
// keep track of how many bytes we've written in this sector
// (when we hit 512 we should expect some extra bytes in the packet data)
bytesWritten=0;
// send the correct token for CMD17/18/24 (0xFE)
// REMEMBER the token for CMD25 is 0xFC
r=sdSpiByte(0xFE);
return true;
}
}
Once the card has responded with an "ok" after starting to write a sector, the next bytes streamed to the card are written to the selected sector. We need to keep track of the number of bytes written - when we hit 512, it's time to close the current sector, read back the crc bytes, then open another - you don't have to write to the sequentially next sector every time, you can write to any old location if you like!
Boolean sdWriteByteToSector(UInt8 b){
UInt8 ix;
r=sdSpiByte(b);
bytesWritten++;
}
Boolean sdWriteToSectorClose(){
// finish closing the sector
UARTPrintLn("writing stuff bytes to close sector");
while(bytesWritten<512){
sdSpiByte(0xFF);
bytesWritten++;
}
// send two CRC bytes
sdSpiByte(0x00);
sdSpiByte(0x00);
// response should immediately follow
// (for writing, we're looking for 0bXXX00101 data accepted)
r=sdSpiByte(0x00);
UARTLog("write finish response",r);
// now wait for the sd card to come out of busy
while(r!=0x00){
r=sdSpiByte(0x00);
}
UARTPrintLn("write finished");
}
// write some data to sector numbered five:
void writeSomeData(){
UARTPrintLn("Writing one block");
ret=sdWriteBlockStart(0x05);
if(!ret){
fatal(3);
}else{
for(iy=0; iy<=255; iy++){
sdWriteByteToSector((255-iy));
}
sdWriteByteToSector(0x10);
sdWriteByteToSector(0x11);
sdWriteByteToSector(0x12);
sdWriteByteToSector(0x13);
sdWriteByteToSector(0x14);
sdWriteToSectorClose();
}
}
That's pretty much it.
We can now read and write to an individual sector on the sd card.
There are commands for reading and writing to/from multiple blocks at a time, but we'll leave those for again; for FAT formatted cards, we need to be able to read data out-of-sequence and possibly even write files into different (non-consecutive) sectors.
No comments:
Post a Comment