2011-11-20 04:51:16 -04:00
|
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-11-12 23:29:07 -04:00
|
|
|
|
/*
|
2012-08-17 03:21:25 -03:00
|
|
|
|
* DataFlash_APM2.cpp - DataFlash log library for AT45DB321D
|
|
|
|
|
* Code by Jordi Muñoz and Jose Julio. DIYDrones.com
|
|
|
|
|
* This code works only on ATMega2560. It uses Serial port 3 in SPI MSPI mdoe.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* Dataflash library for AT45DB321D flash memory
|
|
|
|
|
* Memory organization : 8192 pages of 512 bytes or 528 bytes
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
|
|
|
|
* Methods:
|
|
|
|
|
* Init() : Library initialization (SPI initialization)
|
|
|
|
|
* StartWrite(page) : Start a write session. page=start page.
|
|
|
|
|
* WriteByte(data) : Write a byte
|
|
|
|
|
* WriteInt(data) : Write an integer (2 bytes)
|
|
|
|
|
* WriteLong(data) : Write a long (4 bytes)
|
|
|
|
|
* StartRead(page) : Start a read on (page)
|
|
|
|
|
* GetWritePage() : Returns the last page written to
|
|
|
|
|
* GetPage() : Returns the last page read
|
|
|
|
|
* ReadByte()
|
|
|
|
|
* ReadInt()
|
|
|
|
|
* ReadLong()
|
|
|
|
|
*
|
|
|
|
|
* Properties:
|
|
|
|
|
*
|
|
|
|
|
*/
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
|
|
|
|
extern "C" {
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// AVR LibC Includes
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <avr/interrupt.h>
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
2012-08-15 18:25:28 -03:00
|
|
|
|
//#include <FastSerial.h>
|
|
|
|
|
//#include <AP_Common.h>
|
|
|
|
|
|
2012-01-27 23:25:47 -04:00
|
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
2012-08-17 03:21:25 -03:00
|
|
|
|
#include "Arduino.h"
|
2012-01-27 23:25:47 -04:00
|
|
|
|
#else
|
2012-08-17 03:21:25 -03:00
|
|
|
|
#include "WConstants.h"
|
2012-01-27 23:25:47 -04:00
|
|
|
|
#endif
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2011-11-25 19:11:14 -04:00
|
|
|
|
#include "DataFlash_APM2.h"
|
2012-08-15 18:25:28 -03:00
|
|
|
|
/*
|
2012-08-17 03:21:25 -03:00
|
|
|
|
* #define ENABLE_FASTSERIAL_DEBUG
|
|
|
|
|
*
|
|
|
|
|
* #ifdef ENABLE_FASTSERIAL_DEBUG
|
|
|
|
|
# define serialDebug(fmt, args...) if (FastSerial::getInitialized(0)) do {Serial.printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__ , ##args); delay(0); } while(0)
|
|
|
|
|
##else
|
|
|
|
|
# define serialDebug(fmt, args...)
|
|
|
|
|
##endif
|
|
|
|
|
# //*/
|
2012-08-16 19:37:28 -03:00
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
// DataFlash is connected to Serial Port 3 (we will use SPI mode)
|
|
|
|
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
2012-08-17 03:21:25 -03:00
|
|
|
|
#define DF_DATAOUT 14 // MOSI
|
|
|
|
|
#define DF_DATAIN 15 // MISO
|
|
|
|
|
#define DF_SPICLOCK PJ2 // SCK
|
|
|
|
|
#define DF_SLAVESELECT 28 // SS (PA6)
|
|
|
|
|
#define DF_RESET 41 // RESET (PG0)
|
|
|
|
|
#define DF_CARDDETECT 33 // PC4
|
2011-11-12 23:29:07 -04:00
|
|
|
|
#else
|
2012-08-17 03:21:25 -03:00
|
|
|
|
# error Please check the Tools/Board menu to ensure you have selected Arduino Mega as your target.
|
2011-11-12 23:29:07 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// AT45DB321D Commands (from Datasheet)
|
|
|
|
|
#define DF_TRANSFER_PAGE_TO_BUFFER_1 0x53
|
|
|
|
|
#define DF_TRANSFER_PAGE_TO_BUFFER_2 0x55
|
|
|
|
|
#define DF_STATUS_REGISTER_READ 0xD7
|
|
|
|
|
#define DF_READ_MANUFACTURER_AND_DEVICE_ID 0x9F
|
|
|
|
|
#define DF_PAGE_READ 0xD2
|
|
|
|
|
#define DF_BUFFER_1_READ 0xD4
|
|
|
|
|
#define DF_BUFFER_2_READ 0xD6
|
|
|
|
|
#define DF_BUFFER_1_WRITE 0x84
|
|
|
|
|
#define DF_BUFFER_2_WRITE 0x87
|
|
|
|
|
#define DF_BUFFER_1_TO_PAGE_WITH_ERASE 0x83
|
|
|
|
|
#define DF_BUFFER_2_TO_PAGE_WITH_ERASE 0x86
|
|
|
|
|
#define DF_PAGE_ERASE 0x81
|
|
|
|
|
#define DF_BLOCK_ERASE 0x50
|
|
|
|
|
#define DF_SECTOR_ERASE 0x7C
|
|
|
|
|
#define DF_CHIP_ERASE_0 0xC7
|
|
|
|
|
#define DF_CHIP_ERASE_1 0x94
|
|
|
|
|
#define DF_CHIP_ERASE_2 0x80
|
|
|
|
|
#define DF_CHIP_ERASE_3 0x9A
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// *** INTERNAL FUNCTIONS ***
|
2011-11-25 19:11:14 -04:00
|
|
|
|
unsigned char DataFlash_APM2::SPI_transfer(unsigned char data)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
/* Wait for empty transmit buffer */
|
|
|
|
|
while ( !( UCSR3A & (1<<UDRE3)) ) ;
|
|
|
|
|
/* Put data into buffer, sends the data */
|
|
|
|
|
UDR3 = data;
|
|
|
|
|
/* Wait for data to be received */
|
|
|
|
|
while ( !(UCSR3A & (1<<RXC3)) ) ;
|
|
|
|
|
/* Get and return received data from buffer */
|
|
|
|
|
return UDR3;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 08:14:45 -03:00
|
|
|
|
// disable device
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::CS_inactive()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
digitalWrite(DF_SLAVESELECT,HIGH);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-21 08:14:45 -03:00
|
|
|
|
// enable device
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::CS_active()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
digitalWrite(DF_SLAVESELECT,LOW);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Constructors ////////////////////////////////////////////////////////////////
|
2011-11-25 19:11:14 -04:00
|
|
|
|
DataFlash_APM2::DataFlash_APM2()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::Init(void)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// init to zero
|
|
|
|
|
df_NumPages = 0;
|
|
|
|
|
|
|
|
|
|
pinMode(DF_DATAOUT, OUTPUT);
|
|
|
|
|
pinMode(DF_DATAIN, INPUT);
|
|
|
|
|
pinMode(DF_SLAVESELECT,OUTPUT);
|
|
|
|
|
pinMode(DF_RESET,OUTPUT);
|
|
|
|
|
pinMode(DF_CARDDETECT, INPUT);
|
|
|
|
|
|
|
|
|
|
// Reset the chip
|
|
|
|
|
digitalWrite(DF_RESET,LOW);
|
|
|
|
|
delay(1);
|
|
|
|
|
digitalWrite(DF_RESET,HIGH);
|
|
|
|
|
|
|
|
|
|
// disable device
|
|
|
|
|
CS_inactive();
|
|
|
|
|
|
|
|
|
|
// Setup Serial Port3 in SPI mode (MSPI), Mode 0, Clock: 8Mhz
|
|
|
|
|
UBRR3 = 0;
|
|
|
|
|
DDRJ |= (1<<PJ2); // SPI clock XCK3 (PJ2) as output. This enable SPI Master mode
|
|
|
|
|
// Set MSPI mode of operation and SPI data mode 0.
|
|
|
|
|
UCSR3C = (1<<UMSEL31)|(1<<UMSEL30); //|(1<<1)|(1<<UCPOL3);
|
|
|
|
|
// Enable receiver and transmitter.
|
|
|
|
|
UCSR3B = (1<<RXEN3)|(1<<TXEN3);
|
|
|
|
|
// Set Baud rate
|
|
|
|
|
UBRR3 = 0; // SPI running at 8Mhz
|
|
|
|
|
|
|
|
|
|
// get page size: 512 or 528 (by default: 528)
|
|
|
|
|
df_PageSize=PageSize();
|
|
|
|
|
|
|
|
|
|
ReadManufacturerID();
|
|
|
|
|
|
|
|
|
|
// see page 22 of the spec for the density code
|
|
|
|
|
uint8_t density_code = (df_device >> 8) & 0x1F;
|
|
|
|
|
|
|
|
|
|
// note that we set df_NumPages to one lower than the highest, as
|
|
|
|
|
// the last page is reserved for a config page
|
|
|
|
|
if (density_code == 0x7) {
|
|
|
|
|
// 32 Mbit
|
|
|
|
|
df_NumPages = 8191;
|
|
|
|
|
} else if (density_code == 0x6) {
|
|
|
|
|
// 16 Mbit
|
|
|
|
|
df_NumPages = 4095;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//serialDebug("density_code %d pages %d, size %d\n", density_code, df_NumPages, df_PageSize);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function is mainly to test the device
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::ReadManufacturerID()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// Read manufacturer and ID command...
|
|
|
|
|
SPI_transfer(DF_READ_MANUFACTURER_AND_DEVICE_ID);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
df_manufacturer = SPI_transfer(0xff);
|
|
|
|
|
df_device = SPI_transfer(0xff);
|
|
|
|
|
df_device = (df_device<<8) | SPI_transfer(0xff);
|
|
|
|
|
SPI_transfer(0xff);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function return 1 if Card is inserted on SD slot
|
2011-11-25 19:11:14 -04:00
|
|
|
|
bool DataFlash_APM2::CardInserted()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
//serialDebug("df_NumPages %d, detect:%d\n", df_NumPages, tmp);
|
|
|
|
|
//return (df_NumPages >= 4095 && digitalRead(DF_CARDDETECT) == 0);
|
|
|
|
|
return (df_NumPages >= 4095);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the status register
|
2011-11-25 19:11:14 -04:00
|
|
|
|
byte DataFlash_APM2::ReadStatusReg()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
byte tmp;
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// Read status command
|
|
|
|
|
SPI_transfer(DF_STATUS_REGISTER_READ);
|
|
|
|
|
tmp = SPI_transfer(0x00); // We only want to extract the READY/BUSY bit
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
return tmp;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the status of the DataFlash
|
|
|
|
|
inline
|
2011-11-25 19:11:14 -04:00
|
|
|
|
byte DataFlash_APM2::ReadStatus()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
return(ReadStatusReg()&0x80); // We only want to extract the READY/BUSY bit
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline
|
2011-11-25 19:11:14 -04:00
|
|
|
|
uint16_t DataFlash_APM2::PageSize()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
return(528-((ReadStatusReg()&0x01)<<4)); // if first bit 1 trhen 512 else 528 bytes
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait until DataFlash is in ready state...
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::WaitReady()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
while(!ReadStatus()) ;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::PageToBuffer(unsigned char BufferNum, uint16_t PageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
if (BufferNum==1)
|
|
|
|
|
SPI_transfer(DF_TRANSFER_PAGE_TO_BUFFER_1);
|
|
|
|
|
else
|
|
|
|
|
SPI_transfer(DF_TRANSFER_PAGE_TO_BUFFER_2);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
//initiate the transfer
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
while(!ReadStatus()) ; //monitor the status register, wait until busy-flag is high
|
|
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::BufferToPage (unsigned char BufferNum, uint16_t PageAdr, unsigned char wait)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
if (BufferNum==1)
|
|
|
|
|
SPI_transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE);
|
|
|
|
|
else
|
|
|
|
|
SPI_transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
//initiate the transfer
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::BufferWrite (unsigned char BufferNum, uint16_t IntPageAdr, unsigned char Data)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
if (BufferNum==1)
|
|
|
|
|
SPI_transfer(DF_BUFFER_1_WRITE);
|
|
|
|
|
else
|
|
|
|
|
SPI_transfer(DF_BUFFER_2_WRITE);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
SPI_transfer(0x00); // don't care
|
|
|
|
|
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
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 19:11:14 -04:00
|
|
|
|
unsigned char DataFlash_APM2::BufferRead (unsigned char BufferNum, uint16_t IntPageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
byte tmp;
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
if (BufferNum==1)
|
|
|
|
|
SPI_transfer(DF_BUFFER_1_READ);
|
|
|
|
|
else
|
|
|
|
|
SPI_transfer(DF_BUFFER_2_READ);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
SPI_transfer(0x00);
|
|
|
|
|
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
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
return (tmp);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
// *** END OF INTERNAL FUNCTIONS ***
|
|
|
|
|
|
2011-11-25 19:11:14 -04:00
|
|
|
|
void DataFlash_APM2::PageErase (uint16_t PageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
// Send page erase command
|
|
|
|
|
SPI_transfer(DF_PAGE_ERASE);
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
SPI_transfer(0x00);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
//initiate flash page erase
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
|
|
|
|
while(!ReadStatus()) ;
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-03 23:36:42 -03:00
|
|
|
|
// erase a block of 8 pages.
|
|
|
|
|
void DataFlash_APM2::BlockErase(uint16_t BlockAdr)
|
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
// Send block erase command
|
|
|
|
|
SPI_transfer(DF_BLOCK_ERASE);
|
|
|
|
|
|
|
|
|
|
if (df_PageSize==512) {
|
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr >> 4));
|
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr << 4));
|
|
|
|
|
} else {
|
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr >> 3));
|
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr << 5));
|
|
|
|
|
}
|
|
|
|
|
SPI_transfer(0x00);
|
|
|
|
|
//serialDebug("BL Erase, %d\n", BlockAdr);
|
|
|
|
|
|
|
|
|
|
//initiate flash page erase
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
|
|
|
|
while(!ReadStatus()) ;
|
|
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2012-07-03 23:36:42 -03:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-18 08:28:31 -03:00
|
|
|
|
void DataFlash_APM2::ChipErase(void (*delay_cb)(unsigned long))
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:25 -03:00
|
|
|
|
//serialDebug("Chip Erase\n");
|
2012-08-15 18:25:28 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// opcodes for chip erase
|
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_0);
|
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_1);
|
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_2);
|
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_3);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
//initiate flash page erase
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
2012-05-22 03:35:53 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
while(!ReadStatus()) {
|
2012-05-22 03:35:53 -03:00
|
|
|
|
delay_cb(1);
|
|
|
|
|
}
|
2012-04-21 08:14:45 -03:00
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2012-05-22 03:35:53 -03:00
|
|
|
|
}
|