Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/Blackbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ The Blackbox currently provides two settings (`blackbox_rate_num` and `blackbox_

If you're using a slower MicroSD card, you may need to reduce your logging rate to reduce the number of corrupted logged frames that `blackbox_decode` complains about. A rate of 1/2 is likely to work for most craft.

When logging to an onboard SD card, the Blackbox skips a few loop iterations when the card falls behind, instead of writing frames that would be cut off, and resumes on the next I frame with a `LOGGING_RESUME` event. The CLI command `sd_info` shows how often that happened.

You can change the logging rate settings by entering the CLI tab in the [INAV Configurator][] and using the `set` command, like so:

```
Expand Down
40 changes: 36 additions & 4 deletions src/main/blackbox/blackbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,17 @@ static bool testBlackboxCondition(FlightLogFieldCondition condition)
return (blackboxConditionCache & position) != 0;
}

// Paused by blackboxDeviceBufferLow(), not by the BLACKBOX mode
static bool blackboxPausedForDevice;
uint32_t blackboxDevicePauses;
uint32_t blackboxDevicePausedIterations;

static void blackboxSetState(BlackboxState newState)
{
if (newState != BLACKBOX_STATE_PAUSED) {
blackboxPausedForDevice = false;
}

//Perform initial setup required for the new state
switch (newState) {
case BLACKBOX_STATE_PREPARE_LOG_FILE:
Expand Down Expand Up @@ -2294,7 +2303,7 @@ static void blackboxAdvanceIterationTimers(void)
}

// Called once every FC loop in order to log the current state
static void blackboxLogIteration(timeUs_t currentTimeUs)
static void blackboxLogIterationFrames(timeUs_t currentTimeUs)
{
// Write a keyframe every BLACKBOX_I_INTERVAL frames so we can resynchronise upon missing frames
if (blackboxShouldLogIFrame()) {
Expand Down Expand Up @@ -2347,6 +2356,14 @@ static void blackboxLogIteration(timeUs_t currentTimeUs)
blackboxDeviceFlush();
}

// For the pause threshold in blackboxDeviceBufferLow()
static void blackboxLogIteration(timeUs_t currentTimeUs)
{
blackboxIterationBegin();
blackboxLogIterationFrames(currentTimeUs);
blackboxIterationEnd();
}

/**
* Call each flight loop iteration to perform blackbox logging.
*/
Expand All @@ -2360,7 +2377,7 @@ void blackboxUpdate(timeUs_t currentTimeUs)
}

//incooming request to get access to SD card
if(blackboxSDCardAccessStatus.requestToSdCardAccessState && (blackboxState == BLACKBOX_STATE_RUNNING || blackboxState == BLACKBOX_STATE_STOPPED)){
if(blackboxSDCardAccessStatus.requestToSdCardAccessState && (blackboxState == BLACKBOX_STATE_RUNNING || blackboxState == BLACKBOX_STATE_PAUSED || blackboxState == BLACKBOX_STATE_STOPPED)){
//we have to be sure that all writes are already processed and SD card is in idle
if(afatfs_isIdle()){
blackboxSDCardAccessStatus.requestToSdCardAccessState = false;
Expand Down Expand Up @@ -2457,8 +2474,14 @@ void blackboxUpdate(timeUs_t currentTimeUs)
}
break;
case BLACKBOX_STATE_PAUSED:
// Only allow resume to occur during an I-frame iteration, so that we have an "I" base to work from
if (IS_RC_MODE_ACTIVE(BOXBLACKBOX) && blackboxShouldLogIFrame()) {
// Switched off during a pause for the device: from here on the pause is the switch's
if (blackboxPausedForDevice && blackboxModeActivationConditionPresent && !IS_RC_MODE_ACTIVE(BOXBLACKBOX)) {
blackboxPausedForDevice = false;
}
// Only allow resume to occur during an I-frame iteration, so that we have an "I" base to work from.
// The switch too waits for a device that fell behind, or the I frame could be cut
if ((blackboxPausedForDevice || IS_RC_MODE_ACTIVE(BOXBLACKBOX))
&& blackboxDeviceBufferRecovered() && blackboxShouldLogIFrame()) {
// Write a log entry so the decoder is aware that our large time/iteration skip is intended
flightLogEvent_loggingResume_t resume;

Expand All @@ -2469,6 +2492,9 @@ void blackboxUpdate(timeUs_t currentTimeUs)
blackboxSetState(BLACKBOX_STATE_RUNNING);

blackboxLogIteration(currentTimeUs);
} else if (blackboxPausedForDevice || (IS_RC_MODE_ACTIVE(BOXBLACKBOX) && !blackboxDeviceBufferRecovered())) {
// Not logged because of the device, also once the switch is back on
blackboxDevicePausedIterations++;
}
// Keep the logging timers ticking so our log iteration continues to advance
blackboxAdvanceIterationTimers();
Expand All @@ -2477,6 +2503,12 @@ void blackboxUpdate(timeUs_t currentTimeUs)
// On entry to this state, blackboxIteration, blackboxPFrameIndex and blackboxIFrameIndex are reset to 0
if (blackboxModeActivationConditionPresent && !IS_RC_MODE_ACTIVE(BOXBLACKBOX)) {
blackboxSetState(BLACKBOX_STATE_PAUSED);
} else if (blackboxDeviceBufferLow()) {
// Skip frames the device would drop halfway; resume on an I frame with LOGGING_RESUME
blackboxPausedForDevice = true;
blackboxDevicePauses++;
blackboxDevicePausedIterations++;
blackboxSetState(BLACKBOX_STATE_PAUSED);
} else {
blackboxLogIteration(currentTimeUs);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/blackbox/blackbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ BlackboxState getBlackboxState(void);

bool requestToSdCardAccess(void);
void releaseSdCardAccess(void);
extern uint32_t blackboxDevicePauses;
extern uint32_t blackboxDevicePausedIterations;
66 changes: 66 additions & 0 deletions src/main/blackbox/blackbox_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,26 @@ void blackboxOpen(void)
}
#endif // UNIT_TEST

// Bytes this iteration and the most any iteration wrote, for blackboxDeviceBufferLow()
static uint16_t blackboxIterationBytes;
uint16_t blackboxLargestIteration;

void blackboxIterationBegin(void)
{
blackboxIterationBytes = 0;
}

void blackboxIterationEnd(void)
{
if (blackboxIterationBytes > blackboxLargestIteration) {
blackboxLargestIteration = blackboxIterationBytes;
}
}

void blackboxWrite(uint8_t value)
{
blackboxIterationBytes++;

switch (blackboxConfig()->device) {
#ifdef USE_FLASHFS
case BLACKBOX_DEVICE_FLASH:
Expand Down Expand Up @@ -193,6 +211,54 @@ int blackboxPrint(const char *s)
return length;
}

// afatfs drops what does not fit, often halfway through a frame: pause below the largest
// iteration plus a margin (256 bytes at least), resume from twice that
#define BLACKBOX_SDCARD_SECTOR_SIZE 512
#define BLACKBOX_SDCARD_PAUSE_MIN_BYTES 256
#define BLACKBOX_SDCARD_PAUSE_MARGIN_BYTES 64

#ifdef USE_SDCARD
// The free cache sectors, and what is left of the sector being written
static int32_t blackboxSDCardWritableBytes(void)
{
uint32_t position;
// A busy file (a seek or a new cluster queued) takes nothing, whatever the cache holds
if (!afatfs_ftell(blackboxSDCard.logFile, &position)) {
return 0;
}
int32_t writable = afatfs_getFreeBufferSpace();
if ((position % BLACKBOX_SDCARD_SECTOR_SIZE) != 0) {
writable += BLACKBOX_SDCARD_SECTOR_SIZE - position % BLACKBOX_SDCARD_SECTOR_SIZE;
}
return writable;
}

static int32_t blackboxSDCardPauseBelow(void)
{
return MAX(BLACKBOX_SDCARD_PAUSE_MIN_BYTES, blackboxLargestIteration + BLACKBOX_SDCARD_PAUSE_MARGIN_BYTES);
}
#endif

bool blackboxDeviceBufferLow(void)
{
#ifdef USE_SDCARD
if (blackboxConfig()->device == BLACKBOX_DEVICE_SDCARD) {
return blackboxSDCardWritableBytes() < blackboxSDCardPauseBelow();
}
#endif
return false;
}

bool blackboxDeviceBufferRecovered(void)
{
#ifdef USE_SDCARD
if (blackboxConfig()->device == BLACKBOX_DEVICE_SDCARD) {
return blackboxSDCardWritableBytes() >= 2 * blackboxSDCardPauseBelow();
}
#endif
return true;
}

/**
* If there is data waiting to be written to the blackbox device, attempt to write (a portion of) that now.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/blackbox/blackbox_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void blackboxWrite(uint8_t value);

void blackboxDeviceFlush(void);
bool blackboxDeviceFlushForce(void);
bool blackboxDeviceBufferLow(void);
void blackboxIterationBegin(void);
void blackboxIterationEnd(void);
extern uint16_t blackboxLargestIteration;
bool blackboxDeviceBufferRecovered(void);
bool blackboxDeviceOpen(void);
void blackboxDeviceClose(void);

Expand Down
6 changes: 6 additions & 0 deletions src/main/fc/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
bool cliMode = false;

#include "blackbox/blackbox.h"
#include "blackbox/blackbox_io.h"

#include "build/assert.h"
#include "build/build_config.h"
Expand Down Expand Up @@ -3025,6 +3026,11 @@ static void cliSdInfo(char *cmdline)
break;
}
cliPrintLinefeed();

#ifdef USE_BLACKBOX
cliPrintLinef("Blackbox: paused %u times for a full card buffer, %u loop iterations not logged, largest iteration %u bytes",
(unsigned)blackboxDevicePauses, (unsigned)blackboxDevicePausedIterations, (unsigned)blackboxLargestIteration);
#endif
}

#endif
Expand Down
31 changes: 23 additions & 8 deletions src/main/io/asyncfatfs/asyncfatfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,24 @@
#define ONLY_EXPOSE_FOR_TESTING static
#endif

// Room for the log while the card holds one write (up to 250 ms): 16 sectors cover about
// 85 kB/s, 24 twice that
#ifndef AFATFS_NUM_CACHE_SECTORS
#if defined(STM32H7)
#define AFATFS_NUM_CACHE_SECTORS 32
#elif defined(STM32F7)
#define AFATFS_NUM_CACHE_SECTORS 24
#elif defined(STM32F4) && !defined(USE_SDCARD_SDIO)
#define AFATFS_NUM_CACHE_SECTORS 24
#else
#define AFATFS_NUM_CACHE_SECTORS 8
#endif
#endif

// RAM is short on F4, CCM is not, and a card on SPI is written by the CPU, never by DMA
#if defined(STM32F4) && !defined(USE_SDCARD_SDIO)
#define AFATFS_CACHE_IN_FASTRAM
#endif

// FAT filesystems are allowed to differ from these parameters, but we choose not to support those weird filesystems:
#define AFATFS_SECTOR_SIZE 512
Expand Down Expand Up @@ -437,11 +454,7 @@ typedef struct afatfs_t {
} initState;
#endif

#ifdef STM32H7
uint8_t *cache;
#else
uint8_t cache[AFATFS_SECTOR_SIZE * AFATFS_NUM_CACHE_SECTORS];
#endif
afatfsCacheBlockDescriptor_t cacheDescriptor[AFATFS_NUM_CACHE_SECTORS];
uint32_t cacheTimer;

Expand Down Expand Up @@ -491,7 +504,9 @@ typedef struct afatfs_t {
uint32_t rootDirectorySectors; // Zero on FAT32, for FAT16 the number of sectors that the root directory occupies
} afatfs_t;

#ifdef STM32H7
#ifdef AFATFS_CACHE_IN_FASTRAM
static FASTRAM uint8_t afatfs_cache[AFATFS_SECTOR_SIZE * AFATFS_NUM_CACHE_SECTORS] __attribute__((aligned(32)));
#else
static uint8_t afatfs_cache[AFATFS_SECTOR_SIZE * AFATFS_NUM_CACHE_SECTORS] __attribute__((aligned(32)));
#endif

Expand Down Expand Up @@ -3795,9 +3810,7 @@ bool afatfs_isCurrentDirRoot(void)

void afatfs_init(void)
{
#ifdef STM32H7
afatfs.cache = afatfs_cache;
#endif
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_INITIALIZATION;
afatfs.initPhase = AFATFS_INITIALIZATION_READ_MBR;
afatfs.lastClusterAllocated = FAT_SMALLEST_LEGAL_CLUSTER_NUMBER;
Expand Down Expand Up @@ -3871,7 +3884,9 @@ uint32_t afatfs_getFreeBufferSpace(void)
{
uint32_t result = 0;
for (int i = 0; i < AFATFS_NUM_CACHE_SECTORS; i++) {
if (!afatfs.cacheDescriptor[i].locked && (afatfs.cacheDescriptor[i].state == AFATFS_CACHE_STATE_EMPTY || afatfs.cacheDescriptor[i].state == AFATFS_CACHE_STATE_IN_SYNC)) {
// A retained sector is never evicted (see afatfs_allocateCacheSector()): no room to write
if (!afatfs.cacheDescriptor[i].locked && afatfs.cacheDescriptor[i].retainCount == 0
&& (afatfs.cacheDescriptor[i].state == AFATFS_CACHE_STATE_EMPTY || afatfs.cacheDescriptor[i].state == AFATFS_CACHE_STATE_IN_SYNC)) {
result += AFATFS_SECTOR_SIZE;
}
}
Expand Down
Loading