From 6c231e189b5f02a30167dcef97eed0a3d441b69c Mon Sep 17 00:00:00 2001 From: Nicholas Sherlock Date: Fri, 11 Sep 2015 14:15:43 +1200 Subject: [PATCH] Reduce amount of wasted dataflash space upon power cycle This wasted overhead becomes important when using the Blackbox "pause" switch and logging very few of your flights. --- src/main/io/flashfs.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/io/flashfs.c b/src/main/io/flashfs.c index 429ffe8ad..bfc6007dd 100644 --- a/src/main/io/flashfs.c +++ b/src/main/io/flashfs.c @@ -481,7 +481,7 @@ int flashfsIdentifyStartOfFreeSpace() /* We can choose whatever power of 2 size we like, which determines how much wastage of free space we'll have * at the end of the last written data. But smaller blocksizes will require more searching. */ - FREE_BLOCK_SIZE = 65536, + FREE_BLOCK_SIZE = 2048, /* We don't expect valid data to ever contain this many consecutive uint32_t's of all 1 bits: */ FREE_BLOCK_TEST_SIZE_INTS = 4, // i.e. 16 bytes @@ -493,16 +493,20 @@ int flashfsIdentifyStartOfFreeSpace() uint32_t ints[FREE_BLOCK_TEST_SIZE_INTS]; } testBuffer; - int left = 0; - int right = flashfsGetSize() / FREE_BLOCK_SIZE; - int mid, result = right; + int left = 0; // Smallest block index in the search region + int right = flashfsGetSize() / FREE_BLOCK_SIZE; // One past the largest block index in the search region + int mid; + int result = right; int i; bool blockErased; while (left < right) { mid = (left + right) / 2; - m25p16_readBytes(mid * FREE_BLOCK_SIZE, testBuffer.bytes, FREE_BLOCK_TEST_SIZE_BYTES); + if (m25p16_readBytes(mid * FREE_BLOCK_SIZE, testBuffer.bytes, FREE_BLOCK_TEST_SIZE_BYTES) < FREE_BLOCK_TEST_SIZE_BYTES) { + // Unexpected timeout from flash, so bail early (reporting the device fuller than it really is) + break; + } // Checking the buffer 4 bytes at a time like this is probably faster than byte-by-byte, but I didn't benchmark it :) blockErased = true;