19 February 2010

Work and FIRST Robotics

    Not much new has happened since my last post. I've been very busy at work and I've been volunteering for the FIRST robotics competition. My team, Rolling Thunder Robotics, is almost finished assembling our robot. We have one last night of building before the scrimmage Saturday morning. Being a FIRST mentor has been great and I encourage anyone who is interested to join in. You don't need to know anything about robots to help, but most of the work is about designing and building a robot...mechanical things seem to be the stumbling point for most teams. www.usfirst.org
    With my excuses and public service announcements out of the way it's on to some firmware! As I've mentioned before I decided to use Atmel's ATmega128 microcontroller for this project. There are actually two of them (one for the GPS and cellular module and one for the temp, pressure, acceleration, and rotation sensors). I could have gotten away with just one, but they're cheap and I have room to expand this way. Each micro will have an SD card with a FAT32 file system for data logging. It's somewhat scary using file systems for the first time, but you'll find it can be very easy! I've found a pre-made FAT32 library for AVR's from Dharmanitech (www.dharmanitech.com/2009/01/sd-card-interfacing-with-atmega8-fat32.html). It is written with the ATmega32 in mind, but it's easy enough to change the UART and SPI definitions for use with any AVR. I highly recommend running the example project before trying to modify it. Since the code is a little bit hard to follow it will give you a much better idea of how it works. Below are some pointers on how to get your custom version up and running.

SD FAT32 Initialization:
You must run the following functions to setup communications and the FAT32 tables:
  • spi_init()
  • SD_init()
  • getBootSectorData()
SD FAT32 File Operations:
Reading, writing, and deleting files is as easy as calling a function...
  • readFile(READ, fileName)
    • fileName is a null terminated string containing the name of the file and the extension
    • the output is sent to whatever UART port is defined in the UART_routines.h file
  • writeFile(fileName, dataString)
    • fileName is a null terminated string containing the name of the file and the extension
    • dataString is the text to write to the file terminated by the '~' character
    • this function will append dataString to a file if it already exists or create the file if it doesn't
  • deleteFile(fileName)
    • fileName is a null terminated string containing the name of the file and the extension
!!!!BUG REPORT!!!!
    I found a bug in the SD FAT32 library that really made me scratch my head for a while. The problem happens when you use the readFile, writeFile, or deleteFile functions with the file name entered directly.

e.g. writeFile("Test.txt", data);

    Inside these functions the file name gets expanded to a format that is expected by the FAT32 file system. This format is 13 bytes long. If your file name is shorter than 12 characters you will overwrite whatever data is in memory behind the fileName variable used by these functions... The fix is pretty easy and hopefully it will be integrated into the library for the next update. Follow the steps below and you're all set until then!

In FAT32.c:
1. Place this code at the top of convertFileName:
int j=0, k;

// Copy the file name to a large buffer so memory isn't corrupted by the convertFileName function
while((fileName[j] != 0) && (j<16))
{
FName[j] = fileName[j];
j++;
}
FName[j] = 0;
j=0;

2. In the writeFile function change "dir->name[j] = fileName[j];" to "dir->name[j] = FName[j];"

3. In the readFile function change "dir = findFiles(GET_FILE, fileName); //get the file location" to dir = findFiles(GET_FILE, FName); //get the file location"

4. In the deleteFile function change "findFiles(DELETE, fileName);" to "findFiles(DELETE, FName);"

In FAT32.h:
1. Place this variable declaration with the other global variables:
volatile char FName[16];

No comments:

Post a Comment