123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #include <AP_HAL/AP_HAL.h>
- #include <AP_BoardConfig/AP_BoardConfig.h>
- #include "Storage.h"
- #include "hwdef/common/flash.h"
- #include <AP_Filesystem/AP_Filesystem.h>
- #include "sdcard.h"
- using namespace ChibiOS;
- #ifndef HAL_USE_EMPTY_STORAGE
- extern const AP_HAL::HAL& hal;
- #ifndef HAL_STORAGE_FILE
- #define HAL_STORAGE_FILE "/APM/" SKETCHNAME ".stg"
- #endif
- #ifndef HAL_STORAGE_BACKUP_FILE
- #define HAL_STORAGE_BACKUP_FILE "/APM/" SKETCHNAME ".bak"
- #endif
- #define STORAGE_FLASH_RETRIES 5
- void Storage::_storage_open(void)
- {
- if (_initialised) {
- return;
- }
- #ifdef USE_POSIX
-
- if (log_fd == -1) {
- return;
- }
- #endif
-
- _dirty_mask.clearall();
- #if HAL_WITH_RAMTRON
- using_fram = fram.init();
- if (using_fram) {
- if (!fram.read(0, _buffer, CH_STORAGE_SIZE)) {
- return;
- }
- _save_backup();
- _initialised = true;
- return;
- }
-
- #endif
- #ifdef STORAGE_FLASH_PAGE
-
- _flash_load();
- #elif defined(USE_POSIX)
-
- sdcard_retry();
-
- log_fd = AP::FS().open(HAL_STORAGE_FILE, O_RDWR|O_CREAT);
- if (log_fd == -1) {
- hal.console->printf("open failed of " HAL_STORAGE_FILE "\n");
- return;
- }
- int ret = AP::FS().read(log_fd, _buffer, CH_STORAGE_SIZE);
- if (ret < 0) {
- hal.console->printf("read failed for " HAL_STORAGE_FILE "\n");
- AP::FS().close(log_fd);
- log_fd = -1;
- return;
- }
-
- if (AP::FS().lseek(log_fd, ret, SEEK_SET) != ret ||
- AP::FS().write(log_fd, &_buffer[ret], CH_STORAGE_SIZE-ret) != CH_STORAGE_SIZE-ret) {
- hal.console->printf("setup failed for " HAL_STORAGE_FILE "\n");
- AP::FS().close(log_fd);
- log_fd = -1;
- return;
- }
- using_filesystem = true;
- #endif
- _save_backup();
- _initialised = true;
- }
- void Storage::_save_backup(void)
- {
- #ifdef USE_POSIX
-
- sdcard_retry();
- int fd = AP::FS().open(HAL_STORAGE_BACKUP_FILE, O_WRONLY|O_CREAT|O_TRUNC);
- if (fd != -1) {
- AP::FS().write(fd, _buffer, CH_STORAGE_SIZE);
- AP::FS().close(fd);
- }
- #endif
- }
- void Storage::_mark_dirty(uint16_t loc, uint16_t length)
- {
- uint16_t end = loc + length;
- for (uint16_t line=loc>>CH_STORAGE_LINE_SHIFT;
- line <= end>>CH_STORAGE_LINE_SHIFT;
- line++) {
- _dirty_mask.set(line);
- }
- }
- void Storage::read_block(void *dst, uint16_t loc, size_t n)
- {
- if (loc >= sizeof(_buffer)-(n-1)) {
- return;
- }
- _storage_open();
- memcpy(dst, &_buffer[loc], n);
- }
- void Storage::write_block(uint16_t loc, const void *src, size_t n)
- {
- 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);
- }
- }
- void Storage::_timer_tick(void)
- {
- if (!_initialised) {
- return;
- }
- if (_dirty_mask.empty()) {
- _last_empty_ms = AP_HAL::millis();
- return;
- }
-
-
- uint16_t i;
- for (i=0; i<CH_STORAGE_NUM_LINES; i++) {
- if (_dirty_mask.get(i)) {
- break;
- }
- }
- if (i == CH_STORAGE_NUM_LINES) {
-
- return;
- }
- #if HAL_WITH_RAMTRON
- if (using_fram) {
- if (fram.write(CH_STORAGE_LINE_SIZE*i, &_buffer[CH_STORAGE_LINE_SIZE*i], CH_STORAGE_LINE_SIZE)) {
- _dirty_mask.clear(i);
- }
- return;
- }
- #endif
- #ifdef USE_POSIX
- if (using_filesystem && log_fd != -1) {
- uint32_t offset = CH_STORAGE_LINE_SIZE*i;
- if (AP::FS().lseek(log_fd, offset, SEEK_SET) != offset) {
- return;
- }
- if (AP::FS().write(log_fd, &_buffer[offset], CH_STORAGE_LINE_SIZE) != CH_STORAGE_LINE_SIZE) {
- return;
- }
- if (AP::FS().fsync(log_fd) != 0) {
- return;
- }
- _dirty_mask.clear(i);
- return;
- }
- #endif
-
- #ifdef STORAGE_FLASH_PAGE
-
- _flash_write(i);
- #endif
- }
- void Storage::_flash_load(void)
- {
- #ifdef STORAGE_FLASH_PAGE
- _flash_page = STORAGE_FLASH_PAGE;
- hal.console->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");
- }
- #else
- AP_HAL::panic("unable to init storage");
- #endif
- }
- void Storage::_flash_write(uint16_t line)
- {
- #ifdef STORAGE_FLASH_PAGE
- if (_flash.write(line*CH_STORAGE_LINE_SIZE, CH_STORAGE_LINE_SIZE)) {
-
- _dirty_mask.clear(line);
- }
- #endif
- }
- bool Storage::_flash_write_data(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length)
- {
- #ifdef STORAGE_FLASH_PAGE
- size_t base_address = hal.flash->getpageaddr(_flash_page+sector);
- for (uint8_t i=0; i<STORAGE_FLASH_RETRIES; i++) {
- if (hal.flash->write(base_address+offset, data, length)) {
- return true;
- }
- hal.scheduler->delay(1);
- }
- if (_flash_erase_ok()) {
-
-
- uint32_t now = AP_HAL::millis();
- if (now - _last_re_init_ms > 5000) {
- _last_re_init_ms = now;
- bool ok = _flash.re_initialise();
- hal.console->printf("Storage: failed at %u:%u for %u - re-init %u\n",
- (unsigned)sector, (unsigned)offset, (unsigned)length, (unsigned)ok);
- }
- }
- return false;
- #else
- return false;
- #endif
- }
- bool Storage::_flash_read_data(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length)
- {
- size_t base_address = hal.flash->getpageaddr(_flash_page+sector);
- const uint8_t *b = ((const uint8_t *)base_address)+offset;
- memcpy(data, b, length);
- return true;
- }
- bool Storage::_flash_erase_sector(uint8_t sector)
- {
- for (uint8_t i=0; i<STORAGE_FLASH_RETRIES; i++) {
- if (hal.flash->erasepage(_flash_page+sector)) {
- return true;
- }
- hal.scheduler->delay(1);
- }
- return false;
- }
- bool Storage::_flash_erase_ok(void)
- {
-
- return !hal.util->get_soft_armed();
- }
- bool Storage::healthy(void)
- {
- return _initialised && AP_HAL::millis() - _last_empty_ms < 2000;
- }
- #endif
|