Saturday, April 5, 2014

SD card logger library with log rotation that fits on ATmega8

This library implements an SD card Data Logger that runs on ATmega.
It has a small footprint, so it can be loaded on an ATmega8, leaving space for user code.
It supports SD and microSD cards formatted with FAT16.
It also features log rotation.


The "Petit FAT File System Module" by ChaN (http://elm-chan.org/fsw/ff/00index_p.html) it is used to write on SD card. I've used the Petit library because i would like to build a small footprint logger. Even if it has a few limitations, those can be circumvented.

We had to format the card we would like to use with FAT16, and then load it with a predefined number of empty files of a know dimension. Once we have files on the card, we can write on those files.
You can create empty files by using the python helper provided in code.
Firmware side we will setup the file dimension, and file number. File dimension, can not be greater than 2^16 bytes because a uint16_t type variable it is used to store this information, also max number of files it is limited to 256, becayse a uint8_t variable it is used.

Given the number of files used by the logger and every file size, we just have to record the last written position and the file number we are using to implements the log rotation. When a file is filled up with logged data, it skips to the next one, if the file is the last one, we go back to first.
Note that Petit FAT File System Module write files using blocks, so the "seek to position" can be used only on the block dimension. Typically a block is 512 bytes.

Power loss failure are managed by recording to eeprom the above parameters every 60 seconds (you can setup this timing option in code). When power get's back, the logger will use the last file and position recorded.
File write failure will cause the file to skip to the next log file.

The sample code provided just logs time + a string.
Date and time are obtain using a DS1307 chip. In the code you can also find a python helper to configure date and time through UART.

The SD card board, is an ebay cheap one, it basically just has voltage regulation to prevent the SD card to be damaged.

You can find schematics below:


This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 8MHz.


Code

Notes
  • read risk disclaimer
  • excuse my bad english