22 #define FLASHSTORAGE_DEBUG 0 24 #if FLASHSTORAGE_DEBUG 25 #define debug(fmt, args...) do { printf(fmt, ##args); } while(0) 27 #define debug(fmt, args...) do { } while(0) 32 uint32_t _flash_sector_size,
33 FlashWrite _flash_write,
34 FlashRead _flash_read,
35 FlashErase _flash_erase,
36 FlashEraseOK _flash_erase_ok) :
37 mem_buffer(_mem_buffer),
38 flash_sector_size(_flash_sector_size),
39 flash_write(_flash_write),
40 flash_read(_flash_read),
41 flash_erase(_flash_erase),
42 flash_erase_ok(_flash_erase_ok) {}
47 debug(
"running init()\n");
56 for (uint8_t i=0; i<2; i++) {
57 if (!
flash_read(i, 0, (uint8_t *)&header[i],
sizeof(header[i]))) {
78 if (states[0] == states[1]) {
97 for (uint8_t i=0; i<2; i++) {
98 uint8_t sector = (first_sector + i) & 1;
120 for (uint8_t i=0; i<2; i++) {
140 debug(
"running switch_full_sector()\n");
201 uint8_t n2 = block_nbytes - (offset %
block_size);
224 if (!
flash_read(sector, ofs, (uint8_t *)&header,
sizeof(header))) {
245 uint16_t block_nbytes = (header.num_blocks_minus_one+1)*
block_size;
246 ofs += block_nbytes +
sizeof(header);
251 uint16_t block_nbytes = (header.num_blocks_minus_one+1)*
block_size;
252 uint16_t block_ofs = header.block_num*
block_size;
261 ofs += block_nbytes +
sizeof(header);
285 return flash_write(sector, 0, (
const uint8_t *)&header,
sizeof(header));
314 debug(
"write_all to sector %u at %u with reserved_space=%u\n",
342 debug(
"both sectors are full\n");
350 debug(
"switching to sector %u\n", new_sector);
353 if (!
flash_read(new_sector, 0, (uint8_t *)&header,
sizeof(header))) {
362 debug(
"both sectors full\n");
377 if (!
flash_write(new_sector, 0, (
const uint8_t *)&header,
sizeof(header))) {
bool switch_sectors(void)
static const uint32_t reserve_size
bool switch_full_sector(void)
bool erase_sector(uint8_t sector)
static const uint16_t storage_size
bool all_zero(uint16_t ofs, uint16_t size)
static const uint8_t block_size
bool write(uint16_t offset, uint16_t length)
#define debug(fmt, args...)
FlashEraseOK flash_erase_ok
const uint32_t flash_sector_size
static const uint8_t max_write
bool load_sector(uint8_t sector)
AP_FlashStorage(uint8_t *mem_buffer, uint32_t flash_sector_size, FlashWrite flash_write, FlashRead flash_read, FlashErase flash_erase, FlashEraseOK flash_erase_ok)
static const uint32_t signature