2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
24 #include "build_config.h"
26 #include "blackbox/blackbox_io.h"
28 #include "common/color.h"
29 #include "common/axis.h"
30 #include "common/maths.h"
31 #include "common/filter.h"
33 #include "drivers/sensor.h"
34 #include "drivers/accgyro.h"
35 #include "drivers/compass.h"
36 #include "drivers/system.h"
37 #include "drivers/gpio.h"
38 #include "drivers/timer.h"
39 #include "drivers/pwm_rx.h"
40 #include "drivers/serial.h"
41 #include "drivers/pwm_output.h"
42 #include "drivers/max7456.h"
44 #include "sensors/sensors.h"
45 #include "sensors/gyro.h"
46 #include "sensors/compass.h"
47 #include "sensors/acceleration.h"
48 #include "sensors/barometer.h"
49 #include "sensors/boardalignment.h"
50 #include "sensors/battery.h"
52 #include "io/beeper.h"
53 #include "io/serial.h"
54 #include "io/gimbal.h"
55 #include "io/escservo.h"
56 #include "io/rc_controls.h"
57 #include "io/rc_curves.h"
58 #include "io/ledstrip.h"
65 #include "telemetry/telemetry.h"
67 #include "flight/mixer.h"
68 #include "flight/pid.h"
69 #include "flight/imu.h"
70 #include "flight/failsafe.h"
71 #include "flight/altitudehold.h"
72 #include "flight/navigation.h"
74 #include "config/runtime_config.h"
75 #include "config/config.h"
77 #include "config/config_profile.h"
78 #include "config/config_master.h"
80 #ifndef DEFAULT_RX_FEATURE
81 #define DEFAULT_RX_FEATURE FEATURE_RX_PARALLEL_PWM
84 #define BRUSHED_MOTORS_PWM_RATE 16000
86 #define BRUSHLESS_MOTORS_PWM_RATE 2000
88 #define BRUSHLESS_MOTORS_PWM_RATE 400
91 void useRcControlsConfig(modeActivationCondition_t
*modeActivationConditions
, escAndServoConfig_t
*escAndServoConfigToUse
, pidProfile_t
*pidProfileToUse
);
93 #if !defined(FLASH_SIZE)
94 #error "Flash size not defined for target. (specify in KB)"
98 #ifndef FLASH_PAGE_SIZE
100 #define FLASH_PAGE_SIZE ((uint16_t)0x800)
104 #define FLASH_PAGE_SIZE ((uint16_t)0x400)
108 #define FLASH_PAGE_SIZE ((uint16_t)0x800)
111 #if defined(STM32F40_41xxx)
112 #define FLASH_PAGE_SIZE ((uint32_t)0x20000)
115 #if defined (STM32F411xE)
116 #define FLASH_PAGE_SIZE ((uint32_t)0x20000)
121 #if !defined(FLASH_SIZE) && !defined(FLASH_PAGE_COUNT)
123 #define FLASH_PAGE_COUNT 128
127 #define FLASH_PAGE_COUNT 128
131 #if defined(FLASH_SIZE)
132 #if defined(STM32F40_41xxx)
133 #define FLASH_PAGE_COUNT 4 // just to make calculations work
134 #elif defined (STM32F411xE)
135 #define FLASH_PAGE_COUNT 4 // just to make calculations work
137 #define FLASH_PAGE_COUNT ((FLASH_SIZE * 0x400) / FLASH_PAGE_SIZE)
141 #if !defined(FLASH_PAGE_SIZE)
142 #error "Flash page size not defined for target."
145 #if !defined(FLASH_PAGE_COUNT)
146 #error "Flash page count not defined for target."
149 #if FLASH_SIZE <= 128
150 #define FLASH_TO_RESERVE_FOR_CONFIG 0x800
152 #define FLASH_TO_RESERVE_FOR_CONFIG 0x1000
155 // use the last flash pages for storage
156 #ifdef CUSTOM_FLASH_MEMORY_ADDRESS
157 size_t custom_flash_memory_address
= 0;
158 #define CONFIG_START_FLASH_ADDRESS (custom_flash_memory_address)
160 // use the last flash pages for storage
161 #ifndef CONFIG_START_FLASH_ADDRESS
162 #define CONFIG_START_FLASH_ADDRESS (0x08000000 + (uint32_t)((FLASH_PAGE_SIZE * FLASH_PAGE_COUNT) - FLASH_TO_RESERVE_FOR_CONFIG))
166 master_t masterConfig
; // master config struct with data independent from profiles
167 profile_t
*currentProfile
;
168 static uint32_t activeFeaturesLatch
= 0;
170 static uint8_t currentControlRateProfileIndex
= 0;
171 controlRateConfig_t
*currentControlRateProfile
;
173 static const uint8_t EEPROM_CONF_VERSION
= 141;
175 static void resetAccelerometerTrims(flightDynamicsTrims_t
*accelerometerTrims
)
177 accelerometerTrims
->values
.pitch
= 0;
178 accelerometerTrims
->values
.roll
= 0;
179 accelerometerTrims
->values
.yaw
= 0;
182 void resetPidProfile(pidProfile_t
*pidProfile
)
185 #if (defined(STM32F10X))
186 pidProfile
->pidController
= PID_CONTROLLER_INTEGER
;
188 pidProfile
->pidController
= PID_CONTROLLER_FLOAT
;
191 pidProfile
->P8
[ROLL
] = 45;
192 pidProfile
->I8
[ROLL
] = 40;
193 pidProfile
->D8
[ROLL
] = 18;
194 pidProfile
->P8
[PITCH
] = 50;
195 pidProfile
->I8
[PITCH
] = 40;
196 pidProfile
->D8
[PITCH
] = 18;
197 pidProfile
->P8
[YAW
] = 80;
198 pidProfile
->I8
[YAW
] = 45;
199 pidProfile
->D8
[YAW
] = 20;
200 pidProfile
->P8
[PIDALT
] = 50;
201 pidProfile
->I8
[PIDALT
] = 0;
202 pidProfile
->D8
[PIDALT
] = 0;
203 pidProfile
->P8
[PIDPOS
] = 15; // POSHOLD_P * 100;
204 pidProfile
->I8
[PIDPOS
] = 0; // POSHOLD_I * 100;
205 pidProfile
->D8
[PIDPOS
] = 0;
206 pidProfile
->P8
[PIDPOSR
] = 34; // POSHOLD_RATE_P * 10;
207 pidProfile
->I8
[PIDPOSR
] = 14; // POSHOLD_RATE_I * 100;
208 pidProfile
->D8
[PIDPOSR
] = 53; // POSHOLD_RATE_D * 1000;
209 pidProfile
->P8
[PIDNAVR
] = 25; // NAV_P * 10;
210 pidProfile
->I8
[PIDNAVR
] = 33; // NAV_I * 100;
211 pidProfile
->D8
[PIDNAVR
] = 83; // NAV_D * 1000;
212 pidProfile
->P8
[PIDLEVEL
] = 50;
213 pidProfile
->I8
[PIDLEVEL
] = 50;
214 pidProfile
->D8
[PIDLEVEL
] = 100;
215 pidProfile
->P8
[PIDMAG
] = 40;
216 pidProfile
->P8
[PIDVEL
] = 55;
217 pidProfile
->I8
[PIDVEL
] = 55;
218 pidProfile
->D8
[PIDVEL
] = 75;
220 pidProfile
->yaw_p_limit
= YAW_P_LIMIT_MAX
;
221 pidProfile
->yaw_lpf_hz
= 80;
222 pidProfile
->rollPitchItermIgnoreRate
= 180;
223 pidProfile
->yawItermIgnoreRate
= 35;
224 pidProfile
->dterm_lpf_hz
= 100; // filtering ON by default
225 pidProfile
->deltaMethod
= DELTA_FROM_MEASUREMENT
;
226 pidProfile
->dynamic_pid
= 1;
229 pidProfile
->gtune_lolimP
[ROLL
] = 10; // [0..200] Lower limit of ROLL P during G tune.
230 pidProfile
->gtune_lolimP
[PITCH
] = 10; // [0..200] Lower limit of PITCH P during G tune.
231 pidProfile
->gtune_lolimP
[YAW
] = 10; // [0..200] Lower limit of YAW P during G tune.
232 pidProfile
->gtune_hilimP
[ROLL
] = 100; // [0..200] Higher limit of ROLL P during G tune. 0 Disables tuning for that axis.
233 pidProfile
->gtune_hilimP
[PITCH
] = 100; // [0..200] Higher limit of PITCH P during G tune. 0 Disables tuning for that axis.
234 pidProfile
->gtune_hilimP
[YAW
] = 100; // [0..200] Higher limit of YAW P during G tune. 0 Disables tuning for that axis.
235 pidProfile
->gtune_pwr
= 0; // [0..10] Strength of adjustment
236 pidProfile
->gtune_settle_time
= 450; // [200..1000] Settle time in ms
237 pidProfile
->gtune_average_cycles
= 16; // [8..128] Number of looptime cycles used for gyro average calculation
242 void resetGpsProfile(gpsProfile_t
*gpsProfile
)
244 gpsProfile
->gps_wp_radius
= 200;
245 gpsProfile
->gps_lpf
= 20;
246 gpsProfile
->nav_slew_rate
= 30;
247 gpsProfile
->nav_controls_heading
= 1;
248 gpsProfile
->nav_speed_min
= 100;
249 gpsProfile
->nav_speed_max
= 300;
250 gpsProfile
->ap_mode
= 40;
254 void resetBarometerConfig(barometerConfig_t
*barometerConfig
)
256 barometerConfig
->baro_sample_count
= 21;
257 barometerConfig
->baro_noise_lpf
= 0.6f
;
258 barometerConfig
->baro_cf_vel
= 0.985f
;
259 barometerConfig
->baro_cf_alt
= 0.965f
;
262 void resetSensorAlignment(sensorAlignmentConfig_t
*sensorAlignmentConfig
)
264 sensorAlignmentConfig
->gyro_align
= ALIGN_DEFAULT
;
265 sensorAlignmentConfig
->acc_align
= ALIGN_DEFAULT
;
266 sensorAlignmentConfig
->mag_align
= ALIGN_DEFAULT
;
269 void resetEscAndServoConfig(escAndServoConfig_t
*escAndServoConfig
)
271 #ifdef BRUSHED_MOTORS
272 escAndServoConfig
->minthrottle
= 1000;
273 escAndServoConfig
->maxthrottle
= 2000;
275 escAndServoConfig
->minthrottle
= 1150;
276 escAndServoConfig
->maxthrottle
= 1850;
278 escAndServoConfig
->mincommand
= 1000;
279 escAndServoConfig
->servoCenterPulse
= 1500;
280 escAndServoConfig
->escDesyncProtection
= 0;
283 void resetFlight3DConfig(flight3DConfig_t
*flight3DConfig
)
285 flight3DConfig
->deadband3d_low
= 1406;
286 flight3DConfig
->deadband3d_high
= 1514;
287 flight3DConfig
->neutral3d
= 1460;
288 flight3DConfig
->deadband3d_throttle
= 50;
291 void resetTelemetryConfig(telemetryConfig_t
*telemetryConfig
)
293 telemetryConfig
->telemetry_inversion
= 0;
294 telemetryConfig
->telemetry_switch
= 0;
295 telemetryConfig
->gpsNoFixLatitude
= 0;
296 telemetryConfig
->gpsNoFixLongitude
= 0;
297 telemetryConfig
->frsky_coordinate_format
= FRSKY_FORMAT_DMS
;
298 telemetryConfig
->frsky_unit
= FRSKY_UNIT_METRICS
;
299 telemetryConfig
->frsky_vfas_precision
= 0;
300 telemetryConfig
->frsky_vfas_cell_voltage
= 0;
301 telemetryConfig
->hottAlarmSoundInterval
= 5;
304 void resetBatteryConfig(batteryConfig_t
*batteryConfig
)
306 batteryConfig
->vbatscale
= VBAT_SCALE_DEFAULT
;
307 batteryConfig
->vbatresdivval
= VBAT_RESDIVVAL_DEFAULT
;
308 batteryConfig
->vbatresdivmultiplier
= VBAT_RESDIVMULTIPLIER_DEFAULT
;
309 batteryConfig
->vbatmaxcellvoltage
= 43;
310 batteryConfig
->vbatmincellvoltage
= 33;
311 batteryConfig
->vbatwarningcellvoltage
= 35;
312 batteryConfig
->vbathysteresis
= 1;
313 batteryConfig
->vbatPidCompensation
= 0;
314 batteryConfig
->currentMeterOffset
= 0;
315 batteryConfig
->currentMeterScale
= 400; // for Allegro ACS758LCB-100U (40mV/A)
316 batteryConfig
->batteryCapacity
= 0;
317 batteryConfig
->currentMeterType
= CURRENT_SENSOR_ADC
;
320 #ifdef SWAP_SERIAL_PORT_0_AND_1_DEFAULTS
321 #define FIRST_PORT_INDEX 1
322 #define SECOND_PORT_INDEX 0
324 #define FIRST_PORT_INDEX 0
325 #define SECOND_PORT_INDEX 1
328 void resetSerialConfig(serialConfig_t
*serialConfig
)
331 memset(serialConfig
, 0, sizeof(serialConfig_t
));
333 for (index
= 0; index
< SERIAL_PORT_COUNT
; index
++) {
334 serialConfig
->portConfigs
[index
].identifier
= serialPortIdentifiers
[index
];
335 serialConfig
->portConfigs
[index
].msp_baudrateIndex
= BAUD_115200
;
336 serialConfig
->portConfigs
[index
].gps_baudrateIndex
= BAUD_57600
;
337 serialConfig
->portConfigs
[index
].telemetry_baudrateIndex
= BAUD_AUTO
;
338 serialConfig
->portConfigs
[index
].blackbox_baudrateIndex
= BAUD_115200
;
341 serialConfig
->portConfigs
[0].functionMask
= FUNCTION_MSP
;
344 // This allows MSP connection via USART & VCP so the board can be reconfigured.
345 serialConfig
->portConfigs
[1].functionMask
= FUNCTION_MSP
;
348 serialConfig
->reboot_character
= 'R';
351 static void resetControlRateConfig(controlRateConfig_t
*controlRateConfig
)
353 controlRateConfig
->rcRate8
= 100;
354 controlRateConfig
->rcYawRate8
= 100;
355 controlRateConfig
->rcExpo8
= 10;
356 controlRateConfig
->thrMid8
= 50;
357 controlRateConfig
->thrExpo8
= 0;
358 controlRateConfig
->dynThrPID
= 20;
359 controlRateConfig
->rcYawExpo8
= 10;
360 controlRateConfig
->tpa_breakpoint
= 1650;
362 for (uint8_t axis
= 0; axis
< FLIGHT_DYNAMICS_INDEX_COUNT
; axis
++) {
363 controlRateConfig
->rates
[axis
] = 70;
368 void resetRcControlsConfig(rcControlsConfig_t
*rcControlsConfig
)
370 rcControlsConfig
->deadband
= 0;
371 rcControlsConfig
->yaw_deadband
= 0;
372 rcControlsConfig
->alt_hold_deadband
= 40;
373 rcControlsConfig
->alt_hold_fast_change
= 1;
376 void resetMixerConfig(mixerConfig_t
*mixerConfig
)
378 mixerConfig
->yaw_motor_direction
= 1;
380 mixerConfig
->tri_unarmed_servo
= 1;
381 mixerConfig
->servo_lowpass_freq
= 400;
382 mixerConfig
->servo_lowpass_enable
= 0;
386 uint8_t getCurrentProfile(void)
388 return masterConfig
.current_profile_index
;
391 static void setProfile(uint8_t profileIndex
)
393 currentProfile
= &masterConfig
.profile
[profileIndex
];
394 currentControlRateProfileIndex
= currentProfile
->activeRateProfile
;
395 currentControlRateProfile
= ¤tProfile
->controlRateProfile
[currentControlRateProfileIndex
];
398 uint8_t getCurrentControlRateProfile(void)
400 return currentControlRateProfileIndex
;
403 static void setControlRateProfile(uint8_t profileIndex
)
405 currentControlRateProfileIndex
= profileIndex
;
406 masterConfig
.profile
[getCurrentProfile()].activeRateProfile
= profileIndex
;
407 currentControlRateProfile
= &masterConfig
.profile
[getCurrentProfile()].controlRateProfile
[profileIndex
];
410 controlRateConfig_t
*getControlRateConfig(uint8_t profileIndex
)
412 return &masterConfig
.profile
[profileIndex
].controlRateProfile
[masterConfig
.profile
[profileIndex
].activeRateProfile
];
415 uint16_t getCurrentMinthrottle(void)
417 return masterConfig
.escAndServoConfig
.minthrottle
;
421 static void resetConf(void)
423 // Clear all configuration
424 memset(&masterConfig
, 0, sizeof(master_t
));
428 featureSet(DEFAULT_RX_FEATURE
| FEATURE_FAILSAFE
| FEATURE_SUPEREXPO_RATES
);
429 #ifdef DEFAULT_FEATURES
430 featureSet(DEFAULT_FEATURES
);
437 #ifdef BOARD_HAS_VOLTAGE_DIVIDER
438 // only enable the VBAT feature by default if the board has a voltage divider otherwise
439 // the user may see incorrect readings and unexpected issues with pin mappings may occur.
440 featureSet(FEATURE_VBAT
);
443 masterConfig
.version
= EEPROM_CONF_VERSION
;
444 masterConfig
.mixerMode
= MIXER_QUADX
;
447 masterConfig
.current_profile_index
= 0; // default profile
448 masterConfig
.dcm_kp
= 2500; // 1.0 * 10000
449 masterConfig
.dcm_ki
= 0; // 0.003 * 10000
450 masterConfig
.gyro_lpf
= 0; // 256HZ default
452 masterConfig
.gyro_sync_denom
= 8;
454 masterConfig
.gyro_sync_denom
= 4;
456 masterConfig
.gyro_soft_lpf_hz
= 100;
458 masterConfig
.pid_process_denom
= 2;
460 masterConfig
.debug_mode
= 0;
462 resetAccelerometerTrims(&masterConfig
.accZero
);
464 resetSensorAlignment(&masterConfig
.sensorAlignmentConfig
);
466 masterConfig
.boardAlignment
.rollDegrees
= 0;
467 masterConfig
.boardAlignment
.pitchDegrees
= 0;
468 masterConfig
.boardAlignment
.yawDegrees
= 0;
469 masterConfig
.acc_hardware
= ACC_DEFAULT
; // default/autodetect
470 masterConfig
.max_angle_inclination
= 700; // 70 degrees
471 masterConfig
.yaw_control_direction
= 1;
472 masterConfig
.gyroConfig
.gyroMovementCalibrationThreshold
= 32;
474 // xxx_hardware: 0:default/autodetect, 1: disable
475 masterConfig
.mag_hardware
= 0;
477 masterConfig
.baro_hardware
= 0;
479 resetBatteryConfig(&masterConfig
.batteryConfig
);
481 resetTelemetryConfig(&masterConfig
.telemetryConfig
);
483 #ifdef SERIALRX_PROVIDER
484 masterConfig
.rxConfig
.serialrx_provider
= SERIALRX_PROVIDER
;
486 masterConfig
.rxConfig
.serialrx_provider
= 0;
488 masterConfig
.rxConfig
.sbus_inversion
= 1;
489 masterConfig
.rxConfig
.spektrum_sat_bind
= 0;
490 masterConfig
.rxConfig
.spektrum_sat_bind_autoreset
= 1;
491 masterConfig
.rxConfig
.midrc
= 1500;
492 masterConfig
.rxConfig
.mincheck
= 1100;
493 masterConfig
.rxConfig
.maxcheck
= 1900;
494 masterConfig
.rxConfig
.rx_min_usec
= 885; // any of first 4 channels below this value will trigger rx loss detection
495 masterConfig
.rxConfig
.rx_max_usec
= 2115; // any of first 4 channels above this value will trigger rx loss detection
497 for (int i
= 0; i
< MAX_SUPPORTED_RC_CHANNEL_COUNT
; i
++) {
498 rxFailsafeChannelConfiguration_t
*channelFailsafeConfiguration
= &masterConfig
.rxConfig
.failsafe_channel_configurations
[i
];
499 channelFailsafeConfiguration
->mode
= (i
< NON_AUX_CHANNEL_COUNT
) ? RX_FAILSAFE_MODE_AUTO
: RX_FAILSAFE_MODE_HOLD
;
500 channelFailsafeConfiguration
->step
= (i
== THROTTLE
) ? CHANNEL_VALUE_TO_RXFAIL_STEP(masterConfig
.rxConfig
.rx_min_usec
) : CHANNEL_VALUE_TO_RXFAIL_STEP(masterConfig
.rxConfig
.midrc
);
503 masterConfig
.rxConfig
.rssi_channel
= 0;
504 masterConfig
.rxConfig
.rssi_scale
= RSSI_SCALE_DEFAULT
;
505 masterConfig
.rxConfig
.rssi_ppm_invert
= 0;
506 masterConfig
.rxConfig
.rcSmoothInterval
= 0; // 0 is predefined
507 masterConfig
.rxConfig
.fpvCamAngleDegrees
= 0;
509 masterConfig
.rxConfig
.max_aux_channel
= 99;
511 masterConfig
.rxConfig
.max_aux_channel
= 6;
513 masterConfig
.rxConfig
.airModeActivateThreshold
= 1350;
515 resetAllRxChannelRangeConfigurations(masterConfig
.rxConfig
.channelRanges
);
517 masterConfig
.inputFilteringMode
= INPUT_FILTERING_DISABLED
;
519 masterConfig
.gyro_cal_on_first_arm
= 0; // TODO - Cleanup retarded arm support
520 masterConfig
.disarm_kill_switch
= 1;
521 masterConfig
.auto_disarm_delay
= 5;
522 masterConfig
.small_angle
= 25;
524 resetMixerConfig(&masterConfig
.mixerConfig
);
526 masterConfig
.airplaneConfig
.fixedwing_althold_dir
= 1;
529 resetEscAndServoConfig(&masterConfig
.escAndServoConfig
);
530 resetFlight3DConfig(&masterConfig
.flight3DConfig
);
532 #ifdef BRUSHED_MOTORS
533 masterConfig
.motor_pwm_rate
= BRUSHED_MOTORS_PWM_RATE
;
534 masterConfig
.motor_pwm_protocol
= PWM_TYPE_BRUSHED
;
536 masterConfig
.motor_pwm_rate
= BRUSHLESS_MOTORS_PWM_RATE
;
537 masterConfig
.motor_pwm_protocol
= PWM_TYPE_ONESHOT125
;
539 masterConfig
.servo_pwm_rate
= 50;
542 masterConfig
.use_buzzer_p6
= 0;
547 masterConfig
.gpsConfig
.provider
= GPS_NMEA
;
548 masterConfig
.gpsConfig
.sbasMode
= SBAS_AUTO
;
549 masterConfig
.gpsConfig
.autoConfig
= GPS_AUTOCONFIG_ON
;
550 masterConfig
.gpsConfig
.autoBaud
= GPS_AUTOBAUD_OFF
;
553 resetSerialConfig(&masterConfig
.serialConfig
);
555 masterConfig
.emf_avoidance
= 0; // TODO - needs removal
557 resetPidProfile(¤tProfile
->pidProfile
);
559 for (int rI
= 0; rI
<MAX_RATEPROFILES
; rI
++) {
560 resetControlRateConfig(&masterConfig
.profile
[0].controlRateProfile
[rI
]);
562 resetRollAndPitchTrims(&masterConfig
.accelerometerTrims
);
564 masterConfig
.mag_declination
= 0;
565 masterConfig
.acc_lpf_hz
= 10.0f
;
566 masterConfig
.accDeadband
.xy
= 40;
567 masterConfig
.accDeadband
.z
= 40;
568 masterConfig
.acc_unarmedcal
= 1;
570 resetBarometerConfig(&masterConfig
.barometerConfig
);
573 parseRcChannels("AETR1234", &masterConfig
.rxConfig
);
575 resetRcControlsConfig(&masterConfig
.rcControlsConfig
);
577 masterConfig
.throttle_correction_value
= 0; // could 10 with althold or 40 for fpv
578 masterConfig
.throttle_correction_angle
= 800; // could be 80.0 deg with atlhold or 45.0 for fpv
580 // Failsafe Variables
581 masterConfig
.failsafeConfig
.failsafe_delay
= 10; // 1sec
582 masterConfig
.failsafeConfig
.failsafe_off_delay
= 10; // 1sec
583 masterConfig
.failsafeConfig
.failsafe_throttle
= 1000; // default throttle off.
584 masterConfig
.failsafeConfig
.failsafe_kill_switch
= 0; // default failsafe switch action is identical to rc link loss
585 masterConfig
.failsafeConfig
.failsafe_throttle_low_delay
= 100; // default throttle low delay for "just disarm" on failsafe condition
586 masterConfig
.failsafeConfig
.failsafe_procedure
= 0; // default full failsafe procedure is 0: auto-landing
590 for (int i
= 0; i
< MAX_SUPPORTED_SERVOS
; i
++) {
591 masterConfig
.servoConf
[i
].min
= DEFAULT_SERVO_MIN
;
592 masterConfig
.servoConf
[i
].max
= DEFAULT_SERVO_MAX
;
593 masterConfig
.servoConf
[i
].middle
= DEFAULT_SERVO_MIDDLE
;
594 masterConfig
.servoConf
[i
].rate
= 100;
595 masterConfig
.servoConf
[i
].angleAtMin
= DEFAULT_SERVO_MIN_ANGLE
;
596 masterConfig
.servoConf
[i
].angleAtMax
= DEFAULT_SERVO_MAX_ANGLE
;
597 masterConfig
.servoConf
[i
].forwardFromChannel
= CHANNEL_FORWARDING_DISABLED
;
601 masterConfig
.gimbalConfig
.mode
= GIMBAL_MODE_NORMAL
;
605 resetGpsProfile(&masterConfig
.gpsProfile
);
608 // custom mixer. clear by defaults.
609 for (int i
= 0; i
< MAX_SUPPORTED_MOTORS
; i
++) {
610 masterConfig
.customMotorMixer
[i
].throttle
= 0.0f
;
614 applyDefaultColors(masterConfig
.colors
, CONFIGURABLE_COLOR_COUNT
);
615 applyDefaultLedStripConfig(masterConfig
.ledConfigs
);
616 masterConfig
.ledstrip_visual_beeper
= 0;
620 masterConfig
.vtx_band
= 4; //Fatshark/Airwaves
621 masterConfig
.vtx_channel
= 1; //CH1
622 masterConfig
.vtx_mode
= 0; //CH+BAND mode
623 masterConfig
.vtx_mhz
= 5740; //F0
627 static const uint8_t defaultTransponderData
[6] = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC }; // Note, this is NOT a valid transponder code, it's just for testing production hardware
629 memcpy(masterConfig
.transponderData
, &defaultTransponderData
, sizeof(defaultTransponderData
));
634 #if defined(ENABLE_BLACKBOX_LOGGING_ON_SPIFLASH_BY_DEFAULT)
635 featureSet(FEATURE_BLACKBOX
);
636 masterConfig
.blackbox_device
= BLACKBOX_DEVICE_FLASH
;
637 #elif defined(ENABLE_BLACKBOX_LOGGING_ON_SDCARD_BY_DEFAULT)
638 featureSet(FEATURE_BLACKBOX
);
639 masterConfig
.blackbox_device
= BLACKBOX_DEVICE_SDCARD
;
641 masterConfig
.blackbox_device
= BLACKBOX_DEVICE_SERIAL
;
644 masterConfig
.blackbox_rate_num
= 1;
645 masterConfig
.blackbox_rate_denom
= 1;
650 if (featureConfigured(FEATURE_RX_SERIAL
)) {
651 masterConfig
.serialConfig
.portConfigs
[SERIALRX_UART
].functionMask
= FUNCTION_RX_SERIAL
;
655 // alternative defaults settings for COLIBRI RACE targets
656 #if defined(COLIBRI_RACE)
657 masterConfig
.escAndServoConfig
.minthrottle
= 1025;
658 masterConfig
.escAndServoConfig
.maxthrottle
= 1980;
659 masterConfig
.batteryConfig
.vbatmaxcellvoltage
= 45;
660 masterConfig
.batteryConfig
.vbatmincellvoltage
= 30;
663 #if defined(TARGET_CONFIG)
664 targetConfiguration(&masterConfig
);
667 #if defined(ALIENFLIGHT)
668 featureClear(FEATURE_ONESHOT125
);
670 masterConfig
.mag_hardware
= MAG_NONE
; // disabled by default
672 masterConfig
.rxConfig
.spektrum_sat_bind
= 5;
673 masterConfig
.rxConfig
.spektrum_sat_bind_autoreset
= 1;
674 masterConfig
.motor_pwm_rate
= 32000;
675 masterConfig
.failsafeConfig
.failsafe_delay
= 2;
676 masterConfig
.failsafeConfig
.failsafe_off_delay
= 0;
677 currentControlRateProfile
->rates
[FD_PITCH
] = 40;
678 currentControlRateProfile
->rates
[FD_ROLL
] = 40;
679 currentControlRateProfile
->rates
[FD_YAW
] = 40;
680 parseRcChannels("TAER1234", &masterConfig
.rxConfig
);
682 masterConfig
.customMotorMixer
[0] = (motorMixer_t
){ 1.0f
, -0.414178f
, 1.0f
, -1.0f
}; // REAR_R
683 masterConfig
.customMotorMixer
[1] = (motorMixer_t
){ 1.0f
, -0.414178f
, -1.0f
, 1.0f
}; // FRONT_R
684 masterConfig
.customMotorMixer
[2] = (motorMixer_t
){ 1.0f
, 0.414178f
, 1.0f
, 1.0f
}; // REAR_L
685 masterConfig
.customMotorMixer
[3] = (motorMixer_t
){ 1.0f
, 0.414178f
, -1.0f
, -1.0f
}; // FRONT_L
686 masterConfig
.customMotorMixer
[4] = (motorMixer_t
){ 1.0f
, -1.0f
, -0.414178f
, -1.0f
}; // MIDFRONT_R
687 masterConfig
.customMotorMixer
[5] = (motorMixer_t
){ 1.0f
, 1.0f
, -0.414178f
, 1.0f
}; // MIDFRONT_L
688 masterConfig
.customMotorMixer
[6] = (motorMixer_t
){ 1.0f
, -1.0f
, 0.414178f
, 1.0f
}; // MIDREAR_R
689 masterConfig
.customMotorMixer
[7] = (motorMixer_t
){ 1.0f
, 1.0f
, 0.414178f
, -1.0f
}; // MIDREAR_L#endif
692 // copy first profile into remaining profile
693 for (int i
= 1; i
< MAX_PROFILE_COUNT
; i
++) {
694 memcpy(&masterConfig
.profile
[i
], currentProfile
, sizeof(profile_t
));
699 static uint8_t calculateChecksum(const uint8_t *data
, uint32_t length
)
701 uint8_t checksum
= 0;
702 const uint8_t *byteOffset
;
704 for (byteOffset
= data
; byteOffset
< (data
+ length
); byteOffset
++)
705 checksum
^= *byteOffset
;
709 static bool isEEPROMContentValid(void)
711 const master_t
*temp
= (const master_t
*) CONFIG_START_FLASH_ADDRESS
;
712 uint8_t checksum
= 0;
714 // check version number
715 if (EEPROM_CONF_VERSION
!= temp
->version
)
718 // check size and magic numbers
719 if (temp
->size
!= sizeof(master_t
) || temp
->magic_be
!= 0xBE || temp
->magic_ef
!= 0xEF)
722 // verify integrity of temporary copy
723 checksum
= calculateChecksum((const uint8_t *) temp
, sizeof(master_t
));
727 // looks good, let's roll!
731 void activateControlRateConfig(void)
733 generateThrottleCurve(currentControlRateProfile
, &masterConfig
.escAndServoConfig
);
736 void activateConfig(void)
738 static imuRuntimeConfig_t imuRuntimeConfig
;
740 activateControlRateConfig();
742 resetAdjustmentStates();
745 masterConfig
.modeActivationConditions
,
746 &masterConfig
.escAndServoConfig
,
747 ¤tProfile
->pidProfile
750 gyroUseConfig(&masterConfig
.gyroConfig
, masterConfig
.gyro_soft_lpf_hz
);
753 telemetryUseConfig(&masterConfig
.telemetryConfig
);
755 pidSetController(currentProfile
->pidProfile
.pidController
);
758 gpsUseProfile(&masterConfig
.gpsProfile
);
759 gpsUsePIDs(¤tProfile
->pidProfile
);
762 useFailsafeConfig(&masterConfig
.failsafeConfig
);
763 setAccelerationTrims(&masterConfig
.accZero
);
764 setAccelerationFilter(masterConfig
.acc_lpf_hz
);
768 masterConfig
.servoConf
,
769 &masterConfig
.gimbalConfig
,
771 &masterConfig
.flight3DConfig
,
772 &masterConfig
.escAndServoConfig
,
773 &masterConfig
.mixerConfig
,
774 &masterConfig
.airplaneConfig
,
775 &masterConfig
.rxConfig
778 imuRuntimeConfig
.dcm_kp
= masterConfig
.dcm_kp
/ 10000.0f
;
779 imuRuntimeConfig
.dcm_ki
= masterConfig
.dcm_ki
/ 10000.0f
;
780 imuRuntimeConfig
.acc_unarmedcal
= masterConfig
.acc_unarmedcal
;
781 imuRuntimeConfig
.small_angle
= masterConfig
.small_angle
;
785 ¤tProfile
->pidProfile
,
786 &masterConfig
.accDeadband
,
787 masterConfig
.throttle_correction_angle
790 configureAltitudeHold(
791 ¤tProfile
->pidProfile
,
792 &masterConfig
.barometerConfig
,
793 &masterConfig
.rcControlsConfig
,
794 &masterConfig
.escAndServoConfig
798 useBarometerConfig(&masterConfig
.barometerConfig
);
802 void validateAndFixConfig(void)
804 if (!(featureConfigured(FEATURE_RX_PARALLEL_PWM
) || featureConfigured(FEATURE_RX_PPM
) || featureConfigured(FEATURE_RX_SERIAL
) || featureConfigured(FEATURE_RX_MSP
))) {
805 featureSet(DEFAULT_RX_FEATURE
);
808 if (featureConfigured(FEATURE_RX_PPM
)) {
809 featureClear(FEATURE_RX_PARALLEL_PWM
);
812 if (featureConfigured(FEATURE_RX_MSP
)) {
813 featureClear(FEATURE_RX_SERIAL
);
814 featureClear(FEATURE_RX_PARALLEL_PWM
);
815 featureClear(FEATURE_RX_PPM
);
818 if (featureConfigured(FEATURE_RX_SERIAL
)) {
819 featureClear(FEATURE_RX_PARALLEL_PWM
);
820 featureClear(FEATURE_RX_PPM
);
823 if (featureConfigured(FEATURE_RX_PARALLEL_PWM
)) {
824 #if defined(STM32F10X)
825 // rssi adc needs the same ports
826 featureClear(FEATURE_RSSI_ADC
);
827 // current meter needs the same ports
828 if (masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
) {
829 featureClear(FEATURE_CURRENT_METER
);
833 #if defined(STM32F10X) || defined(CHEBUZZ) || defined(STM32F3DISCOVERY)
834 // led strip needs the same ports
835 featureClear(FEATURE_LED_STRIP
);
838 // software serial needs free PWM ports
839 featureClear(FEATURE_SOFTSERIAL
);
843 #if defined(LED_STRIP) && (defined(USE_SOFTSERIAL1) || defined(USE_SOFTSERIAL2))
844 if (featureConfigured(FEATURE_SOFTSERIAL
) && (
846 #ifdef USE_SOFTSERIAL1
847 || (WS2811_TIMER
== SOFTSERIAL_1_TIMER
)
849 #ifdef USE_SOFTSERIAL2
850 || (WS2811_TIMER
== SOFTSERIAL_2_TIMER
)
853 // led strip needs the same timer as softserial
854 featureClear(FEATURE_LED_STRIP
);
858 #if defined(NAZE) && defined(SONAR)
859 if (featureConfigured(FEATURE_RX_PARALLEL_PWM
) && featureConfigured(FEATURE_SONAR
) && featureConfigured(FEATURE_CURRENT_METER
) && masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
) {
860 featureClear(FEATURE_CURRENT_METER
);
864 #if defined(OLIMEXINO) && defined(SONAR)
865 if (feature(FEATURE_SONAR
) && feature(FEATURE_CURRENT_METER
) && masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
) {
866 featureClear(FEATURE_CURRENT_METER
);
870 #if defined(CC3D) && defined(DISPLAY) && defined(USE_USART3)
871 if (doesConfigurationUsePort(SERIAL_PORT_USART3
) && feature(FEATURE_DISPLAY
)) {
872 featureClear(FEATURE_DISPLAY
);
877 // hardware supports serial port inversion, make users life easier for those that want to connect SBus RX's
878 masterConfig
.telemetryConfig
.telemetry_inversion
= 1;
882 /*#if defined(LED_STRIP) && defined(TRANSPONDER) // TODO - Add transponder feature
883 if ((WS2811_DMA_TC_FLAG == TRANSPONDER_DMA_TC_FLAG) && featureConfigured(FEATURE_TRANSPONDER) && featureConfigured(FEATURE_LED_STRIP)) {
884 featureClear(FEATURE_LED_STRIP);
889 #if defined(CC3D) && defined(SONAR) && defined(USE_SOFTSERIAL1) && defined(RSSI_ADC_GPIO)
891 if ((featureConfigured(FEATURE_SONAR
) + featureConfigured(FEATURE_SOFTSERIAL
) + featureConfigured(FEATURE_RSSI_ADC
)) > 1) {
892 featureClear(FEATURE_SONAR
);
893 featureClear(FEATURE_SOFTSERIAL
);
894 featureClear(FEATURE_RSSI_ADC
);
898 #if defined(COLIBRI_RACE)
899 masterConfig
.serialConfig
.portConfigs
[0].functionMask
= FUNCTION_MSP
;
900 if (featureConfigured(FEATURE_RX_PARALLEL_PWM
) || featureConfigured(FEATURE_RX_MSP
)) {
901 featureClear(FEATURE_RX_PARALLEL_PWM
);
902 featureClear(FEATURE_RX_MSP
);
903 featureSet(FEATURE_RX_PPM
);
907 useRxConfig(&masterConfig
.rxConfig
);
909 serialConfig_t
*serialConfig
= &masterConfig
.serialConfig
;
911 if (!isSerialConfigValid(serialConfig
)) {
912 resetSerialConfig(serialConfig
);
916 void initEEPROM(void)
920 void readEEPROM(void)
923 if (!isEEPROMContentValid())
924 failureMode(FAILURE_INVALID_EEPROM_CONTENTS
);
929 memcpy(&masterConfig
, (char *) CONFIG_START_FLASH_ADDRESS
, sizeof(master_t
));
931 if (masterConfig
.current_profile_index
> MAX_PROFILE_COUNT
- 1) // sanity check
932 masterConfig
.current_profile_index
= 0;
934 setProfile(masterConfig
.current_profile_index
);
936 validateAndFixConfig();
942 void readEEPROMAndNotify(void)
944 // re-read written data
946 beeperConfirmationBeeps(1);
949 void writeEEPROM(void)
951 // Generate compile time error if the config does not fit in the reserved area of flash.
952 BUILD_BUG_ON(sizeof(master_t
) > FLASH_TO_RESERVE_FOR_CONFIG
);
954 FLASH_Status status
= 0;
956 int8_t attemptsRemaining
= 3;
960 // prepare checksum/version constants
961 masterConfig
.version
= EEPROM_CONF_VERSION
;
962 masterConfig
.size
= sizeof(master_t
);
963 masterConfig
.magic_be
= 0xBE;
964 masterConfig
.magic_ef
= 0xEF;
965 masterConfig
.chk
= 0; // erase checksum before recalculating
966 masterConfig
.chk
= calculateChecksum((const uint8_t *) &masterConfig
, sizeof(master_t
));
970 while (attemptsRemaining
--) {
972 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR
);
973 #elif defined(STM32F303)
974 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPERR
);
975 #elif defined(STM32F10X)
976 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPRTERR
);
978 for (wordOffset
= 0; wordOffset
< sizeof(master_t
); wordOffset
+= 4) {
979 if (wordOffset
% FLASH_PAGE_SIZE
== 0) {
980 #if defined(STM32F40_41xxx)
981 status
= FLASH_EraseSector(FLASH_Sector_8
, VoltageRange_3
); //0x08080000 to 0x080A0000
982 #elif defined (STM32F411xE)
983 status
= FLASH_EraseSector(FLASH_Sector_7
, VoltageRange_3
); //0x08060000 to 0x08080000
985 status
= FLASH_ErasePage(CONFIG_START_FLASH_ADDRESS
+ wordOffset
);
987 if (status
!= FLASH_COMPLETE
) {
992 status
= FLASH_ProgramWord(CONFIG_START_FLASH_ADDRESS
+ wordOffset
,
993 *(uint32_t *) ((char *) &masterConfig
+ wordOffset
));
994 if (status
!= FLASH_COMPLETE
) {
998 if (status
== FLASH_COMPLETE
) {
1004 // Flash write failed - just die now
1005 if (status
!= FLASH_COMPLETE
|| !isEEPROMContentValid()) {
1006 failureMode(FAILURE_FLASH_WRITE_FAILED
);
1012 void ensureEEPROMContainsValidData(void)
1014 if (isEEPROMContentValid()) {
1021 void resetEEPROM(void)
1027 void saveConfigAndNotify(void)
1030 readEEPROMAndNotify();
1033 void changeProfile(uint8_t profileIndex
)
1035 masterConfig
.current_profile_index
= profileIndex
;
1038 beeperConfirmationBeeps(profileIndex
+ 1);
1041 void changeControlRateProfile(uint8_t profileIndex
)
1043 if (profileIndex
> MAX_RATEPROFILES
) {
1044 profileIndex
= MAX_RATEPROFILES
- 1;
1046 setControlRateProfile(profileIndex
);
1047 activateControlRateConfig();
1050 void latchActiveFeatures()
1052 activeFeaturesLatch
= masterConfig
.enabledFeatures
;
1055 bool featureConfigured(uint32_t mask
)
1057 return masterConfig
.enabledFeatures
& mask
;
1060 bool feature(uint32_t mask
)
1062 return activeFeaturesLatch
& mask
;
1065 void featureSet(uint32_t mask
)
1067 masterConfig
.enabledFeatures
|= mask
;
1070 void featureClear(uint32_t mask
)
1072 masterConfig
.enabledFeatures
&= ~(mask
);
1075 void featureClearAll()
1077 masterConfig
.enabledFeatures
= 0;
1080 uint32_t featureMask(void)
1082 return masterConfig
.enabledFeatures
;
1085 void beeperOffSet(uint32_t mask
)
1087 masterConfig
.beeper_off_flags
|= mask
;
1090 void beeperOffSetAll(uint8_t beeperCount
)
1092 masterConfig
.beeper_off_flags
= (1 << beeperCount
) -1;
1095 void beeperOffClear(uint32_t mask
)
1097 masterConfig
.beeper_off_flags
&= ~(mask
);
1100 void beeperOffClearAll(void)
1102 masterConfig
.beeper_off_flags
= 0;
1105 uint32_t getBeeperOffMask(void)
1107 return masterConfig
.beeper_off_flags
;
1110 void setBeeperOffMask(uint32_t mask
)
1112 masterConfig
.beeper_off_flags
= mask
;
1115 uint32_t getPreferredBeeperOffMask(void)
1117 return masterConfig
.preferred_beeper_off_flags
;
1120 void setPreferredBeeperOffMask(uint32_t mask
)
1122 masterConfig
.preferred_beeper_off_flags
= mask
;