diff --git a/libraries/DataFlash/DataFlash.cpp b/libraries/DataFlash/DataFlash.cpp index 10c798e906..427d1ce2c5 100644 --- a/libraries/DataFlash/DataFlash.cpp +++ b/libraries/DataFlash/DataFlash.cpp @@ -31,24 +31,12 @@ */ -extern "C" { - // AVR LibC Includes - #include - #include - #include "WConstants.h" -} - #include "DataFlash.h" +#include #define OVERWRITE_DATA 0 // 0: When reach the end page stop, 1: Start overwritten from page 1 // *** INTERNAL FUNCTIONS *** -unsigned char dataflash_SPI_transfer(unsigned char output) -{ - SPDR = output; // Start the transmission - while (!(SPSR & (1<> 7)); - dataflash_SPI_transfer((unsigned char)(PageAdr << 1)); + SPI.transfer((unsigned char)(PageAdr >> 7)); + SPI.transfer((unsigned char)(PageAdr << 1)); }else{ - dataflash_SPI_transfer((unsigned char)(PageAdr >> 6)); - dataflash_SPI_transfer((unsigned char)(PageAdr << 2)); + SPI.transfer((unsigned char)(PageAdr >> 6)); + SPI.transfer((unsigned char)(PageAdr << 2)); } - dataflash_SPI_transfer(0x00); // don´t care bytes + SPI.transfer(0x00); // don´t care bytes dataflash_CS_inactive(); //initiate the transfer dataflash_CS_active(); while(!ReadStatus()); //monitor the status register, wait until busy-flag is high + + dataflash_CS_inactive(); } void DataFlash_Class::BufferToPage (unsigned char BufferNum, unsigned int PageAdr, unsigned char wait) { - dataflash_CS_inactive(); // Reset dataflash command decoder - dataflash_CS_active(); + dataflash_CS_active(); // activate dataflash command decoder if (BufferNum==1) - dataflash_SPI_transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE); + SPI.transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE); else - dataflash_SPI_transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE); + SPI.transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE); if(df_PageSize==512){ - dataflash_SPI_transfer((unsigned char)(PageAdr >> 7)); - dataflash_SPI_transfer((unsigned char)(PageAdr << 1)); + SPI.transfer((unsigned char)(PageAdr >> 7)); + SPI.transfer((unsigned char)(PageAdr << 1)); }else{ - dataflash_SPI_transfer((unsigned char)(PageAdr >> 6)); - dataflash_SPI_transfer((unsigned char)(PageAdr << 2)); + SPI.transfer((unsigned char)(PageAdr >> 6)); + SPI.transfer((unsigned char)(PageAdr << 2)); } - dataflash_SPI_transfer(0x00); // don´t care bytes + SPI.transfer(0x00); // don´t care bytes dataflash_CS_inactive(); //initiate the transfer dataflash_CS_active(); @@ -197,39 +189,43 @@ void DataFlash_Class::BufferToPage (unsigned char BufferNum, unsigned int PageAd // Check if we need to wait to write the buffer to memory or we can continue... if (wait) while(!ReadStatus()); //monitor the status register, wait until busy-flag is high + + dataflash_CS_inactive(); //deactivate dataflash command decoder } void DataFlash_Class::BufferWrite (unsigned char BufferNum, unsigned int IntPageAdr, unsigned char Data) { - dataflash_CS_inactive(); // Reset dataflash command decoder - dataflash_CS_active(); + dataflash_CS_active(); // activate dataflash command decoder if (BufferNum==1) - dataflash_SPI_transfer(DF_BUFFER_1_WRITE); + SPI.transfer(DF_BUFFER_1_WRITE); else - dataflash_SPI_transfer(DF_BUFFER_2_WRITE); - dataflash_SPI_transfer(0x00); //don't cares - dataflash_SPI_transfer((unsigned char)(IntPageAdr>>8)); //upper part of internal buffer address - dataflash_SPI_transfer((unsigned char)(IntPageAdr)); //lower part of internal buffer address - dataflash_SPI_transfer(Data); //write data byte + SPI.transfer(DF_BUFFER_2_WRITE); + SPI.transfer(0x00); //don't cares + SPI.transfer((unsigned char)(IntPageAdr>>8)); //upper part of internal buffer address + SPI.transfer((unsigned char)(IntPageAdr)); //lower part of internal buffer address + SPI.transfer(Data); //write data byte + + dataflash_CS_inactive(); // disable dataflash command decoder } unsigned char DataFlash_Class::BufferRead (unsigned char BufferNum, unsigned int IntPageAdr) { byte tmp; - - dataflash_CS_inactive(); // Reset dataflash command decoder - dataflash_CS_active(); + + dataflash_CS_active(); // activate dataflash command decoder if (BufferNum==1) - dataflash_SPI_transfer(DF_BUFFER_1_READ); + SPI.transfer(DF_BUFFER_1_READ); else - dataflash_SPI_transfer(DF_BUFFER_2_READ); - dataflash_SPI_transfer(0x00); //don't cares - dataflash_SPI_transfer((unsigned char)(IntPageAdr>>8)); //upper part of internal buffer address - dataflash_SPI_transfer((unsigned char)(IntPageAdr)); //lower part of internal buffer address - dataflash_SPI_transfer(0x00); //don't cares - tmp = dataflash_SPI_transfer(0x00); //read data byte + SPI.transfer(DF_BUFFER_2_READ); + SPI.transfer(0x00); //don't cares + SPI.transfer((unsigned char)(IntPageAdr>>8)); //upper part of internal buffer address + SPI.transfer((unsigned char)(IntPageAdr)); //lower part of internal buffer address + SPI.transfer(0x00); //don't cares + tmp = SPI.transfer(0x00); //read data byte + + dataflash_CS_inactive(); // deactivate dataflash command decoder return (tmp); } @@ -237,38 +233,41 @@ unsigned char DataFlash_Class::BufferRead (unsigned char BufferNum, unsigned int void DataFlash_Class::PageErase (unsigned int PageAdr) { - dataflash_CS_inactive(); //make sure to toggle CS signal in order - dataflash_CS_active(); //to reset Dataflash command decoder - dataflash_SPI_transfer(DF_PAGE_ERASE); // Command + dataflash_CS_active(); // activate dataflash command decoder + SPI.transfer(DF_PAGE_ERASE); // Command if(df_PageSize==512){ - dataflash_SPI_transfer((unsigned char)(PageAdr >> 7)); - dataflash_SPI_transfer((unsigned char)(PageAdr << 1)); + SPI.transfer((unsigned char)(PageAdr >> 7)); + SPI.transfer((unsigned char)(PageAdr << 1)); }else{ - dataflash_SPI_transfer((unsigned char)(PageAdr >> 6)); - dataflash_SPI_transfer((unsigned char)(PageAdr << 2)); + SPI.transfer((unsigned char)(PageAdr >> 6)); + SPI.transfer((unsigned char)(PageAdr << 2)); } - dataflash_SPI_transfer(0x00); // "dont cares" + SPI.transfer(0x00); // "dont cares" dataflash_CS_inactive(); //initiate flash page erase dataflash_CS_active(); while(!ReadStatus()); + + dataflash_CS_inactive(); // deactivate dataflash command decoder } void DataFlash_Class::ChipErase () { - dataflash_CS_inactive(); //make sure to toggle CS signal in order - dataflash_CS_active(); //to reset Dataflash command decoder + + dataflash_CS_active(); // activate dataflash command decoder // opcodes for chip erase - dataflash_SPI_transfer(DF_CHIP_ERASE_0); - dataflash_SPI_transfer(DF_CHIP_ERASE_1); - dataflash_SPI_transfer(DF_CHIP_ERASE_2); - dataflash_SPI_transfer(DF_CHIP_ERASE_3); + SPI.transfer(DF_CHIP_ERASE_0); + SPI.transfer(DF_CHIP_ERASE_1); + SPI.transfer(DF_CHIP_ERASE_2); + SPI.transfer(DF_CHIP_ERASE_3); dataflash_CS_inactive(); //initiate flash page erase dataflash_CS_active(); while(!ReadStatus()); + + dataflash_CS_inactive(); // deactivate dataflash command decoder } // *** DATAFLASH PUBLIC FUNCTIONS *** diff --git a/libraries/DataFlash/examples/DataFlash_test/DataFlash_test.pde b/libraries/DataFlash/examples/DataFlash_test/DataFlash_test.pde index 42354af979..f3930f6a12 100644 --- a/libraries/DataFlash/examples/DataFlash_test/DataFlash_test.pde +++ b/libraries/DataFlash/examples/DataFlash_test/DataFlash_test.pde @@ -3,6 +3,8 @@ Code by Jordi MuÒoz and Jose Julio. DIYDrones.com */ +// Libraries +#include // Arduino SPI lib #include #define HEAD_BYTE1 0xA3