2011-11-25 23:45:48 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2011-11-07 07:22:54 -04:00
|
|
|
/*
|
|
|
|
hacked up DataFlash library for Desktop support
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "DataFlash.h"
|
|
|
|
#include <SPI.h>
|
2012-10-18 07:25:53 -03:00
|
|
|
#include <AP_Semaphore.h>
|
2011-11-07 07:22:54 -04:00
|
|
|
|
|
|
|
#define DF_PAGE_SIZE 512
|
|
|
|
#define DF_NUM_PAGES 4096
|
|
|
|
|
|
|
|
static int flash_fd;
|
|
|
|
static uint8_t buffer[2][DF_PAGE_SIZE];
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2011-11-13 01:48:30 -04:00
|
|
|
void DataFlash_APM1::Init(void)
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
if (flash_fd == 0) {
|
2011-11-07 08:35:57 -04:00
|
|
|
flash_fd = open("dataflash.bin", O_RDWR, 0777);
|
|
|
|
if (flash_fd == -1) {
|
2011-11-08 17:09:56 -04:00
|
|
|
uint8_t *fill;
|
2011-11-08 17:47:49 -04:00
|
|
|
fill = (uint8_t *)malloc(DF_PAGE_SIZE*DF_NUM_PAGES);
|
2011-11-07 08:35:57 -04:00
|
|
|
flash_fd = open("dataflash.bin", O_RDWR | O_CREAT, 0777);
|
2011-11-08 17:09:56 -04:00
|
|
|
memset(fill, 0xFF, DF_PAGE_SIZE*DF_NUM_PAGES);
|
|
|
|
write(flash_fd, fill, DF_PAGE_SIZE*DF_NUM_PAGES);
|
|
|
|
free(fill);
|
2011-11-07 08:35:57 -04:00
|
|
|
}
|
2011-11-07 07:22:54 -04:00
|
|
|
}
|
|
|
|
df_PageSize = DF_PAGE_SIZE;
|
2011-12-28 00:52:36 -04:00
|
|
|
|
|
|
|
// reserve last page for config information
|
|
|
|
df_NumPages = DF_NUM_PAGES - 1;
|
2011-11-07 07:22:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is mainly to test the device
|
2011-11-13 01:48:30 -04:00
|
|
|
void DataFlash_APM1::ReadManufacturerID()
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
df_manufacturer = 1;
|
2011-12-28 00:52:36 -04:00
|
|
|
df_device = 0x0203;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataFlash_APM1::CardInserted(void)
|
|
|
|
{
|
|
|
|
return true;
|
2011-11-07 07:22:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the status register
|
2011-11-13 01:48:30 -04:00
|
|
|
byte DataFlash_APM1::ReadStatusReg()
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the status of the DataFlash
|
|
|
|
inline
|
2011-11-13 01:48:30 -04:00
|
|
|
byte DataFlash_APM1::ReadStatus()
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
2011-11-13 01:48:30 -04:00
|
|
|
uint16_t DataFlash_APM1::PageSize()
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
return df_PageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Wait until DataFlash is in ready state...
|
2011-11-13 01:48:30 -04:00
|
|
|
void DataFlash_APM1::WaitReady()
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
while(!ReadStatus());
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:48:30 -04:00
|
|
|
void DataFlash_APM1::PageToBuffer(unsigned char BufferNum, uint16_t PageAdr)
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
pread(flash_fd, buffer[BufferNum-1], DF_PAGE_SIZE, PageAdr*DF_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:48:30 -04:00
|
|
|
void DataFlash_APM1::BufferToPage (unsigned char BufferNum, uint16_t PageAdr, unsigned char wait)
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
pwrite(flash_fd, buffer[BufferNum-1], DF_PAGE_SIZE, PageAdr*DF_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:48:30 -04:00
|
|
|
void DataFlash_APM1::BufferWrite (unsigned char BufferNum, uint16_t IntPageAdr, unsigned char Data)
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
buffer[BufferNum-1][IntPageAdr] = (uint8_t)Data;
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:48:30 -04:00
|
|
|
unsigned char DataFlash_APM1::BufferRead (unsigned char BufferNum, uint16_t IntPageAdr)
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
|
|
|
return (unsigned char)buffer[BufferNum-1][IntPageAdr];
|
|
|
|
}
|
|
|
|
|
|
|
|
// *** END OF INTERNAL FUNCTIONS ***
|
|
|
|
|
2011-11-13 01:48:30 -04:00
|
|
|
void DataFlash_APM1::PageErase (uint16_t PageAdr)
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
2011-11-07 08:35:57 -04:00
|
|
|
uint8_t fill[DF_PAGE_SIZE];
|
|
|
|
memset(fill, 0xFF, sizeof(fill));
|
|
|
|
pwrite(flash_fd, fill, DF_PAGE_SIZE, PageAdr*DF_PAGE_SIZE);
|
2011-11-07 07:22:54 -04:00
|
|
|
}
|
|
|
|
|
2012-07-04 00:44:01 -03:00
|
|
|
void DataFlash_APM1::BlockErase (uint16_t BlockAdr)
|
|
|
|
{
|
|
|
|
uint8_t fill[DF_PAGE_SIZE*8];
|
|
|
|
memset(fill, 0xFF, sizeof(fill));
|
|
|
|
pwrite(flash_fd, fill, DF_PAGE_SIZE*8, BlockAdr*DF_PAGE_SIZE*8);
|
|
|
|
}
|
|
|
|
|
2011-11-07 07:22:54 -04:00
|
|
|
|
2012-05-23 04:32:00 -03:00
|
|
|
void DataFlash_APM1::ChipErase(void (*delay_cb)(unsigned long))
|
2011-11-07 07:22:54 -04:00
|
|
|
{
|
2012-05-23 08:15:10 -03:00
|
|
|
for (int i=0; i<DF_NUM_PAGES; i++) {
|
2011-11-07 07:22:54 -04:00
|
|
|
PageErase(i);
|
2012-05-23 04:32:00 -03:00
|
|
|
delay_cb(1);
|
2011-11-07 07:22:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 21:21:42 -03:00
|
|
|
|