2011-11-20 04:51:16 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-08-29 02:34:34 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* StartRead(page) : Start a read on (page)
|
|
|
|
* GetWritePage() : Returns the last page written to
|
|
|
|
* GetPage() : Returns the last page read
|
|
|
|
*
|
|
|
|
* Properties:
|
|
|
|
*
|
|
|
|
*/
|
2011-11-12 23:29:07 -04:00
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h> // for removing conflict with optical flow sensor on SPI3 bus
|
2011-11-25 19:11:14 -04:00
|
|
|
#include "DataFlash_APM2.h"
|
2012-12-08 00:24:44 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
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
|
|
|
|
2012-12-08 00:24:44 -04:00
|
|
|
#define DF_RESET 41 // RESET (PG0)
|
|
|
|
#define DF_CARDDETECT 33 // PC4
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
2013-03-02 02:42:06 -04:00
|
|
|
/*
|
|
|
|
try to take a semaphore safely from both in a timer and outside
|
|
|
|
*/
|
|
|
|
bool DataFlash_APM2::_sem_take(uint8_t timeout)
|
|
|
|
{
|
|
|
|
if (hal.scheduler->in_timerprocess()) {
|
|
|
|
return _spi_sem->take_nonblocking();
|
|
|
|
}
|
|
|
|
return _spi_sem->take(timeout);
|
|
|
|
}
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2013-12-16 20:12:42 -04:00
|
|
|
void DataFlash_APM2::Init(const struct LogStructure *structure, uint8_t num_types)
|
2011-11-12 23:29:07 -04:00
|
|
|
{
|
2015-06-25 10:53:20 -03:00
|
|
|
DataFlash_Backend::Init(structure, num_types);
|
2012-08-17 03:21:25 -03:00
|
|
|
// init to zero
|
|
|
|
df_NumPages = 0;
|
|
|
|
|
2014-06-01 20:27:31 -03:00
|
|
|
hal.gpio->pinMode(DF_RESET, HAL_GPIO_OUTPUT);
|
|
|
|
hal.gpio->pinMode(DF_CARDDETECT, HAL_GPIO_INPUT);
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
// Reset the chip
|
2012-12-08 00:24:44 -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:24:44 -04:00
|
|
|
PSTR("PANIC: DataFlash SPIDeviceDriver not found"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_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-12-08 00:24:44 -04:00
|
|
|
}
|
2012-08-17 03:21:25 -03:00
|
|
|
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(5))
|
2013-01-03 14:34:25 -04:00
|
|
|
return;
|
2012-08-17 03:21:25 -03:00
|
|
|
// get page size: 512 or 528 (by default: 528)
|
|
|
|
df_PageSize=PageSize();
|
|
|
|
|
|
|
|
ReadManufacturerID();
|
2013-01-03 14:34:25 -04:00
|
|
|
|
|
|
|
_spi_sem->give();
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
// 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
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2011-11-12 23:29:07 -04:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// Read manufacturer and ID command...
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer(DF_READ_MANUFACTURER_AND_DEVICE_ID);
|
2011-11-12 23:29:07 -04:00
|
|
|
|
2012-12-08 00:24:44 -04: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
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
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
|
2012-12-07 23:20:17 -04:00
|
|
|
uint8_t DataFlash_APM2::ReadStatusReg()
|
2011-11-12 23:29:07 -04:00
|
|
|
{
|
2012-12-07 23:20:17 -04:00
|
|
|
uint8_t tmp;
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// Read status command
|
2012-12-08 00:24:44 -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-08-17 03:21:25 -03:00
|
|
|
// release SPI bus for use by other sensors
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
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
|
2012-12-07 23:20:17 -04:00
|
|
|
uint8_t 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
|
|
|
}
|
|
|
|
|
2012-12-07 23:20:17 -04:00
|
|
|
void DataFlash_APM2::PageToBuffer(uint8_t BufferNum, uint16_t PageAdr)
|
2011-11-12 23:29:07 -04:00
|
|
|
{
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(1))
|
2013-01-03 14:34:25 -04:00
|
|
|
return;
|
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-08-17 03:21:25 -03:00
|
|
|
|
2013-01-12 07:14:35 -04:00
|
|
|
uint8_t cmd[4];
|
|
|
|
cmd[0] = BufferNum?DF_TRANSFER_PAGE_TO_BUFFER_2:DF_TRANSFER_PAGE_TO_BUFFER_1;
|
2012-08-17 03:21:25 -03:00
|
|
|
if(df_PageSize==512) {
|
2013-01-12 07:14:35 -04:00
|
|
|
cmd[1] = (uint8_t)(PageAdr >> 7);
|
|
|
|
cmd[2] = (uint8_t)(PageAdr << 1);
|
2012-08-17 03:21:25 -03:00
|
|
|
}else{
|
2013-01-12 07:14:35 -04:00
|
|
|
cmd[1] = (uint8_t)(PageAdr >> 6);
|
|
|
|
cmd[2] = (uint8_t)(PageAdr << 2);
|
2012-08-17 03:21:25 -03:00
|
|
|
}
|
2013-01-12 07:14:35 -04:00
|
|
|
cmd[3] = 0;
|
|
|
|
_spi->transfer(cmd, sizeof(cmd));
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
//initiate the transfer
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
|
|
|
_spi->cs_assert();
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
while(!ReadStatus()) ; //monitor the status register, wait until busy-flag is high
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
2012-12-08 00:24:44 -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
|
|
|
void DataFlash_APM2::BufferToPage (uint8_t BufferNum, uint16_t PageAdr, uint8_t wait)
|
2011-11-12 23:29:07 -04:00
|
|
|
{
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(1))
|
2013-01-03 14:34:25 -04:00
|
|
|
return;
|
2012-08-17 03:21:25 -03:00
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-08-17 03:21:25 -03:00
|
|
|
|
2013-01-12 07:14:35 -04:00
|
|
|
uint8_t cmd[4];
|
|
|
|
cmd[0] = BufferNum?DF_BUFFER_2_TO_PAGE_WITH_ERASE:DF_BUFFER_1_TO_PAGE_WITH_ERASE;
|
2012-08-17 03:21:25 -03:00
|
|
|
if(df_PageSize==512) {
|
2013-01-12 07:14:35 -04:00
|
|
|
cmd[1] = (uint8_t)(PageAdr >> 7);
|
|
|
|
cmd[2] = (uint8_t)(PageAdr << 1);
|
2012-08-17 03:21:25 -03:00
|
|
|
}else{
|
2013-01-12 07:14:35 -04:00
|
|
|
cmd[1] = (uint8_t)(PageAdr >> 6);
|
|
|
|
cmd[2] = (uint8_t)(PageAdr << 2);
|
2012-08-17 03:21:25 -03:00
|
|
|
}
|
2013-01-12 07:14:35 -04:00
|
|
|
cmd[3] = 0;
|
|
|
|
_spi->transfer(cmd, sizeof(cmd));
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
//initiate the transfer
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
2012-08-17 03:21:25 -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
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
2013-01-03 14:34:25 -04:00
|
|
|
_spi_sem->give();
|
2011-11-12 23:29:07 -04:00
|
|
|
}
|
|
|
|
|
2013-01-12 02:21:04 -04:00
|
|
|
void DataFlash_APM2::BlockWrite (uint8_t BufferNum, uint16_t IntPageAdr,
|
|
|
|
const void *pHeader, uint8_t hdr_size,
|
|
|
|
const void *pBuffer, uint16_t size)
|
2011-11-12 23:29:07 -04:00
|
|
|
{
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(1))
|
2013-01-03 14:34:25 -04:00
|
|
|
return;
|
2012-08-17 03:21:25 -03:00
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2013-01-12 07:14:35 -04:00
|
|
|
uint8_t cmd[] = {
|
|
|
|
(uint8_t)(BufferNum?DF_BUFFER_2_WRITE:DF_BUFFER_1_WRITE),
|
|
|
|
0,
|
|
|
|
(uint8_t)(IntPageAdr>>8),
|
|
|
|
(uint8_t)(IntPageAdr)
|
|
|
|
};
|
|
|
|
_spi->transfer(cmd, sizeof(cmd));
|
2013-01-12 02:21:04 -04:00
|
|
|
|
|
|
|
// transfer header, if any
|
2013-01-12 07:14:35 -04:00
|
|
|
if (hdr_size != 0) {
|
|
|
|
_spi->transfer((const uint8_t *)pHeader, hdr_size);
|
2013-01-12 02:21:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// transfer data
|
2013-01-12 07:14:35 -04:00
|
|
|
_spi->transfer((const uint8_t *)pBuffer, size);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// release SPI bus for use by other sensors
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
2013-01-03 14:34:25 -04:00
|
|
|
_spi_sem->give();
|
2011-11-12 23:29:07 -04:00
|
|
|
}
|
|
|
|
|
2013-01-12 02:21:04 -04:00
|
|
|
bool DataFlash_APM2::BlockRead(uint8_t BufferNum, uint16_t IntPageAdr, void *pBuffer, uint16_t size)
|
2011-11-12 23:29:07 -04:00
|
|
|
{
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(1))
|
2013-01-12 02:21:04 -04:00
|
|
|
return false;
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2013-01-12 02:21:04 -04:00
|
|
|
if (BufferNum==0)
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer(DF_BUFFER_1_READ);
|
2012-08-17 03:21:25 -03:00
|
|
|
else
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer(DF_BUFFER_2_READ);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-12-08 00:24:44 -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
|
2013-01-12 02:21:04 -04:00
|
|
|
|
|
|
|
uint8_t *pData = (uint8_t *)pBuffer;
|
|
|
|
while (size--) {
|
|
|
|
*pData++ = _spi->transfer(0x00);
|
|
|
|
}
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// release SPI bus for use by other sensors
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2013-01-03 14:34:25 -04:00
|
|
|
_spi_sem->give();
|
2013-01-12 02:21:04 -04:00
|
|
|
return true;
|
2011-11-12 23:29:07 -04:00
|
|
|
}
|
2013-01-12 02:21:04 -04:00
|
|
|
|
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
|
|
|
{
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(1))
|
2013-01-03 14:34:25 -04:00
|
|
|
return;
|
2012-08-17 03:21:25 -03:00
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
// Send page erase command
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer(DF_PAGE_ERASE);
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
if(df_PageSize==512) {
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 7));
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 1));
|
2012-08-17 03:21:25 -03:00
|
|
|
}else{
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer((uint8_t)(PageAdr >> 6));
|
|
|
|
_spi->transfer((uint8_t)(PageAdr << 2));
|
2012-08-17 03:21:25 -03:00
|
|
|
}
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer(0x00);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
//initiate flash page erase
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
2012-08-17 03:21:25 -03:00
|
|
|
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
|
2013-01-03 14:34:25 -04:00
|
|
|
_spi_sem->give();
|
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)
|
|
|
|
{
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(1))
|
2013-01-03 14:34:25 -04:00
|
|
|
return;
|
2012-08-17 03:21:25 -03:00
|
|
|
// activate dataflash command decoder
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
// Send block erase command
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer(DF_BLOCK_ERASE);
|
2012-08-17 03:21:25 -03:00
|
|
|
|
|
|
|
if (df_PageSize==512) {
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer((uint8_t)(BlockAdr >> 4));
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr << 4));
|
2012-08-17 03:21:25 -03:00
|
|
|
} else {
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer((uint8_t)(BlockAdr >> 3));
|
|
|
|
_spi->transfer((uint8_t)(BlockAdr << 5));
|
2012-08-17 03:21:25 -03:00
|
|
|
}
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->transfer(0x00);
|
2012-08-17 03:21:25 -03:00
|
|
|
//serialDebug("BL Erase, %d\n", BlockAdr);
|
|
|
|
|
|
|
|
//initiate flash page erase
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
2012-08-17 03:21:25 -03:00
|
|
|
while(!ReadStatus()) ;
|
|
|
|
|
|
|
|
// release SPI bus for use by other sensors
|
2013-01-03 14:34:25 -04:00
|
|
|
_spi_sem->give();
|
2012-07-03 23:36:42 -03:00
|
|
|
}
|
|
|
|
|
2011-11-12 23:29:07 -04:00
|
|
|
|
2012-12-08 00:24:44 -04:00
|
|
|
void DataFlash_APM2::ChipErase()
|
2011-11-12 23:29:07 -04:00
|
|
|
{
|
2013-03-02 02:42:06 -04:00
|
|
|
if (!_sem_take(1))
|
2013-01-03 14:34:25 -04:00
|
|
|
return;
|
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
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_assert();
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// opcodes for chip erase
|
2012-12-08 00:24:44 -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);
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
//initiate flash page erase
|
2012-12-08 00:24:44 -04:00
|
|
|
_spi->cs_release();
|
2012-05-22 03:35:53 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
while(!ReadStatus()) {
|
2012-12-08 00:24:44 -04:00
|
|
|
hal.scheduler->delay(1);
|
2012-05-22 03:35:53 -03:00
|
|
|
}
|
2012-04-21 08:14:45 -03:00
|
|
|
|
2012-08-17 03:21:25 -03:00
|
|
|
// release SPI bus for use by other sensors
|
2013-01-03 14:34:25 -04:00
|
|
|
_spi_sem->give();
|
2012-05-22 03:35:53 -03:00
|
|
|
}
|
2015-08-06 09:18:28 -03:00
|
|
|
|
|
|
|
// Writing too quickly to DF on APM1/APM2 corrupts the flash. This
|
|
|
|
// could be done at a lower level to more generally restrict bandwidth
|
|
|
|
void DataFlash_APM2::WroteStartupFormat()
|
|
|
|
{
|
|
|
|
if (! hal.util->get_soft_armed()) {
|
|
|
|
hal.scheduler->delay(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void DataFlash_APM2::WroteStartupParam()
|
|
|
|
{
|
|
|
|
if (! hal.util->get_soft_armed()) {
|
|
|
|
hal.scheduler->delay(2);
|
|
|
|
}
|
|
|
|
}
|