uncrustify libraries/DataFlash/DataFlash_APM1.cpp

This commit is contained in:
uncrustify 2012-08-16 23:21:26 -07:00 committed by Pat Hickey
parent a32a9bd986
commit 0fd0f46568

View File

@ -1,36 +1,36 @@
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- /// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/* /*
DataFlash_APM1.cpp - DataFlash log library for AT45DB161 * DataFlash_APM1.cpp - DataFlash log library for AT45DB161
Code by Jordi Munoz and Jose Julio. DIYDrones.com * Code by Jordi Munoz and Jose Julio. DIYDrones.com
This code works with boards based on ATMega168/328 and ATMega1280/2560 using SPI port * This code works with boards based on ATMega168/328 and ATMega1280/2560 using SPI port
*
This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
*
Dataflash library for AT45DB161D flash memory * Dataflash library for AT45DB161D flash memory
Memory organization : 4096 pages of 512 bytes or 528 bytes * Memory organization : 4096 pages of 512 bytes or 528 bytes
*
Maximun write bandwidth : 512 bytes in 14ms * Maximun write bandwidth : 512 bytes in 14ms
This code is written so the master never has to wait to write the data on the eeprom * This code is written so the master never has to wait to write the data on the eeprom
*
Methods: * Methods:
Init() : Library initialization (SPI initialization) * Init() : Library initialization (SPI initialization)
StartWrite(page) : Start a write session. page=start page. * StartWrite(page) : Start a write session. page=start page.
WriteByte(data) : Write a byte * WriteByte(data) : Write a byte
WriteInt(data) : Write an integer (2 bytes) * WriteInt(data) : Write an integer (2 bytes)
WriteLong(data) : Write a long (4 bytes) * WriteLong(data) : Write a long (4 bytes)
StartRead(page) : Start a read on (page) * StartRead(page) : Start a read on (page)
GetWritePage() : Returns the last page written to * GetWritePage() : Returns the last page written to
GetPage() : Returns the last page read * GetPage() : Returns the last page read
ReadByte() * ReadByte()
ReadInt() * ReadInt()
ReadLong() * ReadLong()
*
Properties: * Properties:
*
*/ */
#include <stdint.h> #include <stdint.h>
#include "DataFlash.h" #include "DataFlash.h"
@ -41,16 +41,16 @@
// arduino mega SPI pins // arduino mega SPI pins
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define DF_DATAOUT 51 // MOSI #define DF_DATAOUT 51 // MOSI
#define DF_DATAIN 50 // MISO #define DF_DATAIN 50 // MISO
#define DF_SPICLOCK 52 // SCK #define DF_SPICLOCK 52 // SCK
#define DF_SLAVESELECT 53 // SS (PB0) #define DF_SLAVESELECT 53 // SS (PB0)
#define DF_RESET 31 // RESET (PC6) #define DF_RESET 31 // RESET (PC6)
#else // normal arduino SPI pins... #else // normal arduino SPI pins...
#define DF_DATAOUT 11 //MOSI #define DF_DATAOUT 11 //MOSI
#define DF_DATAIN 12 //MISO #define DF_DATAIN 12 //MISO
#define DF_SPICLOCK 13 //SCK #define DF_SPICLOCK 13 //SCK
#define DF_SLAVESELECT 10 //SS #define DF_SLAVESELECT 10 //SS
#endif #endif
// AT45DB161D Commands (from Datasheet) // AT45DB161D Commands (from Datasheet)
@ -77,12 +77,12 @@
void dataflash_CS_inactive() void dataflash_CS_inactive()
{ {
digitalWrite(DF_SLAVESELECT,HIGH); //disable device digitalWrite(DF_SLAVESELECT,HIGH); //disable device
} }
void dataflash_CS_active() void dataflash_CS_active()
{ {
digitalWrite(DF_SLAVESELECT,LOW); //enable device digitalWrite(DF_SLAVESELECT,LOW); //enable device
} }
// Constructors //////////////////////////////////////////////////////////////// // Constructors ////////////////////////////////////////////////////////////////
@ -93,47 +93,47 @@ DataFlash_APM1::DataFlash_APM1()
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
void DataFlash_APM1::Init(void) void DataFlash_APM1::Init(void)
{ {
pinMode(DF_DATAOUT, OUTPUT); pinMode(DF_DATAOUT, OUTPUT);
pinMode(DF_DATAIN, INPUT); pinMode(DF_DATAIN, INPUT);
pinMode(DF_SPICLOCK,OUTPUT); pinMode(DF_SPICLOCK,OUTPUT);
pinMode(DF_SLAVESELECT,OUTPUT); pinMode(DF_SLAVESELECT,OUTPUT);
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
pinMode(DF_RESET,OUTPUT); pinMode(DF_RESET,OUTPUT);
// Reset the chip // Reset the chip
digitalWrite(DF_RESET,LOW); digitalWrite(DF_RESET,LOW);
delay(1); delay(1);
digitalWrite(DF_RESET,HIGH); digitalWrite(DF_RESET,HIGH);
#endif #endif
dataflash_CS_inactive(); //disable device dataflash_CS_inactive(); //disable device
// Setup SPI Master, Mode 3, fosc/4 = 4MHz // Setup SPI Master, Mode 3, fosc/4 = 4MHz
SPI.begin(); SPI.begin();
SPI.setBitOrder(MSBFIRST); SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3); SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV2); SPI.setClockDivider(SPI_CLOCK_DIV2);
// get page size: 512 or 528 // get page size: 512 or 528
df_PageSize=PageSize(); df_PageSize=PageSize();
// the last page is reserved for config information // the last page is reserved for config information
df_NumPages = DF_LAST_PAGE - 1; df_NumPages = DF_LAST_PAGE - 1;
} }
// This function is mainly to test the device // This function is mainly to test the device
void DataFlash_APM1::ReadManufacturerID() void DataFlash_APM1::ReadManufacturerID()
{ {
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
// Read manufacturer and ID command... // Read manufacturer and ID command...
SPI.transfer(DF_READ_MANUFACTURER_AND_DEVICE_ID); SPI.transfer(DF_READ_MANUFACTURER_AND_DEVICE_ID);
df_manufacturer = SPI.transfer(0xff); df_manufacturer = SPI.transfer(0xff);
df_device = SPI.transfer(0xff); df_device = SPI.transfer(0xff);
df_device = (df_device<<8) | SPI.transfer(0xff); df_device = (df_device<<8) | SPI.transfer(0xff);
SPI.transfer(0xff); SPI.transfer(0xff);
dataflash_CS_inactive(); // Reset dataflash command decoder dataflash_CS_inactive(); // Reset dataflash command decoder
} }
@ -145,174 +145,174 @@ bool DataFlash_APM1::CardInserted(void)
// Read the status register // Read the status register
byte DataFlash_APM1::ReadStatusReg() byte DataFlash_APM1::ReadStatusReg()
{ {
byte tmp; byte tmp;
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
// Read status command // Read status command
SPI.transfer(DF_STATUS_REGISTER_READ); SPI.transfer(DF_STATUS_REGISTER_READ);
tmp = SPI.transfer(0x00); // We only want to extract the READY/BUSY bit tmp = SPI.transfer(0x00); // We only want to extract the READY/BUSY bit
dataflash_CS_inactive(); // Reset dataflash command decoder dataflash_CS_inactive(); // Reset dataflash command decoder
return tmp; return tmp;
} }
// Read the status of the DataFlash // Read the status of the DataFlash
inline inline
byte DataFlash_APM1::ReadStatus() byte DataFlash_APM1::ReadStatus()
{ {
return(ReadStatusReg()&0x80); // We only want to extract the READY/BUSY bit return(ReadStatusReg()&0x80); // We only want to extract the READY/BUSY bit
} }
inline inline
uint16_t DataFlash_APM1::PageSize() uint16_t DataFlash_APM1::PageSize()
{ {
return(528-((ReadStatusReg()&0x01)<<4)); // if first bit 1 trhen 512 else 528 bytes return(528-((ReadStatusReg()&0x01)<<4)); // if first bit 1 trhen 512 else 528 bytes
} }
// Wait until DataFlash is in ready state... // Wait until DataFlash is in ready state...
void DataFlash_APM1::WaitReady() void DataFlash_APM1::WaitReady()
{ {
while(!ReadStatus()); while(!ReadStatus()) ;
} }
void DataFlash_APM1::PageToBuffer(unsigned char BufferNum, uint16_t PageAdr) void DataFlash_APM1::PageToBuffer(unsigned char BufferNum, uint16_t PageAdr)
{ {
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
if (BufferNum==1) if (BufferNum==1)
SPI.transfer(DF_TRANSFER_PAGE_TO_BUFFER_1); SPI.transfer(DF_TRANSFER_PAGE_TO_BUFFER_1);
else else
SPI.transfer(DF_TRANSFER_PAGE_TO_BUFFER_2); SPI.transfer(DF_TRANSFER_PAGE_TO_BUFFER_2);
if(df_PageSize==512){ if(df_PageSize==512) {
SPI.transfer((unsigned char)(PageAdr >> 7)); SPI.transfer((unsigned char)(PageAdr >> 7));
SPI.transfer((unsigned char)(PageAdr << 1)); SPI.transfer((unsigned char)(PageAdr << 1));
}else{ }else{
SPI.transfer((unsigned char)(PageAdr >> 6)); SPI.transfer((unsigned char)(PageAdr >> 6));
SPI.transfer((unsigned char)(PageAdr << 2)); SPI.transfer((unsigned char)(PageAdr << 2));
} }
SPI.transfer(0x00); // don´t care bytes SPI.transfer(0x00); // don´t care bytes
dataflash_CS_inactive(); //initiate the transfer dataflash_CS_inactive(); //initiate the transfer
dataflash_CS_active(); dataflash_CS_active();
while(!ReadStatus()); //monitor the status register, wait until busy-flag is high while(!ReadStatus()) ; //monitor the status register, wait until busy-flag is high
dataflash_CS_inactive(); dataflash_CS_inactive();
} }
void DataFlash_APM1::BufferToPage (unsigned char BufferNum, uint16_t PageAdr, unsigned char wait) void DataFlash_APM1::BufferToPage (unsigned char BufferNum, uint16_t PageAdr, unsigned char wait)
{ {
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
if (BufferNum==1) if (BufferNum==1)
SPI.transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE); SPI.transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE);
else else
SPI.transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE); SPI.transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE);
if(df_PageSize==512){ if(df_PageSize==512) {
SPI.transfer((unsigned char)(PageAdr >> 7)); SPI.transfer((unsigned char)(PageAdr >> 7));
SPI.transfer((unsigned char)(PageAdr << 1)); SPI.transfer((unsigned char)(PageAdr << 1));
}else{ }else{
SPI.transfer((unsigned char)(PageAdr >> 6)); SPI.transfer((unsigned char)(PageAdr >> 6));
SPI.transfer((unsigned char)(PageAdr << 2)); SPI.transfer((unsigned char)(PageAdr << 2));
} }
SPI.transfer(0x00); // don´t care bytes SPI.transfer(0x00); // don´t care bytes
dataflash_CS_inactive(); //initiate the transfer dataflash_CS_inactive(); //initiate the transfer
dataflash_CS_active(); dataflash_CS_active();
// Check if we need to wait to write the buffer to memory or we can continue... // Check if we need to wait to write the buffer to memory or we can continue...
if (wait) if (wait)
while(!ReadStatus()); //monitor the status register, wait until busy-flag is high while(!ReadStatus()) ; //monitor the status register, wait until busy-flag is high
dataflash_CS_inactive(); //deactivate dataflash command decoder dataflash_CS_inactive(); //deactivate dataflash command decoder
} }
void DataFlash_APM1::BufferWrite (unsigned char BufferNum, uint16_t IntPageAdr, unsigned char Data) void DataFlash_APM1::BufferWrite (unsigned char BufferNum, uint16_t IntPageAdr, unsigned char Data)
{ {
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
if (BufferNum==1) if (BufferNum==1)
SPI.transfer(DF_BUFFER_1_WRITE); SPI.transfer(DF_BUFFER_1_WRITE);
else else
SPI.transfer(DF_BUFFER_2_WRITE); SPI.transfer(DF_BUFFER_2_WRITE);
SPI.transfer(0x00); //don't cares SPI.transfer(0x00); //don't cares
SPI.transfer((unsigned char)(IntPageAdr>>8)); //upper part of internal buffer address 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((unsigned char)(IntPageAdr)); //lower part of internal buffer address
SPI.transfer(Data); //write data byte SPI.transfer(Data); //write data byte
dataflash_CS_inactive(); // disable dataflash command decoder dataflash_CS_inactive(); // disable dataflash command decoder
} }
unsigned char DataFlash_APM1::BufferRead (unsigned char BufferNum, uint16_t IntPageAdr) unsigned char DataFlash_APM1::BufferRead (unsigned char BufferNum, uint16_t IntPageAdr)
{ {
byte tmp; byte tmp;
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
if (BufferNum==1) if (BufferNum==1)
SPI.transfer(DF_BUFFER_1_READ); SPI.transfer(DF_BUFFER_1_READ);
else else
SPI.transfer(DF_BUFFER_2_READ); SPI.transfer(DF_BUFFER_2_READ);
SPI.transfer(0x00); //don't cares SPI.transfer(0x00); //don't cares
SPI.transfer((unsigned char)(IntPageAdr>>8)); //upper part of internal buffer address 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((unsigned char)(IntPageAdr)); //lower part of internal buffer address
SPI.transfer(0x00); //don't cares SPI.transfer(0x00); //don't cares
tmp = SPI.transfer(0x00); //read data byte tmp = SPI.transfer(0x00); //read data byte
dataflash_CS_inactive(); // deactivate dataflash command decoder dataflash_CS_inactive(); // deactivate dataflash command decoder
return (tmp); return (tmp);
} }
// *** END OF INTERNAL FUNCTIONS *** // *** END OF INTERNAL FUNCTIONS ***
void DataFlash_APM1::PageErase (uint16_t PageAdr) void DataFlash_APM1::PageErase (uint16_t PageAdr)
{ {
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
SPI.transfer(DF_PAGE_ERASE); // Command SPI.transfer(DF_PAGE_ERASE); // Command
if(df_PageSize==512){ if(df_PageSize==512) {
SPI.transfer((unsigned char)(PageAdr >> 7)); SPI.transfer((unsigned char)(PageAdr >> 7));
SPI.transfer((unsigned char)(PageAdr << 1)); SPI.transfer((unsigned char)(PageAdr << 1));
}else{ }else{
SPI.transfer((unsigned char)(PageAdr >> 6)); SPI.transfer((unsigned char)(PageAdr >> 6));
SPI.transfer((unsigned char)(PageAdr << 2)); SPI.transfer((unsigned char)(PageAdr << 2));
} }
SPI.transfer(0x00); // "dont cares" SPI.transfer(0x00); // "dont cares"
dataflash_CS_inactive(); //initiate flash page erase dataflash_CS_inactive(); //initiate flash page erase
dataflash_CS_active(); dataflash_CS_active();
while(!ReadStatus()); while(!ReadStatus()) ;
dataflash_CS_inactive(); // deactivate dataflash command decoder dataflash_CS_inactive(); // deactivate dataflash command decoder
} }
void DataFlash_APM1::BlockErase (uint16_t BlockAdr) void DataFlash_APM1::BlockErase (uint16_t BlockAdr)
{ {
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
SPI.transfer(DF_BLOCK_ERASE); // Command SPI.transfer(DF_BLOCK_ERASE); // Command
if (df_PageSize==512) { if (df_PageSize==512) {
SPI.transfer((unsigned char)(BlockAdr >> 3)); SPI.transfer((unsigned char)(BlockAdr >> 3));
SPI.transfer((unsigned char)(BlockAdr << 5)); SPI.transfer((unsigned char)(BlockAdr << 5));
} else { } else {
SPI.transfer((unsigned char)(BlockAdr >> 4)); SPI.transfer((unsigned char)(BlockAdr >> 4));
SPI.transfer((unsigned char)(BlockAdr << 4)); SPI.transfer((unsigned char)(BlockAdr << 4));
} }
SPI.transfer(0x00); // "dont cares" SPI.transfer(0x00); // "dont cares"
dataflash_CS_inactive(); //initiate flash page erase dataflash_CS_inactive(); //initiate flash page erase
dataflash_CS_active(); dataflash_CS_active();
while(!ReadStatus()); while(!ReadStatus()) ;
dataflash_CS_inactive(); // deactivate dataflash command decoder dataflash_CS_inactive(); // deactivate dataflash command decoder
} }
@ -320,19 +320,19 @@ void DataFlash_APM1::BlockErase (uint16_t BlockAdr)
void DataFlash_APM1::ChipErase(void (*delay_cb)(unsigned long)) void DataFlash_APM1::ChipErase(void (*delay_cb)(unsigned long))
{ {
dataflash_CS_active(); // activate dataflash command decoder dataflash_CS_active(); // activate dataflash command decoder
// opcodes for chip erase // opcodes for chip erase
SPI.transfer(DF_CHIP_ERASE_0); SPI.transfer(DF_CHIP_ERASE_0);
SPI.transfer(DF_CHIP_ERASE_1); SPI.transfer(DF_CHIP_ERASE_1);
SPI.transfer(DF_CHIP_ERASE_2); SPI.transfer(DF_CHIP_ERASE_2);
SPI.transfer(DF_CHIP_ERASE_3); SPI.transfer(DF_CHIP_ERASE_3);
dataflash_CS_inactive(); //initiate flash page erase dataflash_CS_inactive(); //initiate flash page erase
dataflash_CS_active(); dataflash_CS_active();
while (!ReadStatus()) { while (!ReadStatus()) {
delay_cb(1); delay_cb(1);
} }
dataflash_CS_inactive(); // deactivate dataflash command decoder dataflash_CS_inactive(); // deactivate dataflash command decoder
} }