Compiling an SD/MMC and fat16 library

... continues from previous post

/* work in progress ... 2008 02 17
 * it´s still quite dirty! take care!
 *
 * using stuff from:
 * arduino.cc -> got the basics, they made it easy and fun!
 * Roland Riegel http://www.roland-riegel.de/sd-reader/ -> the awesome mmc&fat libs
 * Ulrich Radig http://www.ulrichradig.de/home/index.php/avr/mmc-sd -> hardware hint
 *                 has also made an interesting mmc & fat lib, but not used here.
 * www.mikrocontroller.net -> got some good soft-&hardware hints (german language)
 * http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1199737337 -> the important things for the code
 *                 from the nice people here to get started with this project.
 *
 * www.navilock.de -> the gps module (got the rs232, better would be the ttl level one)
 * so i had to add a max232 IC for serial communication. with ttl no need for that.
 *    http://www.navilock.de/produkte/gruppen/13/Boards_und_Module/60409_NL-500ERS_Sirf3_RS232_Modul.html
  *
 * This little project is for getting the basics of gps reading 
 * and long time data storage together.
 * Should get a useful gps-data-logger when ready.
 *
 * Ideas&ToDo:
 * buttons for r/w/a (read write append) functionality?
 * Adding a Siemens S65 LCD display.
 * Connect it to a WiiRemote -- log acceleration, degree, communicate with BT &
 *    add XBeePro for reality mesh network activity (great potential in it!)
 *    maybe for exploring land- and city scapes, reality gaming... with a group of people.
 */
// including the mmc libraries for raw r/w support:
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>

// defines for splitting the unsigned long int into bytes for storage:
#define HIGHBYTELONG(x)  ((byte)((x) >> 24))
#define LOWBYTELONG(x)  ((byte)((x)  >> 16))
#define HIGHBYTE(x)  ((byte)((x) >> 8))
#define LOWBYTE(x)  ((byte)((x) & 0x00ff))
#define WORD(x,y)   (( ( (byte)(x) ) << 8) | ((byte)(y)))

unsigned long int sdoffset=4096;  // r/w offset
// offset 0 to 4096 is for rw offset and whatever future storage.
// for the first usage have to write zeroes to the card.
// the last offset on my 1G card is 1009254384
// get as much buffer length as the serial port has:
int in_buf_length=128;
byte incomingBuffer[128];  // for incoming serial data

// optical function control:
int ledWrite = 9; 
int ledRead = 8; 

void setup() {
  pinMode(ledWrite, OUTPUT);      // sets the digital pin as output
  pinMode(ledRead, OUTPUT);      // sets the digital pin as output

  //Init of the MMC/SD-Card
  // give it some time
  delay(500);
  digitalWrite(ledRead, HIGH);   // sets the Read-LED on
  digitalWrite(ledWrite, HIGH);   // sets the Write-LED on
  sd_raw_init ();
  delay(500);
  digitalWrite(ledRead, LOW);   // sets the Reaad-LED off
  digitalWrite(ledWrite, LOW);   // sets the Write-LED off
  delay(500);
  // and a second time...
  sd_raw_init ();
  // let´s see where we go:
  for (int i=0; i <= 3; i++){
    digitalWrite(ledRead, HIGH);   // sets the Read-LED on
    digitalWrite(ledWrite, HIGH);   // sets the Read-LED on
    delay(300);
    digitalWrite(ledRead, LOW);   // sets the Read-LED off
    digitalWrite(ledWrite, LOW);   // sets the Read-LED off
  } 
  Serial.begin(9600);      // navilock works with 9600bps from scratch
  // For programming you have to disconnect the gps physically! 
  // check where we stopped writing the last time:
  checkStartOffset();
}


void loop() {
  digitalWrite(ledRead, HIGH);   // sets the Read-LED on
  fillSerialbuffer();
  digitalWrite(ledRead, LOW);   // sets the Read-LED off
  digitalWrite(ledWrite, HIGH);   // sets the Write-LED on
  sd_raw_write( sdoffset , incomingBuffer , in_buf_length );
  sdoffset=sdoffset+in_buf_length;
  writeLastStartOffset();
  sd_raw_sync ();      
  digitalWrite(ledWrite, LOW);   // sets the Write-LED off
}


void writeLastStartOffset(){
  byte offset[4];
  offset[0]=(HIGHBYTELONG(sdoffset));
  offset[1]=(LOWBYTELONG(sdoffset));
  offset[2]=(HIGHBYTE(sdoffset));
  offset[3]=(LOWBYTE(sdoffset));

  sd_raw_write(0, offset ,4);
}
void fillSerialbuffer(){
  // we have enough storage with 1G
  // so we take the hole input from gps.
  // don´t care about single lines, get the whole stuff!
  // read one serial buffer with length of 128:
  int var = 0;
  while(var < in_buf_length-1){
    if (Serial.available() > 0) {
      incomingBuffer[var] = Serial.read();
      var++;
    }
  }
}
void checkStartOffset(){
  byte offsetstored[4];
  sd_raw_read(0, offsetstored ,4);
  unsigned int wri = WORD(offsetstored[0],offsetstored[1]);
  unsigned int wri2 = WORD(offsetstored[2],offsetstored[3]);
  unsigned long int readdoffset=( (((unsigned long int) wri) <<16)) | ((unsigned  int) wri2);
  // when it is a clean card with 00 00 00 00 on it,
  // start anyways at the right offset.
  if (readdoffset < 4096)
  {
    sdoffset=4096;
  }
  else
  {
    sdoffset=readdoffset;
  }
}

i had to cut out the out-commented debug code, it was to long.

@agent_orange
Thanks again for help! I feel having hijacked this thread, hope that is ok for everyone? Should i stay here with future additions and improvements? Because it is not purely the topic of the post now?

If there is serious interest i can describe the project with more detail, pictures and so on.

Tomorrow i will try to log my way to work. I´ll post information especially when success is not satisfying.