diff --git a/src/main/blackbox/blackbox.c b/src/main/blackbox/blackbox.c index fb07315bf9..f03eb2b0e7 100644 --- a/src/main/blackbox/blackbox.c +++ b/src/main/blackbox/blackbox.c @@ -1129,6 +1129,11 @@ void handleBlackbox(void) default: break; } + + // Did we run out of room on the device? Stop! + if (isBlackboxDeviceFull()) { + blackboxSetState(BLACKBOX_STATE_STOPPED); + } } static bool canUseBlackboxWithCurrentConfiguration(void) diff --git a/src/main/blackbox/blackbox_io.c b/src/main/blackbox/blackbox_io.c index 2bf8a54b9f..f6a80ebf81 100644 --- a/src/main/blackbox/blackbox_io.c +++ b/src/main/blackbox/blackbox_io.c @@ -457,8 +457,9 @@ bool blackboxDeviceOpen(void) break; #ifdef FLASHFS case BLACKBOX_DEVICE_FLASH: - if (flashfsGetSize() == 0) + if (flashfsGetSize() == 0 || isBlackboxDeviceFull()) { return false; + } return true; break; @@ -509,3 +510,19 @@ bool isBlackboxDeviceIdle(void) return false; } } + +bool isBlackboxDeviceFull(void) +{ + switch (masterConfig.blackbox_device) { + case BLACKBOX_DEVICE_SERIAL: + return false; + +#ifdef FLASHFS + case BLACKBOX_DEVICE_FLASH: + return flashfsIsEOF(); +#endif + + default: + return false; + } +} diff --git a/src/main/blackbox/blackbox_io.h b/src/main/blackbox/blackbox_io.h index 4692b0d413..219aab7306 100644 --- a/src/main/blackbox/blackbox_io.h +++ b/src/main/blackbox/blackbox_io.h @@ -42,3 +42,4 @@ bool blackboxDeviceOpen(void); void blackboxDeviceClose(void); bool isBlackboxDeviceIdle(void); +bool isBlackboxDeviceFull(void); diff --git a/src/main/io/flashfs.c b/src/main/io/flashfs.c index c80de7c37b..a4f8cefc3b 100644 --- a/src/main/io/flashfs.c +++ b/src/main/io/flashfs.c @@ -18,12 +18,15 @@ /** * This provides a stream interface to a flash chip if one is present. * - * On statup, call flashfsInit after initialising the flash chip, in order to init the filesystem. This will + * On statup, call flashfsInit() after initialising the flash chip in order to init the filesystem. This will * result in the file pointer being pointed at the first free block found, or at the end of the device if the * flash chip is full. * * Note that bits can only be set to 0 when writing, not back to 1 from 0. You must erase sectors in order * to bring bits back to 1 again. + * + * In future, we can add support for multiple different flash chips by adding a flash device driver vtable + * and make calls through that, at the moment flashfs just calls m25p16_* routines explicitly. */ #include @@ -185,8 +188,12 @@ static uint32_t flashfsWriteBuffers(uint8_t const **buffers, uint32_t *bufferSiz } // Are we at EOF already? Abort. - if (tailAddress >= flashfsGetSize()) + if (flashfsIsEOF()) { + // May as well throw away any buffered data + flashfsClearBuffer(); + break; + } m25p16_pageProgramBegin(tailAddress); @@ -532,6 +539,13 @@ int flashfsIdentifyStartOfFreeSpace() return result * FREE_BLOCK_SIZE; } +/** + * Returns true if the file pointer is at the end of the device. + */ +bool flashfsIsEOF() { + return tailAddress >= flashfsGetSize(); +} + /** * Call after initializing the flash chip in order to set up the filesystem. */ diff --git a/src/main/io/flashfs.h b/src/main/io/flashfs.h index bbea1e0587..840f560445 100644 --- a/src/main/io/flashfs.h +++ b/src/main/io/flashfs.h @@ -49,3 +49,4 @@ void flashfsFlushSync(); void flashfsInit(); bool flashfsIsReady(); +bool flashfsIsEOF();