Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Reading / writing on SD card

Status
Not open for further replies.

gebeclair

New Member
Hi,

I am facing the following problem in trying to interface a PIC micro with an SD card via SPI :
1- I take a card I usually use with my camera and format it with Windows in FAT32.

2- I try to read the first 512 bytes block on the card with the PIC (BOOT sector) by reading directly the bytes on the card.
=> the card positively answers to the reading request, but I read mostly zeros, except some bytes, but they correspond to nothing really on the card.

3- I then erase this first 512 block with the PIC by writing for example 0x1F everywhere.
=> If I then read the content of this first block again with the PIC, I properly get what I just wrote.

4- Moreover, if I format the card again in Windows, I can now read with the PIC this same first block (which now holds the BOOT sector data written by Windows when formatting), which was not working at step 2-.

In summary, everything happens as if the card was initially locked, and I had to write at least once this first block with the PIC or it can never be read with the PIC.

5- Now, if I reformat the card with my camera, I'm back to step 1- and can't read the card anymore with the PIC.

Does anybody have any idea on what's happening ?

Note :
- I have tested this with several cards. All do the same.
- I have fully tested my PIC program with apparently properly initializes the card and reads / writes data in it. I say this because once I have "unlocked" the card once as described above, all my program perfectly works.

Thank's in advance,
GB
 
Hi,

Thank's for your answer.

Yes, the very first thing my driver does is send these 80 clock cycles to the card.

In fact, where I'm a bit lost is that, once I have "unlocked" a card as described, everything works perfectly : I can remove the card, shut my system OFF, format the card on PC, put it back in my system and read and write RAW data or in FAT32 format, etc...

All this suggested to me that the hardware and software were fine, but that there was something else I didn't know about SD cards which made that, in cirtain circumstances, they can't be read.

Regards,
GB
 
Hi,

What Windows op system are you using? Some will not format to FAT32 unless you have at least one drive that is already FAT32. Just a long shot guess, but it could be that it is originally formatting to NTFS and then after you change some bytes it sees it as a FAT32 and so it works normally. This is a real long shot but hey maybe it will show something. The camera might be formatting differently than your PIC or Windows too.

There's a chance i might actually want to try this myself too. Im wondering if it is possible with a low end PIC.
 
Last edited:
Hi,

I'm using XP.

The thing is, I initially don't try to use FAT or FAT32 or any high level format. I'm just trying to read RAW data with the PIC. This doesn't work.

I think formatting with Windows doesn't change anything to that because Windows is just changing the data I can't read anyway.

The only thing that "unlocks" the situation is when I write RAW data to the first sector with the PIC. I then can read, write and do anything I want, even after power recycling. The card will only lock again if I format it again with my camera.

I tried 1 or 2 more things :
- Once the card was "unlocked", I read the first sector content with EditHexa.
I then re-locked the card with my camera, opened it in EditHexa and forced the content of the the first sector to be identical to it's original state. The card was still locked, even if first sector was now identical to what it was when the card worked !

That is what made me think there might be another locking feature I didn't know and which prevented me from reading through SPI.

Regards,
GB
 
Hello again,

Yes, it sounds like something has the ability to stop you from reading the card unless you destroy all the contents of the card and re-write.
That's the way PIC's are too, where you can copy protect so they cant be read but you can erase and re write if you want too, but at least no one else can get your program.

Sorry i cant help more, but maybe someone more familiar with these cards can help here.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top