PIC/SD card low level code

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
I'm been looking for hours for good example low level code for writing and reading an SC card using my PIC. I found a great site for generic FAT file systems, but I need low level code that I can read through and understand. Finally, I found this:

http://hades.mech.northwestern.edu/...terfacing_to_a_Secure_Digital_(SD)_Flash_Card

Under "Code" heading, I downloaded the code archive. It appears to have complete code using SPI to read and write data. I'll be going over the code and trying to understand, and finally use it.

And so, my question: Does anyone know of other PIC code examples? Something perhaps simpler or with better documentation?

Thanks alot!
 

takao21203

Joined Apr 28, 2012
3,702
What PIC do you use? What about serial FLASH? much easier.
And you could build a USB reader with one of these new 16F USB PICs- small and low cost.

SD cards actually have a controller and a quite complex command set. Manuals for that can be found online.

If you include a library it will need a lot of program mem, and filesysstem needs extra prog mem on top of that.
 

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
I'm using PIC18F45K20. Actually, it's the debug board.

Serial FLASH is fine. I'm looking at all options, but eventually need to use all commodity devices.
 

ErnieM

Joined Apr 24, 2011
8,377
I found some awesome SD card routines on teh interwebs.

It's on a very obscure website: Microchip Libraries for Applications

I use their "MDD File System" all the time and it works flawlessly right out of the box. They even have examples (sample programs) and complete app notes on their use.

Wow !!!
 

spinnaker

Joined Oct 29, 2009
7,830
I posted this

http://forum.allaboutcircuits.com/attachment.php?attachmentid=64235&d=1391309487

in another thread. It has low level stuff but also FAT,


The code is from this site:
http://elm-chan.org/

I did modify the code a bit.

You will need a Pic with more than 512 bytes of memory if you want to use FAT. He has a petite FAT that I have not looked at yet.

My project is a fully working project and based off of the PIC18F27. If you wan to use a similar sized RAM pic, you will need to change the linker file and update the config header I have included.

Let me know if you have any questions.
 

spinnaker

Joined Oct 29, 2009
7,830
I'm using PIC18F45K20. Actually, it's the debug board.

Serial FLASH is fine. I'm looking at all options, but eventually need to use all commodity devices.
I have the same board. Looks like that Pic has enough RAM for the project that I posted. If you decide to use it and run into issue let me know and I will see if I can work something up,
 

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
I found this low-level I/O code. It looks pretty good, at least from the little I understand about using SD cards. I want to take it one step at a time, first getting
SPI to work, then attempting to initialize a card, then basic read and write. The code is attached if anyone wants to take a look.
 

Attachments

atferrari

Joined Jan 6, 2004
4,771
I'm been looking for hours for good example low level code for writing and reading an SC card using my PIC. I found a great site for generic FAT file systems, but I need low level code that I can read through and understand. Finally, I found this:

http://hades.mech.northwestern.edu/...terfacing_to_a_Secure_Digital_(SD)_Flash_Card

Under "Code" heading, I downloaded the code archive. It appears to have complete code using SPI to read and write data. I'll be going over the code and trying to understand, and finally use it.

And so, my question: Does anyone know of other PIC code examples? Something perhaps simpler or with better documentation?

Thanks alot!
THe page as per your link, reads just at the top

DO NOT USE THE FOLLOWING CODE AS WRITTEN - IT DOES NOT WORK - MODIFY BEFORE USING :p

Weird and encouraging.
 

spinnaker

Joined Oct 29, 2009
7,830
I've never had to use a linker script. Why would I need one now?
Actually you do. You just don't realize it. The compiler automaticlly links the right linker file for you.

By default the 8bit pic breaks your memory down into 255 byte banks. Variables cannot cross banks. Why they break it into banks I am not sure, My guess it adds efficiency in managing memory. I will need to look that one up.

Anyway you can create your own bank sizes:


DATABANK NAME=gpr0 START=0x60 END=0xFF
DATABANK NAME=gpr1 START=0x100 END=0x1FF
DATABANK NAME=gpr2 START=0x200 END=0x2FF
DATABANK NAME=FATFS_0 START=0x300 END=0x5C0
//DATABANK NAME=gpr4 START=0x400 END=0x4FF
DATABANK NAME=gpr5 START=0x5C1 END=0x5FF
DATABANK NAME=myMSD START=0x600 END=0x7FF PROTECTED

The changes are just a bit more complicated than this. See the linker file in my code for a full example. But you can see here the normal banks that come from Microchip and the ones that I modified. You will also need to look at the C source. When you define a variable, you will need to specify which bank you want to use.

A starter linker file can be found under:
Program Files (x86)\Microchip\mplabc18\v3.46\bin\LKR

for mplabc. Copy the one that fits you pic and make modifications. Note your version of mplabc might not be the same as mine in the directory above.

I need to look at the Microchip code library. It might be more efficient. I think it only requires that you modify one custom bank of memory.

The larger memory size in both libraries is needed to read in the FAT record and sectors as I recall.
 

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
Actually you do. See the linker file in my code for a full example. But you can see here the normal banks that come from Microchip and the ones that I modified. You will also need to look at the C source. When you define a variable, you will need to specify which bank you want to use.
Is your code posted somewhere? I only found a link to a schematic and the website your code was modified from.
 

ErnieM

Joined Apr 24, 2011
8,377
Probably that you need to customize it for your particular configuration.
Or not. Seems the project never was completed:

PIC32MX: Interfacing to a Secure Digital (SD) Flash Card said:
NOTE: While ultimately, we were able to initialize the SD card and send data along the output and input lines, we were unsuccessful in executing the read/write demo. We are confident that the hardware configuration and circuit are correct, however the code likely contains errors.
If you put FSIO.c and SD-SPI.c into your project you've added Microchip's fat file system. These guys didn't do that. I don't have di Jasio book handy to see what he was up to (but generally that book is an excellent work).
 

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
Actually you do. You just don't realize it. The compiler automaticlly links the right linker file for you.

By default the 8bit pic breaks your memory down into 255 byte banks. Variables cannot cross banks. Why they break it into banks I am not sure, My guess it adds efficiency in managing memory. I will need to look that one up.

Anyway you can create your own bank sizes:


DATABANK NAME=gpr0 START=0x60 END=0xFF
DATABANK NAME=gpr1 START=0x100 END=0x1FF
DATABANK NAME=gpr2 START=0x200 END=0x2FF
DATABANK NAME=FATFS_0 START=0x300 END=0x5C0
//DATABANK NAME=gpr4 START=0x400 END=0x4FF
DATABANK NAME=gpr5 START=0x5C1 END=0x5FF
DATABANK NAME=myMSD START=0x600 END=0x7FF PROTECTED

The changes are just a bit more complicated than this. See the linker file in my code for a full example. But you can see here the normal banks that come from Microchip and the ones that I modified. You will also need to look at the C source. When you define a variable, you will need to specify which bank you want to use.

A starter linker file can be found under:
Program Files (x86)\Microchip\mplabc18\v3.46\bin\LKR

for mplabc. Copy the one that fits you pic and make modifications. Note your version of mplabc might not be the same as mine in the directory above.

I need to look at the Microchip code library. It might be more efficient. I think it only requires that you modify one custom bank of memory.

The larger memory size in both libraries is needed to read in the FAT record and sectors as I recall.
I was just forced to do this when I needed an integer array of size 200 for my temperature log program. It was tough trying to figure out how to get a linker script, modify and specify it in my project. But it's done now, and not too complicated, looking back at it.
 
Top