From 3ef57bf3bfeca4051c46231d143646a687322bb5 Mon Sep 17 00:00:00 2001 From: "rmackay9@yahoo.com" Date: Sat, 30 Apr 2011 03:25:31 +0000 Subject: [PATCH] DataFlash - modified to use Arduino SPI interface instead of directly using registers. Also moved "dataflash_CS_inactive" calls to end of functions instead of beginning so that it doesn't monopolize the SPI bus. git-svn-id: https://arducopter.googlecode.com/svn/trunk@1931 f9c3cf11-9bcb-44bc-f272-b75c42450872 --- libraries/DataFlash/DataFlash.cpp | 162 +++++++++++++++--------------- 1 file changed, 83 insertions(+), 79 deletions(-) diff --git a/libraries/DataFlash/DataFlash.cpp b/libraries/DataFlash/DataFlash.cpp index 10c798e906..0b770a919b 100644 --- a/libraries/DataFlash/DataFlash.cpp +++ b/libraries/DataFlash/DataFlash.cpp @@ -39,16 +39,11 @@ extern "C" { } #include "DataFlash.h" +#include "../SPI/SPI.h" #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(); - if (BufferNum==1) - dataflash_SPI_transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE); - else - dataflash_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)); + if (BufferNum==1) { + SPI.transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE); }else{ - dataflash_SPI_transfer((unsigned char)(PageAdr >> 6)); - dataflash_SPI_transfer((unsigned char)(PageAdr << 2)); + SPI.transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE); } - dataflash_SPI_transfer(0x00); // don´t care bytes + + if(df_PageSize==512){ + SPI.transfer((unsigned char)(PageAdr >> 7)); + SPI.transfer((unsigned char)(PageAdr << 1)); + }else{ + SPI.transfer((unsigned char)(PageAdr >> 6)); + SPI.transfer((unsigned char)(PageAdr << 2)); + } + SPI.transfer(0x00); // don´t care bytes dataflash_CS_inactive(); //initiate the transfer dataflash_CS_active(); @@ -197,39 +193,45 @@ 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(); // Reset 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(); - if (BufferNum==1) - dataflash_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 + if (BufferNum==1) { + SPI.transfer(DF_BUFFER_1_WRITE); + }else{ + 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(); // Reset 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(); - if (BufferNum==1) - dataflash_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 + if (BufferNum==1) { + SPI.transfer(DF_BUFFER_1_READ); + }else{ + 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(); // Reset dataflash command decoder return (tmp); } @@ -237,38 +239,40 @@ 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 + 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(); //make sure to toggle CS signal in order } void DataFlash_Class::ChipErase () { - dataflash_CS_inactive(); //make sure to toggle CS signal in order dataflash_CS_active(); //to reset 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(); //make sure to toggle CS signal in order } // *** DATAFLASH PUBLIC FUNCTIONS ***