2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-01-03 04:35:05 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_PX4
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2013-01-23 01:37:11 -04:00
|
|
|
#include <stdio.h>
|
2014-01-13 00:04:22 -04:00
|
|
|
#include <ctype.h>
|
2016-11-17 05:22:00 -04:00
|
|
|
#include <nuttx/progmem.h>
|
2013-01-03 04:35:05 -04:00
|
|
|
|
2017-02-22 15:40:03 -04:00
|
|
|
#include <AP_BoardConfig/AP_BoardConfig.h>
|
|
|
|
|
2013-01-03 04:35:05 -04:00
|
|
|
#include "Storage.h"
|
|
|
|
using namespace PX4;
|
|
|
|
|
2013-01-23 01:37:11 -04:00
|
|
|
/*
|
2014-01-15 04:32:32 -04:00
|
|
|
This stores eeprom data in the PX4 MTD interface with a 4k size, and
|
|
|
|
a in-memory buffer. This keeps the latency and devices IOs down.
|
2016-11-17 02:16:03 -04:00
|
|
|
*/
|
2013-01-21 07:04:17 -04:00
|
|
|
|
2013-01-23 01:37:11 -04:00
|
|
|
// name the storage file after the sketch so you can use the same sd
|
2013-01-21 07:04:17 -04:00
|
|
|
// card for ArduCopter and ArduPlane
|
2013-01-23 01:37:11 -04:00
|
|
|
#define STORAGE_DIR "/fs/microsd/APM"
|
2018-07-11 02:43:57 -03:00
|
|
|
#define SAVE_STORAGE_FILE STORAGE_DIR "/" SKETCHNAME ".bak"
|
2014-01-15 04:32:32 -04:00
|
|
|
#define MTD_PARAMS_FILE "/fs/mtd"
|
2013-01-03 04:35:05 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2017-02-22 15:40:03 -04:00
|
|
|
extern "C" int mtd_main(int, char **);
|
|
|
|
|
2014-01-13 00:04:22 -04:00
|
|
|
PX4Storage::PX4Storage(void) :
|
|
|
|
_perf_storage(perf_alloc(PC_ELAPSED, "APM_storage")),
|
|
|
|
_perf_errors(perf_alloc(PC_COUNT, "APM_storage_errors"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-23 01:37:11 -04:00
|
|
|
void PX4Storage::_storage_open(void)
|
2013-01-03 04:35:05 -04:00
|
|
|
{
|
2016-11-17 02:16:03 -04:00
|
|
|
if (_initialised) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-23 01:37:11 -04:00
|
|
|
|
2016-11-17 03:19:25 -04:00
|
|
|
_dirty_mask.clearall();
|
2016-11-17 03:36:37 -04:00
|
|
|
|
2016-11-17 05:22:00 -04:00
|
|
|
// load from storage backend
|
|
|
|
#if USE_FLASH_STORAGE
|
|
|
|
_flash_load();
|
|
|
|
#else
|
2016-11-17 03:36:37 -04:00
|
|
|
_mtd_load();
|
2016-11-17 05:22:00 -04:00
|
|
|
#endif
|
2016-01-06 04:15:43 -04:00
|
|
|
|
|
|
|
#ifdef SAVE_STORAGE_FILE
|
2018-07-11 02:43:57 -03:00
|
|
|
int fd = open(SAVE_STORAGE_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
2016-11-17 02:16:03 -04:00
|
|
|
if (fd != -1) {
|
|
|
|
write(fd, _buffer, sizeof(_buffer));
|
|
|
|
close(fd);
|
|
|
|
}
|
2016-01-06 04:15:43 -04:00
|
|
|
#endif
|
2016-11-17 02:16:03 -04:00
|
|
|
_initialised = true;
|
2013-01-03 04:35:05 -04:00
|
|
|
}
|
|
|
|
|
2013-01-23 18:21:01 -04:00
|
|
|
/*
|
|
|
|
mark some lines as dirty. Note that there is no attempt to avoid
|
|
|
|
the race condition between this code and the _timer_tick() code
|
|
|
|
below, which both update _dirty_mask. If we lose the race then the
|
|
|
|
result is that a line is written more than once, but it won't result
|
|
|
|
in a line not being written.
|
2016-11-17 02:16:03 -04:00
|
|
|
*/
|
2013-01-23 01:37:11 -04:00
|
|
|
void PX4Storage::_mark_dirty(uint16_t loc, uint16_t length)
|
2013-01-03 04:35:05 -04:00
|
|
|
{
|
2014-09-11 05:52:31 -03:00
|
|
|
uint16_t end = loc + length;
|
2016-11-17 03:36:37 -04:00
|
|
|
for (uint16_t line=loc>>PX4_STORAGE_LINE_SHIFT;
|
2014-09-11 05:52:31 -03:00
|
|
|
line <= end>>PX4_STORAGE_LINE_SHIFT;
|
|
|
|
line++) {
|
2016-11-17 03:19:25 -04:00
|
|
|
_dirty_mask.set(line);
|
2014-09-11 05:52:31 -03:00
|
|
|
}
|
2013-01-03 04:35:05 -04:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:16:03 -04:00
|
|
|
void PX4Storage::read_block(void *dst, uint16_t loc, size_t n)
|
2013-01-23 01:37:11 -04:00
|
|
|
{
|
2016-11-17 02:16:03 -04:00
|
|
|
if (loc >= sizeof(_buffer)-(n-1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_storage_open();
|
|
|
|
memcpy(dst, &_buffer[loc], n);
|
2013-01-23 01:37:11 -04:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:16:03 -04:00
|
|
|
void PX4Storage::write_block(uint16_t loc, const void *src, size_t n)
|
2013-01-03 04:35:05 -04:00
|
|
|
{
|
2016-11-17 02:16:03 -04:00
|
|
|
if (loc >= sizeof(_buffer)-(n-1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (memcmp(src, &_buffer[loc], n) != 0) {
|
|
|
|
_storage_open();
|
|
|
|
memcpy(&_buffer[loc], src, n);
|
|
|
|
_mark_dirty(loc, n);
|
|
|
|
}
|
2013-01-03 04:35:05 -04:00
|
|
|
}
|
|
|
|
|
2013-01-23 01:37:11 -04:00
|
|
|
void PX4Storage::_timer_tick(void)
|
2013-01-03 04:35:05 -04:00
|
|
|
{
|
2016-11-17 03:19:25 -04:00
|
|
|
if (!_initialised || _dirty_mask.empty()) {
|
2016-11-17 02:16:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
perf_begin(_perf_storage);
|
2013-01-26 22:38:47 -04:00
|
|
|
|
2016-11-27 17:52:19 -04:00
|
|
|
#if !USE_FLASH_STORAGE
|
2016-11-17 02:16:03 -04:00
|
|
|
if (_fd == -1) {
|
|
|
|
_fd = open(MTD_PARAMS_FILE, O_WRONLY);
|
|
|
|
if (_fd == -1) {
|
|
|
|
perf_end(_perf_storage);
|
|
|
|
perf_count(_perf_errors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 17:52:19 -04:00
|
|
|
#endif
|
2013-01-26 22:38:47 -04:00
|
|
|
|
2016-11-17 03:19:25 -04:00
|
|
|
// write out the first dirty line. We don't write more
|
2016-11-17 02:16:03 -04:00
|
|
|
// than one to keep the latency of this call to a minimum
|
2016-11-17 03:19:25 -04:00
|
|
|
uint16_t i;
|
2016-11-17 02:16:03 -04:00
|
|
|
for (i=0; i<PX4_STORAGE_NUM_LINES; i++) {
|
2016-11-17 03:19:25 -04:00
|
|
|
if (_dirty_mask.get(i)) {
|
2016-11-17 02:16:03 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == PX4_STORAGE_NUM_LINES) {
|
|
|
|
// this shouldn't be possible
|
|
|
|
perf_end(_perf_storage);
|
|
|
|
perf_count(_perf_errors);
|
|
|
|
return;
|
|
|
|
}
|
2016-11-17 03:19:25 -04:00
|
|
|
|
2016-11-17 05:22:00 -04:00
|
|
|
// save to storage backend
|
|
|
|
#if USE_FLASH_STORAGE
|
|
|
|
_flash_write(i);
|
|
|
|
#else
|
2016-11-17 03:36:37 -04:00
|
|
|
_mtd_write(i);
|
2016-11-17 05:22:00 -04:00
|
|
|
#endif
|
2016-11-17 03:36:37 -04:00
|
|
|
|
|
|
|
perf_end(_perf_storage);
|
|
|
|
}
|
2013-01-23 01:37:11 -04:00
|
|
|
|
2016-11-27 17:52:19 -04:00
|
|
|
#if !USE_FLASH_STORAGE
|
|
|
|
void PX4Storage::bus_lock(bool lock)
|
|
|
|
{
|
|
|
|
#if defined (CONFIG_ARCH_BOARD_PX4FMU_V4)
|
|
|
|
/*
|
|
|
|
this is needed on Pixracer where the ms5611 may be on the same
|
|
|
|
bus as FRAM, and the NuttX ramtron driver does not go via the
|
|
|
|
PX4 spi bus abstraction. The ramtron driver relies on
|
|
|
|
SPI_LOCK(). We need to prevent the ms5611 reads which happen in
|
|
|
|
interrupt context from interfering with the FRAM operations. As
|
|
|
|
the px4 spi bus abstraction just uses interrupt blocking as the
|
|
|
|
locking mechanism we need to block interrupts here as well.
|
|
|
|
*/
|
|
|
|
if (lock) {
|
|
|
|
irq_state = irqsave();
|
|
|
|
} else {
|
|
|
|
irqrestore(irq_state);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-17 03:36:37 -04:00
|
|
|
/*
|
|
|
|
write one storage line. This also updates _dirty_mask.
|
|
|
|
*/
|
|
|
|
void PX4Storage::_mtd_write(uint16_t line)
|
|
|
|
{
|
|
|
|
uint16_t ofs = line * PX4_STORAGE_LINE_SIZE;
|
|
|
|
if (lseek(_fd, ofs, SEEK_SET) != ofs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark the line clean
|
|
|
|
_dirty_mask.clear(line);
|
|
|
|
|
|
|
|
bus_lock(true);
|
|
|
|
ssize_t ret = write(_fd, &_buffer[ofs], PX4_STORAGE_LINE_SIZE);
|
|
|
|
bus_lock(false);
|
|
|
|
|
|
|
|
if (ret != PX4_STORAGE_LINE_SIZE) {
|
|
|
|
// write error - likely EINTR
|
|
|
|
_dirty_mask.set(line);
|
|
|
|
close(_fd);
|
|
|
|
_fd = -1;
|
|
|
|
perf_count(_perf_errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
load all data from mtd
|
|
|
|
*/
|
|
|
|
void PX4Storage::_mtd_load(void)
|
|
|
|
{
|
2017-02-22 15:40:03 -04:00
|
|
|
if (AP_BoardConfig::px4_start_driver(mtd_main, "mtd", "start " MTD_PARAMS_FILE)) {
|
|
|
|
printf("mtd: started OK\n");
|
|
|
|
if (AP_BoardConfig::px4_start_driver(mtd_main, "mtd", "readtest " MTD_PARAMS_FILE)) {
|
|
|
|
printf("mtd: readtest OK\n");
|
|
|
|
} else {
|
2017-05-01 23:06:13 -03:00
|
|
|
AP_BoardConfig::sensor_config_error("mtd: failed readtest");
|
2017-02-22 15:40:03 -04:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-01 23:06:13 -03:00
|
|
|
AP_BoardConfig::sensor_config_error("mtd: failed start");
|
2017-02-22 15:40:03 -04:00
|
|
|
}
|
|
|
|
|
2016-11-17 03:36:37 -04:00
|
|
|
int fd = open(MTD_PARAMS_FILE, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
AP_HAL::panic("Failed to open " MTD_PARAMS_FILE);
|
|
|
|
}
|
|
|
|
const uint16_t chunk_size = 128;
|
|
|
|
for (uint16_t ofs=0; ofs<sizeof(_buffer); ofs += chunk_size) {
|
2016-11-17 02:16:03 -04:00
|
|
|
bus_lock(true);
|
2016-11-17 03:36:37 -04:00
|
|
|
ssize_t ret = read(fd, &_buffer[ofs], chunk_size);
|
2016-11-17 02:16:03 -04:00
|
|
|
bus_lock(false);
|
2016-11-17 03:36:37 -04:00
|
|
|
if (ret != chunk_size) {
|
|
|
|
::printf("storage read of %u bytes at %u to %p failed - got %d errno=%d\n",
|
|
|
|
(unsigned)sizeof(_buffer), (unsigned)ofs, &_buffer[ofs], (int)ret, (int)errno);
|
|
|
|
AP_HAL::panic("Failed to read " MTD_PARAMS_FILE);
|
2016-11-17 02:16:03 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-17 03:36:37 -04:00
|
|
|
close(fd);
|
2013-01-03 04:35:05 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 17:52:19 -04:00
|
|
|
#else // USE_FLASH_STORAGE
|
|
|
|
|
2016-11-17 05:22:00 -04:00
|
|
|
/*
|
|
|
|
load all data from flash
|
|
|
|
*/
|
|
|
|
void PX4Storage::_flash_load(void)
|
|
|
|
{
|
|
|
|
_flash_page = up_progmem_npages() - 2;
|
|
|
|
if (up_progmem_pagesize(_flash_page) != 128*1024U ||
|
|
|
|
up_progmem_pagesize(_flash_page+1) != 128*1024U) {
|
|
|
|
AP_HAL::panic("Bad flash page sizes %u %u",
|
|
|
|
up_progmem_pagesize(_flash_page),
|
|
|
|
up_progmem_pagesize(_flash_page+1));
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Storage: Using flash pages %u and %u\n", _flash_page, _flash_page+1);
|
|
|
|
|
|
|
|
if (!_flash.init()) {
|
|
|
|
AP_HAL::panic("unable to init flash storage");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
write one storage line. This also updates _dirty_mask.
|
|
|
|
*/
|
|
|
|
void PX4Storage::_flash_write(uint16_t line)
|
|
|
|
{
|
|
|
|
if (_flash.write(line*PX4_STORAGE_LINE_SIZE, PX4_STORAGE_LINE_SIZE)) {
|
|
|
|
// mark the line clean
|
2016-11-18 20:29:43 -04:00
|
|
|
_dirty_mask.clear(line);
|
2016-11-17 05:22:00 -04:00
|
|
|
} else {
|
|
|
|
perf_count(_perf_errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callback to write data to flash
|
|
|
|
*/
|
|
|
|
bool PX4Storage::_flash_write_data(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
size_t base_address = up_progmem_getaddress(_flash_page+sector);
|
2017-03-22 05:37:10 -03:00
|
|
|
bool ret = up_progmem_write(base_address+offset, data, length) == length;
|
|
|
|
if (!ret && _flash_erase_ok()) {
|
|
|
|
// we are getting flash write errors while disarmed. Try
|
|
|
|
// re-writing all of flash
|
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (now - _last_re_init_ms > 5000) {
|
|
|
|
_last_re_init_ms = now;
|
|
|
|
bool ok = _flash.re_initialise();
|
|
|
|
printf("Storage: failed at %u:%u for %u - re-init %u\n",
|
|
|
|
sector, offset, length, (unsigned)ok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2016-11-17 05:22:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callback to read data from flash
|
|
|
|
*/
|
|
|
|
bool PX4Storage::_flash_read_data(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length)
|
|
|
|
{
|
|
|
|
size_t base_address = up_progmem_getaddress(_flash_page+sector);
|
|
|
|
const uint8_t *b = ((const uint8_t *)base_address)+offset;
|
|
|
|
memcpy(data, b, length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
callback to erase flash sector
|
|
|
|
*/
|
|
|
|
bool PX4Storage::_flash_erase_sector(uint8_t sector)
|
|
|
|
{
|
|
|
|
return up_progmem_erasepage(_flash_page+sector) > 0;
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:29:43 -04:00
|
|
|
/*
|
|
|
|
callback to check if erase is allowed
|
|
|
|
*/
|
|
|
|
bool PX4Storage::_flash_erase_ok(void)
|
|
|
|
{
|
|
|
|
// only allow erase while disarmed
|
|
|
|
return !hal.util->get_soft_armed();
|
|
|
|
}
|
2016-11-27 17:52:19 -04:00
|
|
|
#endif // USE_FLASH_STORAGE
|
|
|
|
|
2016-11-18 20:29:43 -04:00
|
|
|
|
2013-01-23 01:37:11 -04:00
|
|
|
#endif // CONFIG_HAL_BOARD
|