2012-12-12 18:19:09 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
hacked up DataFlash library for Desktop support
|
|
|
|
*/
|
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2012-12-12 18:19:09 -04:00
|
|
|
|
2015-05-04 03:17:39 -03:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
|
2012-12-12 18:19:09 -04:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
2013-11-25 17:43:39 -04:00
|
|
|
#include <assert.h>
|
2012-12-12 18:19:09 -04:00
|
|
|
#include "DataFlash.h"
|
|
|
|
|
2015-05-04 04:27:15 -03:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-result"
|
|
|
|
|
2012-12-12 18:19:09 -04:00
|
|
|
#define DF_PAGE_SIZE 512
|
2013-11-06 19:18:38 -04:00
|
|
|
#define DF_NUM_PAGES 16384
|
2012-12-12 18:19:09 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
static int flash_fd;
|
|
|
|
static uint8_t buffer[2][DF_PAGE_SIZE];
|
|
|
|
|
|
|
|
// Public Methods //////////////////////////////////////////////////////////////
|
2013-12-16 20:12:42 -04:00
|
|
|
void DataFlash_SITL::Init(const struct LogStructure *structure, uint8_t num_types)
|
2012-12-12 18:19:09 -04:00
|
|
|
{
|
2015-06-25 10:53:20 -03:00
|
|
|
DataFlash_Backend::Init(structure, num_types);
|
2012-12-12 18:19:09 -04:00
|
|
|
if (flash_fd == 0) {
|
|
|
|
flash_fd = open("dataflash.bin", O_RDWR, 0777);
|
|
|
|
if (flash_fd == -1) {
|
|
|
|
uint8_t *fill;
|
|
|
|
fill = (uint8_t *)malloc(DF_PAGE_SIZE*DF_NUM_PAGES);
|
|
|
|
flash_fd = open("dataflash.bin", O_RDWR | O_CREAT, 0777);
|
|
|
|
memset(fill, 0xFF, DF_PAGE_SIZE*DF_NUM_PAGES);
|
|
|
|
write(flash_fd, fill, DF_PAGE_SIZE*DF_NUM_PAGES);
|
|
|
|
free(fill);
|
|
|
|
}
|
2013-11-06 19:18:38 -04:00
|
|
|
ftruncate(flash_fd, DF_PAGE_SIZE*DF_NUM_PAGES);
|
2012-12-12 18:19:09 -04:00
|
|
|
}
|
|
|
|
df_PageSize = DF_PAGE_SIZE;
|
|
|
|
|
|
|
|
// reserve last page for config information
|
|
|
|
df_NumPages = DF_NUM_PAGES - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function is mainly to test the device
|
|
|
|
void DataFlash_SITL::ReadManufacturerID()
|
|
|
|
{
|
|
|
|
df_manufacturer = 1;
|
|
|
|
df_device = 0x0203;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataFlash_SITL::CardInserted(void)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the status register
|
|
|
|
uint8_t DataFlash_SITL::ReadStatusReg()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the status of the DataFlash
|
|
|
|
inline
|
|
|
|
uint8_t DataFlash_SITL::ReadStatus()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
uint16_t DataFlash_SITL::PageSize()
|
|
|
|
{
|
|
|
|
return df_PageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Wait until DataFlash is in ready state...
|
|
|
|
void DataFlash_SITL::WaitReady()
|
|
|
|
{
|
|
|
|
while(!ReadStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_SITL::PageToBuffer(unsigned char BufferNum, uint16_t PageAdr)
|
|
|
|
{
|
2013-11-25 17:43:39 -04:00
|
|
|
assert(PageAdr>=1);
|
|
|
|
pread(flash_fd, buffer[BufferNum], DF_PAGE_SIZE, (PageAdr-1)*DF_PAGE_SIZE);
|
2012-12-12 18:19:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_SITL::BufferToPage (unsigned char BufferNum, uint16_t PageAdr, unsigned char wait)
|
|
|
|
{
|
2013-11-25 17:43:39 -04:00
|
|
|
assert(PageAdr>=1);
|
|
|
|
pwrite(flash_fd, buffer[BufferNum], DF_PAGE_SIZE, (PageAdr-1)*(uint32_t)DF_PAGE_SIZE);
|
2012-12-12 18:19:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_SITL::BufferWrite (unsigned char BufferNum, uint16_t IntPageAdr, unsigned char Data)
|
|
|
|
{
|
2013-01-12 02:21:04 -04:00
|
|
|
buffer[BufferNum][IntPageAdr] = (uint8_t)Data;
|
2012-12-12 18:19:09 -04:00
|
|
|
}
|
|
|
|
|
2013-01-12 07:28:53 -04:00
|
|
|
void DataFlash_SITL::BlockWrite(uint8_t BufferNum, uint16_t IntPageAdr,
|
|
|
|
const void *pHeader, uint8_t hdr_size,
|
|
|
|
const void *pBuffer, uint16_t size)
|
|
|
|
{
|
2014-01-07 09:36:47 -04:00
|
|
|
if (!_writes_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-12 07:28:53 -04:00
|
|
|
if (hdr_size) {
|
|
|
|
memcpy(&buffer[BufferNum][IntPageAdr],
|
|
|
|
pHeader,
|
|
|
|
hdr_size);
|
|
|
|
}
|
|
|
|
memcpy(&buffer[BufferNum][IntPageAdr+hdr_size],
|
|
|
|
pBuffer,
|
|
|
|
size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read size bytes of data to a page. The caller must ensure that
|
|
|
|
// the data fits within the page, otherwise it will wrap to the
|
|
|
|
// start of the page
|
|
|
|
bool DataFlash_SITL::BlockRead(uint8_t BufferNum, uint16_t IntPageAdr, void *pBuffer, uint16_t size)
|
|
|
|
{
|
|
|
|
memcpy(pBuffer, &buffer[BufferNum][IntPageAdr], size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-12 18:19:09 -04:00
|
|
|
// *** END OF INTERNAL FUNCTIONS ***
|
|
|
|
|
|
|
|
void DataFlash_SITL::PageErase (uint16_t PageAdr)
|
|
|
|
{
|
|
|
|
uint8_t fill[DF_PAGE_SIZE];
|
|
|
|
memset(fill, 0xFF, sizeof(fill));
|
2013-11-25 17:43:39 -04:00
|
|
|
assert(PageAdr>=1);
|
|
|
|
pwrite(flash_fd, fill, DF_PAGE_SIZE, (PageAdr-1)*DF_PAGE_SIZE);
|
2012-12-12 18:19:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataFlash_SITL::BlockErase (uint16_t BlockAdr)
|
|
|
|
{
|
|
|
|
uint8_t fill[DF_PAGE_SIZE*8];
|
|
|
|
memset(fill, 0xFF, sizeof(fill));
|
2013-11-25 17:43:39 -04:00
|
|
|
assert(BlockAdr>=1);
|
|
|
|
pwrite(flash_fd, fill, DF_PAGE_SIZE*8, (BlockAdr-1)*DF_PAGE_SIZE*8);
|
2012-12-12 18:19:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DataFlash_SITL::ChipErase()
|
|
|
|
{
|
|
|
|
for (int i=0; i<DF_NUM_PAGES; i++) {
|
|
|
|
PageErase(i);
|
|
|
|
hal.scheduler->delay(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
2012-12-12 18:33:48 -04:00
|
|
|
|
2012-12-12 18:19:09 -04:00
|
|
|
|