diff --git a/src/main/cli/cli.c b/src/main/cli/cli.c index 0c87f205d6..68ee37b897 100644 --- a/src/main/cli/cli.c +++ b/src/main/cli/cli.c @@ -676,7 +676,7 @@ static void backupAndResetConfigs(const bool useCustomDefaults) backupConfigs(); // reset all configs to defaults to do differencing - resetConfigs(); + resetConfig(); #if defined(USE_CUSTOM_DEFAULTS) if (useCustomDefaults) { @@ -4136,7 +4136,7 @@ static void cliBatch(char *cmdline) } #endif -static bool doSave(void) +static bool prepareSave(void) { #if defined(USE_CUSTOM_DEFAULTS) if (processingCustomDefaults) { @@ -4162,53 +4162,56 @@ static bool doSave(void) #endif // USE_BOARD_INFO if (featureMaskIsCopied) { - writeEEPROMWithFeatures(featureMaskCopy); - } else { - writeEEPROM(); + featureDisableAll(); + featureEnable(featureMaskCopy); } return true; } +bool tryPrepareSave(void) +{ + bool success = prepareSave(); +#if defined(USE_CLI_BATCH) + if (!success) { + cliPrintCommandBatchWarning("PLEASE FIX ERRORS THEN 'SAVE'"); + resetCommandBatch(); + + return false; + } +#else + UNUSED(success); +#endif + + return true; +} + static void cliSave(char *cmdline) { UNUSED(cmdline); -#if defined(USE_CLI_BATCH) - if (!doSave()) { - cliPrintCommandBatchWarning("PLEASE FIX ERRORS THEN 'SAVE'"); - resetCommandBatch(); - - return; - } else -#endif - { + if (tryPrepareSave()) { + writeEEPROM(); cliPrintHashLine("saving"); - } - cliReboot(); + cliReboot(); + } } -bool cliResetConfig(bool useCustomDefaults) +#if defined(USE_CUSTOM_DEFAULTS) +bool resetConfigToCustomDefaults(void) { - resetConfigs(); + resetConfig(); #ifdef USE_CLI_BATCH commandBatchError = false; #endif -#if defined(USE_CUSTOM_DEFAULTS) - if (useCustomDefaults) { - cliProcessCustomDefaults(); - } -#else - UNUSED(useCustomDefaults); -#endif + cliProcessCustomDefaults(); - return doSave(); + return prepareSave(); } -#if defined(USE_CUSTOM_DEFAULTS) static bool isCustomDefaults(char *ptr) { return strncmp(ptr, "# " FC_FIRMWARE_NAME, 12) == 0; @@ -4263,7 +4266,7 @@ static void cliDefaults(char *cmdline) cliPrintHashLine("resetting to defaults"); - resetConfigs(); + resetConfig(); #ifdef USE_CLI_BATCH // Reset only the error state and allow the batch active state to remain. @@ -4279,8 +4282,10 @@ static void cliDefaults(char *cmdline) } #endif - if (saveConfigs) { - cliSave(NULL); + if (saveConfigs && tryPrepareSave()) { + writeUnmodifiedConfigToEEPROM(); + + cliReboot(); } } @@ -6533,6 +6538,8 @@ bool cliProcessCustomDefaults(void) memcpy(cliBuffer, cliBufferTemp, sizeof(cliBuffer)); bufferIndex = bufferIndexTemp; + systemConfigMutable()->configurationState = CONFIGURATION_STATE_DEFAULTS_CUSTOM; + return true; } #endif diff --git a/src/main/cli/cli.h b/src/main/cli/cli.h index 87787f6c6f..e0281bbafd 100644 --- a/src/main/cli/cli.h +++ b/src/main/cli/cli.h @@ -28,4 +28,4 @@ void cliProcess(void); bool hasCustomDefaults(void); struct serialPort_s; void cliEnter(struct serialPort_s *serialPort); -bool cliResetConfig(bool useCustomDefaults); +bool resetConfigToCustomDefaults(void); diff --git a/src/main/fc/config.c b/src/main/fc/config.c index db6ac29e15..c173d03112 100644 --- a/src/main/fc/config.c +++ b/src/main/fc/config.c @@ -112,7 +112,7 @@ PG_RESET_TEMPLATE(systemConfig_t, systemConfig, .powerOnArmingGraceTime = 5, .boardIdentifier = TARGET_BOARD_IDENTIFIER, .hseMhz = SYSTEM_HSE_VALUE, // Not used for non-F4 targets - .configured = false, + .configurationState = CONFIGURATION_STATE_DEFAULTS_BARE, .schedulerOptimizeRate = true, ); @@ -136,7 +136,7 @@ uint16_t getCurrentMinthrottle(void) return motorConfig()->minthrottle; } -void resetConfigs(void) +void resetConfig(void) { pgResetAll(); @@ -688,20 +688,12 @@ bool readEEPROM(void) return success; } -static void ValidateAndWriteConfigToEEPROM(bool setConfigured) +void writeUnmodifiedConfigToEEPROM(void) { validateAndFixConfig(); suspendRxPwmPpmSignal(); -#ifdef USE_CONFIGURATION_STATE - if (setConfigured) { - systemConfigMutable()->configured = true; - } -#else - UNUSED(setConfigured); -#endif - writeConfigToEEPROM(); resumeRxPwmPpmSignal(); @@ -710,7 +702,9 @@ static void ValidateAndWriteConfigToEEPROM(bool setConfigured) void writeEEPROM(void) { - ValidateAndWriteConfigToEEPROM(true); + systemConfigMutable()->configurationState = CONFIGURATION_STATE_CONFIGURED; + + writeUnmodifiedConfigToEEPROM(); } void writeEEPROMWithFeatures(uint32_t features) @@ -718,18 +712,27 @@ void writeEEPROMWithFeatures(uint32_t features) featureDisableAll(); featureEnable(features); - ValidateAndWriteConfigToEEPROM(true); + writeEEPROM(); } bool resetEEPROM(bool useCustomDefaults) { - if (cliResetConfig(useCustomDefaults)) { - activateConfig(); - - return true; +#if !defined(USE_CUSTOM_DEFAULTS) + UNUSED(useCustomDefaults); +#else + if (useCustomDefaults) { + if (!resetConfigToCustomDefaults()) { + return false; + } + } else +#endif + { + resetConfig(); } - - return false; + + writeUnmodifiedConfigToEEPROM(); + + return true; } void ensureEEPROMStructureIsValid(void) @@ -742,7 +745,7 @@ void ensureEEPROMStructureIsValid(void) void saveConfigAndNotify(void) { - ValidateAndWriteConfigToEEPROM(true); + writeEEPROM(); readEEPROM(); beeperConfirmationBeeps(1); } @@ -797,11 +800,7 @@ void changePidProfile(uint8_t pidProfileIndex) bool isSystemConfigured(void) { -#ifdef USE_CONFIGURATION_STATE - return systemConfig()->configured; -#else - return true; -#endif + return systemConfig()->configurationState == CONFIGURATION_STATE_CONFIGURED; } void setRebootRequired(void) diff --git a/src/main/fc/config.h b/src/main/fc/config.h index 7da9178f8c..590633f215 100644 --- a/src/main/fc/config.h +++ b/src/main/fc/config.h @@ -29,6 +29,12 @@ #define MAX_PROFILE_NAME_LENGTH 8u +typedef enum { + CONFIGURATION_STATE_DEFAULTS_BARE = 0, + CONFIGURATION_STATE_DEFAULTS_CUSTOM, + CONFIGURATION_STATE_CONFIGURED, +} configurationState_e; + typedef struct pilotConfig_s { char name[MAX_NAME_LENGTH + 1]; char displayName[MAX_NAME_LENGTH + 1]; @@ -46,7 +52,7 @@ typedef struct systemConfig_s { uint8_t powerOnArmingGraceTime; // in seconds char boardIdentifier[sizeof(TARGET_BOARD_IDENTIFIER) + 1]; uint8_t hseMhz; // Not used for non-F4 targets - uint8_t configured; + uint8_t configurationState; // The state of the configuration (defaults / configured) uint8_t schedulerOptimizeRate; } systemConfig_t; @@ -55,12 +61,12 @@ PG_DECLARE(systemConfig_t, systemConfig); struct pidProfile_s; extern struct pidProfile_s *currentPidProfile; - void initEEPROM(void); bool resetEEPROM(bool useCustomDefaults); bool readEEPROM(void); void writeEEPROM(void); void writeEEPROMWithFeatures(uint32_t features); +void writeUnmodifiedConfigToEEPROM(void); void ensureEEPROMStructureIsValid(void); void saveConfigAndNotify(void); @@ -82,7 +88,7 @@ bool canSoftwareSerialBeUsed(void); uint16_t getCurrentMinthrottle(void); -void resetConfigs(void); +void resetConfig(void); void targetConfiguration(void); void targetValidateConfiguration(void); diff --git a/src/main/fc/init.c b/src/main/fc/init.c index 6e4f7db9bb..3a1fc1be51 100644 --- a/src/main/fc/init.c +++ b/src/main/fc/init.c @@ -662,7 +662,11 @@ void init(void) if (!sensorsAutodetect()) { // if gyro was not detected due to whatever reason, notify and don't arm. - if (isSystemConfigured()) { + if (true +#if defined(USE_UNIFIED_TARGET) + && isSystemConfigured() +#endif + ) { indicateFailure(FAILURE_MISSING_ACC, 2); } setArmingDisabled(ARMING_DISABLED_NO_GYRO); diff --git a/src/main/msp/msp.c b/src/main/msp/msp.c index 1e6abc78e7..fa5ace59cf 100644 --- a/src/main/msp/msp.c +++ b/src/main/msp/msp.c @@ -531,8 +531,8 @@ static bool mspCommonProcessOutCommand(uint8_t cmdMSP, sbuf_t *dst, mspPostProce #define TARGET_HAS_SOFTSERIAL_BIT 1 #define TARGET_IS_UNIFIED_BIT 2 #define TARGET_HAS_FLASH_BOOTLOADER_BIT 3 -#define TARGET_SUPPORTS_CUSTOM_DEFAULTS 4 -#define TARGET_HAS_CUSTOM_DEFAULTS 5 +#define TARGET_SUPPORTS_CUSTOM_DEFAULTS_BIT 4 +#define TARGET_HAS_CUSTOM_DEFAULTS_BIT 5 uint8_t targetCapabilities = 0; #ifdef USE_VCP @@ -548,9 +548,9 @@ static bool mspCommonProcessOutCommand(uint8_t cmdMSP, sbuf_t *dst, mspPostProce targetCapabilities |= 1 << TARGET_HAS_FLASH_BOOTLOADER_BIT; #endif #if defined(USE_CUSTOM_DEFAULTS) - targetCapabilities |= 1 << TARGET_SUPPORTS_CUSTOM_DEFAULTS; + targetCapabilities |= 1 << TARGET_SUPPORTS_CUSTOM_DEFAULTS_BIT; if (hasCustomDefaults()) { - targetCapabilities |= 1 << TARGET_HAS_CUSTOM_DEFAULTS; + targetCapabilities |= 1 << TARGET_HAS_CUSTOM_DEFAULTS_BIT; } #endif @@ -586,6 +586,9 @@ static bool mspCommonProcessOutCommand(uint8_t cmdMSP, sbuf_t *dst, mspPostProce sbufWriteU8(dst, MCU_TYPE_ID); + // Added in API version 1.42 + sbufWriteU8(dst, systemConfig()->configurationState); + break; } diff --git a/src/main/target/common_post.h b/src/main/target/common_post.h index 67dd5708e8..35183ee530 100644 --- a/src/main/target/common_post.h +++ b/src/main/target/common_post.h @@ -226,10 +226,6 @@ #define USE_RX_XN297 #endif -#ifdef USE_UNIFIED_TARGET -#define USE_CONFIGURATION_STATE -#endif - // Setup crystal frequency on F4 for backward compatibility // Should be set to zero for generic targets to ensure USB is working // when unconfigured for targets with non-standard crystal. diff --git a/src/test/unit/cli_unittest.cc b/src/test/unit/cli_unittest.cc index 65622d75e2..c6d9fa3f9d 100644 --- a/src/test/unit/cli_unittest.cc +++ b/src/test/unit/cli_unittest.cc @@ -331,8 +331,9 @@ void setArmingDisabled(armingDisableFlags_e) {} void waitForSerialPortToFinishTransmitting(serialPort_t *) {} void systemResetToBootloader(void) {} -void resetConfigs(void) {} +void resetConfig(void) {} void systemReset(void) {} +void writeUnmodifiedConfigToEEPROM(void) {} void changePidProfile(uint8_t) {} bool serialIsPortAvailable(serialPortIdentifier_e) { return false; }