Skip to content

Commit

Permalink
flashfs: more cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gongtao0607 committed Dec 27, 2024
1 parent d83147e commit 8c84894
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
7 changes: 0 additions & 7 deletions src/main/blackbox/blackbox_fielddefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
46 changes: 21 additions & 25 deletions src/main/io/flashfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -548,11 +549,6 @@ void flashfsWriteByte(uint8_t byte)
if (bufferHead >= FLASHFS_WRITE_BUFFER_SIZE) {
bufferHead = 0;
}

if (flashfsBufferIsEmpty()) {
bufferDropped++;
}

}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/test/unit/flashfs_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ extern uint32_t tailAddress;
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <thread>

/*
* There are some weird logic behind flashfs.c and flash.c. This unittest is
* written to accomedate them
Expand Down
1 change: 0 additions & 1 deletion src/test/unit/flashfs_unittest.include/flash_c_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

extern "C" {
#include "drivers/flash.h"
#include "blackbox/blackbox.h"
}

#include "flash_interface.h"
Expand Down
1 change: 0 additions & 1 deletion src/test/unit/flashfs_unittest.include/flash_emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <cstdlib>
#include <cstring>
#include <thread>
#include <iostream>

#include "drivers/flash.h"
#include "pg/flash.h"
Expand Down

0 comments on commit 8c84894

Please sign in to comment.