From 8bf05982aeb90a5cb242e0a88931dc0ee3f34fbc Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 26 Dec 2011 18:25:43 +1100 Subject: [PATCH] dataflash: fixed APM2 flash logging we had log wrap disabled on APM2, and also had the wrong log size. The mainline code assumed 4096, but it is 8192 pages long. --- libraries/DataFlash/DataFlash.h | 6 ++++-- libraries/DataFlash/DataFlash_APM1.cpp | 8 +++++--- libraries/DataFlash/DataFlash_APM1.h | 4 +--- libraries/DataFlash/DataFlash_APM2.cpp | 20 ++++++++++---------- libraries/DataFlash/DataFlash_APM2.h | 1 + 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/libraries/DataFlash/DataFlash.h b/libraries/DataFlash/DataFlash.h index 37388feb7d..2a0417583a 100644 --- a/libraries/DataFlash/DataFlash.h +++ b/libraries/DataFlash/DataFlash.h @@ -6,19 +6,21 @@ #include +#define DF_OVERWRITE_DATA 1 // 0: When reach the end page stop, 1: Start overwriting from page 1 + class DataFlash_Class { public: DataFlash_Class() {} // Constructor - virtual void Init() = 0; + virtual void Init() = 0; virtual void ReadManufacturerID() = 0; virtual int16_t GetPage() = 0; virtual int16_t GetWritePage() = 0; virtual void PageErase (uint16_t PageAdr) = 0; virtual void ChipErase () = 0; - // Write methods + // Write methods virtual void StartWrite(int16_t PageAdr) = 0; virtual void FinishWrite() = 0; virtual void WriteByte(unsigned char data) = 0; diff --git a/libraries/DataFlash/DataFlash_APM1.cpp b/libraries/DataFlash/DataFlash_APM1.cpp index 20b8cd62a9..0012acfc16 100644 --- a/libraries/DataFlash/DataFlash_APM1.cpp +++ b/libraries/DataFlash/DataFlash_APM1.cpp @@ -36,7 +36,8 @@ #include "DataFlash.h" #include -#define OVERWRITE_DATA 1 // 0: When reach the end page stop, 1: Start overwriting from page 1 +// flash size +#define DF_LAST_PAGE 4096 // arduino mega SPI pins #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) @@ -116,6 +117,7 @@ void DataFlash_APM1::Init(void) // get page size: 512 or 528 df_PageSize=PageSize(); + df_NumPages = DF_LAST_PAGE; } // This function is mainly to test the device @@ -323,7 +325,7 @@ void DataFlash_APM1::FinishWrite(void) df_BufferIdx=0; BufferToPage(df_BufferNum,df_PageAdr,0); // Write Buffer to memory, NO WAIT df_PageAdr++; - if (OVERWRITE_DATA==1) + if (DF_OVERWRITE_DATA==1) { if (df_PageAdr>DF_LAST_PAGE) // If we reach the end of the memory, start from the begining df_PageAdr = 1; @@ -352,7 +354,7 @@ void DataFlash_APM1::WriteByte(byte data) df_BufferIdx=4; //(4 bytes for FileNumber, FilePage) BufferToPage(df_BufferNum,df_PageAdr,0); // Write Buffer to memory, NO WAIT df_PageAdr++; - if (OVERWRITE_DATA==1) + if (DF_OVERWRITE_DATA==1) { if (df_PageAdr>DF_LAST_PAGE) // If we reach the end of the memory, start from the begining df_PageAdr = 1; diff --git a/libraries/DataFlash/DataFlash_APM1.h b/libraries/DataFlash/DataFlash_APM1.h index 5df090905e..66b108e9ea 100644 --- a/libraries/DataFlash/DataFlash_APM1.h +++ b/libraries/DataFlash/DataFlash_APM1.h @@ -6,9 +6,6 @@ #include "DataFlash.h" -// flash size -#define DF_LAST_PAGE 4096 - class DataFlash_APM1 : public DataFlash_Class { private: @@ -38,6 +35,7 @@ class DataFlash_APM1 : public DataFlash_Class unsigned char df_device_0; unsigned char df_device_1; uint16_t df_PageSize; + uint16_t df_NumPages; DataFlash_APM1(); // Constructor void Init(); diff --git a/libraries/DataFlash/DataFlash_APM2.cpp b/libraries/DataFlash/DataFlash_APM2.cpp index f0946cfde5..f22c852b92 100644 --- a/libraries/DataFlash/DataFlash_APM2.cpp +++ b/libraries/DataFlash/DataFlash_APM2.cpp @@ -53,7 +53,7 @@ extern "C" { # error Please check the Tools/Board menu to ensure you have selected Arduino Mega as your target. #endif -#define DF_MAX_PAGE 8192 +#define DF_LAST_PAGE 8192 // AT45DB321D Commands (from Datasheet) #define DF_TRANSFER_PAGE_TO_BUFFER_1 0x53 @@ -76,8 +76,6 @@ extern "C" { #define DF_CHIP_ERASE_3 0x9A -#define OVERWRITE_DATA 0 // 0: When reach the end page stop, 1: Start overwritten from page 1 - // *** INTERNAL FUNCTIONS *** unsigned char DataFlash_APM2::SPI_transfer(unsigned char data) { @@ -136,6 +134,7 @@ void DataFlash_APM2::Init(void) // get page size: 512 or 528 (by default: 528) df_PageSize=PageSize(); + df_NumPages = DF_LAST_PAGE; } // This function is mainly to test the device @@ -335,14 +334,14 @@ void DataFlash_APM2::FinishWrite(void) df_BufferIdx=0; BufferToPage(df_BufferNum,df_PageAdr,0); // Write Buffer to memory, NO WAIT df_PageAdr++; - if (OVERWRITE_DATA==1) + if (DF_OVERWRITE_DATA==1) { - if (df_PageAdr>=DF_MAX_PAGE) // If we reach the end of the memory, start from the begining + if (df_PageAdr>DF_LAST_PAGE) // If we reach the end of the memory, start from the begining df_PageAdr = 1; } else { - if (df_PageAdr>=DF_MAX_PAGE) // If we reach the end of the memory, stop here + if (df_PageAdr>DF_LAST_PAGE) // If we reach the end of the memory, stop here df_Stop_Write=1; } @@ -364,14 +363,14 @@ void DataFlash_APM2::WriteByte(byte data) df_BufferIdx=4; //(4 bytes for FileNumber, FilePage) BufferToPage(df_BufferNum,df_PageAdr,0); // Write Buffer to memory, NO WAIT df_PageAdr++; - if (OVERWRITE_DATA==1) + if (DF_OVERWRITE_DATA==1) { - if (df_PageAdr>=DF_MAX_PAGE) // If we reach the end of the memory, start from the begining + if (df_PageAdr>DF_LAST_PAGE) // If we reach the end of the memory, start from the begining df_PageAdr = 1; } else { - if (df_PageAdr>=DF_MAX_PAGE) // If we reach the end of the memory, stop here + if (df_PageAdr>DF_LAST_PAGE) // If we reach the end of the memory, stop here df_Stop_Write=1; } @@ -437,13 +436,14 @@ byte DataFlash_APM2::ReadByte() WaitReady(); result = BufferRead(df_Read_BufferNum,df_Read_BufferIdx); + df_Read_BufferIdx++; if (df_Read_BufferIdx >= df_PageSize) // End of buffer? { df_Read_BufferIdx=4; //(4 bytes for FileNumber, FilePage) PageToBuffer(df_Read_BufferNum,df_Read_PageAdr); // Write memory page to Buffer df_Read_PageAdr++; - if (df_Read_PageAdr>=DF_MAX_PAGE) // If we reach the end of the memory, start from the begining + if (df_Read_PageAdr>DF_LAST_PAGE) // If we reach the end of the memory, start from the begining { df_Read_PageAdr = 0; df_Read_END = true; diff --git a/libraries/DataFlash/DataFlash_APM2.h b/libraries/DataFlash/DataFlash_APM2.h index 3fec545c4f..b20edc9a46 100644 --- a/libraries/DataFlash/DataFlash_APM2.h +++ b/libraries/DataFlash/DataFlash_APM2.h @@ -42,6 +42,7 @@ class DataFlash_APM2 : public DataFlash_Class unsigned char df_device_0; unsigned char df_device_1; uint16_t df_PageSize; + uint16_t df_NumPages; DataFlash_APM2(); // Constructor void Init();