I am working with the dataflash library for the AT45DB series of flash memory which can be found here.
http://playground.arduino.cc/code/dataflashI have the library working perfectly in some simple sketches but when I try to add it to my main code it becomes erratic and inconsistent. For example sometimes it wont write the page, it will fail to read pages sometimes, sometimes it will read a buffer but part of it. The errors are all over the place.
I am running a breadboard ATMEGA328P with the internal 8mhz oscillator and have a external 32.768 crystal. I use the external crystal to keep time and as an interrupt from sleep. A process of elimination has revealed that the cause of my problems is the timer2 interrupt. If I disable the timer2 interrupt my problems go away and the AT45DB works perfectly. In the sketch below I have it set to write the string from messageline into page 0. This loop will increment to the subsequent page and keep writing every page in the memory. This sketch example will consistently make it to page 3 and then hang for a a few seconds and then starts back at page 0. It does not seem to reset since I don't get the print statement from setup(), it just seems to start back from zero in the main loop which is quite strange. When I disable timer2 interrupt this sketch runs perfectly and writes/reads pages forever as it should (or at least until it runs out of pages).
My issue is that I don't want to disable timer2 even for a brief time since that will screw up my time keeping. Is there any way to keep the flash memory happy and keep my timer2 interrupt running?
#include <dataflash.h>
int page = 0;
int buffer = 1;
char messageline[60] = "#This is a test string for the flash memory";
char readFlash[60];
Dataflash dflash;
int i = 0;
void setup(){
//Setup TIMER2
TCCR2A = 0x00;
TCCR2B = (1<<CS22)|(1<<CS21)|(1<<CS20); //Set CLK/1024 or overflow interrupt every 8s
ASSR = (1<<AS2); //Enable asynchronous operation, 32kHz xtal needed
TIMSK2 = (1<<TOIE2); //Enable the timer 2 interrupt. If this is disabled the sketch works properly
sei(); //Enable global interrupts
pinMode(A3, OUTPUT); //Flash VCC
digitalWrite(A3, HIGH); //powers the flash memory up
delay(4000);
Serial.begin(9600);
Serial.println("starting the sketch now");
dflash.init(); //initialize the memory (pins are defined in dataflash.cpp
}
void loop(){
writeData();
readData();
page++;
delay(1000); //keep it from running so quickly in testing
}
void writeData(){
Serial.print("I am writing page ");
Serial.println(page);
i = 0;
while (i < 60){
dflash.Buffer_Write_Byte(1, i, messageline[i]); //write to buffer 1, 1 byte at a time
i++;
}
dflash.Buffer_To_Page(1, page); //write the buffer to the memory on page: page
}
void readData(){
Serial.print("reading page ");
Serial.println(page);
dflash.Page_To_Buffer(page, buffer);//copy page to the buffer
i = 0;
while(i < 60){
readFlash[i] = dflash.Buffer_Read_Byte(buffer, i); //read the buffer
i++;
}
Serial.println(' ');
Serial.println(readFlash); //print what was read from the buffer
}