Dataflash AT45DB161D and Arduino Leonardo

Okay so i finally got the AT45DB161D to work with the leonardo. I used another library i found online http://blockos.github.com/arduino-dataflash/ maybe this can be helpful for someone else. The pageTest example in the library should work with little work, but i found it to be more geared towards uno and duemilanove. Below is a little sketch i wrote to test the Lenonardo. you can also download it from here Google Code Archive - Long-term storage for Google Code Project Hosting.

// DATAFLASH LEONARDO TEST.
//
// Pin Connections Dataflash to Arduino Leonardo
//
//  DataFlash  Arduino Leonardo
//    pin 1  ---   pin 16
//    pin 2  ---   pin 15
//    pin 3  ---   pin  8
//    pin 4  ---   pin 10
//    pin 5  ---   pin  7
//    pin 6  ---   3.3V
//    pin 7  ---   GND
//    pin 8  ---   pin 14
//
// AT45DB161B Pin Layout
//         ___ 
//  SI  1 | u | 8 SO 
//  SCK 2 |   | 7 GND 
//  RST 3 |   | 6 VCC
//  CS  4 |___| 5 WP 
//
// Arduino Leonardo ICSP Pin Layout
//
//              _____
//  D14 MISO 1 |__|__| 2 VDD
//  D15 SCK  3 |__|__| 4 MOSI D16
//      RST  5 |__|__| 6 GND
//
// This sketch is using blockos arduino-dataflash library available here:
// http://blockos.github.com/arduino-dataflash/
// and uses the arduino-timerone Library avaiable here:
// http://code.google.com/p/arduino-timerone/downloads/list
// Example by Taha Bintahir, http://mtaha.wordpress.com
//************************************************************************************


#include <DataFlash.h>
#include <TimerOne.h>
#include <SPI.h>

#define ledPin 13 // LED @ pin 13
#define baudRate 115200 // Baudrate for serial communication
#define timerIntterupt 1000 // Microsecond tick
#define lF 10 // ASCII Linefeed
#define msgLength 512 // Message Length

DataFlash dF; // DataFlash library object

unsigned long tickCounter = 0; // Tick counter used for subtimers 
byte mode = 0; // 0 - default waiting mode, 1 - store mode, 2 - read mode

void setup(){
  uint8_t dFStatus;
  DataFlash::ID id;
  
  pinMode(ledPin, OUTPUT); // Initialise Led Pin
  digitalWrite(ledPin, LOW); // Ensure Led is turned OFF
  
  Serial.begin(baudRate); // Initialise Serial Communication @ 115200 bps
  SPI.begin(); // Initialise SPI
  
  Timer1.initialize(timerIntterupt); // Initialise Timer1 @ 1ms tick
  Timer1.attachInterrupt(timerIsr); // Initialise Timer1 Interrupt Subroutine function
  
  //delay(25000); // 25sec delay, allow us to press the serial monitor button and to compensate for the Leonardo to be detected by the PC
  while(!Serial){
    // Wait for serial port to connect. Needed for Leonardo only
  }
  
  dF.setup(10, 8, 7); // Initialise DataFlash SS - pin 10, RST - pin 8, WP - pin 7
  
  delay(10); // Settling time
  
  dFStatus = dF.status(); // Read status register
  
  dF.readID(id);
  
  // Display status register 
  Serial.print("Status register :");
  Serial.print(dFStatus, BIN);
  Serial.print('\n');

  // Display manufacturer and device ID 
  Serial.print("Manufacturer ID :\n");  // Should be 00011111
  Serial.print(id.manufacturer, HEX);
  Serial.print('\n');

  Serial.print("Device ID (part 1) :\n"); // Should be 100110
  Serial.print(id.device[0], HEX);
  Serial.print('\n');

  Serial.print("Device ID (part 2)  :\n"); // Should be 00000000
  Serial.print(id.device[1], HEX);
  Serial.print('\n');

  Serial.print("Extended Device Information String Length  :\n"); // 00000000
  Serial.print(id.extendedInfoLength, HEX);
  Serial.print('\n');
  
  Serial.println();
  Serial.println("Press 'S' to store data or press 'R' to read data");  
    
}
void loop(){
  // Read Serial buffer only when there is data present
  if(Serial.available() > 0){
    uint16_t inByte = 0;
    
    inByte = Serial.read(); // Read Serial Buffer
    if(inByte != 0){
      // If 'S' byte is read
      if(inByte == 'S'){
        mode = 1; // Set state machine to mode 1
        Serial.println("What page do you want to store your data on?");
        while(1){
          // Store Data;
          if(Serial.available()){
            inByte = Serial.read();
            // If '`' is recieved exit out to main menu
            if(inByte == '`'){
              inByte = 0;
              mode = 0;
              mainMenu();
              break;
            }
            // Store data in page specified by user
            storeData(inByte);
            mainMenu();
            break;
          }
        }        
      }
      // if 'R' byte is read
      else if(inByte == 'R'){
        mode = 2; // Set state machine to mode 2
        Serial.println("What page do you want to read data from?");
        while(1){
          // Read Data
          if(Serial.available()){
            inByte = Serial.read();
            // If '`' is recieved exit out to main menu
            if(inByte == '`'){
              inByte = 0;
              mode = 0;
              mainMenu();
              break;
            }
            // Read data from user specified page
            readData(inByte);
            mainMenu();
            break;
          }
        }        
      }
    }
  }
}

