diff --git a/src/main/blackbox/blackbox.c b/src/main/blackbox/blackbox.c index 542af0f0f3..b2c9ed802b 100644 --- a/src/main/blackbox/blackbox.c +++ b/src/main/blackbox/blackbox.c @@ -367,6 +367,14 @@ static blackboxMainState_t* blackboxHistory[3]; static bool blackboxModeActivationConditionPresent = false; +/** + * Return true if it is safe to edit the Blackbox configuration in the emasterConfig. + */ +bool blackboxMayEditConfig() +{ + return blackboxState <= BLACKBOX_STATE_STOPPED; +} + static bool blackboxIsOnlyLoggingIntraframes() { return masterConfig.blackbox_rate_num == 1 && masterConfig.blackbox_rate_denom == 32; } diff --git a/src/main/blackbox/blackbox.h b/src/main/blackbox/blackbox.h index 6a94ccad6c..082b8f3cb5 100644 --- a/src/main/blackbox/blackbox.h +++ b/src/main/blackbox/blackbox.h @@ -25,3 +25,5 @@ void initBlackbox(void); void handleBlackbox(void); void startBlackbox(void); void finishBlackbox(void); + +bool blackboxMayEditConfig(); diff --git a/src/main/io/serial_msp.c b/src/main/io/serial_msp.c index b003f63297..37a1b22fc8 100644 --- a/src/main/io/serial_msp.c +++ b/src/main/io/serial_msp.c @@ -42,7 +42,7 @@ #include "drivers/timer.h" #include "drivers/pwm_rx.h" #include "drivers/gyro_sync.h" - +#include "drivers/sdcard.h" #include "drivers/buf_writer.h" #include "rx/rx.h" #include "rx/msp.h" @@ -55,6 +55,7 @@ #include "io/serial.h" #include "io/ledstrip.h" #include "io/flashfs.h" +#include "io/asyncfatfs/asyncfatfs.h" #include "telemetry/telemetry.h" @@ -74,6 +75,8 @@ #include "flight/navigation.h" #include "flight/altitudehold.h" +#include "blackbox/blackbox.h" + #include "mw.h" #include "config/runtime_config.h" @@ -238,6 +241,11 @@ static const char * const boardIdentifier = TARGET_BOARD_IDENTIFIER; #define MSP_RXFAIL_CONFIG 77 //out message Returns RXFAIL settings #define MSP_SET_RXFAIL_CONFIG 78 //in message Sets RXFAIL settings +#define MSP_SDCARD_SUMMARY 79 //out message Get the state of the SD card + +#define MSP_BLACKBOX_CONFIG 80 //out message Get blackbox settings +#define MSP_SET_BLACKBOX_CONFIG 81 //in message Set blackbox settings + // // Baseflight MSP commands (if enabled they exist in Cleanflight) // @@ -393,6 +401,14 @@ typedef enum { COMMAND_RECEIVED } mspState_e; +typedef enum { + MSP_SDCARD_STATE_NOT_PRESENT = 0, + MSP_SDCARD_STATE_FATAL = 1, + MSP_SDCARD_STATE_CARD_INIT = 2, + MSP_SDCARD_STATE_FS_INIT = 3, + MSP_SDCARD_STATE_READY = 4 +} mspSDCardState_e; + typedef enum { UNUSED_PORT = 0, FOR_GENERAL_MSP, @@ -553,17 +569,65 @@ reset: } } +static void serializeSDCardSummaryReply(void) +{ + headSerialReply(3 + 4 + 4); + +#ifdef USE_SDCARD + uint8_t flags = 1 /* SD card supported */ ; + uint8_t state; + + serialize8(flags); + + // Merge the card and filesystem states together + if (!sdcard_isInserted()) { + state = MSP_SDCARD_STATE_NOT_PRESENT; + } else { + switch (afatfs_getFilesystemState()) { + case AFATFS_FILESYSTEM_STATE_READY: + state = MSP_SDCARD_STATE_READY; + break; + case AFATFS_FILESYSTEM_STATE_INITIALIZATION: + if (sdcard_isInitialized()) { + state = MSP_SDCARD_STATE_FS_INIT; + } else { + state = MSP_SDCARD_STATE_CARD_INIT; + } + break; + case AFATFS_FILESYSTEM_STATE_FATAL: + case AFATFS_FILESYSTEM_STATE_UNKNOWN: + state = MSP_SDCARD_STATE_FATAL; + break; + } + } + + serialize8(state); + serialize8(afatfs_getLastError()); + // Write free space and total space in kilobytes + serialize32(afatfs_getContiguousFreeSpace() / 1024); + serialize32(sdcard_getMetadata()->numBlocks / 2); // Block size is half a kilobyte +#else + serialize8(0); + serialize8(0); + serialize8(0); + serialize32(0); + serialize32(0); +#endif +} + static void serializeDataflashSummaryReply(void) { headSerialReply(1 + 3 * 4); #ifdef USE_FLASHFS const flashGeometry_t *geometry = flashfsGetGeometry(); - serialize8(flashfsIsReady() ? 1 : 0); + uint8_t flags = (flashfsIsReady() ? 1 : 0) | 2 /* FlashFS is supported */; + + serialize8(flags); serialize32(geometry->sectors); serialize32(geometry->totalSize); serialize32(flashfsGetOffset()); // Effectively the current number of bytes stored on the volume #else - serialize8(0); + serialize8(0); // FlashFS is neither ready nor supported serialize32(0); serialize32(0); serialize32(0); @@ -1304,6 +1368,26 @@ static bool processOutCommand(uint8_t cmdMSP) break; #endif + case MSP_BLACKBOX_CONFIG: + headSerialReply(4); + +#ifdef BLACKBOX + serialize8(1); //Blackbox supported + serialize8(masterConfig.blackbox_device); + serialize8(masterConfig.blackbox_rate_num); + serialize8(masterConfig.blackbox_rate_denom); +#else + serialize8(0); // Blackbox not supported + serialize8(0); + serialize8(0); + serialize8(0); +#endif + break; + + case MSP_SDCARD_SUMMARY: + serializeSDCardSummaryReply(); + break; + case MSP_BF_BUILD_INFO: headSerialReply(11 + 4 + 4); for (i = 0; i < 11; i++) @@ -1575,6 +1659,17 @@ static bool processInCommand(void) readEEPROM(); break; +#ifdef BLACKBOX + case MSP_SET_BLACKBOX_CONFIG: + // Don't allow config to be updated while Blackbox is logging + if (blackboxMayEditConfig()) { + masterConfig.blackbox_device = read8(); + masterConfig.blackbox_rate_num = read8(); + masterConfig.blackbox_rate_denom = read8(); + } + break; +#endif + #ifdef USE_FLASHFS case MSP_DATAFLASH_ERASE: flashfsEraseCompletely();