From 5a30d9f5a0db0b51f4275c3207580e1e8c83fa34 Mon Sep 17 00:00:00 2001 From: Steve Evans Date: Sun, 26 Jun 2022 22:31:00 +0100 Subject: [PATCH] Only write to FLASH once 64 bytes are buffered --- src/main/blackbox/blackbox_io.c | 6 +++--- src/main/io/flashfs.c | 9 ++++++--- src/main/io/flashfs.h | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/blackbox/blackbox_io.c b/src/main/blackbox/blackbox_io.c index 7db5301dd8..f966a0f2f5 100644 --- a/src/main/blackbox/blackbox_io.c +++ b/src/main/blackbox/blackbox_io.c @@ -214,7 +214,7 @@ void blackboxDeviceFlush(void) * devices will progressively write in the background without Blackbox calling anything. */ case BLACKBOX_DEVICE_FLASH: - flashfsFlushAsync(); + flashfsFlushAsync(false); break; #endif // USE_FLASHFS @@ -237,7 +237,7 @@ bool blackboxDeviceFlushForce(void) #ifdef USE_FLASHFS case BLACKBOX_DEVICE_FLASH: - return flashfsFlushAsync(); + return flashfsFlushAsync(true); #endif // USE_FLASHFS #ifdef USE_SDCARD @@ -735,7 +735,7 @@ blackboxBufferReserveStatus_e blackboxDeviceReserveBufferSpace(int32_t bytes) * that the Blackbox header writing code doesn't have to guess about the best time to ask flashfs to * flush, and doesn't stall waiting for a flush that would otherwise not automatically be called. */ - flashfsFlushAsync(); + flashfsFlushAsync(true); } return BLACKBOX_RESERVE_TEMPORARY_FAILURE; #endif // USE_FLASHFS diff --git a/src/main/io/flashfs.c b/src/main/io/flashfs.c index 0e02edd45e..8e8c16bd57 100644 --- a/src/main/io/flashfs.c +++ b/src/main/io/flashfs.c @@ -38,6 +38,7 @@ #include "platform.h" +#include "build/debug.h" #include "common/printf.h" #include "drivers/flash.h" @@ -343,7 +344,7 @@ uint32_t flashfsGetOffset(void) * Returns true if all data in the buffer has been flushed to the device, or false if * there is still data to be written (call flush again later). */ -bool flashfsFlushAsync(void) +bool flashfsFlushAsync(bool force) { uint8_t const * buffers[2]; uint32_t bufferSizes[2]; @@ -373,7 +374,9 @@ bool flashfsFlushAsync(void) #endif bufCount = flashfsGetDirtyDataBuffers(buffers, bufferSizes); - if (bufCount) { + uint32_t bufferedBytes = bufferSizes[0] + bufferSizes[1]; + + if (bufCount && (force || (bufferedBytes >= FLASHFS_WRITE_BUFFER_AUTO_FLUSH_LEN))) { flashfsWriteBuffers(buffers, bufferSizes, bufCount, false); } @@ -427,7 +430,7 @@ void flashfsWriteByte(uint8_t byte) } if (flashfsTransmitBufferUsed() >= FLASHFS_WRITE_BUFFER_AUTO_FLUSH_LEN) { - flashfsFlushAsync(); + flashfsFlushAsync(false); } } diff --git a/src/main/io/flashfs.h b/src/main/io/flashfs.h index c131326ee4..0817d26b05 100644 --- a/src/main/io/flashfs.h +++ b/src/main/io/flashfs.h @@ -44,7 +44,7 @@ void flashfsWrite(const uint8_t *data, unsigned int len, bool sync); int flashfsReadAbs(uint32_t offset, uint8_t *data, unsigned int len); -bool flashfsFlushAsync(void); +bool flashfsFlushAsync(bool force); void flashfsFlushSync(void); void flashfsClose(void);