Repository navigation
Conversation
…boot The freefile is created once, on the largest free block of the card and at most 4 GB (the largest FAT32 file), and reused as it is at every later boot. Its size is a whole number of superclusters plus one cluster, so once the logs have taken the superclusters one cluster is left: the card reads as full and the blackbox stops logging until the card is formatted, although the rest of a card larger than 4 GB and any log deleted from a PC are free. When the freefile is below 2 GB at boot, search the FAT for the largest free block as on the first boot, and if that is larger, free the old freefile's clusters and move the freefile there. Larger freefiles are reused without a search, as before.
…w it Logs take their clusters from the start of the freefile, so it only ever shrank until the next format, and one log could never grow past what was left of it at boot. On a card larger than 4 GB the clusters that follow the freefile are normally free, so at boot extend it over them, up to the largest file size, leaving AFATFS_FREEFILE_LEAVE_CLUSTERS for regular files as a new freefile does. Only a freefile that is still below 2 GB afterwards is moved to the largest free block. The new clusters are chained and flushed before the freefile's last cluster points at them, and the new size is saved after that, so a power cut leaves at worst unreferenced clusters, never a chain that runs into free ones.
After the skip the search went on reading the FAT sector of the freefile's first cluster while the cluster number had moved past it, so the entries it examined belonged to other clusters. It could only matter when those sectors were already cached, and the search at boot now runs with the freefile in place.
afatfs_FATFillWithPattern() counted a final FAT sector it only writes part of among the sectors to pre-erase. That sector has to be read first, and on an SD card on SPI the read ends the multiple-block write started with ACMD23 before it reaches that block; the SD specification leaves an unwritten pre-erased block undefined, erased or old. Its other entries, which can belong to the next file, could then be read back erased. A new freefile ends that way (whole superclusters plus one cluster), and so does every extension of it.
…rectory entry each step A power cut while the freefile was regrown or moved at boot left whatever chain was in flight as a lost FAT chain: up to 4 GB of new chain, and up to 2 GB of the old freefile when moving it. Three cuts on a TBS Lucid H7 Wing left three lost chains of 650 MB on average. The new chain now grows in 256 MB blocks, each one chained, linked, saved and flushed before the next. A moved freefile gives its old clusters back the same way from its end: the smaller size is saved, then the chain is cut there, then the tail is freed. A cut strands at most one block either way.
… end and freeing from the front
The freefile holds only part of the card, at most 4 GiB, and with the regrow at boot later logs also reach the free clusters outside it. After mount, while no file is open, those clusters are counted one FAT sector at a time with a gap after each, so that terrain can still take the card while afatfs is idle, and the count is kept up to date by allocations and frees. MSP_SDCARD_SUMMARY gets the freefile plus that count appended as cardFreeSpaceKB, 0xFFFFFFFF until the count is done (or for good if a FAT sector cannot be read). Older configurators read the fields before it as they did.
|
ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing |
PR Summary by QodoRegrow the SD freefile and report whole-card free space over MSP
AI Description
Diagram
High-Level Assessment
Files changed (6)
|
Code Review by Qodo
1.
|
…dy card The freefile's chain holds no free cluster, so the count no longer reads its FAT sectors: about a quarter of them on a 16 GB card, nearly all on cards of 4 GB or less. And a removed card's count stays in afatfs, so the summary sends it only while the card is ready.
|
RAM / Flash usage vs. base commit
See RAM/flash optimization guide for techniques to reduce usage. |
|
Test firmware build ready — commit Download firmware for PR #12156 251 targets built. Find your board's
|
…ding to KiB With 512-byte clusters an odd freefile and an odd outside count each lost half a KiB.
|
tested and it works, just I would like to add couple notes
|
The count read the whole FAT after every boot, from the realtime callbacks, and went on while armed whenever no file was open (a card used only for terrain, or before the log opens). It now starts at the first MSP_SDCARD_SUMMARY, which the Configurator sends from the Blackbox tab, and arming stops it; a stopped count resumes where it was at the next request.
After the free count the card's directories are read, one sector at a time with the count's gap, and MSP_SDCARD_SUMMARY gets three fields appended: - logTailsKB: each log takes whole superclusters from the freefile, and the rest of its last one stays taken without data until the log is deleted; - terrainKB: the terrain files (*.TER); - lostKB: what is taken outside the freefile but held by no file or directory, left by writes cut short. Any change to a directory entry makes the walk run again at the next request. A directory that can't be read, or nests deeper than 4, leaves the three unknown until the card changes.
|
@error414 thanks for testing it, and both points are right. The count started after every boot and only paused while a file was open: with the blackbox on the card the log opens at arm and stopped it, but with the card used only for terrain, or between arming and the log opening, it went on. Now it starts only when the Configurator asks for the card's summary (the Blackbox tab), and arming stops it, so nothing is read at the boots before a flight. The tab also shows what holds the taken space now: terrain data, the unused end of logs (each log takes whole blocks of a few MB) and space no file holds, left by writes cut short. |
…rst cluster The directory walk skipped the freefile's entry by its first cluster. Once the freefile is used up, that cluster is the first of whatever file follows its region, so the walk skipped that file instead and reported its size as lost: on a full card in the SITL, 512 KiB for the log after it, and the same after every lap of iNavFlight#12150's wraparound, whose relocated freefile is used up by the log created in it. The entry is now told by its sector and index.
|
Rebased nothing, one commit on top (83ac671): the directory walk told the freefile's entry apart by its first cluster, and once the freefile is used up that cluster is the first of the file after its region, which the walk then skipped and reported as lost. I found it testing this PR over #12150 in the SITL: after each lap of the wraparound the log after the relocated freefile showed as 512 KiB lost. The entry is now told by its sector and index. Flash +48 B on KAKUTEF7, KAKUTEF7HDV and MATEKF405SE, +32 B on MATEKF722SE and TBS_LUCID_H7_WING; RAM and ITCM unchanged; unit tests 620/620. The test itself, on this branch merged with next-10.1 (345233c): a 1 GB card full but for a 6 MB and a 1.5 MB hole, 13 short logs, so the freefile runs out at the 10th and #12150 evicts logs 1 to 3 for logs 11 to 13. The free space reported over MSP matched the image's FAT at every step (8192 KiB at boot, 3064 KiB after the three laps); the only lost space is the last cluster of the used-up freefile, 4 KiB, which #12150 leaves behind when it relocates it. Nothing of #12158 is needed over #12150: its reclaim touches the directory, not the FAT, so the count neither waits behind it nor has to start over. |
Builds on #12056 (freefile regrow at boot), so this branch contains it and the diff shows both until #12056 is merged. The changes of its own are the commits after it.
What it does:
MSP_SDCARD_SUMMARY, which the Configurator sends from the Blackbox tab, and arming stops it; stopped, it resumes where it was at the next request while disarmed. So nothing reads the FAT at the boots before a flight, and nothing in flight.afatfs_isIdle(), and back-to-back reads would keep it waiting until the count ends. A FAT sector that can't be read within a second ends the count, and the value stays unknown.LOG*.TXT) take whole superclusters from the freefile, so the rest of each log's last one stays taken without data until the log is deleted;*.TER);Any change to a directory entry makes the walk run again at the next request. A directory that can't be read, or nests deeper than 4, leaves these unknown.
MSP_SDCARD_SUMMARYgetscardFreeSpaceKB,logTailsKB,terrainKBandlostKB(uint32 each) appended,0xFFFFFFFFuntil counted or while the card is not ready. The fields before them are unchanged, so older configurators read the reply as before.Size against #12056:
The count step and the walk are kept out of line, or LTO would pull them into
scheduler()'s ITCM. Bootloaders: MATEKF722SE_bl +80 B (91.4 % of 32 KB), MATEKF405SE_bl +24 B (83.7 %).Tested in the SITL with
--sdcard, each value checked against the image read by an independent script:On the TBS Lucid H7 Wing (2026-10-20), a 16 GB card (FAT32, 32 KB clusters) with bench logs and the lost chains of earlier power-cut tests: the count and the directory walk ended 8.7 s after the first request, and the free space (11163168 KiB), the log ends (308608 KiB), terrain (0) and lost space (1098880 KiB) all equal what the script reads from the card, read back in mass storage mode.
On a TBS Lucid H7 (SDIO) with a 16 GB card, FAT32 with 32 KB clusters, about 3800 FAT sectors, measured before the freefile skip (which leaves out about a quarter of them on that card):
afatfs_isIdle()was true 91-92 % of the time, and 100 % after it. Without the gap the count took 0.7 s, with afatfs busy the whole time.A 1 ms gap would roughly halve the count time and still leave afatfs idle most of the time; I kept the 2 ms I measured.
#12060 refills the freefile while running; #12158 joins the two.