Potential incomplete initialization on AP_Flashstorage

While troubleshooting aninternal error 0x200000 I noticed that parameters could no longer be saved permanently. Notably, the parameters STAT_BOOTCNT and STAT_RUNTIME—which usually increment automatically—kept reverting to their previous values ​​after a restart.
Further analysis revealed that:
Sector 0 was marked with status [SECTOR_STATE_AVAILABLE]
Sector 1 was marked with status [SECTOR_STATE_IN_USE]
Consequently, the current parameters were being loaded from Sector 1.
However, when writing new parameters, the system attempted to write to Sector 0.
In my opinion, the cause of this is that the variable current_sector is not set to the active sector during initialization.
I was able to resolve the issue—at least for testing purposes—by inserting the initialization of the current_sector variable after the following code block:

// work out the first sector to read from using sector states
enum SectorState states[2] {header[0].get_state(), header[1].get_state()}; 
uint8_t first_sector; 

if (states[0] == states[1]) {
if (states[0] != SECTOR_STATE_AVAILABLE) {
return erase_all(); 
}
first_sector = 0; 
} else if (states[0] == SECTOR_STATE_FULL) {
first_sector = 0; 
} else if (states[1] == SECTOR_STATE_FULL) {
first_sector = 1; 
} else if (states[0] == SECTOR_STATE_IN_USE) {
first_sector = 0; 
} else if (states[1] == SECTOR_STATE_IN_USE) {
first_sector = 1; 
} else {
// doesn't matter which is first
first_sector = 0; 
}
//added initialization current_sector
current_sector = first_sector;

Hi @rmackay9
I think this is all together related to your issue Copter-4.7.0: Internal error: 0x200000, Param storage failed · Issue #33538 · ArduPilot/ardupilot · GitHub
will it checked / updated there or should I open a new issue?

Hi @Juergen-Fahlbusch,

I’ll bring it up again on next Tuesday’s dev call. A PR with the potential fix might be interesting.

TBH, this is not my area so I can pass it along to the devs who are responsible (Tridge, PeterB, etc) but I’m not really qualified to own the issue. Anyway, let’s see what they say

OK, also this is a new field for me but I tried to open a PR.
Hopefully I did it correct.

1 Like

To keep things tied together, here is the PR AP_FlashStorage: set current_sector during init by Juergen-Fahlbusch · Pull Request #33640 · ArduPilot/ardupilot · GitHub

Thanks for all your help while had a good sleep :sweat_smile:

1 Like