From 8c84894256349fbf4c278b229394eb313382e022 Mon Sep 17 00:00:00 2001 From: Tao Date: Fri, 27 Dec 2024 00:37:26 +0000 Subject: [PATCH] flashfs: more cleanups --- src/main/blackbox/blackbox_fielddefs.h | 7 --- src/main/io/flashfs.c | 46 +++++++++---------- src/test/unit/flashfs_unittest.cc | 2 - .../flashfs_unittest.include/flash_c_stub.cc | 1 - .../flash_emulator.cc | 1 - 5 files changed, 21 insertions(+), 36 deletions(-) diff --git a/src/main/blackbox/blackbox_fielddefs.h b/src/main/blackbox/blackbox_fielddefs.h index 209fdeb101..163635f2a5 100644 --- a/src/main/blackbox/blackbox_fielddefs.h +++ b/src/main/blackbox/blackbox_fielddefs.h @@ -219,12 +219,6 @@ typedef struct flightLogEvent_loggingResume_s { uint32_t currentTime; } flightLogEvent_loggingResume_t; -typedef struct flightLogEvent_flashfsState_s { - uint32_t headAddress; - uint32_t tailAddress; - uint16_t bufferDropped; -} flightLogEvent_flashfsState_t; - #define FLIGHT_LOG_EVENT_INFLIGHT_ADJUSTMENT_FUNCTION_FLOAT_VALUE_FLAG 128 typedef union flightLogEventData_u { @@ -233,7 +227,6 @@ typedef union flightLogEventData_u { flightLogEvent_govState_t govState; flightLogEvent_rescueState_t rescueState; flightLogEvent_airborneState_t airborneState; - flightLogEvent_flashfsState_t flashfsState; flightLogEvent_disarm_t disarm; flightLogEvent_inflightAdjustment_t inflightAdjustment; flightLogEvent_customData_t data; diff --git a/src/main/io/flashfs.c b/src/main/io/flashfs.c index 0807e4f86c..91dd1c9fb8 100644 --- a/src/main/io/flashfs.c +++ b/src/main/io/flashfs.c @@ -51,15 +51,14 @@ #include "pg/flashfs.h" -#include "blackbox/blackbox.h" - - /* - * How background erase work: - * 1. When start programming (flush) a page, if erase is needed and can be started, set FLASHFS_BACKGROUND_ERASE_PENDING - * 2. No more flush can be done when FLASHFS_BACKGROUND_ERASE_PENDING - * 3. When page program finishes, EraseAsync task picks up the erase task and set FLASHFS_BACKGROUND_ERASING - * 4. When erase finishes, EraseAsync task resets state to FLASHFS_IDLE + * How background erase works: + * 1. When start programming (flush) a page, if erase is needed and can be + * started, set FLASHFS_BACKGROUND_ERASE_PENDING. + * 2. No more flush can be done when FLASHFS_BACKGROUND_ERASE_PENDING. + * 3. When page program finishes, EraseAsync task picks up the erase task and + * sets FLASHFS_BACKGROUND_ERASING. + * 4. When erase finishes, EraseAsync task resets state to FLASHFS_IDLE. */ typedef enum { FLASHFS_IDLE, @@ -90,7 +89,6 @@ static DMA_DATA_ZERO_INIT uint8_t flashWriteBuffer[FLASHFS_WRITE_BUFFER_SIZE]; */ static uint16_t bufferHead = 0; static volatile uint16_t bufferTail = 0; -static uint16_t bufferDropped = 0; /* Track if there is new data to write. Until the contents of the buffer have been completely * written flashfsFlushAsync() will be repeatedly called. The tail pointer is only updated @@ -125,7 +123,6 @@ static inline uint32_t flashfsAddressShift(uint32_t address, int32_t offset) { static void flashfsClearBuffer(void) { bufferTail = bufferHead = 0; - bufferDropped = 0; } static bool flashfsBufferIsEmpty(void) @@ -304,15 +301,14 @@ static uint32_t flashfsWriteBuffers(uint8_t const **buffers, uint32_t *bufferSiz #ifdef USE_FLASHFS_LOOP /* - * * Bail out if we are running any of the erases. * There are a few cases: * * FLASHFS_ARMING_ERASING: logging is running ("switch" mode) and * arming erase has triggered or running. We can't write. * * FLASHFS_BACKGROUND_ERASE_PENDING: background erase is pending. We - * can't write. (If we write, those bytes will be discarded on erase). + * can't write. (If we write, those bytes will be discarded on erase.) * * FLASHFS_BACKGROUND_ERASING: technically we can write (under this - * state and flashIsReady()). Because background erase erases only 1 + * state and flashIsReady()), because background erase erases only 1 * sector. * * FLASHFS_ALL_ERASING: we can't write. We may land between erase * sectors. @@ -323,15 +319,19 @@ static uint32_t flashfsWriteBuffers(uint8_t const **buffers, uint32_t *bufferSiz // Check if background erase is needed. Why here? Because we can only // erase when a page (aligned) program is completed. - const uint32_t freeSpace = flashfsSize - flashfsGetOffset(); - if (flashfsConfig()->backgroundErase && freeSpace < flashGeometry->sectorSize) { + if (flashfsConfig()->backgroundErase && + freeSpace < flashGeometry->sectorSize) { // See if we are finishing a page. uint32_t bytes_to_write = 0; - if ( bufferCount > 0 ) bytes_to_write += bufferSizes[0]; - if ( bufferCount > 1 ) bytes_to_write += bufferSizes[1]; - if (tailAddress / flashGeometry->pageSize != (tailAddress + bytes_to_write)/flashGeometry->pageSize) { - // We will write to or write over a page boundary. We can erase when this write is done. + if (bufferCount > 0) + bytes_to_write += bufferSizes[0]; + if (bufferCount > 1) + bytes_to_write += bufferSizes[1]; + if (tailAddress / flashGeometry->pageSize != + (tailAddress + bytes_to_write) / flashGeometry->pageSize) { + // We will write to or write over a page boundary. We can erase when + // this write is done. flashfsState = FLASHFS_BACKGROUND_ERASE_PENDING; } } @@ -510,7 +510,8 @@ void flashfsEraseAsync(void) } } else if (flashfsState == FLASHFS_BACKGROUND_ERASE_PENDING) { flashEraseSector(headAddress); - headAddress = (headAddress + flashGeometry->sectorSize) % flashfsSize; + headAddress = + (headAddress + flashGeometry->sectorSize) % flashfsSize; flashfsState = FLASHFS_BACKGROUND_ERASING; LED1_TOGGLE; } else if (flashfsState == FLASHFS_BACKGROUND_ERASING) { @@ -548,11 +549,6 @@ void flashfsWriteByte(uint8_t byte) if (bufferHead >= FLASHFS_WRITE_BUFFER_SIZE) { bufferHead = 0; } - - if (flashfsBufferIsEmpty()) { - bufferDropped++; - } - } /** diff --git a/src/test/unit/flashfs_unittest.cc b/src/test/unit/flashfs_unittest.cc index 03e06916ac..4cec26ec2a 100644 --- a/src/test/unit/flashfs_unittest.cc +++ b/src/test/unit/flashfs_unittest.cc @@ -37,8 +37,6 @@ extern uint32_t tailAddress; #include "gmock/gmock.h" #include "gtest/gtest.h" -#include - /* * There are some weird logic behind flashfs.c and flash.c. This unittest is * written to accomedate them diff --git a/src/test/unit/flashfs_unittest.include/flash_c_stub.cc b/src/test/unit/flashfs_unittest.include/flash_c_stub.cc index a4f07db540..8540640686 100644 --- a/src/test/unit/flashfs_unittest.include/flash_c_stub.cc +++ b/src/test/unit/flashfs_unittest.include/flash_c_stub.cc @@ -23,7 +23,6 @@ extern "C" { #include "drivers/flash.h" -#include "blackbox/blackbox.h" } #include "flash_interface.h" diff --git a/src/test/unit/flashfs_unittest.include/flash_emulator.cc b/src/test/unit/flashfs_unittest.include/flash_emulator.cc index c75ea2d717..b3d2120286 100644 --- a/src/test/unit/flashfs_unittest.include/flash_emulator.cc +++ b/src/test/unit/flashfs_unittest.include/flash_emulator.cc @@ -20,7 +20,6 @@ #include #include #include -#include #include "drivers/flash.h" #include "pg/flash.h"