Search This Blog

Thursday, November 3, 2011

Micro SD Card - Low Level I/O

   This has been a most satisfying week.  With the help of Marcus Bannerman and his well laid out website, I now have low level routines working to read, write, and erase an SD card.  Marcus, if you are reading this, thank you again for all your help.  This working code is really all his for the SD card routines.  Here is a link to his site at the Fat32 library page, complete with tutorials, a forum, and loads of helpful information, all backed by a very generous and intelligent man: marcusbannerman.co.uk.  I used a version 2, low capacity, 2 GB Sandisk micro SD card for my stuff, but I am confident that Marcus' code will work for almost any card.  I haven't tried the FAT library yet, but it looks really useful.  I'm satisfied with low level reading and writing on an unformatted card.  I'll let my application transfer data to a PC as needed.  If I'm really in a bind, I'll do what I did when debugging.  I simply insert the card and use:
$sudo dd bs=512 count=2 if=/dev/sdx of=dump
to spew out the data.  You'll have to adjust the parameters for your use, of course.  I've heard that if you are going to do a full dump of the card, it is best to set a really high value for the bs.

[ On a side note, it was interesting to dump out the raw data from the card and look at it.  I had never used the card before, yet there was a host of plain text and other stuff on the card.  Most of it was in French. Try it sometime.  I zeroed out the entire card before I began working on it with dd and the if= set to /dev/zero. ]

   Here are the links to my code for the Pic18F2455, running at 20MHz:
logger.asm (main routine)
20MhzConfig.conf
sdMacros.asm
lcd-serial.asm
sdCard.asm

   The conf file sets all the fuses.  The way gpasm has you lay out the fuses for the 18F Pics is rather long.  It was cluttering up the files, so I made it its own include.  The logger file is the main routines.  It also includes delay routines that are usable at 4, 8, 16, and 20 MHz.  A define statement sets the loop based on the clock.  The LCD routines are for your basic 2 line Hitachi based LCD.  I have the code set up for 6 wire interface, four data in four bit mode, RS, and E.  RW is tied to ground.  I wired mine up to a little backpack with an 8 bit logic shifter.  This way, it only takes two I/O lines from the Pic, Vdd, and ground to run.  Myke Predko has a cool method of tying the data line to the E pin on the shifter that acts as an AND gate.  This lets you strobe with the data line with the clock not strobing.  Here is a link to Myke's work: 2 Wire LCD Interface.  The lcd function duplicates some delay routines.  I jammed it together with the sd code pretty quickly.  I'll need to clean it up later.

   Below are some shots of my test rig.  I'm running the Pic and LCD at 5V.  I have a voltage regulator to power the SD card and shift the voltage for the SD card.  Marcus' site has a good Eagle schematic.


Above is the whole assembly.  The Pic lives in my carrier board that BatchPCB made from my Eagle design.  It includes the resonator, smoothing cap, and an ICSP header.  On the right is the LCD and backpack.  Behind the ribbon cables is the micro SD card in a breakout board.  The upper far left corner is my voltage regulator for five Volts.  Here are some closeups:

Overhead shot of the SD card breakout and level shifter circuit.

The Pic carrier board

LCD Logic Shifter Backpack

5 Volt regulator

   The ribbon cables are like the one I posted about previously.  They're made from old IDE cables and swiss machine pin headers.  I need to make more of these.  They're really great for neatening up a layout. 

5 comments:

  1. Hey,
    Nice work, you're far too neat with your wiring though, These little modules you're working on are far too tidy!

    I can recommend the LCD-DOG displays as you can avoid the shifter hardware and use a SPI bus for the display. If you were really tight on pins you could even have the SD card and display on the same SPI bus.

    I'll take a look around your site and look forward to seeing what you build.
    Marcus

    Link to the DOG display's product page
    http://www.lcd-module.com/produkte/dog.html

    ReplyDelete
  2. Marcus,
    Thanks for stopping by. I have taken a shine to making modules for reusable parts. You should see the backs of these boards, though. In fact, you will, if you take a look at the rest of the blog. :) I think I ruined my good soldering tip when I touched an insulated wire on the L293 motor driver module. You can see the burn in the wire on the close-up.

    Thanks for the link on the LCD. I bought a cheapie LCD to learn and added the backpack when it looked useful. I think if I need a real display, I will either go the 4 line LCD route or the Nokia knock-off route. I've been looking at a cheap Pic12F683. I might use that to do the four wire interface to the LCD with 2 wires for I2C. Might be fun to try. I've got some large bots in mind that would operate on I2C. Might make for a good interface.
    In addition to the bot, I've got some ideas in mind for model rocketry with my son, an MP3 player, and a GPS data logger. We'll see where whimsy takes me.

    Cheers!
    Tom

    ReplyDelete
  3. Hi,
    Im also using Marcus' routines, but am having trouble with the fat32 routines. Have you tried using them?

    ReplyDelete
  4. Hello Felipe,
    No, I have not tried them as yet. I was looking into pulling in just the read routines at some point. I have a use for an app that will read from the card to process instructions. I haven't gotten back around to that project yet. It will need more time around the mechanical and physical construction.

    I would encourage you to post on the forum at Marcus' site. He is very responsive and helpful. Also, there has been some recent activity by one of his followers. His work has cleared up some bugs, particularly with high capacity cards.

    Good Luck!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete