Working with SD cards
NOTE!!! We are going to use the SD card as flash memory. It will not have a full (
FAT) file system, and can not be read on a PC by the usual methods. It will not be possible to drag and drop a raw audio file to the card and read it with the MSP430, nor will it be possible see recordings from the file browser on a Mac or PC. Special disk tools provide access to the raw disk, but a full FAT file system implementation is too big for the tiny microcontroller. The F2013 has 128 bytes of RAM and 2 kilobytes of flash. A
workable FAT implementation requires 1024 bytes of
RAM and several kilobytes of flash program space.
The prototype design accesses the SD card using the simple
SPI protocol. SPI is a simple three wire interface that allows a microcontroller to read and write data from external chips. If you want the dirty details on how the SD card works in SPI mode, read some of these tutorials [
Tutorial 1,
Tutorial 2]. A
source code library from TI provides easy access to the disk without writing our own initialization and read/write routines (mmc.c/mmc.h in the project archive).
SD card storage is divided into 512 byte chunks called
sectors. A full 512 byte sector must be read or written at once. The MSP430F2013 has only 128 bytes of RAM -- not enough to hold a whole sector for random access. We can still read/write each byte in order, as it's needed. This method will suffice as long as each byte is read or written sequentially.
Playing an audio file is as simple as reading a byte from the SD card and writing it to the PWM duty cycle register.
Converting audio to a raw format
My 'hello world' audio clip comes from the intro to my videos. I converted the clip to an 8KHz .wav file with
Audacity and then saved it to raw text file with the free version of
Switch, as described
here.
- First, install Switch.
- Start Switch, and add your desired files to the queue by clicking "add files".
- Now set the output format to ".raw", and then click "Encoder Options". Set the encoder format to "8 bit unsigned", sample rate to "8000", and channels to "mono".
- Now click convert and the raw version of the audio files are created.
The raw text version of the audio file is included in the
project archive.
Source
Reader comments (Page 1 of 1)
Ian, I tried to write 64 byte chunks to a micro SD card and had no luck. I can read small chunks but writing anything other 512 bytes gives an error with my cards. Since I got these 1GB cards for very little (£3) I've decided to write 64 bytes to each 512 sector, makes usable size of the card 125 MB, which is fine for my needs.
ReplyHi Michael,
Sorry for my delayed reply, I've been without a PC for a few days.
SD cards require that the whole 512 byte sector be read, or written, at once. Try writing your 64 bytes, followed by 448 zeros -- that should net you a successful save.
I just finished uploading the next project in this series, it will be published soon. I show how to write a whole sector, one byte at a time, to get around the memory limitations of the F2013. You can use the SD card as a 512 byte sequential buffer to hold a partial sector while you wait for the remainder of your data.
Let me know how you progress.
-ian