2011-11-20 04:50:45 -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:26 -03:00
|
|
|
|
* DataFlash_APM1.cpp - DataFlash log library for AT45DB161
|
2012-11-10 02:02:16 -04:00
|
|
|
|
* 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.
|
2012-08-17 03:21:26 -03:00
|
|
|
|
*
|
|
|
|
|
* 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 AT45DB161D flash memory
|
|
|
|
|
* Memory organization : 4096 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
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
extern "C" {
|
|
|
|
|
// AVR LibC Includes
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
}
|
|
|
|
|
#include <FastSerial.h>
|
2011-11-12 23:29:07 -04:00
|
|
|
|
#include <SPI.h>
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
#else
|
|
|
|
|
#include "WConstants.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <AP_Semaphore.h> // for removing conflict with optical flow sensor on SPI3 bus
|
|
|
|
|
#include "DataFlash_APM2.h"
|
|
|
|
|
|
|
|
|
|
///*
|
|
|
|
|
#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
|
|
|
|
|
//*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-26 03:25:43 -04:00
|
|
|
|
// flash size
|
|
|
|
|
#define DF_LAST_PAGE 4096
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2011-11-20 04:50:45 -04:00
|
|
|
|
// arduino mega SPI pins
|
2012-11-18 18:03:35 -04:00
|
|
|
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(DESKTOP_BUILD)
|
2012-08-17 03:21:26 -03:00
|
|
|
|
#define DF_DATAOUT 51 // MOSI
|
|
|
|
|
#define DF_DATAIN 50 // MISO
|
|
|
|
|
#define DF_SPICLOCK 52 // SCK
|
|
|
|
|
#define DF_SLAVESELECT 53 // SS (PB0)
|
|
|
|
|
#define DF_RESET 31 // RESET (PC6)
|
2011-11-20 04:50:45 -04:00
|
|
|
|
#else // normal arduino SPI pins...
|
2012-08-17 03:21:26 -03:00
|
|
|
|
#define DF_DATAOUT 11 //MOSI
|
|
|
|
|
#define DF_DATAIN 12 //MISO
|
|
|
|
|
#define DF_SPICLOCK 13 //SCK
|
|
|
|
|
#define DF_SLAVESELECT 10 //SS
|
2011-11-20 04:50:45 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// AT45DB161D 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
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
// *** INTERNAL FUNCTIONS ***
|
|
|
|
|
|
2012-10-18 05:03:50 -03:00
|
|
|
|
unsigned char DataFlash_APM1::SPI_transfer(unsigned char data)
|
|
|
|
|
{
|
|
|
|
|
unsigned char retval;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// get spi semaphore if required. if failed to get semaphore then
|
|
|
|
|
// just quietly fail
|
2012-10-18 05:03:50 -03:00
|
|
|
|
if ( _spi_semaphore != NULL) {
|
|
|
|
|
if( !_spi_semaphore->get(this) ) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// send the data
|
|
|
|
|
retval = SPI.transfer(data);
|
|
|
|
|
|
|
|
|
|
// release spi3 semaphore
|
|
|
|
|
if ( _spi_semaphore != NULL) {
|
|
|
|
|
_spi_semaphore->release(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// disable device
|
|
|
|
|
void DataFlash_APM1::CS_inactive()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
digitalWrite(DF_SLAVESELECT,HIGH);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// enable device
|
|
|
|
|
void DataFlash_APM1::CS_active()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
digitalWrite(DF_SLAVESELECT,LOW);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
void DataFlash_APM1::Init(void)
|
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// init to zero
|
|
|
|
|
df_NumPages = 0;
|
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
pinMode(DF_DATAOUT, OUTPUT);
|
|
|
|
|
pinMode(DF_DATAIN, INPUT);
|
|
|
|
|
pinMode(DF_SPICLOCK,OUTPUT);
|
|
|
|
|
pinMode(DF_SLAVESELECT,OUTPUT);
|
2012-11-18 18:03:35 -04:00
|
|
|
|
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(DESKTOP_BUILD)
|
2012-08-17 03:21:26 -03:00
|
|
|
|
pinMode(DF_RESET,OUTPUT);
|
|
|
|
|
// Reset the chip
|
|
|
|
|
digitalWrite(DF_RESET,LOW);
|
|
|
|
|
delay(1);
|
|
|
|
|
digitalWrite(DF_RESET,HIGH);
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// disable device
|
|
|
|
|
CS_inactive();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
// Setup SPI Master, Mode 3, fosc/4 = 4MHz
|
|
|
|
|
SPI.begin();
|
|
|
|
|
SPI.setBitOrder(MSBFIRST);
|
|
|
|
|
SPI.setDataMode(SPI_MODE3);
|
|
|
|
|
SPI.setClockDivider(SPI_CLOCK_DIV2);
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// get page size: 512 or 528 (by default: 528)
|
|
|
|
|
df_PageSize = PageSize();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
// the last page is reserved for config information
|
|
|
|
|
df_NumPages = DF_LAST_PAGE - 1;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function is mainly to test the device
|
|
|
|
|
void DataFlash_APM1::ReadManufacturerID()
|
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
// Read manufacturer and ID command...
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_READ_MANUFACTURER_AND_DEVICE_ID);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-10-18 05:03:50 -03:00
|
|
|
|
df_manufacturer = SPI_transfer(0xff);
|
|
|
|
|
df_device = SPI_transfer(0xff);
|
2012-11-10 02:02:16 -04:00
|
|
|
|
df_device = (df_device << 8) | SPI_transfer(0xff);
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(0xff);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// This function return 1 if Card is inserted on SD slot
|
|
|
|
|
bool DataFlash_APM1::CardInserted()
|
2011-12-28 00:52:36 -04:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
// Read the status register
|
|
|
|
|
byte DataFlash_APM1::ReadStatusReg()
|
|
|
|
|
{
|
2012-08-17 03:21:26 -03:00
|
|
|
|
byte tmp;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
// Read status command
|
2012-10-18 05:03:50 -03:00
|
|
|
|
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-11-10 02:02:16 -04:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
return tmp;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the status of the DataFlash
|
|
|
|
|
inline
|
|
|
|
|
byte DataFlash_APM1::ReadStatus()
|
|
|
|
|
{
|
2012-08-17 03:21:26 -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-13 01:47:28 -04:00
|
|
|
|
uint16_t DataFlash_APM1::PageSize()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04: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...
|
|
|
|
|
void DataFlash_APM1::WaitReady()
|
|
|
|
|
{
|
2012-08-17 03:21:26 -03:00
|
|
|
|
while(!ReadStatus()) ;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 01:47:28 -04:00
|
|
|
|
void DataFlash_APM1::PageToBuffer(unsigned char BufferNum, uint16_t PageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
if (BufferNum==1)
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_TRANSFER_PAGE_TO_BUFFER_1);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_TRANSFER_PAGE_TO_BUFFER_2);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
if(df_PageSize==512) {
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(PageAdr >> 7));
|
|
|
|
|
SPI_transfer((unsigned char)(PageAdr << 1));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}else{
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(PageAdr >> 6));
|
|
|
|
|
SPI_transfer((unsigned char)(PageAdr << 2));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(0x00); // don´t care bytes
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
//initiate the transfer
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
while(!ReadStatus()) ; //monitor the status register, wait until busy-flag is high
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 01:47:28 -04:00
|
|
|
|
void DataFlash_APM1::BufferToPage (unsigned char BufferNum, uint16_t PageAdr, unsigned char wait)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if (BufferNum==1)
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if(df_PageSize==512) {
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(PageAdr >> 7));
|
|
|
|
|
SPI_transfer((unsigned char)(PageAdr << 1));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}else{
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(PageAdr >> 6));
|
|
|
|
|
SPI_transfer((unsigned char)(PageAdr << 2));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(0x00); // don´t care bytes
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
//initiate the transfer
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 01:47:28 -04:00
|
|
|
|
void DataFlash_APM1::BufferWrite (unsigned char BufferNum, uint16_t IntPageAdr, unsigned char Data)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if (BufferNum==1)
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_BUFFER_1_WRITE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_BUFFER_2_WRITE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
2012-11-10 02:02:16 -04: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
|
|
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-13 01:47:28 -04:00
|
|
|
|
unsigned char DataFlash_APM1::BufferRead (unsigned char BufferNum, uint16_t IntPageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-08-17 03:21:26 -03:00
|
|
|
|
byte tmp;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
if (BufferNum==1)
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_BUFFER_1_READ);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_BUFFER_2_READ);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04: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
|
|
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
return (tmp);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
// *** END OF INTERNAL FUNCTIONS ***
|
|
|
|
|
|
2011-11-13 01:47:28 -04:00
|
|
|
|
void DataFlash_APM1::PageErase (uint16_t PageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
// Send page erase command
|
|
|
|
|
SPI_transfer(DF_PAGE_ERASE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if(df_PageSize==512) {
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(PageAdr >> 7));
|
|
|
|
|
SPI_transfer((unsigned char)(PageAdr << 1));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}else{
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(PageAdr >> 6));
|
|
|
|
|
SPI_transfer((unsigned char)(PageAdr << 2));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
SPI_transfer(0x00);
|
|
|
|
|
|
|
|
|
|
//initiate flash page erase
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
while(!ReadStatus()) ;
|
|
|
|
|
|
2012-11-10 02:02:16 -04: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
|
|
|
|
void DataFlash_APM1::BlockErase (uint16_t BlockAdr)
|
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
// Send block erase command
|
|
|
|
|
SPI_transfer(DF_BLOCK_ERASE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
/*
|
2012-08-17 03:21:26 -03:00
|
|
|
|
if (df_PageSize==512) {
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr >> 3));
|
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr << 5));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
} else {
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr >> 4));
|
|
|
|
|
SPI_transfer((unsigned char)(BlockAdr << 4));
|
2012-11-10 02:02:16 -04:00
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
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));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
SPI_transfer(0x00);
|
|
|
|
|
serialDebug("BL Erase, %d\n", BlockAdr);
|
|
|
|
|
|
|
|
|
|
//initiate flash page erase
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
while(!ReadStatus()) ;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2012-07-03 23:36:42 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-18 08:28:31 -03:00
|
|
|
|
void DataFlash_APM1::ChipErase(void (*delay_cb)(unsigned long))
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
//serialDebug("Chip Erase\n");
|
|
|
|
|
|
|
|
|
|
// activate dataflash command decoder
|
|
|
|
|
CS_active();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
// opcodes for chip erase
|
2012-10-18 05:03:50 -03:00
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_0);
|
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_1);
|
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_2);
|
|
|
|
|
SPI_transfer(DF_CHIP_ERASE_3);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
//initiate flash page erase
|
|
|
|
|
CS_inactive();
|
|
|
|
|
CS_active();
|
|
|
|
|
|
|
|
|
|
while(!ReadStatus()) {
|
2012-08-17 03:21:26 -03:00
|
|
|
|
delay_cb(1);
|
|
|
|
|
}
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// release SPI bus for use by other sensors
|
|
|
|
|
CS_inactive();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|