MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / switch_sectors

Method switch_sectors

libraries/AP_FlashStorage/AP_FlashStorage.cpp:396–447  ·  view source on GitHub ↗

switch to next sector for writing

Source from the content-addressed store, hash-verified

394
395// switch to next sector for writing
396bool AP_FlashStorage::switch_sectors(void)
397{
398 if (reserved_space != 0) {
399 // other sector is already full
400 debug("both sectors are full\n");
401 return false;
402 }
403
404 struct sector_header header;
405
406 uint8_t new_sector = current_sector ^ 1;
407 debug("switching to sector %u\n", new_sector);
408
409 // check sector is available
410 if (!flash_read(new_sector, 0, (uint8_t *)&header, sizeof(header))) {
411 return false;
412 }
413 if (!header.signature_ok()) {
414 write_error = true;
415 return false;
416 }
417 if (SECTOR_STATE_AVAILABLE != header.get_state()) {
418 write_error = true;
419 debug("new sector unavailable; state=0x%02x\n", (unsigned)header.get_state());
420 return false;
421 }
422
423 // mark current sector as full. This needs to be done before we
424 // mark the new sector as in-use so that a power failure between
425 // the two steps doesn't leave us with an erase on the
426 // reboot. Thanks to night-ghost for spotting this.
427 header.set_state(SECTOR_STATE_FULL);
428 if (!flash_write(current_sector, 0, (const uint8_t *)&header, sizeof(header))) {
429 return false;
430 }
431
432 // mark new sector as in-use
433 header.set_state(SECTOR_STATE_IN_USE);
434 if (!flash_write(new_sector, 0, (const uint8_t *)&header, sizeof(header))) {
435 return false;
436 }
437
438 // switch sectors
439 current_sector = new_sector;
440
441 // we need to reserve some space in next sector to ensure we can successfully do a
442 // full write out on init()
443 reserved_space = reserve_size;
444
445 write_offset = sizeof(header);
446 return true;
447}
448
449/*
450 re-initialise, using current mem_buffer

Callers

nothing calls this directly

Calls 4

debugFunction · 0.85
signature_okMethod · 0.80
get_stateMethod · 0.45
set_stateMethod · 0.45

Tested by

no test coverage detected