2016-11-17 01:24:16 -04:00
|
|
|
/*
|
2021-05-26 23:33:17 -03:00
|
|
|
Please contribute your ideas! See https://ardupilot.org/dev for details
|
2016-11-17 01:24:16 -04: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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
a class to allow for FLASH to be used as a memory backed storage
|
|
|
|
backend for any HAL. The basic methodology is to use a log based
|
|
|
|
storage system over two flash sectors. Key design elements:
|
|
|
|
|
|
|
|
- erase of sectors only called on init, as erase will lock the flash
|
|
|
|
and prevent code execution
|
|
|
|
|
|
|
|
- write using log based system
|
|
|
|
|
|
|
|
- read requires scan of all log elements. This is expected to be called rarely
|
|
|
|
|
|
|
|
- assumes flash that erases to 0xFF and where writing can only clear
|
|
|
|
bits, not set them
|
|
|
|
|
|
|
|
- assumes flash sectors are much bigger than storage size. If they
|
|
|
|
aren't then caller can aggregate multiple sectors. Designed for
|
|
|
|
128k flash sectors with 16k storage size.
|
|
|
|
|
|
|
|
- assumes two flash sectors are available
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2020-04-11 23:16:00 -03:00
|
|
|
/*
|
|
|
|
we support 3 different types of flash which have different restrictions
|
|
|
|
*/
|
|
|
|
#define AP_FLASHSTORAGE_TYPE_F1 1 // F1 and F3
|
|
|
|
#define AP_FLASHSTORAGE_TYPE_F4 2 // F4 and F7
|
|
|
|
#define AP_FLASHSTORAGE_TYPE_H7 3 // H7
|
2021-03-11 16:08:24 -04:00
|
|
|
#define AP_FLASHSTORAGE_TYPE_G4 4 // G4
|
2020-04-11 23:16:00 -03:00
|
|
|
|
|
|
|
#ifndef AP_FLASHSTORAGE_TYPE
|
2019-10-31 08:28:10 -03:00
|
|
|
#if defined(STM32F1) || defined(STM32F3)
|
2019-05-26 22:39:41 -03:00
|
|
|
/*
|
2019-10-31 08:28:10 -03:00
|
|
|
the STM32F1 and STM32F3 can't change individual bits from 1 to 0
|
|
|
|
unless all bits in the 16 bit word are 1
|
2019-05-26 22:39:41 -03:00
|
|
|
*/
|
2020-04-11 23:16:00 -03:00
|
|
|
#define AP_FLASHSTORAGE_TYPE AP_FLASHSTORAGE_TYPE_F1
|
|
|
|
#elif defined(STM32H7)
|
|
|
|
/*
|
|
|
|
STM32H7 can only write in 32 byte chunks, and must only write when all bits are 1
|
|
|
|
*/
|
|
|
|
#define AP_FLASHSTORAGE_TYPE AP_FLASHSTORAGE_TYPE_H7
|
2023-04-12 00:41:35 -03:00
|
|
|
#elif defined(STM32G4) || defined(STM32L4) || defined(STM32L4PLUS)
|
2021-03-11 16:08:24 -04:00
|
|
|
/*
|
|
|
|
STM32G4 can only write in 8 byte chunks, and must only write when all bits are 1
|
|
|
|
*/
|
|
|
|
#define AP_FLASHSTORAGE_TYPE AP_FLASHSTORAGE_TYPE_G4
|
|
|
|
#else // F4, F7
|
2020-04-11 23:16:00 -03:00
|
|
|
/*
|
|
|
|
STM32HF4 and STM32H7 can update bits from 1 to 0
|
|
|
|
*/
|
|
|
|
#define AP_FLASHSTORAGE_TYPE AP_FLASHSTORAGE_TYPE_F4
|
|
|
|
#endif
|
2019-05-26 22:39:41 -03:00
|
|
|
#endif
|
|
|
|
|
2016-11-17 01:24:16 -04:00
|
|
|
/*
|
2023-02-13 06:37:45 -04:00
|
|
|
The StorageManager holds the layout of non-volatile storage
|
2016-11-17 01:24:16 -04:00
|
|
|
*/
|
|
|
|
class AP_FlashStorage {
|
|
|
|
private:
|
2020-04-11 23:16:00 -03:00
|
|
|
#if AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_H7
|
|
|
|
// need to write in 32 byte chunks, with 2 byte header
|
|
|
|
static const uint8_t block_size = 30;
|
|
|
|
static const uint8_t max_write = block_size;
|
2021-03-11 16:08:24 -04:00
|
|
|
#elif AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_G4
|
|
|
|
// write in 8 byte chunks, with 2 byte header
|
|
|
|
static const uint8_t block_size = 6;
|
|
|
|
static const uint8_t max_write = block_size;
|
2020-04-11 23:16:00 -03:00
|
|
|
#else
|
2016-11-17 01:24:16 -04:00
|
|
|
static const uint8_t block_size = 8;
|
|
|
|
static const uint8_t max_write = 64;
|
2020-04-11 23:16:00 -03:00
|
|
|
#endif
|
|
|
|
static const uint16_t num_blocks = (HAL_STORAGE_SIZE+(block_size-1)) / block_size;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
// caller provided function to write to a flash sector
|
|
|
|
FUNCTOR_TYPEDEF(FlashWrite, bool, uint8_t , uint32_t , const uint8_t *, uint16_t );
|
|
|
|
|
|
|
|
// caller provided function to read from a flash sector. Only called on init()
|
|
|
|
FUNCTOR_TYPEDEF(FlashRead, bool, uint8_t , uint32_t , uint8_t *, uint16_t );
|
|
|
|
|
|
|
|
// caller provided function to erase a flash sector. Only called from init()
|
|
|
|
FUNCTOR_TYPEDEF(FlashErase, bool, uint8_t );
|
|
|
|
|
2016-11-18 19:22:58 -04:00
|
|
|
// caller provided function to indicate if erasing is allowed
|
|
|
|
FUNCTOR_TYPEDEF(FlashEraseOK, bool);
|
|
|
|
|
2016-11-17 01:24:16 -04:00
|
|
|
// constructor.
|
|
|
|
AP_FlashStorage(uint8_t *mem_buffer, // buffer of storage_size bytes
|
|
|
|
uint32_t flash_sector_size, // size of each flash sector in bytes
|
|
|
|
FlashWrite flash_write, // function to write to flash
|
|
|
|
FlashRead flash_read, // function to read from flash
|
2016-11-18 19:22:58 -04:00
|
|
|
FlashErase flash_erase, // function to erase flash
|
|
|
|
FlashEraseOK flash_erase_ok); // function to check if erasing allowed
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// initialise storage, filling mem_buffer with current contents
|
|
|
|
bool init(void);
|
2016-11-18 19:22:58 -04:00
|
|
|
|
2019-12-26 20:45:20 -04:00
|
|
|
// erase sectors and re-initialise
|
2020-04-08 21:25:52 -03:00
|
|
|
bool erase(void) WARN_IF_UNUSED {
|
2019-12-26 20:45:20 -04:00
|
|
|
return erase_all();
|
|
|
|
}
|
|
|
|
|
2017-03-22 05:36:45 -03:00
|
|
|
// re-initialise storage, using current mem_buffer
|
2020-04-08 21:25:52 -03:00
|
|
|
bool re_initialise(void) WARN_IF_UNUSED;
|
2017-03-22 05:36:45 -03:00
|
|
|
|
2016-11-18 19:22:58 -04:00
|
|
|
// switch full sector - should only be called when safe to have CPU
|
|
|
|
// offline for considerable periods as an erase will be needed
|
2020-04-08 21:25:52 -03:00
|
|
|
bool switch_full_sector(void) WARN_IF_UNUSED;
|
|
|
|
|
2016-11-17 01:24:16 -04:00
|
|
|
// write some data to storage from mem_buffer
|
2020-04-08 21:25:52 -03:00
|
|
|
bool write(uint16_t offset, uint16_t length) WARN_IF_UNUSED;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// fixed storage size
|
2020-04-11 23:16:00 -03:00
|
|
|
static const uint16_t storage_size = HAL_STORAGE_SIZE;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t *mem_buffer;
|
|
|
|
const uint32_t flash_sector_size;
|
|
|
|
FlashWrite flash_write;
|
|
|
|
FlashRead flash_read;
|
|
|
|
FlashErase flash_erase;
|
2016-11-18 19:22:58 -04:00
|
|
|
FlashEraseOK flash_erase_ok;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
uint8_t current_sector;
|
|
|
|
uint32_t write_offset;
|
|
|
|
uint32_t reserved_space;
|
|
|
|
bool write_error;
|
|
|
|
|
|
|
|
// 24 bit signature
|
2020-04-11 23:16:00 -03:00
|
|
|
#if AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_F4
|
2016-11-17 01:24:16 -04:00
|
|
|
static const uint32_t signature = 0x51685B;
|
2020-04-11 23:16:00 -03:00
|
|
|
#elif AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_F1
|
2019-05-26 22:39:41 -03:00
|
|
|
static const uint32_t signature = 0x51;
|
2020-04-11 23:16:00 -03:00
|
|
|
#elif AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_H7
|
|
|
|
static const uint32_t signature = 0x51685B62;
|
2021-03-11 16:08:24 -04:00
|
|
|
#elif AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_G4
|
|
|
|
static const uint32_t signature = 0x1586B562;
|
2020-04-11 23:16:00 -03:00
|
|
|
#else
|
|
|
|
#error "Unknown AP_FLASHSTORAGE_TYPE"
|
2019-05-26 22:39:41 -03:00
|
|
|
#endif
|
2016-11-17 01:24:16 -04:00
|
|
|
|
2020-04-11 23:16:00 -03:00
|
|
|
// sector states, representation depends on storage type
|
2016-11-17 01:24:16 -04:00
|
|
|
enum SectorState {
|
2020-04-11 23:16:00 -03:00
|
|
|
SECTOR_STATE_AVAILABLE = 1,
|
|
|
|
SECTOR_STATE_IN_USE = 2,
|
|
|
|
SECTOR_STATE_FULL = 3,
|
|
|
|
SECTOR_STATE_INVALID = 4
|
2016-11-17 01:24:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// header in first word of each sector
|
|
|
|
struct sector_header {
|
2020-04-11 23:16:00 -03:00
|
|
|
#if AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_F4
|
|
|
|
uint32_t state1:8;
|
|
|
|
uint32_t signature1:24;
|
|
|
|
#elif AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_F1
|
|
|
|
uint32_t state1:32;
|
|
|
|
uint32_t signature1:16;
|
|
|
|
#elif AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_H7
|
|
|
|
// needs to be 96 bytes on H7 to support 3 states
|
|
|
|
uint32_t state1;
|
|
|
|
uint32_t signature1;
|
|
|
|
uint32_t pad1[6];
|
|
|
|
uint32_t state2;
|
|
|
|
uint32_t signature2;
|
|
|
|
uint32_t pad2[6];
|
|
|
|
uint32_t state3;
|
|
|
|
uint32_t signature3;
|
|
|
|
uint32_t pad3[6];
|
2021-03-11 16:08:24 -04:00
|
|
|
#elif AP_FLASHSTORAGE_TYPE == AP_FLASHSTORAGE_TYPE_G4
|
|
|
|
// needs to be 24 bytes on G4 to support 3 states
|
|
|
|
uint32_t state1;
|
|
|
|
uint32_t signature1;
|
|
|
|
uint32_t state2;
|
|
|
|
uint32_t signature2;
|
|
|
|
uint32_t state3;
|
|
|
|
uint32_t signature3;
|
2019-05-26 22:39:41 -03:00
|
|
|
#endif
|
2020-04-11 23:16:00 -03:00
|
|
|
bool signature_ok(void) const;
|
|
|
|
SectorState get_state() const;
|
|
|
|
void set_state(SectorState state);
|
2016-11-17 01:24:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
enum BlockState {
|
|
|
|
BLOCK_STATE_AVAILABLE = 0x3,
|
|
|
|
BLOCK_STATE_WRITING = 0x1,
|
|
|
|
BLOCK_STATE_VALID = 0x0
|
|
|
|
};
|
|
|
|
|
|
|
|
// header of each block of data
|
|
|
|
struct block_header {
|
|
|
|
uint16_t state:2;
|
|
|
|
uint16_t block_num:11;
|
|
|
|
uint16_t num_blocks_minus_one:3;
|
|
|
|
};
|
|
|
|
|
|
|
|
// amount of space needed to write full storage
|
|
|
|
static const uint32_t reserve_size = (storage_size / max_write) * (sizeof(block_header) + max_write) + max_write;
|
|
|
|
|
|
|
|
// load data from a sector
|
2020-04-08 21:25:52 -03:00
|
|
|
bool load_sector(uint8_t sector) WARN_IF_UNUSED;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// erase a sector and write header
|
2020-04-08 21:25:52 -03:00
|
|
|
bool erase_sector(uint8_t sector, bool mark_available) WARN_IF_UNUSED;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// erase all sectors and reset
|
2020-04-08 21:25:52 -03:00
|
|
|
bool erase_all() WARN_IF_UNUSED;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// write all of mem_buffer to current sector
|
2020-04-08 21:25:52 -03:00
|
|
|
bool write_all() WARN_IF_UNUSED;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// return true if all bytes are zero
|
2020-04-08 21:25:52 -03:00
|
|
|
bool all_zero(uint16_t ofs, uint16_t size) WARN_IF_UNUSED;
|
2016-11-17 01:24:16 -04:00
|
|
|
|
|
|
|
// switch to next sector for writing
|
2020-04-08 21:25:52 -03:00
|
|
|
bool switch_sectors(void) WARN_IF_UNUSED;
|
|
|
|
|
|
|
|
// _switch_full_sector is protected by switch_full_sector to avoid
|
2023-10-11 04:41:52 -03:00
|
|
|
// an infinite recursion problem; switch_full_sector calls
|
2020-04-08 21:25:52 -03:00
|
|
|
// write() which can call switch_full_sector. This has been seen
|
|
|
|
// in practice.
|
|
|
|
bool protected_switch_full_sector(void) WARN_IF_UNUSED;
|
|
|
|
bool in_switch_full_sector;
|
2016-11-17 01:24:16 -04:00
|
|
|
};
|