// Timer1 Interrupt Subroutine Function triggered every 1ms
void timerIsr(){
  tickCounter++; // Count number of ticks
  
  // SubTimer1 @ 250ms 
  if(tickCounter % 100 == 0){
    //Serial.println(mode);
    if(mode == 1){
      digitalWrite( ledPin, digitalRead(ledPin)^1 ); //  Blink LED every 250ms in Store mode
    }    
  }
  
  // SubTimer1 @ 500ms
  if(tickCounter % 500 == 0){
    //Serial.println(mode);
    if(mode == 2){
      digitalWrite( ledPin, digitalRead(ledPin)^1 ); // Blink LED every 500ms in Read mode
    }
  }
}

void mainMenu(){
  mode = 0; // Set state machine to default - 0
  
  // Check if led is turned ON if it is turn it OFF
  if(digitalRead(ledPin) == HIGH){
    digitalWrite(ledPin, LOW);
  }
  
  Serial.println();
  Serial.println("Press 'S' to store data or press 'R' to read data"); // Print main menu again
}

void storeData(uint16_t nPage){
  char msg[msgLength]; // Serial Read message buffer
  byte dataLength = 0; // Number of bytes recieved in the message
  
  Serial.println("Enter Data String: ");
  while(1){
    if(Serial.available()){
      dataLength = Serial.readBytesUntil('\n', msg, msgLength);
      break;      
    }    
  }
  msg[dataLength] = '\0'; // Terminate end of message with '\0' to easily find EOF
  
  dF.bufferWrite(1,0); // Select dataflash buffer 1 @ 0 offset
  
  // Transfer message to DataFlash Buffer
  for(int i = 0; msg[i] != '\0'; i++){
    SPI.transfer(msg[i]);
  }
  
  SPI.transfer('\0'); // Add '/0' to locate EOF within the DataFlash 
  
  dF.bufferToPage(1, nPage); // Write DataFlash Buffer 1 to Pag
}

void readData(uint16_t nPage){
  uint8_t data; // Byte storage of data as recieved
  
  Serial.print('Page being read: ');
  Serial.println(nPage);
  
  dF.pageRead(nPage, 0); // Read user specified page from offset 0
  
  // Send FF to retrieve bytes from DataFlash
  do{
    data = SPI.transfer(0xff); 
    if(data != '\0'){
      Serial.print(data);
    }
  }while(data != '\0');
 Serial.println(); 
}