While preparing for my SD tutorial, I realized it would be a long, uphill battle to debug SPI communications with a two-channel oscilloscope, especially when I had no prior experience on implementing SPI with AVR. To break the learning curve to manageable steps, I decided to get a Bus Pirate, a serial protocol tool I had heard a lot of good things about. What made the decision even easier was the fact that my local electronics supplier had just started selling the SparkFun version of Bus Pirate.
In this compact post, I’ll discuss my first impressions of the Bus Pirate, and try out the logic analyzer functionality.
Bus Pirate General Impressions
The first thought that crossed my mind when I saw the Bus Pirate in real life was how small the device was. As you can see, it’s hardly larger than an SD card. All the components are of surface mount type, LEDs included. USB cable was not included but fortunately I had several in my cable drawer. The thing does not need any other setup than plugging it in and installing the FTDI virtual COM port drivers. After that, it showed up as COM19 and I could connect to it with Putty. See the Bus Pirate 101 tutorial for installation details.
The v3 hardware is based on a PIC chip running at 3.3 volts and it took me a while to get comfortable with the power supply & pullup resistor logic – basically you can either use the Bus Pirate pins “normally” so an output pin is driven by the PIC, or in “open drain” mode, where a CD4066B analog switch connects the lines via pullup resistors to whatever is wired to the Vpu pin. This way, the Bus Pirate can interface with other than 3.3V devices (the PIC only pulls the line down or “lets it go” and the pullup takes the line high).
Continue reading Logic analysis with Bus Pirate
In the previous parts of this tutorial, we have built both an FAT library as well as a test program to communicate with the SD card. Now it’s time to wrap it up with final parts of code to read a file from SD card and print it out.
This part will utilize the previously made FAT library with a few tweaks – it turned out that some byte and word-sized values needed explicit casting to
unsigned long so that the calculations worked as they should. The new library and all code shown here can be found from the updated project zip.
Initializing the SD card automatically
Instead of manually pressing 1, 2, and 3, we’ll now write a single function to initialize the SD card to SPI mode. For standard SD (not high capacity SDHC) cards, it’s enough to:
- Send clock pulses for 80 cycles (“read” 10 bytes)
- Send command 0x40 (it should return 1)
- Send command 0x41 until it returns 0 (it returns 1 while the card is busy)
- Send command 0x50 to set read block size (as well as write block size)
Here’s the code to do just that (sd_sector and sd_pos will be used shortly):
Continue reading Simple FAT and SD Tutorial Part 4
Finally back! Sorry for taking a while, but getting the SD cards to work required quite a bit of research! In this part of the tutorial, we’ll start talking with the SD card. Once you get that working, adding the FAT library developed in the previous part will be easy.
This tutorial will most likely be the most challenging covered in my blog so far, so beware: You may need to do some troubleshooting on this one! If you encounter problems, I recommend you to ask any questions at the AVR Freaks tutorial forum topic, so more people might be able to help you than just me!
Basic hardware setup: ATmega88 with UART console
The 3.3V UART I covered just a while ago will form the basis for this project. Only change we will be doing is to replace ATtiny2313 with ATmega88. This is because SPI, SD and FAT code will eat up almost 3 kB of program memory, and the ATtiny chips with that much program memory and separate RX/TX and SPI pins are not that common, while ATmega88 is readily available (48 and 168 work as well, of course). To accommodate the new chip, the following hardware changes are made to the ATtiny2313 version:
- ATmega88 will require two ground connections
- In addition to VCC, also AVCC for analog circuitry needs to be wired to VCC
- Additional capacitor between AVCC and GND is recommended (I used 10 uF)
- Programming header MOSI/MISO/SCK are also in different place
Continue reading Simple FAT and SD Tutorial Part 3
Are you limited by 128 bytes of EEPROM on your MCU or even the few kilobytes of flash in your project? Instead of just downloading a library like Petit FAT File System Module and following blindly a tutorial on how to customize it to your microcontroller and SD card, would you like to really understand what you are doing, and maybe learn a bit about filesystems and SPI in the process?
In this first part of my FAT and SD tutorial, we’ll take a SD card image, and create a simple C program to interpret its contents. For this part, you don’t need any hardware at all, just a computer with gcc (GNU C Compiler) or any other ANSI C compatible compiler installed.
Getting ready: Hex editor and disk image
To make the coding easier, I recommend a good hex editor. The one I’m using is the free and excellent HxD by Maël Hörz. You can also use it to create a 1:1 disk image from a physical SD card. To have a filesystem to read, I purchased a 1 GB micro-SD card with SD adapter for 5€, plugged it into my computer and formatted it as FAT16 (over 2 GB cards will likely get formatted as FAT32), and copied Hamlet from Project Gutenberg and some other dummy test files to it (also created a subdirectory with a few text files in it):
Continue reading Simple FAT and SD Tutorial Part 1