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

Method init

libraries/AP_FlashStorage/AP_FlashStorage.cpp:47–134  ·  view source on GitHub ↗

initialise storage

Source from the content-addressed store, hash-verified

45
46// initialise storage
47bool AP_FlashStorage::init(void)
48{
49 debug("running init()\n");
50
51 // start with empty memory buffer
52 memset(mem_buffer, 0, storage_size);
53
54 // find state of sectors
55 struct sector_header header[2];
56
57 // read headers and possibly initialise if bad signature
58 for (uint8_t i=0; i<2; i++) {
59 if (!flash_read(i, 0, (uint8_t *)&header[i], sizeof(header[i]))) {
60 return false;
61 }
62 bool bad_header = !header[i].signature_ok();
63 enum SectorState state = header[i].get_state();
64 if (state != SECTOR_STATE_AVAILABLE &&
65 state != SECTOR_STATE_IN_USE &&
66 state != SECTOR_STATE_FULL) {
67 bad_header = true;
68 }
69
70 // initialise if bad header
71 if (bad_header) {
72 return erase_all();
73 }
74 }
75
76 // work out the first sector to read from using sector states
77 enum SectorState states[2] {header[0].get_state(), header[1].get_state()};
78 uint8_t first_sector;
79
80 if (states[0] == states[1]) {
81 if (states[0] != SECTOR_STATE_AVAILABLE) {
82 return erase_all();
83 }
84 first_sector = 0;
85 } else if (states[0] == SECTOR_STATE_FULL) {
86 first_sector = 0;
87 } else if (states[1] == SECTOR_STATE_FULL) {
88 first_sector = 1;
89 } else if (states[0] == SECTOR_STATE_IN_USE) {
90 first_sector = 0;
91 } else if (states[1] == SECTOR_STATE_IN_USE) {
92 first_sector = 1;
93 } else {
94 // doesn't matter which is first
95 first_sector = 0;
96 }
97
98 // load data from any current sectors
99 for (uint8_t i=0; i<2; i++) {
100 uint8_t sector = (first_sector + i) & 1;
101 if (states[sector] == SECTOR_STATE_IN_USE ||
102 states[sector] == SECTOR_STATE_FULL) {
103 if (!load_sector(sector)) {
104 return erase_all();

Callers 1

FlashTest.cppFile · 0.45

Calls 4

debugFunction · 0.85
memsetFunction · 0.85
signature_okMethod · 0.80
get_stateMethod · 0.45

Tested by

no test coverage detected