2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
28 #include "blackbox/blackbox.h"
30 #include "build/debug.h"
34 #include "common/sensor_alignment.h"
36 #include "config/config_eeprom.h"
37 #include "config/feature.h"
39 #include "drivers/dshot_command.h"
40 #include "drivers/motor.h"
41 #include "drivers/system.h"
43 #include "fc/controlrate_profile.h"
46 #include "fc/rc_adjustments.h"
47 #include "fc/rc_controls.h"
48 #include "fc/runtime_config.h"
50 #include "flight/failsafe.h"
51 #include "flight/imu.h"
52 #include "flight/mixer.h"
53 #include "flight/pid.h"
54 #include "flight/pid_init.h"
55 #include "flight/rpm_filter.h"
56 #include "flight/servos.h"
58 #include "io/beeper.h"
60 #include "io/ledstrip.h"
61 #include "io/serial.h"
64 #include "msp/msp_box.h"
69 #include "pg/beeper.h"
70 #include "pg/beeper_dev.h"
71 #include "pg/displayport_profiles.h"
72 #include "pg/gyrodev.h"
75 #include "pg/pg_ids.h"
77 #include "pg/rx_spi.h"
78 #include "pg/sdcard.h"
79 #include "pg/vtx_table.h"
82 #include "rx/rx_spi.h"
84 #include "scheduler/scheduler.h"
86 #include "sensors/acceleration.h"
87 #include "sensors/battery.h"
88 #include "sensors/compass.h"
89 #include "sensors/gyro.h"
93 #include "drivers/dshot.h"
95 static bool configIsDirty
; /* someone indicated that the config is modified and it is not yet saved */
97 static bool rebootRequired
= false; // set if a config change requires a reboot to take effect
99 pidProfile_t
*currentPidProfile
;
101 #ifndef RX_SPI_DEFAULT_PROTOCOL
102 #define RX_SPI_DEFAULT_PROTOCOL 0
105 PG_REGISTER_WITH_RESET_TEMPLATE(pilotConfig_t
, pilotConfig
, PG_PILOT_CONFIG
, 1);
107 PG_RESET_TEMPLATE(pilotConfig_t
, pilotConfig
,
109 .displayName
= { 0 },
112 PG_REGISTER_WITH_RESET_TEMPLATE(systemConfig_t
, systemConfig
, PG_SYSTEM_CONFIG
, 3);
114 PG_RESET_TEMPLATE(systemConfig_t
, systemConfig
,
115 .pidProfileIndex
= 0,
116 .activeRateProfile
= 0,
117 .debug_mode
= DEBUG_MODE
,
118 .task_statistics
= true,
119 .rateProfile6PosSwitch
= false,
120 .cpu_overclock
= DEFAULT_CPU_OVERCLOCK
,
121 .powerOnArmingGraceTime
= 5,
122 .boardIdentifier
= TARGET_BOARD_IDENTIFIER
,
123 .hseMhz
= SYSTEM_HSE_VALUE
, // Only used for F4 and G4 targets
124 .configurationState
= CONFIGURATION_STATE_DEFAULTS_BARE
,
125 .enableStickArming
= false,
128 uint8_t getCurrentPidProfileIndex(void)
130 return systemConfig()->pidProfileIndex
;
133 static void loadPidProfile(void)
135 currentPidProfile
= pidProfilesMutable(systemConfig()->pidProfileIndex
);
138 uint8_t getCurrentControlRateProfileIndex(void)
140 return systemConfig()->activeRateProfile
;
143 uint16_t getCurrentMinthrottle(void)
145 return motorConfig()->minthrottle
;
148 void resetConfig(void)
152 #if defined(USE_TARGET_CONFIG)
153 targetConfiguration();
157 static void activateConfig(void)
160 loadControlRateProfile();
164 activeAdjustmentRangeReset();
166 pidInit(currentPidProfile
);
172 setAccelerationTrims(&accelerometerConfigMutable()->accZero
);
176 imuConfigure(throttleCorrectionConfig()->throttle_correction_angle
, throttleCorrectionConfig()->throttle_correction_value
);
178 #if defined(USE_LED_STRIP_STATUS_MODE)
179 reevaluateLedConfig();
185 static void adjustFilterLimit(uint16_t *parm
, uint16_t resetValue
)
187 if (*parm
> LPF_MAX_HZ
) {
192 static void validateAndFixRatesSettings(void)
194 for (unsigned profileIndex
= 0; profileIndex
< CONTROL_RATE_PROFILE_COUNT
; profileIndex
++) {
195 const ratesType_e ratesType
= controlRateProfilesMutable(profileIndex
)->rates_type
;
196 for (unsigned axis
= FD_ROLL
; axis
<= FD_YAW
; axis
++) {
197 controlRateProfilesMutable(profileIndex
)->rcRates
[axis
] = constrain(controlRateProfilesMutable(profileIndex
)->rcRates
[axis
], 0, ratesSettingLimits
[ratesType
].rc_rate_limit
);
198 controlRateProfilesMutable(profileIndex
)->rates
[axis
] = constrain(controlRateProfilesMutable(profileIndex
)->rates
[axis
], 0, ratesSettingLimits
[ratesType
].srate_limit
);
199 controlRateProfilesMutable(profileIndex
)->rcExpo
[axis
] = constrain(controlRateProfilesMutable(profileIndex
)->rcExpo
[axis
], 0, ratesSettingLimits
[ratesType
].expo_limit
);
204 static void validateAndFixConfig(void)
206 #if !defined(USE_QUAD_MIXER_ONLY)
207 // Reset unsupported mixer mode to default.
208 // This check will be gone when motor/servo mixers are loaded dynamically
209 // by configurator as a part of configuration procedure.
211 mixerMode_e mixerMode
= mixerConfigMutable()->mixerMode
;
213 if (!(mixerMode
== MIXER_CUSTOM
|| mixerMode
== MIXER_CUSTOM_AIRPLANE
|| mixerMode
== MIXER_CUSTOM_TRI
)) {
214 if (mixers
[mixerMode
].motorCount
&& mixers
[mixerMode
].motor
== NULL
)
215 mixerConfigMutable()->mixerMode
= MIXER_CUSTOM
;
217 if (mixers
[mixerMode
].useServo
&& servoMixers
[mixerMode
].servoRuleCount
== 0)
218 mixerConfigMutable()->mixerMode
= MIXER_CUSTOM_AIRPLANE
;
223 if (!isSerialConfigValid(serialConfig())) {
224 pgResetFn_serialConfig(serialConfigMutable());
228 const serialPortConfig_t
*gpsSerial
= findSerialPortConfig(FUNCTION_GPS
);
229 if (gpsConfig()->provider
== GPS_MSP
&& gpsSerial
) {
230 serialRemovePort(gpsSerial
->identifier
);
235 gpsConfig()->provider
!= GPS_MSP
&& !gpsSerial
&&
238 featureDisableImmediate(FEATURE_GPS
);
241 for (unsigned i
= 0; i
< PID_PROFILE_COUNT
; i
++) {
242 // Fix filter settings to handle cases where an older configurator was used that
243 // allowed higher cutoff limits from previous firmware versions.
244 adjustFilterLimit(&pidProfilesMutable(i
)->dterm_lpf1_static_hz
, LPF_MAX_HZ
);
245 adjustFilterLimit(&pidProfilesMutable(i
)->dterm_lpf2_static_hz
, LPF_MAX_HZ
);
246 adjustFilterLimit(&pidProfilesMutable(i
)->dterm_notch_hz
, LPF_MAX_HZ
);
247 adjustFilterLimit(&pidProfilesMutable(i
)->dterm_notch_cutoff
, 0);
249 // Prevent invalid notch cutoff
250 if (pidProfilesMutable(i
)->dterm_notch_cutoff
>= pidProfilesMutable(i
)->dterm_notch_hz
) {
251 pidProfilesMutable(i
)->dterm_notch_hz
= 0;
255 //Prevent invalid dynamic lowpass
256 if (pidProfilesMutable(i
)->dterm_lpf1_dyn_min_hz
> pidProfilesMutable(i
)->dterm_lpf1_dyn_max_hz
) {
257 pidProfilesMutable(i
)->dterm_lpf1_dyn_min_hz
= 0;
261 if (pidProfilesMutable(i
)->motor_output_limit
> 100 || pidProfilesMutable(i
)->motor_output_limit
== 0) {
262 pidProfilesMutable(i
)->motor_output_limit
= 100;
265 if (pidProfilesMutable(i
)->auto_profile_cell_count
> MAX_AUTO_DETECT_CELL_COUNT
|| pidProfilesMutable(i
)->auto_profile_cell_count
< AUTO_PROFILE_CELL_COUNT_CHANGE
) {
266 pidProfilesMutable(i
)->auto_profile_cell_count
= AUTO_PROFILE_CELL_COUNT_STAY
;
269 // If the d_min value for any axis is >= the D gain then reset d_min to 0 for consistent Configurator behavior
270 for (unsigned axis
= 0; axis
<= FD_YAW
; axis
++) {
271 if (pidProfilesMutable(i
)->d_min
[axis
] > pidProfilesMutable(i
)->pid
[axis
].D
) {
272 pidProfilesMutable(i
)->d_min
[axis
] = 0;
276 #if defined(USE_BATTERY_VOLTAGE_SAG_COMPENSATION)
277 if (batteryConfig()->voltageMeterSource
!= VOLTAGE_METER_ADC
) {
278 pidProfilesMutable(i
)->vbat_sag_compensation
= 0;
283 if (motorConfig()->dev
.motorPwmProtocol
== PWM_TYPE_BRUSHED
) {
284 featureDisableImmediate(FEATURE_3D
);
286 if (motorConfig()->mincommand
< 1000) {
287 motorConfigMutable()->mincommand
= 1000;
291 if ((motorConfig()->dev
.motorPwmProtocol
== PWM_TYPE_STANDARD
) && (motorConfig()->dev
.motorPwmRate
> BRUSHLESS_MOTORS_PWM_RATE
)) {
292 motorConfigMutable()->dev
.motorPwmRate
= BRUSHLESS_MOTORS_PWM_RATE
;
295 validateAndFixGyroConfig();
298 buildAlignmentFromStandardAlignment(&compassConfigMutable()->mag_customAlignment
, compassConfig()->mag_alignment
);
300 buildAlignmentFromStandardAlignment(&gyroDeviceConfigMutable(0)->customAlignment
, gyroDeviceConfig(0)->alignment
);
301 #if defined(USE_MULTI_GYRO)
302 buildAlignmentFromStandardAlignment(&gyroDeviceConfigMutable(1)->customAlignment
, gyroDeviceConfig(1)->alignment
);
306 if (accelerometerConfig()->accZero
.values
.roll
!= 0 ||
307 accelerometerConfig()->accZero
.values
.pitch
!= 0 ||
308 accelerometerConfig()->accZero
.values
.yaw
!= 0) {
309 accelerometerConfigMutable()->accZero
.values
.calibrationCompleted
= 1;
313 if (!(featureIsConfigured(FEATURE_RX_PARALLEL_PWM
) || featureIsConfigured(FEATURE_RX_PPM
) || featureIsConfigured(FEATURE_RX_SERIAL
) || featureIsConfigured(FEATURE_RX_MSP
) || featureIsConfigured(FEATURE_RX_SPI
))) {
314 featureEnableImmediate(DEFAULT_RX_FEATURE
);
317 if (featureIsConfigured(FEATURE_RX_PPM
)) {
318 featureDisableImmediate(FEATURE_RX_SERIAL
| FEATURE_RX_PARALLEL_PWM
| FEATURE_RX_MSP
| FEATURE_RX_SPI
);
321 if (featureIsConfigured(FEATURE_RX_MSP
)) {
322 featureDisableImmediate(FEATURE_RX_SERIAL
| FEATURE_RX_PARALLEL_PWM
| FEATURE_RX_PPM
| FEATURE_RX_SPI
);
325 if (featureIsConfigured(FEATURE_RX_SERIAL
)) {
326 featureDisableImmediate(FEATURE_RX_PARALLEL_PWM
| FEATURE_RX_MSP
| FEATURE_RX_PPM
| FEATURE_RX_SPI
);
330 if (featureIsConfigured(FEATURE_RX_SPI
)) {
331 featureDisableImmediate(FEATURE_RX_SERIAL
| FEATURE_RX_PARALLEL_PWM
| FEATURE_RX_PPM
| FEATURE_RX_MSP
);
335 if (featureIsConfigured(FEATURE_RX_PARALLEL_PWM
)) {
336 featureDisableImmediate(FEATURE_RX_SERIAL
| FEATURE_RX_MSP
| FEATURE_RX_PPM
| FEATURE_RX_SPI
);
340 if (featureIsConfigured(FEATURE_RSSI_ADC
)) {
341 rxConfigMutable()->rssi_channel
= 0;
342 rxConfigMutable()->rssi_src_frame_errors
= false;
345 if (rxConfigMutable()->rssi_channel
346 #if defined(USE_PWM) || defined(USE_PPM)
347 || featureIsConfigured(FEATURE_RX_PPM
) || featureIsConfigured(FEATURE_RX_PARALLEL_PWM
)
350 rxConfigMutable()->rssi_src_frame_errors
= false;
354 featureIsConfigured(FEATURE_3D
) || !featureIsConfigured(FEATURE_GPS
) || mixerModeIsFixedWing(mixerConfig()->mixerMode
)
355 #if !defined(USE_GPS) || !defined(USE_GPS_RESCUE)
360 #ifdef USE_GPS_RESCUE
361 if (failsafeConfig()->failsafe_procedure
== FAILSAFE_PROCEDURE_GPS_RESCUE
) {
362 failsafeConfigMutable()->failsafe_procedure
= FAILSAFE_PROCEDURE_DROP_IT
;
366 if (isModeActivationConditionPresent(BOXGPSRESCUE
)) {
367 removeModeActivationCondition(BOXGPSRESCUE
);
371 #if defined(USE_ESC_SENSOR)
372 if (!findSerialPortConfig(FUNCTION_ESC_SENSOR
)) {
373 featureDisableImmediate(FEATURE_ESC_SENSOR
);
377 for (int i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
378 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
381 if (mac
->modeId
== BOXARM
|| isModeActivationConditionLinked(mac
->linkedTo
)) {
382 removeModeActivationCondition(mac
->modeId
);
387 #if defined(USE_DSHOT_TELEMETRY) && defined(USE_DSHOT_BITBANG)
388 if (motorConfig()->dev
.motorPwmProtocol
== PWM_TYPE_PROSHOT1000
&& motorConfig()->dev
.useDshotTelemetry
&&
389 motorConfig()->dev
.useDshotBitbang
== DSHOT_BITBANG_ON
) {
390 motorConfigMutable()->dev
.useDshotBitbang
= DSHOT_BITBANG_AUTO
;
395 adcConfigMutable()->vbat
.enabled
= (batteryConfig()->voltageMeterSource
== VOLTAGE_METER_ADC
);
396 adcConfigMutable()->current
.enabled
= (batteryConfig()->currentMeterSource
== CURRENT_METER_ADC
);
398 // The FrSky D SPI RX sends RSSI_ADC_PIN (if configured) as A2
399 adcConfigMutable()->rssi
.enabled
= featureIsEnabled(FEATURE_RSSI_ADC
);
401 adcConfigMutable()->rssi
.enabled
|= (featureIsEnabled(FEATURE_RX_SPI
) && rxSpiConfig()->rx_spi_protocol
== RX_SPI_FRSKY_D
);
406 // clear features that are not supported.
407 // I have kept them all here in one place, some could be moved to sections of code above.
410 featureDisableImmediate(FEATURE_RX_PPM
);
413 #ifndef USE_SERIAL_RX
414 featureDisableImmediate(FEATURE_RX_SERIAL
);
417 #if !defined(USE_SOFTSERIAL1) && !defined(USE_SOFTSERIAL2)
418 featureDisableImmediate(FEATURE_SOFTSERIAL
);
421 #ifndef USE_RANGEFINDER
422 featureDisableImmediate(FEATURE_RANGEFINDER
);
425 #ifndef USE_TELEMETRY
426 featureDisableImmediate(FEATURE_TELEMETRY
);
430 featureDisableImmediate(FEATURE_RX_PARALLEL_PWM
);
434 featureDisableImmediate(FEATURE_RX_MSP
);
437 #ifndef USE_LED_STRIP
438 featureDisableImmediate(FEATURE_LED_STRIP
);
441 #ifndef USE_DASHBOARD
442 featureDisableImmediate(FEATURE_DASHBOARD
);
446 featureDisableImmediate(FEATURE_OSD
);
450 featureDisableImmediate(FEATURE_SERVO_TILT
| FEATURE_CHANNEL_FORWARDING
);
453 #ifndef USE_TRANSPONDER
454 featureDisableImmediate(FEATURE_TRANSPONDER
);
458 featureDisableImmediate(FEATURE_RX_SPI
);
461 #ifndef USE_ESC_SENSOR
462 featureDisableImmediate(FEATURE_ESC_SENSOR
);
465 #if !defined(USE_ADC)
466 featureDisableImmediate(FEATURE_RSSI_ADC
);
469 #if defined(USE_BEEPER)
471 if (beeperDevConfig()->frequency
&& !timerGetConfiguredByTag(beeperDevConfig()->ioTag
)) {
472 beeperDevConfigMutable()->frequency
= 0;
476 if (beeperConfig()->beeper_off_flags
& ~BEEPER_ALLOWED_MODES
) {
477 beeperConfigMutable()->beeper_off_flags
= 0;
481 if (beeperConfig()->dshotBeaconOffFlags
& ~DSHOT_BEACON_ALLOWED_MODES
) {
482 beeperConfigMutable()->dshotBeaconOffFlags
= 0;
485 if (beeperConfig()->dshotBeaconTone
< DSHOT_CMD_BEACON1
486 || beeperConfig()->dshotBeaconTone
> DSHOT_CMD_BEACON5
) {
487 beeperConfigMutable()->dshotBeaconTone
= DSHOT_CMD_BEACON1
;
492 bool configuredMotorProtocolDshot
= false;
493 checkMotorProtocolEnabled(&motorConfig()->dev
, &configuredMotorProtocolDshot
);
494 #if defined(USE_DSHOT)
495 // If using DSHOT protocol disable unsynched PWM as it's meaningless
496 if (configuredMotorProtocolDshot
) {
497 motorConfigMutable()->dev
.useUnsyncedPwm
= false;
500 #if defined(USE_DSHOT_TELEMETRY)
501 bool nChannelTimerUsed
= false;
502 for (unsigned i
= 0; i
< getMotorCount(); i
++) {
503 const ioTag_t tag
= motorConfig()->dev
.ioTags
[i
];
505 const timerHardware_t
*timer
= timerGetConfiguredByTag(tag
);
506 if (timer
&& timer
->output
& TIMER_OUTPUT_N_CHANNEL
) {
507 nChannelTimerUsed
= true;
514 if ((!configuredMotorProtocolDshot
|| (motorConfig()->dev
.useDshotBitbang
== DSHOT_BITBANG_OFF
&& (motorConfig()->dev
.useBurstDshot
== DSHOT_DMAR_ON
|| nChannelTimerUsed
))) && motorConfig()->dev
.useDshotTelemetry
) {
515 motorConfigMutable()->dev
.useDshotTelemetry
= false;
517 #endif // USE_DSHOT_TELEMETRY
521 for (int i
= 0; i
< OSD_TIMER_COUNT
; i
++) {
522 const uint16_t t
= osdConfig()->timers
[i
];
523 if (OSD_TIMER_SRC(t
) >= OSD_TIMER_SRC_COUNT
||
524 OSD_TIMER_PRECISION(t
) >= OSD_TIMER_PREC_COUNT
) {
525 osdConfigMutable()->timers
[i
] = osdTimerDefault
[i
];
530 #if defined(USE_VTX_COMMON) && defined(USE_VTX_TABLE)
531 // reset vtx band, channel, power if outside range specified by vtxtable
532 if (vtxSettingsConfig()->channel
> vtxTableConfig()->channels
) {
533 vtxSettingsConfigMutable()->channel
= 0;
534 if (vtxSettingsConfig()->band
> 0) {
535 vtxSettingsConfigMutable()->freq
= 0; // band/channel determined frequency can't be valid anymore
538 if (vtxSettingsConfig()->band
> vtxTableConfig()->bands
) {
539 vtxSettingsConfigMutable()->band
= 0;
540 vtxSettingsConfigMutable()->freq
= 0; // band/channel determined frequency can't be valid anymore
542 if (vtxSettingsConfig()->power
> vtxTableConfig()->powerLevels
) {
543 vtxSettingsConfigMutable()->power
= 0;
547 validateAndFixRatesSettings(); // constrain the various rates settings to limits imposed by the rates type
549 #if defined(USE_RX_MSP_OVERRIDE)
550 if (!rxConfig()->msp_override_channels_mask
) {
551 removeModeActivationCondition(BOXMSPOVERRIDE
);
554 for (int i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
555 const modeActivationCondition_t
*mac
= modeActivationConditions(i
);
556 if (mac
->modeId
== BOXMSPOVERRIDE
&& ((1 << (mac
->auxChannelIndex
) & (rxConfig()->msp_override_channels_mask
)))) {
557 rxConfigMutable()->msp_override_channels_mask
&= ~(1 << (mac
->auxChannelIndex
+ NON_AUX_CHANNEL_COUNT
));
562 validateAndfixMotorOutputReordering(motorConfigMutable()->dev
.motorOutputReordering
, MAX_SUPPORTED_MOTORS
);
564 // validate that the minimum battery cell voltage is less than the maximum cell voltage
565 // reset to defaults if not
566 if (batteryConfig()->vbatmincellvoltage
>= batteryConfig()->vbatmaxcellvoltage
) {
567 batteryConfigMutable()->vbatmincellvoltage
= VBAT_CELL_VOLTAGE_DEFAULT_MIN
;
568 batteryConfigMutable()->vbatmaxcellvoltage
= VBAT_CELL_VOLTAGE_DEFAULT_MAX
;
571 #ifdef USE_MSP_DISPLAYPORT
572 // validate that displayport_msp_serial is referencing a valid UART that actually has MSP enabled
573 if (displayPortProfileMsp()->displayPortSerial
!= SERIAL_PORT_NONE
) {
574 const serialPortConfig_t
*portConfig
= serialFindPortConfiguration(displayPortProfileMsp()->displayPortSerial
);
575 if (!portConfig
|| !(portConfig
->functionMask
& FUNCTION_MSP
)
576 #ifndef USE_MSP_PUSH_OVER_VCP
577 || (portConfig
->identifier
== SERIAL_PORT_USB_VCP
)
580 displayPortProfileMspMutable()->displayPortSerial
= SERIAL_PORT_NONE
;
585 #if defined(TARGET_VALIDATECONFIG)
586 // This should be done at the end of the validation
587 targetValidateConfiguration();
591 void validateAndFixGyroConfig(void)
593 // Fix gyro filter settings to handle cases where an older configurator was used that
594 // allowed higher cutoff limits from previous firmware versions.
595 adjustFilterLimit(&gyroConfigMutable()->gyro_lpf1_static_hz
, LPF_MAX_HZ
);
596 adjustFilterLimit(&gyroConfigMutable()->gyro_lpf2_static_hz
, LPF_MAX_HZ
);
597 adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_hz_1
, LPF_MAX_HZ
);
598 adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_cutoff_1
, 0);
599 adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_hz_2
, LPF_MAX_HZ
);
600 adjustFilterLimit(&gyroConfigMutable()->gyro_soft_notch_cutoff_2
, 0);
602 // Prevent invalid notch cutoff
603 if (gyroConfig()->gyro_soft_notch_cutoff_1
>= gyroConfig()->gyro_soft_notch_hz_1
) {
604 gyroConfigMutable()->gyro_soft_notch_hz_1
= 0;
606 if (gyroConfig()->gyro_soft_notch_cutoff_2
>= gyroConfig()->gyro_soft_notch_hz_2
) {
607 gyroConfigMutable()->gyro_soft_notch_hz_2
= 0;
610 //Prevent invalid dynamic lowpass filter
611 if (gyroConfig()->gyro_lpf1_dyn_min_hz
> gyroConfig()->gyro_lpf1_dyn_max_hz
) {
612 gyroConfigMutable()->gyro_lpf1_dyn_min_hz
= 0;
616 if (gyro
.sampleRateHz
> 0) {
617 float samplingTime
= 1.0f
/ gyro
.sampleRateHz
;
619 // check for looptime restrictions based on motor protocol. Motor times have safety margin
620 float motorUpdateRestriction
;
622 #if defined(STM32F411xE)
623 /* If bidirectional DSHOT is being used on an F411 then force DSHOT300. The motor update restrictions then applied
624 * will automatically consider the loop time and adjust pid_process_denom appropriately
626 if (motorConfig()->dev
.useDshotTelemetry
&& (motorConfig()->dev
.motorPwmProtocol
== PWM_TYPE_DSHOT600
)) {
627 motorConfigMutable()->dev
.motorPwmProtocol
= PWM_TYPE_DSHOT300
;
631 switch (motorConfig()->dev
.motorPwmProtocol
) {
632 case PWM_TYPE_STANDARD
:
633 motorUpdateRestriction
= 1.0f
/ BRUSHLESS_MOTORS_PWM_RATE
;
635 case PWM_TYPE_ONESHOT125
:
636 motorUpdateRestriction
= 0.0005f
;
638 case PWM_TYPE_ONESHOT42
:
639 motorUpdateRestriction
= 0.0001f
;
642 case PWM_TYPE_DSHOT150
:
643 motorUpdateRestriction
= 0.000250f
;
645 case PWM_TYPE_DSHOT300
:
646 motorUpdateRestriction
= 0.0001f
;
650 motorUpdateRestriction
= 0.00003125f
;
654 if (motorConfig()->dev
.useUnsyncedPwm
) {
655 bool configuredMotorProtocolDshot
= false;
656 checkMotorProtocolEnabled(&motorConfig()->dev
, &configuredMotorProtocolDshot
);
657 // Prevent overriding the max rate of motors
658 if (!configuredMotorProtocolDshot
&& motorConfig()->dev
.motorPwmProtocol
!= PWM_TYPE_STANDARD
) {
659 const uint32_t maxEscRate
= lrintf(1.0f
/ motorUpdateRestriction
);
660 motorConfigMutable()->dev
.motorPwmRate
= MIN(motorConfig()->dev
.motorPwmRate
, maxEscRate
);
663 const float pidLooptime
= samplingTime
* pidConfig()->pid_process_denom
;
664 if (motorConfig()->dev
.useDshotTelemetry
) {
665 motorUpdateRestriction
*= 2;
667 if (pidLooptime
< motorUpdateRestriction
) {
668 uint8_t minPidProcessDenom
= motorUpdateRestriction
/ samplingTime
;
669 if (motorUpdateRestriction
/ samplingTime
> minPidProcessDenom
) {
670 // if any fractional part then round up
671 minPidProcessDenom
++;
673 minPidProcessDenom
= constrain(minPidProcessDenom
, 1, MAX_PID_PROCESS_DENOM
);
674 pidConfigMutable()->pid_process_denom
= MAX(pidConfigMutable()->pid_process_denom
, minPidProcessDenom
);
681 if (blackboxConfig()->device
== BLACKBOX_DEVICE_FLASH
) {
682 blackboxConfigMutable()->device
= BLACKBOX_DEVICE_NONE
;
684 #endif // USE_FLASHFS
686 if (blackboxConfig()->device
== BLACKBOX_DEVICE_SDCARD
) {
687 #if defined(USE_SDCARD)
688 if (!sdcardConfig()->mode
)
691 blackboxConfigMutable()->device
= BLACKBOX_DEVICE_NONE
;
694 #endif // USE_BLACKBOX
696 if (systemConfig()->activeRateProfile
>= CONTROL_RATE_PROFILE_COUNT
) {
697 systemConfigMutable()->activeRateProfile
= 0;
699 loadControlRateProfile();
701 if (systemConfig()->pidProfileIndex
>= PID_PROFILE_COUNT
) {
702 systemConfigMutable()->pidProfileIndex
= 0;
707 bool readEEPROM(void)
709 suspendRxPwmPpmSignal();
711 // Sanity check, read flash
712 bool success
= loadEEPROM();
716 validateAndFixConfig();
720 resumeRxPwmPpmSignal();
725 void writeUnmodifiedConfigToEEPROM(void)
727 validateAndFixConfig();
729 suspendRxPwmPpmSignal();
731 writeConfigToEEPROM();
733 resumeRxPwmPpmSignal();
734 configIsDirty
= false;
737 void writeEEPROM(void)
739 systemConfigMutable()->configurationState
= CONFIGURATION_STATE_CONFIGURED
;
741 writeUnmodifiedConfigToEEPROM();
744 bool resetEEPROM(bool useCustomDefaults
)
746 #if !defined(USE_CUSTOM_DEFAULTS)
747 UNUSED(useCustomDefaults
);
749 if (useCustomDefaults
) {
750 if (!resetConfigToCustomDefaults()) {
759 writeUnmodifiedConfigToEEPROM();
764 void ensureEEPROMStructureIsValid(void)
766 if (isEEPROMStructureValid()) {
772 void saveConfigAndNotify(void)
774 // The write to EEPROM will cause a big delay in the current task, so ignore
775 schedulerIgnoreTaskExecTime();
779 beeperConfirmationBeeps(1);
782 void setConfigDirty(void)
784 configIsDirty
= true;
787 bool isConfigDirty(void)
789 return configIsDirty
;
792 void changePidProfileFromCellCount(uint8_t cellCount
)
794 if (currentPidProfile
->auto_profile_cell_count
== cellCount
|| currentPidProfile
->auto_profile_cell_count
== AUTO_PROFILE_CELL_COUNT_STAY
) {
798 unsigned profileIndex
= (systemConfig()->pidProfileIndex
+ 1) % PID_PROFILE_COUNT
;
799 int matchingProfileIndex
= -1;
800 while (profileIndex
!= systemConfig()->pidProfileIndex
) {
801 if (pidProfiles(profileIndex
)->auto_profile_cell_count
== cellCount
) {
802 matchingProfileIndex
= profileIndex
;
805 } else if (matchingProfileIndex
< 0 && pidProfiles(profileIndex
)->auto_profile_cell_count
== AUTO_PROFILE_CELL_COUNT_STAY
) {
806 matchingProfileIndex
= profileIndex
;
809 profileIndex
= (profileIndex
+ 1) % PID_PROFILE_COUNT
;
812 if (matchingProfileIndex
>= 0) {
813 changePidProfile(matchingProfileIndex
);
817 void changePidProfile(uint8_t pidProfileIndex
)
819 // The config switch will cause a big enough delay in the current task to upset the scheduler
820 schedulerIgnoreTaskExecTime();
822 if (pidProfileIndex
< PID_PROFILE_COUNT
) {
823 systemConfigMutable()->pidProfileIndex
= pidProfileIndex
;
826 pidInit(currentPidProfile
);
831 beeperConfirmationBeeps(pidProfileIndex
+ 1);
834 bool isSystemConfigured(void)
836 return systemConfig()->configurationState
== CONFIGURATION_STATE_CONFIGURED
;
839 void setRebootRequired(void)
841 rebootRequired
= true;
842 setArmingDisabled(ARMING_DISABLED_REBOOT_REQUIRED
);
845 bool getRebootRequired(void)
847 return rebootRequired
;