Go Down

Topic: help with dataflash library example (Read 1 time) previous topic - next topic

PaulS

Quote
the rest is the same.

That can't be, to produce the output you show. Humor me, and post all the code.

fbriggs4

PaulS- here is all the code. One thing I did notice is that there is a Serial.print('\t'): which never prints in the output. I am not sure what that means but I suspect it is significant.


Code: [Select]

#include <dataflash.h>


int lastpage=0; //last page written to
int pages=5; //total pages that will be used changed from 25 to 5
Dataflash dflash;

void setup()
{
  Serial.begin(115200);
  Serial.write('h');
  Serial.write('i');
  Serial.write('\n');//debug
  dflash.init(); //initialize the memory (pins are defined in dataflash.cpp
}

void loop()
{
 
  char messageline[35] = "This is only a test on page: ";
  char lpstring[15] = "lastpage: ";
  char int_string[10];

  itoa(lastpage, int_string, 10); // make string of the pagenumber
  strcat(messageline, int_string); //append to the messagline string
 
  int j = 0;
  int i = 0;
  while (messageline[i] != '\0'){
    Serial.write(messageline[i]); //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
}



Here is the output based on the code above.

Code: [Select]

This is only a test on page: 0 lastpage: 0
This is only a test on page: 1 lastpage: 1
This is only a test on page: 2 lastpage: 2
This is only a test on page: 3 lastpage: 3
This is only a test on page: 4 lastpage: 4
This is only a test on page: 5 lastpage: 5
255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255  page: 0
255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255  page: 1
255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255  page: 2
255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255  page: 3
255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255  page: 4
255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255255  page: 5


PaulS

Quote
One thing I did notice is that there is a Serial.print('\t'): which never prints in the output. I am not sure what that means but I suspect it is significant.

That's a tab character. Exactly what the receiver is supposed to do with it is not defined. To some, it means something. To others, it is meaningless.

Code: [Select]
  Serial.write('h');
  Serial.write('i');
  Serial.write('\n');//debug

Why are you sending these characters as binary data?

Quote
Here is the output based on the code above.

I don't see hi...

Code: [Select]
  i=0;
  dflash.Buffer_Write_Byte(1, j+1, '\0'); //terminate the string in the buffer

Swap the order of these, and get rid of j.

Code: [Select]
  dflash.Buffer_To_Page(1, lastpage); //write the buffer to the memory on page: lastpage
lastPage is not a buffer. So, what, exactly is this function call doing? Where, specifically, is it writing to on the page?

What is the first argument to both of the functions?

Code: [Select]
      dflash.Page_To_Buffer(i, 1);//copy page i to the buffer
Since when is 1 a buffer?

It appears that you are writing data to the flash, and then dumping garbage over the top of it.

Then, the garbage trashes memory when you misuse the Page_To_Buffer() method. After that, you can't reasonably expect ANYTHING to work.

fbriggs4

PaulS- Just to clarify this code was the example provided in the library and I didn't write any of it. I am just attempting to get it working and learn in the process. The original code was written by ATMEL for the butterfly to work with the external SPI flash memory. Someone took that and turned it into a library and created this example. I am just learning so I don't understand all of it but I will try to answer as best I can and try your suggestions.

Quote

  Serial.write('h');
  Serial.write('i');
  Serial.write('\n');//debug

Why are you sending these characters as binary data?

I have no idea why they did this in the example code but I do get "hi" at the start of the output. I did not post that since the output loops and the initial data was overwritten quickly and I did not stop the output quick enough to get it. But it does output properly.

Quote

dflash.Buffer_Write_Byte(1, j+1, '\0'); //terminate the string in the buffer
dflash.Buffer_To_Page(1, lastpage); //write the buffer to the memory on page: lastpage
What is the first argument to both of the functions?


The first argument to both of these is specifying to use buffer 1. There are 2 buffers and you can either write to either buffer 1 or buffer 2. If I understand everything correctly (and I may not) the buffer_Write_Byte function writes to buffer 1 with index j +1 which was the last written index +1 and it puts a NULL termination at the end of it. Once the buffer is written function Buffer_To_Page is called and the 1 specifies that buffer 1 should be written to the page lastpage which is a variable from 0-5.

Quote

Since when is 1 a buffer?

it is not a buffer, it just specifies which buffer to use. In this case either buffer 1 or 2 can be used and this code species buffer 1.

Quote

lastPage is not a buffer. So, what, exactly is this function call doing? Where, specifically, is it writing to on the page?


I don't have a good answer for that. I am trying to read the datasheet and files in the library but progress is sloooooow. I am reading as much as I can so I can try to understand all the logic in the library which is why my replies are slow. I will keep trying to understand this and see if I can come up with an answer.

PaulS

Quote
If I understand everything correctly (and I may not) the buffer_Write_Byte function writes to buffer 1 with index j +1 which was the last written index +1 and it puts a NULL termination at the end of it.

It does, but the value in j is the same value in i, before you reset i. Use i in the Buffer_Write_Byte() function before you reset it, and you don't need j.

Quote
Once the buffer is written function Buffer_To_Page is called and the 1 specifies that buffer 1 should be written to the page lastpage which is a variable from 0-5.

OK, so Buffer_To_Page is what commits the buffer to the flash memory. Prior to that, the data was being written to the buffer only.

Quote
So, what, exactly is this function call doing? Where, specifically, is it writing to on the page?

Never mind. You've answered that, already.

The Page_To_Buffer() method, then, is doing the reverse - getting data from the flash into a buffer, one page at a time.


Go Up