Go Down

Topic: Sd card read/write sector library (Read 9926 times) previous topic - next topic

carl47

This class was written for the Arduino library.

It reads and writes directly to sectors on a sd card.
It's use is for mass storage on the arduino platform.
It's not to store data and then load it(without a special program) to a PC.

Import the library:

Quote

#include <SDCARD.h>



It has only two functions:

Quote

SDCARD.readblock(sector number); //read 512 bytes from this sector
SDCARD.writeblock(sector number); //write 512 bytes to this sector



The 512 bytes are read/write from a buffer which must be declared as global:

Quote

unsigned char buffer[512] ; //contains the 512 bytes to read/write



Operational notes:
 
The code reads/writes direct to the sectors on the sd card.
It does not use a FAT(always FAT16).
If a 2GB card has been formatted the partition(sector 0), boot (around sector 135),
FAT's(around sectors 100-600) and the root directory(around 600-700) can be written over.
            
The data in files(above sector 700) can also be written over.

The card can be reformated, but be aware that if the partition(sector 0)
has been written over formating with windows XP or 7 will not restore it.
It will put the boot sector at 0 which can confuse some(not all) programs.

The playground has SDuFAT-basic - library to handle SD cards from Arduino compatible platforms.
This program cannot run when the boot sector is 0.

I have found that my cannon digital camera will restore the partition
and boot to the origional factory conditions.
No doubt there are other format programs which will use a partition sector.

I have previously described this library class.

http://arduino.cc/forum/index.php/topic,8133.0.html

I have made several improvements to the code. It is now faster and can handle
a greater range of sd cards.

It can handle Ver2.00 or later Standard SD Memory Cards.
256 MB, 1.0 and 2.0 GB cards purchased from 2008-2011 work well.

The library and the circuit are in SDCARDsectors.zip
Download if from:

http://code.google.com/p/mysudoku/downloads/list

Paste the folder SDCARD into the library:
C:\arduino-0021\arduino-0021\libraries

Then from the IDE under:

file-examples-SDCARD

You should see two programs:
checksdcard
readwritesector


tecmac

#1
Feb 22, 2011, 04:39 pm Last Edit: Feb 22, 2011, 04:44 pm by tecmac Reason: 1
Hey carl47,

Many thanks for this library, almost exactly what I was looking but I had to make some minor modifications to suit my needs:

1. Be able to use any pin for SS that I choose, not just D10.
2. Be able to use the existing SPI library instead of implementing its own SPI comms code.

In case anyone needs the same requirements I had, I packaged my changes on to a modded version of carl47 library called SDCARDmodded ;-)

Main visible changes are:

SDCARDmodded.readblock(sector number, port); //read 512 bytes from this sector, port is the SS pin (any pin you choose)
SDCARDmodded.writeblock(sector number, port); //write 512 bytes to this sector, port is the SS pin (any pin you choose)
and sketch will require an #include <sdi.h> to run.

Read description and get modded library from here: http://code.google.com/p/wifispeak/downloads/detail?name=SDCARDmodded.zip&can=2&q=

Hope it helps,
Cheers and thanks carl47
Rui
--

carl47

Happy it worked for you.

Your mods are a definate improvement.
I had no end of trouble trying to expain how to change my code to someone who needed a different SS pin.
You have done it very well.

Like your blog.
cheers.

balajitenetchat

I have updated a small changed code reading analog values from sensors and update in sd card with 10 files.
I have already posted in the below link.
http://arduino.cc/forum/index.php/topic,54014.0.html

Regards,
Bala
Tenet Technetronics

pieturp

Hmmpf!

This library looks more then very interesting, but it doesn't seem to work here. Also the modded version mentioned above is not giving anything usefull.

The example "checksdcard" fails at various different start sectors I tried; always returning error 2 (Failed to initialize)

The weird thing is, my SD cards (one brand new Sandisk 2GB / and an older Toshiba/Kingston 1GB) are perfectly readable with the relatively huge Arduino SD lib. There I'm able to create files and read them back. That library is, unfortunately, way too big for my already heavy sketch. Also; I only need raw access, nothing fancy. I just need too write a huge amount of data once and  then "play" that back.

I don't necessarily need to do a lowlevel format or something on the card, do I?

Any ideas?


Also, could it be that my cards aren't fully compatible or something? Any hints for alternative brands?

carl47

I am disappointed that it does not work for you.

A format will make no difference to this program.

I have 2 SanDisk 2GB cards bought in the last 6 months.

Both work perfectly.

As long as the connections are as for my setup and the leads are not too long
(2 inches in my case)I don't have any idea.


As the SD lib reset must work how about using their reset function in:

unsigned char SDCARDclass::SD_reset(void)
{
.........
use SD lib reset
.........
}//end of SD_reset function


I'm sorry I can't be more helpfull.

pieturp

Thanks for your reply. I bought another (512MB) card that works fine :)

Will conduct more testing soon, to see if I'm able to use my other two cards too.

carl47

Happy that it works on at least one card.

Please let me know if you have any luck diagnosing the problem with the other two.

ahmed_mosad

hi . i have problem when tray to run your library
some message error say that     fatal error: WProgram.h: No such file or directory

pls can you help me solve this

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy