I added a library to the playground for interfacing with the atmel dataflash memory.
After trying lots of different things this was the easiest way to add some memory to the arduino. It doesn't have a filesystem so you to think for yourself about the memory usage. I added a very simple example to the library so you can check if it works. After that your are on your own. But comments, additions and improvements are always welcome.
This library is based on code for the Atmel butterfly. It was changed by Martin Thomas to compile with gcc. I only changed it to C++ to make a library for it.
My results compiling the example under arduino-0012,
In file included from dataflash.cpp:50:
dataflash.h:70: warning: only initialized variables can be placed into program memory area
dataflash.h:71: warning: only initialized variables can be placed into program memory area
In file included from /home/zumbi/ELECTRONICS/ARDUINO_SOFT/tmp/arduino-0012/hardware/cores/arduino/WProgram.h:4,
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:80: error: expected unqualified-id before 'int'
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:80: error: expected `)' before 'int'
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:80: error: expected `)' before 'int'
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:111: error: expected `)' before 'int'
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:111: error: expected `)' before 'int'
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:144: error: expected identifier before '(' token
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:144: error: expected `)' before '(' token
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:144: error: expected ',' or '...' before '(' token
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:144: error: expected initializer before ')' token
/usr/lib/gcc/avr/4.3.2/../../../avr/include/stdlib.h:176: error: '__compar_fn_t' has not been declared
In file included from /home/zumbi/ELECTRONICS/ARDUINO_SOFT/tmp/arduino-0012/hardware/cores/arduino/WProgram.h:6,
Did not know that there is an example code in the zipped download content for this library at the playground.
Here it is for those that are lazy
#include <dataflash.h>
int lastpage=0; //last page written to
int pages=25; //total pages that will be used
Dataflash dflash;
void setup()
{
Serial.begin(115200);
Serial.print('h',BYTE);
Serial.print('i',BYTE);
Serial.print('\n',BYTE);//debug
dflash.init(); //initialize the memory (pins are defined in dataflash.cpp
}
void loop()
{
int j = 0;
int i = 0;
char messageline[] = "this is only a test on page: ";
char lpstring[] = "lastpage: ";
char int_string[10];
itoa(lastpage, int_string, 10); // make string of the pagenumber
strcat(messageline, int_string); //append to the messagline string
//for (int i=0; messageline[i] != '\0'; i++)
while (messageline[i] != '\0')
{
Serial.print(messageline[i], BYTE); //just a check to see if the loop is working
dflash.Buffer_Write_Byte(1, i, messageline[i]); //write to buffer 1, 1 byte at the time
j = i;
i++;
}
i=0;
dflash.Buffer_Write_Byte(1, j+1, '\0'); //terminate the string in the buffer
Serial.print('\t');
dflash.Buffer_To_Page(1, lastpage); //write the buffer to the memory on page: lastpage
strcat(lpstring, int_string);
for(int i=0; lpstring[i] != '\0';i++)
{
dflash.Buffer_Write_Byte(2, 20, lpstring[i]); //write to buffer 2 the lastpage number that was used
Serial.print(lpstring[i]); //write to serial port the lastpage number written to
}
Serial.println();
lastpage++;
if (lastpage > pages) //if we reach the end of the range of pages
{
lastpage = 0;
for (int i=0;i<=pages;i++)
{
dflash.Page_To_Buffer(i, 1);//copy page i to the buffer
for(int j=0;j<32;j++) //32 depends on the amount of data on the page
// testing for a specific charater is also possible
{
Serial.print(dflash.Buffer_Read_Byte(1, j)); //print the buffer data to the serial port
}
Serial.print(" page: ");
Serial.println(i); //print the last read page number
}
}
delay(200); //slow it down a bit, just for easier reading
}
Hi,
Add the following line at the top of dataflash.h #include <WProgram.h>
Sample sketch (dataflash_intro.pde) worked with my AT45DB161D.
However, if you have D version like AT45DB161D, lot of commands have been changed/added on D version.