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:
|
|
|
|
|
*
|
|
|
|
|
*/
|
2012-12-08 00:07:24 -04:00
|
|
|
|
#include <AP_HAL.h>
|
2012-11-10 02:02:16 -04:00
|
|
|
|
#include "DataFlash_APM2.h"
|
|
|
|
|
|
2012-12-08 00:24:16 -04:00
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
2012-12-19 22:31:49 -04:00
|
|
|
|
//#define ENABLE_FASTSERIAL_DEBUG
|
2012-11-10 02:02:16 -04:00
|
|
|
|
#ifdef ENABLE_FASTSERIAL_DEBUG
|
2012-12-08 00:24:16 -04:00
|
|
|
|
#define serialDebug(fmt, args...) do {hal.console->printf_P(PSTR( __FUNCTION__ ":%d:" fmt "\n"), __LINE__, ##args); } while(0)
|
2012-11-10 02:02:16 -04:00
|
|
|
|
#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
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
#define DF_RESET 31 // RESET (PC6)
|
2011-11-20 04:50:45 -04:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
|
|
|
|
void DataFlash_APM1::Init(void)
|
|
|
|
|
{
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// init to zero
|
|
|
|
|
df_NumPages = 0;
|
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
hal.gpio->pinMode(DF_RESET,GPIO_OUTPUT);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
// Reset the chip
|
2012-12-08 00:07:24 -04:00
|
|
|
|
hal.gpio->write(DF_RESET,0);
|
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
|
hal.gpio->write(DF_RESET,1);
|
|
|
|
|
|
|
|
|
|
_spi = hal.spi->device(AP_HAL::SPIDevice_Dataflash);
|
|
|
|
|
if (_spi == NULL) {
|
2013-01-03 14:34:25 -04:00
|
|
|
|
hal.scheduler->panic(
|
2012-12-08 00:07:24 -04:00
|
|
|
|
PSTR("PANIC: DataFlash SPIDeviceDriver not found"));
|
2013-01-03 14:34:25 -04:00
|
|
|
|
return; /* never reached */
|
2012-12-08 00:07:24 -04:00
|
|
|
|
}
|
2013-01-03 14:34:25 -04:00
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi_sem = _spi->get_semaphore();
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (_spi_sem == NULL) {
|
|
|
|
|
hal.scheduler->panic(
|
|
|
|
|
PSTR("PANIC: DataFlash SPIDeviceDriver semaphore is null"));
|
|
|
|
|
return; /* never reached */
|
|
|
|
|
}
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
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()
|
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(5))
|
|
|
|
|
return;
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
// Read manufacturer and ID command...
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_READ_MANUFACTURER_AND_DEVICE_ID);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
df_manufacturer = _spi->transfer(0xff);
|
|
|
|
|
df_device = _spi->transfer(0xff);
|
|
|
|
|
df_device = (df_device << 8) | _spi->transfer(0xff);
|
|
|
|
|
_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
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
|
|
|
|
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
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
|
2012-12-08 00:07:24 -04:00
|
|
|
|
// Assumes _spi_sem handled by caller
|
2012-12-07 23:20:17 -04:00
|
|
|
|
uint8_t DataFlash_APM1::ReadStatusReg()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-12-07 23:20:17 -04:00
|
|
|
|
uint8_t tmp;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
// Read status command
|
2012-12-08 00:07:24 -04: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
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
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
|
2012-12-08 00:07:24 -04:00
|
|
|
|
// Assumes _spi_sem handled by caller.
|
2011-11-12 23:29:07 -04:00
|
|
|
|
inline
|
2012-12-07 23:20:17 -04:00
|
|
|
|
uint8_t DataFlash_APM1::ReadStatus()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(5))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
return(528-((ReadStatusReg()&0x01) << 4)); // if first bit 1 trhen 512 else 528 bytes
|
2012-12-08 00:07:24 -04:00
|
|
|
|
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait until DataFlash is in ready state...
|
2012-12-08 00:07:24 -04:00
|
|
|
|
// Assumes _spi_sem handled by caller.
|
2011-11-12 23:29:07 -04:00
|
|
|
|
void DataFlash_APM1::WaitReady()
|
|
|
|
|
{
|
2012-08-17 03:21:26 -03:00
|
|
|
|
while(!ReadStatus()) ;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 23:20:17 -04:00
|
|
|
|
void DataFlash_APM1::PageToBuffer(uint8_t BufferNum, uint16_t PageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(1))
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
if (BufferNum==1)
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_TRANSFER_PAGE_TO_BUFFER_1);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-12-08 00:07:24 -04: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-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 7));
|
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 1));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}else{
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 6));
|
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 2));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
2012-12-08 00:07:24 -04: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
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
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
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 23:20:17 -04:00
|
|
|
|
void DataFlash_APM1::BufferToPage (uint8_t BufferNum, uint16_t PageAdr, uint8_t wait)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(1))
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if (BufferNum==1)
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_BUFFER_1_TO_PAGE_WITH_ERASE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_BUFFER_2_TO_PAGE_WITH_ERASE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if(df_PageSize==512) {
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 7));
|
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 1));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}else{
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 6));
|
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 2));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
2012-12-08 00:07:24 -04: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
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
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
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 23:20:17 -04:00
|
|
|
|
void DataFlash_APM1::BufferWrite (uint8_t BufferNum, uint16_t IntPageAdr, uint8_t Data)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(1))
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if (BufferNum==1)
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_BUFFER_1_WRITE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_BUFFER_2_WRITE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(0x00); // don't care
|
|
|
|
|
_spi->transfer((uint8_t)(IntPageAdr>>8)); // upper part of internal buffer address
|
|
|
|
|
_spi->transfer((uint8_t)(IntPageAdr)); // lower part of internal buffer address
|
|
|
|
|
_spi->transfer(Data); // write data byte
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 23:20:17 -04:00
|
|
|
|
uint8_t DataFlash_APM1::BufferRead (uint8_t BufferNum, uint16_t IntPageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2012-12-07 23:20:17 -04:00
|
|
|
|
uint8_t tmp;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(1))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
if (BufferNum==1)
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_BUFFER_1_READ);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
else
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_BUFFER_2_READ);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(0x00);
|
|
|
|
|
_spi->transfer((uint8_t)(IntPageAdr>>8)); // upper part of internal buffer address
|
|
|
|
|
_spi->transfer((uint8_t)(IntPageAdr)); // lower part of internal buffer address
|
|
|
|
|
_spi->transfer(0x00); // don't cares
|
|
|
|
|
tmp = _spi->transfer(0x00); // read data byte
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
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
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(1))
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
// Send page erase command
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(DF_PAGE_ERASE);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
|
|
|
|
if(df_PageSize==512) {
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 7));
|
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 1));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}else{
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 6));
|
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 2));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(0x00);
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
//initiate flash page erase
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
2012-12-08 00:07:24 -04:00
|
|
|
|
while(!ReadStatus()) ;
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-03 23:36:42 -03:00
|
|
|
|
void DataFlash_APM1::BlockErase (uint16_t BlockAdr)
|
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(1))
|
|
|
|
|
return;
|
|
|
|
|
|
2012-11-10 02:02:16 -04:00
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
// Send block erase command
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_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-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr >> 3));
|
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr << 5));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
} else {
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr >> 4));
|
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr << 4));
|
2012-11-10 02:02:16 -04:00
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
if (df_PageSize==512) {
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr >> 4));
|
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr << 4));
|
2012-11-10 02:02:16 -04:00
|
|
|
|
} else {
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr >> 3));
|
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr << 5));
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->transfer(0x00);
|
|
|
|
|
//serialDebug("BL Erase, %d\n", BlockAdr);
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
//initiate flash page erase
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
2012-08-17 03:21:26 -03:00
|
|
|
|
while(!ReadStatus()) ;
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
2012-07-03 23:36:42 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-12-08 00:24:16 -04:00
|
|
|
|
void DataFlash_APM1::ChipErase()
|
2011-11-12 23:29:07 -04:00
|
|
|
|
{
|
2013-01-03 14:34:25 -04:00
|
|
|
|
if (!_spi_sem->take(5))
|
|
|
|
|
return;
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_assert();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
2012-08-17 03:21:26 -03:00
|
|
|
|
// opcodes for chip erase
|
2012-12-08 00:07:24 -04: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
|
2012-12-08 00:07:24 -04:00
|
|
|
|
_spi->cs_release();
|
2012-11-10 02:02:16 -04:00
|
|
|
|
|
|
|
|
|
while(!ReadStatus()) {
|
2012-12-08 00:24:16 -04:00
|
|
|
|
hal.scheduler->delay(6);
|
2012-08-17 03:21:26 -03:00
|
|
|
|
}
|
2012-12-08 00:07:24 -04:00
|
|
|
|
|
2013-01-03 14:34:25 -04:00
|
|
|
|
_spi_sem->give();
|
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
}
|