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 "common/color.h"
27 #include "common/axis.h"
28 #include "common/maths.h"
29 #include "common/filter.h"
31 #include "drivers/sensor.h"
32 #include "drivers/accgyro.h"
33 #include "drivers/compass.h"
34 #include "drivers/system.h"
35 #include "drivers/gpio.h"
36 #include "drivers/timer.h"
37 #include "drivers/pwm_rx.h"
38 #include "drivers/serial.h"
39 #include "drivers/gyro_sync.h"
41 #include "sensors/sensors.h"
42 #include "sensors/gyro.h"
43 #include "sensors/compass.h"
44 #include "sensors/acceleration.h"
45 #include "sensors/barometer.h"
46 #include "sensors/boardalignment.h"
47 #include "sensors/battery.h"
49 #include "io/beeper.h"
50 #include "io/serial.h"
51 #include "io/gimbal.h"
52 #include "io/escservo.h"
53 #include "io/rc_controls.h"
54 #include "io/rc_curves.h"
55 #include "io/ledstrip.h"
60 #include "telemetry/telemetry.h"
62 #include "flight/mixer.h"
63 #include "flight/pid.h"
64 #include "flight/imu.h"
65 #include "flight/failsafe.h"
66 #include "flight/altitudehold.h"
67 #include "flight/navigation.h"
69 #include "config/runtime_config.h"
70 #include "config/config.h"
72 #include "config/config_profile.h"
73 #include "config/config_master.h"
75 #define BRUSHED_MOTORS_PWM_RATE 16000
76 #define BRUSHLESS_MOTORS_PWM_RATE 400
78 void useRcControlsConfig(modeActivationCondition_t
*modeActivationConditions
, escAndServoConfig_t
*escAndServoConfigToUse
, pidProfile_t
*pidProfileToUse
);
80 #if !defined(FLASH_SIZE)
81 #error "Flash size not defined for target. (specify in KB)"
85 #ifndef FLASH_PAGE_SIZE
87 #define FLASH_PAGE_SIZE ((uint16_t)0x800)
91 #define FLASH_PAGE_SIZE ((uint16_t)0x400)
95 #define FLASH_PAGE_SIZE ((uint16_t)0x800)
99 #if !defined(FLASH_SIZE) && !defined(FLASH_PAGE_COUNT)
101 #define FLASH_PAGE_COUNT 128
105 #define FLASH_PAGE_COUNT 128
109 #if defined(FLASH_SIZE)
110 #define FLASH_PAGE_COUNT ((FLASH_SIZE * 0x400) / FLASH_PAGE_SIZE)
113 #if !defined(FLASH_PAGE_SIZE)
114 #error "Flash page size not defined for target."
117 #if !defined(FLASH_PAGE_COUNT)
118 #error "Flash page count not defined for target."
121 #if FLASH_SIZE <= 128
122 #define FLASH_TO_RESERVE_FOR_CONFIG 0x800
124 #define FLASH_TO_RESERVE_FOR_CONFIG 0x1000
127 // use the last flash pages for storage
128 #define CONFIG_START_FLASH_ADDRESS (0x08000000 + (uint32_t)((FLASH_PAGE_SIZE * FLASH_PAGE_COUNT) - FLASH_TO_RESERVE_FOR_CONFIG))
130 master_t masterConfig
; // master config struct with data independent from profiles
131 profile_t
*currentProfile
;
132 static uint32_t activeFeaturesLatch
= 0;
134 static uint8_t currentControlRateProfileIndex
= 0;
135 controlRateConfig_t
*currentControlRateProfile
;
137 static const uint8_t EEPROM_CONF_VERSION
= 123;
139 static void resetAccelerometerTrims(flightDynamicsTrims_t
*accelerometerTrims
)
141 accelerometerTrims
->values
.pitch
= 0;
142 accelerometerTrims
->values
.roll
= 0;
143 accelerometerTrims
->values
.yaw
= 0;
146 static void resetPidProfile(pidProfile_t
*pidProfile
)
148 pidProfile
->pidController
= 1;
150 pidProfile
->P8
[ROLL
] = 42;
151 pidProfile
->I8
[ROLL
] = 40;
152 pidProfile
->D8
[ROLL
] = 13;
153 pidProfile
->P8
[PITCH
] = 54;
154 pidProfile
->I8
[PITCH
] = 40;
155 pidProfile
->D8
[PITCH
] = 18;
156 pidProfile
->P8
[YAW
] = 90;
157 pidProfile
->I8
[YAW
] = 50;
158 pidProfile
->D8
[YAW
] = 0;
159 pidProfile
->P8
[PIDALT
] = 50;
160 pidProfile
->I8
[PIDALT
] = 0;
161 pidProfile
->D8
[PIDALT
] = 0;
162 pidProfile
->P8
[PIDPOS
] = 15; // POSHOLD_P * 100;
163 pidProfile
->I8
[PIDPOS
] = 0; // POSHOLD_I * 100;
164 pidProfile
->D8
[PIDPOS
] = 0;
165 pidProfile
->P8
[PIDPOSR
] = 34; // POSHOLD_RATE_P * 10;
166 pidProfile
->I8
[PIDPOSR
] = 14; // POSHOLD_RATE_I * 100;
167 pidProfile
->D8
[PIDPOSR
] = 53; // POSHOLD_RATE_D * 1000;
168 pidProfile
->P8
[PIDNAVR
] = 25; // NAV_P * 10;
169 pidProfile
->I8
[PIDNAVR
] = 33; // NAV_I * 100;
170 pidProfile
->D8
[PIDNAVR
] = 83; // NAV_D * 1000;
171 pidProfile
->P8
[PIDLEVEL
] = 30;
172 pidProfile
->I8
[PIDLEVEL
] = 30;
173 pidProfile
->D8
[PIDLEVEL
] = 100;
174 pidProfile
->P8
[PIDMAG
] = 40;
175 pidProfile
->P8
[PIDVEL
] = 120;
176 pidProfile
->I8
[PIDVEL
] = 45;
177 pidProfile
->D8
[PIDVEL
] = 1;
179 pidProfile
->yaw_p_limit
= YAW_P_LIMIT_MAX
;
180 pidProfile
->dterm_average_count
= 3;
181 pidProfile
->dterm_lpf_hz
= 0; // filtering ON by default
182 pidProfile
->deltaMethod
= DELTA_FROM_MEASUREMENT
;
183 pidProfile
->airModeInsaneAcrobilityFactor
= 0;
185 pidProfile
->P_f
[ROLL
] = 1.1f
;
186 pidProfile
->I_f
[ROLL
] = 0.4f
;
187 pidProfile
->D_f
[ROLL
] = 0.01f
;
188 pidProfile
->P_f
[PITCH
] = 1.4f
;
189 pidProfile
->I_f
[PITCH
] = 0.4f
;
190 pidProfile
->D_f
[PITCH
] = 0.01f
;
191 pidProfile
->P_f
[YAW
] = 4.0f
;
192 pidProfile
->I_f
[YAW
] = 0.4f
;
193 pidProfile
->D_f
[YAW
] = 0.00f
;
194 pidProfile
->A_level
= 4.0f
;
195 pidProfile
->H_level
= 4.0f
;
196 pidProfile
->H_sensitivity
= 75;
199 pidProfile
->gtune_lolimP
[ROLL
] = 10; // [0..200] Lower limit of ROLL P during G tune.
200 pidProfile
->gtune_lolimP
[PITCH
] = 10; // [0..200] Lower limit of PITCH P during G tune.
201 pidProfile
->gtune_lolimP
[YAW
] = 10; // [0..200] Lower limit of YAW P during G tune.
202 pidProfile
->gtune_hilimP
[ROLL
] = 100; // [0..200] Higher limit of ROLL P during G tune. 0 Disables tuning for that axis.
203 pidProfile
->gtune_hilimP
[PITCH
] = 100; // [0..200] Higher limit of PITCH P during G tune. 0 Disables tuning for that axis.
204 pidProfile
->gtune_hilimP
[YAW
] = 100; // [0..200] Higher limit of YAW P during G tune. 0 Disables tuning for that axis.
205 pidProfile
->gtune_pwr
= 0; // [0..10] Strength of adjustment
206 pidProfile
->gtune_settle_time
= 450; // [200..1000] Settle time in ms
207 pidProfile
->gtune_average_cycles
= 16; // [8..128] Number of looptime cycles used for gyro average calculation
212 void resetGpsProfile(gpsProfile_t
*gpsProfile
)
214 gpsProfile
->gps_wp_radius
= 200;
215 gpsProfile
->gps_lpf
= 20;
216 gpsProfile
->nav_slew_rate
= 30;
217 gpsProfile
->nav_controls_heading
= 1;
218 gpsProfile
->nav_speed_min
= 100;
219 gpsProfile
->nav_speed_max
= 300;
220 gpsProfile
->ap_mode
= 40;
224 void resetBarometerConfig(barometerConfig_t
*barometerConfig
)
226 barometerConfig
->baro_sample_count
= 21;
227 barometerConfig
->baro_noise_lpf
= 0.6f
;
228 barometerConfig
->baro_cf_vel
= 0.985f
;
229 barometerConfig
->baro_cf_alt
= 0.965f
;
232 void resetSensorAlignment(sensorAlignmentConfig_t
*sensorAlignmentConfig
)
234 sensorAlignmentConfig
->gyro_align
= ALIGN_DEFAULT
;
235 sensorAlignmentConfig
->acc_align
= ALIGN_DEFAULT
;
236 sensorAlignmentConfig
->mag_align
= ALIGN_DEFAULT
;
239 void resetEscAndServoConfig(escAndServoConfig_t
*escAndServoConfig
)
241 escAndServoConfig
->minthrottle
= 1150;
242 escAndServoConfig
->maxthrottle
= 1850;
243 escAndServoConfig
->mincommand
= 1000;
244 escAndServoConfig
->servoCenterPulse
= 1500;
247 void resetFlight3DConfig(flight3DConfig_t
*flight3DConfig
)
249 flight3DConfig
->deadband3d_low
= 1406;
250 flight3DConfig
->deadband3d_high
= 1514;
251 flight3DConfig
->neutral3d
= 1460;
252 flight3DConfig
->deadband3d_throttle
= 50;
255 void resetTelemetryConfig(telemetryConfig_t
*telemetryConfig
)
257 telemetryConfig
->telemetry_inversion
= 0;
258 telemetryConfig
->telemetry_switch
= 0;
259 telemetryConfig
->gpsNoFixLatitude
= 0;
260 telemetryConfig
->gpsNoFixLongitude
= 0;
261 telemetryConfig
->frsky_coordinate_format
= FRSKY_FORMAT_DMS
;
262 telemetryConfig
->frsky_unit
= FRSKY_UNIT_METRICS
;
263 telemetryConfig
->frsky_vfas_precision
= 0;
264 telemetryConfig
->hottAlarmSoundInterval
= 5;
267 void resetBatteryConfig(batteryConfig_t
*batteryConfig
)
269 batteryConfig
->vbatscale
= VBAT_SCALE_DEFAULT
;
270 batteryConfig
->vbatresdivval
= VBAT_RESDIVVAL_DEFAULT
;
271 batteryConfig
->vbatresdivmultiplier
= VBAT_RESDIVMULTIPLIER_DEFAULT
;
272 batteryConfig
->vbatmaxcellvoltage
= 43;
273 batteryConfig
->vbatmincellvoltage
= 33;
274 batteryConfig
->vbatwarningcellvoltage
= 35;
275 batteryConfig
->vbatPidCompensation
= 0;
276 batteryConfig
->currentMeterOffset
= 0;
277 batteryConfig
->currentMeterScale
= 400; // for Allegro ACS758LCB-100U (40mV/A)
278 batteryConfig
->batteryCapacity
= 0;
279 batteryConfig
->currentMeterType
= CURRENT_SENSOR_ADC
;
282 #ifdef SWAP_SERIAL_PORT_0_AND_1_DEFAULTS
283 #define FIRST_PORT_INDEX 1
284 #define SECOND_PORT_INDEX 0
286 #define FIRST_PORT_INDEX 0
287 #define SECOND_PORT_INDEX 1
290 void resetSerialConfig(serialConfig_t
*serialConfig
)
293 memset(serialConfig
, 0, sizeof(serialConfig_t
));
295 for (index
= 0; index
< SERIAL_PORT_COUNT
; index
++) {
296 serialConfig
->portConfigs
[index
].identifier
= serialPortIdentifiers
[index
];
297 serialConfig
->portConfigs
[index
].msp_baudrateIndex
= BAUD_115200
;
298 serialConfig
->portConfigs
[index
].gps_baudrateIndex
= BAUD_57600
;
299 serialConfig
->portConfigs
[index
].telemetry_baudrateIndex
= BAUD_AUTO
;
300 serialConfig
->portConfigs
[index
].blackbox_baudrateIndex
= BAUD_115200
;
303 serialConfig
->portConfigs
[0].functionMask
= FUNCTION_MSP
;
306 // This allows MSP connection via USART & VCP so the board can be reconfigured.
307 serialConfig
->portConfigs
[1].functionMask
= FUNCTION_MSP
;
310 serialConfig
->reboot_character
= 'R';
313 static void resetControlRateConfig(controlRateConfig_t
*controlRateConfig
) {
314 controlRateConfig
->rcRate8
= 100;
315 controlRateConfig
->rcExpo8
= 70;
316 controlRateConfig
->thrMid8
= 50;
317 controlRateConfig
->thrExpo8
= 0;
318 controlRateConfig
->dynThrPID
= 0;
319 controlRateConfig
->rcYawExpo8
= 20;
320 controlRateConfig
->tpa_breakpoint
= 1500;
322 for (uint8_t axis
= 0; axis
< FLIGHT_DYNAMICS_INDEX_COUNT
; axis
++) {
323 controlRateConfig
->rates
[axis
] = 0;
328 void resetRcControlsConfig(rcControlsConfig_t
*rcControlsConfig
) {
329 rcControlsConfig
->deadband
= 0;
330 rcControlsConfig
->yaw_deadband
= 0;
331 rcControlsConfig
->alt_hold_deadband
= 40;
332 rcControlsConfig
->alt_hold_fast_change
= 1;
335 void resetMixerConfig(mixerConfig_t
*mixerConfig
) {
336 mixerConfig
->yaw_motor_direction
= 1;
337 mixerConfig
->airmode_saturation_limit
= 50;
338 mixerConfig
->yaw_jump_prevention_limit
= 200;
340 mixerConfig
->tri_unarmed_servo
= 1;
341 mixerConfig
->servo_lowpass_freq
= 400;
342 mixerConfig
->servo_lowpass_enable
= 0;
346 uint8_t getCurrentProfile(void)
348 return masterConfig
.current_profile_index
;
351 static void setProfile(uint8_t profileIndex
)
353 currentProfile
= &masterConfig
.profile
[profileIndex
];
354 currentControlRateProfile
= ¤tProfile
->controlRateProfile
;
357 uint8_t getCurrentControlRateProfile(void)
359 return currentControlRateProfileIndex
;
362 controlRateConfig_t
*getControlRateConfig(uint8_t profileIndex
) {
363 return &masterConfig
.profile
[profileIndex
].controlRateProfile
;
366 uint16_t getCurrentMinthrottle(void)
368 return masterConfig
.escAndServoConfig
.minthrottle
;
372 static void resetConf(void)
376 // Clear all configuration
377 memset(&masterConfig
, 0, sizeof(master_t
));
380 masterConfig
.beeper_off
.flags
= BEEPER_OFF_FLAGS_MIN
;
381 masterConfig
.version
= EEPROM_CONF_VERSION
;
382 masterConfig
.mixerMode
= MIXER_QUADX
;
384 #if defined(CJMCU) || defined(SPARKY) || defined(COLIBRI_RACE) || defined(MOTOLAB) || defined(SPRACINGF3MINI) || defined(LUX_RACE)
385 featureSet(FEATURE_RX_PPM
);
388 //#if defined(SPRACINGF3MINI)
389 // featureSet(FEATURE_DISPLAY);
392 #ifdef BOARD_HAS_VOLTAGE_DIVIDER
393 // only enable the VBAT feature by default if the board has a voltage divider otherwise
394 // the user may see incorrect readings and unexpected issues with pin mappings may occur.
395 featureSet(FEATURE_VBAT
);
398 featureSet(FEATURE_FAILSAFE
);
399 featureSet(FEATURE_ONESHOT125
);
402 masterConfig
.current_profile_index
= 0; // default profile
403 masterConfig
.dcm_kp
= 2500; // 1.0 * 10000
404 masterConfig
.dcm_ki
= 0; // 0.003 * 10000
405 masterConfig
.gyro_lpf
= 1; // 188HZ
406 masterConfig
.gyro_sync_denom
= 1;
407 masterConfig
.gyro_soft_lpf_hz
= 60;
409 resetAccelerometerTrims(&masterConfig
.accZero
);
411 resetSensorAlignment(&masterConfig
.sensorAlignmentConfig
);
413 masterConfig
.boardAlignment
.rollDegrees
= 0;
414 masterConfig
.boardAlignment
.pitchDegrees
= 0;
415 masterConfig
.boardAlignment
.yawDegrees
= 0;
416 masterConfig
.acc_hardware
= ACC_DEFAULT
; // default/autodetect
417 masterConfig
.max_angle_inclination
= 700; // 70 degrees
418 masterConfig
.yaw_control_direction
= 1;
419 masterConfig
.gyroConfig
.gyroMovementCalibrationThreshold
= 32;
421 // xxx_hardware: 0:default/autodetect, 1: disable
422 masterConfig
.mag_hardware
= 0;
424 masterConfig
.baro_hardware
= 0;
426 resetBatteryConfig(&masterConfig
.batteryConfig
);
428 resetTelemetryConfig(&masterConfig
.telemetryConfig
);
430 masterConfig
.rxConfig
.serialrx_provider
= 0;
431 masterConfig
.rxConfig
.spektrum_sat_bind
= 0;
432 masterConfig
.rxConfig
.midrc
= 1500;
433 masterConfig
.rxConfig
.mincheck
= 1100;
434 masterConfig
.rxConfig
.maxcheck
= 1900;
435 masterConfig
.rxConfig
.rx_min_usec
= 885; // any of first 4 channels below this value will trigger rx loss detection
436 masterConfig
.rxConfig
.rx_max_usec
= 2115; // any of first 4 channels above this value will trigger rx loss detection
438 for (i
= 0; i
< MAX_SUPPORTED_RC_CHANNEL_COUNT
; i
++) {
439 rxFailsafeChannelConfiguration_t
*channelFailsafeConfiguration
= &masterConfig
.rxConfig
.failsafe_channel_configurations
[i
];
440 channelFailsafeConfiguration
->mode
= (i
< NON_AUX_CHANNEL_COUNT
) ? RX_FAILSAFE_MODE_AUTO
: RX_FAILSAFE_MODE_HOLD
;
441 channelFailsafeConfiguration
->step
= (i
== THROTTLE
) ? CHANNEL_VALUE_TO_RXFAIL_STEP(masterConfig
.rxConfig
.rx_min_usec
) : CHANNEL_VALUE_TO_RXFAIL_STEP(masterConfig
.rxConfig
.midrc
);
444 masterConfig
.rxConfig
.rssi_channel
= 0;
445 masterConfig
.rxConfig
.rssi_scale
= RSSI_SCALE_DEFAULT
;
446 masterConfig
.rxConfig
.rssi_ppm_invert
= 0;
447 masterConfig
.rxConfig
.rcSmoothing
= 0;
448 masterConfig
.rxConfig
.fpvCamAngleDegrees
= 0;
449 masterConfig
.rxConfig
.max_aux_channel
= 4;
451 resetAllRxChannelRangeConfigurations(masterConfig
.rxConfig
.channelRanges
);
453 masterConfig
.inputFilteringMode
= INPUT_FILTERING_DISABLED
;
455 masterConfig
.retarded_arm
= 0; // TODO - Cleanup retarded arm support
456 masterConfig
.disarm_kill_switch
= 1;
457 masterConfig
.auto_disarm_delay
= 5;
458 masterConfig
.small_angle
= 25;
460 resetMixerConfig(&masterConfig
.mixerConfig
);
462 masterConfig
.airplaneConfig
.fixedwing_althold_dir
= 1;
465 resetEscAndServoConfig(&masterConfig
.escAndServoConfig
);
466 resetFlight3DConfig(&masterConfig
.flight3DConfig
);
468 #ifdef BRUSHED_MOTORS
469 masterConfig
.motor_pwm_rate
= BRUSHED_MOTORS_PWM_RATE
;
471 masterConfig
.motor_pwm_rate
= BRUSHLESS_MOTORS_PWM_RATE
;
473 masterConfig
.servo_pwm_rate
= 50;
474 masterConfig
.use_fast_pwm
= 0;
476 masterConfig
.use_buzzer_p6
= 0;
481 masterConfig
.gpsConfig
.provider
= GPS_NMEA
;
482 masterConfig
.gpsConfig
.sbasMode
= SBAS_AUTO
;
483 masterConfig
.gpsConfig
.autoConfig
= GPS_AUTOCONFIG_ON
;
484 masterConfig
.gpsConfig
.autoBaud
= GPS_AUTOBAUD_OFF
;
487 resetSerialConfig(&masterConfig
.serialConfig
);
489 masterConfig
.emf_avoidance
= 0;
491 resetPidProfile(¤tProfile
->pidProfile
);
493 resetControlRateConfig(&masterConfig
.profile
[0].controlRateProfile
);
495 resetRollAndPitchTrims(&masterConfig
.accelerometerTrims
);
497 masterConfig
.mag_declination
= 0;
498 masterConfig
.acc_lpf_hz
= 10.0f
;
499 masterConfig
.accDeadband
.xy
= 40;
500 masterConfig
.accDeadband
.z
= 40;
501 masterConfig
.acc_unarmedcal
= 1;
503 resetBarometerConfig(&masterConfig
.barometerConfig
);
506 parseRcChannels("AETR1234", &masterConfig
.rxConfig
);
508 resetRcControlsConfig(&masterConfig
.rcControlsConfig
);
510 masterConfig
.throttle_correction_value
= 0; // could 10 with althold or 40 for fpv
511 masterConfig
.throttle_correction_angle
= 800; // could be 80.0 deg with atlhold or 45.0 for fpv
513 // Failsafe Variables
514 masterConfig
.failsafeConfig
.failsafe_delay
= 10; // 1sec
515 masterConfig
.failsafeConfig
.failsafe_off_delay
= 10; // 1sec
516 masterConfig
.failsafeConfig
.failsafe_throttle
= 1000; // default throttle off.
517 masterConfig
.failsafeConfig
.failsafe_kill_switch
= 0; // default failsafe switch action is identical to rc link loss
518 masterConfig
.failsafeConfig
.failsafe_throttle_low_delay
= 100; // default throttle low delay for "just disarm" on failsafe condition
519 masterConfig
.failsafeConfig
.failsafe_procedure
= 0; // default full failsafe procedure is 0: auto-landing
523 for (i
= 0; i
< MAX_SUPPORTED_SERVOS
; i
++) {
524 masterConfig
.servoConf
[i
].min
= DEFAULT_SERVO_MIN
;
525 masterConfig
.servoConf
[i
].max
= DEFAULT_SERVO_MAX
;
526 masterConfig
.servoConf
[i
].middle
= DEFAULT_SERVO_MIDDLE
;
527 masterConfig
.servoConf
[i
].rate
= 100;
528 masterConfig
.servoConf
[i
].angleAtMin
= DEFAULT_SERVO_MIN_ANGLE
;
529 masterConfig
.servoConf
[i
].angleAtMax
= DEFAULT_SERVO_MAX_ANGLE
;
530 masterConfig
.servoConf
[i
].forwardFromChannel
= CHANNEL_FORWARDING_DISABLED
;
534 masterConfig
.gimbalConfig
.mode
= GIMBAL_MODE_NORMAL
;
538 resetGpsProfile(&masterConfig
.gpsProfile
);
541 // custom mixer. clear by defaults.
542 for (i
= 0; i
< MAX_SUPPORTED_MOTORS
; i
++)
543 masterConfig
.customMotorMixer
[i
].throttle
= 0.0f
;
546 applyDefaultColors(masterConfig
.colors
, CONFIGURABLE_COLOR_COUNT
);
547 applyDefaultLedStripConfig(masterConfig
.ledConfigs
);
551 featureSet(FEATURE_BLACKBOX
);
552 masterConfig
.blackbox_device
= 1;
554 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
556 memcpy(masterConfig
.transponderData
, &defaultTransponderData
, sizeof(defaultTransponderData
));
559 #if defined(ENABLE_BLACKBOX_LOGGING_ON_SPIFLASH_BY_DEFAULT)
560 featureSet(FEATURE_BLACKBOX
);
561 masterConfig
.blackbox_device
= BLACKBOX_DEVICE_FLASH
;
562 #elif defined(ENABLE_BLACKBOX_LOGGING_ON_SDCARD_BY_DEFAULT)
563 featureSet(FEATURE_BLACKBOX
);
564 masterConfig
.blackbox_device
= BLACKBOX_DEVICE_SDCARD
;
566 masterConfig
.blackbox_device
= 0;
569 masterConfig
.blackbox_rate_num
= 1;
570 masterConfig
.blackbox_rate_denom
= 1;
573 // alternative defaults settings for COLIBRI RACE targets
574 #if defined(COLIBRI_RACE)
575 masterConfig
.escAndServoConfig
.minthrottle
= 1025;
576 masterConfig
.escAndServoConfig
.maxthrottle
= 1980;
577 masterConfig
.batteryConfig
.vbatmaxcellvoltage
= 45;
578 masterConfig
.batteryConfig
.vbatmincellvoltage
= 30;
580 featureSet(FEATURE_VBAT
);
581 featureSet(FEATURE_FAILSAFE
);
584 // alternative defaults settings for ALIENFLIGHTF1 and ALIENFLIGHTF3 targets
586 featureSet(FEATURE_RX_SERIAL
);
587 featureSet(FEATURE_MOTOR_STOP
);
588 featureClear(FEATURE_ONESHOT125
);
590 masterConfig
.serialConfig
.portConfigs
[2].functionMask
= FUNCTION_RX_SERIAL
;
591 masterConfig
.batteryConfig
.vbatscale
= 20;
592 masterConfig
.mag_hardware
= MAG_NONE
; // disabled by default
594 masterConfig
.serialConfig
.portConfigs
[1].functionMask
= FUNCTION_RX_SERIAL
;
596 masterConfig
.rxConfig
.serialrx_provider
= 1;
597 masterConfig
.rxConfig
.spektrum_sat_bind
= 5;
598 masterConfig
.escAndServoConfig
.minthrottle
= 1000;
599 masterConfig
.escAndServoConfig
.maxthrottle
= 2000;
600 masterConfig
.motor_pwm_rate
= 32000;
601 currentProfile
->pidProfile
.pidController
= 2;
602 masterConfig
.failsafeConfig
.failsafe_delay
= 2;
603 masterConfig
.failsafeConfig
.failsafe_off_delay
= 0;
604 masterConfig
.mixerConfig
.yaw_jump_prevention_limit
= 500;
605 currentControlRateProfile
->rcRate8
= 100;
606 currentControlRateProfile
->rates
[FD_PITCH
] = 20;
607 currentControlRateProfile
->rates
[FD_ROLL
] = 20;
608 currentControlRateProfile
->rates
[FD_YAW
] = 20;
609 parseRcChannels("TAER1234", &masterConfig
.rxConfig
);
611 // { 1.0f, -0.414178f, 1.0f, -1.0f }, // REAR_R
612 masterConfig
.customMotorMixer
[0].throttle
= 1.0f
;
613 masterConfig
.customMotorMixer
[0].roll
= -0.414178f
;
614 masterConfig
.customMotorMixer
[0].pitch
= 1.0f
;
615 masterConfig
.customMotorMixer
[0].yaw
= -1.0f
;
617 // { 1.0f, -0.414178f, -1.0f, 1.0f }, // FRONT_R
618 masterConfig
.customMotorMixer
[1].throttle
= 1.0f
;
619 masterConfig
.customMotorMixer
[1].roll
= -0.414178f
;
620 masterConfig
.customMotorMixer
[1].pitch
= -1.0f
;
621 masterConfig
.customMotorMixer
[1].yaw
= 1.0f
;
623 // { 1.0f, 0.414178f, 1.0f, 1.0f }, // REAR_L
624 masterConfig
.customMotorMixer
[2].throttle
= 1.0f
;
625 masterConfig
.customMotorMixer
[2].roll
= 0.414178f
;
626 masterConfig
.customMotorMixer
[2].pitch
= 1.0f
;
627 masterConfig
.customMotorMixer
[2].yaw
= 1.0f
;
629 // { 1.0f, 0.414178f, -1.0f, -1.0f }, // FRONT_L
630 masterConfig
.customMotorMixer
[3].throttle
= 1.0f
;
631 masterConfig
.customMotorMixer
[3].roll
= 0.414178f
;
632 masterConfig
.customMotorMixer
[3].pitch
= -1.0f
;
633 masterConfig
.customMotorMixer
[3].yaw
= -1.0f
;
635 // { 1.0f, -1.0f, -0.414178f, -1.0f }, // MIDFRONT_R
636 masterConfig
.customMotorMixer
[4].throttle
= 1.0f
;
637 masterConfig
.customMotorMixer
[4].roll
= -1.0f
;
638 masterConfig
.customMotorMixer
[4].pitch
= -0.414178f
;
639 masterConfig
.customMotorMixer
[4].yaw
= -1.0f
;
641 // { 1.0f, 1.0f, -0.414178f, 1.0f }, // MIDFRONT_L
642 masterConfig
.customMotorMixer
[5].throttle
= 1.0f
;
643 masterConfig
.customMotorMixer
[5].roll
= 1.0f
;
644 masterConfig
.customMotorMixer
[5].pitch
= -0.414178f
;
645 masterConfig
.customMotorMixer
[5].yaw
= 1.0f
;
647 // { 1.0f, -1.0f, 0.414178f, 1.0f }, // MIDREAR_R
648 masterConfig
.customMotorMixer
[6].throttle
= 1.0f
;
649 masterConfig
.customMotorMixer
[6].roll
= -1.0f
;
650 masterConfig
.customMotorMixer
[6].pitch
= 0.414178f
;
651 masterConfig
.customMotorMixer
[6].yaw
= 1.0f
;
653 // { 1.0f, 1.0f, 0.414178f, -1.0f }, // MIDREAR_L
654 masterConfig
.customMotorMixer
[7].throttle
= 1.0f
;
655 masterConfig
.customMotorMixer
[7].roll
= 1.0f
;
656 masterConfig
.customMotorMixer
[7].pitch
= 0.414178f
;
657 masterConfig
.customMotorMixer
[7].yaw
= -1.0f
;
660 // copy first profile into remaining profile
661 for (i
= 1; i
< MAX_PROFILE_COUNT
; i
++) {
662 memcpy(&masterConfig
.profile
[i
], currentProfile
, sizeof(profile_t
));
667 static uint8_t calculateChecksum(const uint8_t *data
, uint32_t length
)
669 uint8_t checksum
= 0;
670 const uint8_t *byteOffset
;
672 for (byteOffset
= data
; byteOffset
< (data
+ length
); byteOffset
++)
673 checksum
^= *byteOffset
;
677 static bool isEEPROMContentValid(void)
679 const master_t
*temp
= (const master_t
*) CONFIG_START_FLASH_ADDRESS
;
680 uint8_t checksum
= 0;
682 // check version number
683 if (EEPROM_CONF_VERSION
!= temp
->version
)
686 // check size and magic numbers
687 if (temp
->size
!= sizeof(master_t
) || temp
->magic_be
!= 0xBE || temp
->magic_ef
!= 0xEF)
690 // verify integrity of temporary copy
691 checksum
= calculateChecksum((const uint8_t *) temp
, sizeof(master_t
));
695 // looks good, let's roll!
699 void activateControlRateConfig(void)
701 generatePitchRollCurve(currentControlRateProfile
);
702 generateYawCurve(currentControlRateProfile
);
703 generateThrottleCurve(currentControlRateProfile
, &masterConfig
.escAndServoConfig
);
706 void activateConfig(void)
708 static imuRuntimeConfig_t imuRuntimeConfig
;
710 activateControlRateConfig();
712 resetAdjustmentStates();
715 masterConfig
.modeActivationConditions
,
716 &masterConfig
.escAndServoConfig
,
717 ¤tProfile
->pidProfile
720 useGyroConfig(&masterConfig
.gyroConfig
, masterConfig
.gyro_soft_lpf_hz
);
723 telemetryUseConfig(&masterConfig
.telemetryConfig
);
725 pidSetController(currentProfile
->pidProfile
.pidController
);
728 gpsUseProfile(&masterConfig
.gpsProfile
);
729 gpsUsePIDs(¤tProfile
->pidProfile
);
732 useFailsafeConfig(&masterConfig
.failsafeConfig
);
733 setAccelerationTrims(&masterConfig
.accZero
);
734 setAccelerationFilter(masterConfig
.acc_lpf_hz
);
738 masterConfig
.servoConf
,
739 &masterConfig
.gimbalConfig
,
741 &masterConfig
.flight3DConfig
,
742 &masterConfig
.escAndServoConfig
,
743 &masterConfig
.mixerConfig
,
744 &masterConfig
.airplaneConfig
,
745 &masterConfig
.rxConfig
748 imuRuntimeConfig
.dcm_kp
= masterConfig
.dcm_kp
/ 10000.0f
;
749 imuRuntimeConfig
.dcm_ki
= masterConfig
.dcm_ki
/ 10000.0f
;
750 imuRuntimeConfig
.acc_unarmedcal
= masterConfig
.acc_unarmedcal
;
751 imuRuntimeConfig
.small_angle
= masterConfig
.small_angle
;
755 ¤tProfile
->pidProfile
,
756 &masterConfig
.accDeadband
,
757 masterConfig
.throttle_correction_angle
760 configureAltitudeHold(
761 ¤tProfile
->pidProfile
,
762 &masterConfig
.barometerConfig
,
763 &masterConfig
.rcControlsConfig
,
764 &masterConfig
.escAndServoConfig
768 useBarometerConfig(&masterConfig
.barometerConfig
);
772 void validateAndFixConfig(void)
774 if (!(featureConfigured(FEATURE_RX_PARALLEL_PWM
) || featureConfigured(FEATURE_RX_PPM
) || featureConfigured(FEATURE_RX_SERIAL
) || featureConfigured(FEATURE_RX_MSP
))) {
775 featureSet(FEATURE_RX_PARALLEL_PWM
); // Consider changing the default to PPM
778 if (featureConfigured(FEATURE_RX_PPM
)) {
779 featureClear(FEATURE_RX_PARALLEL_PWM
);
782 if (featureConfigured(FEATURE_RX_MSP
)) {
783 featureClear(FEATURE_RX_SERIAL
);
784 featureClear(FEATURE_RX_PARALLEL_PWM
);
785 featureClear(FEATURE_RX_PPM
);
788 if (featureConfigured(FEATURE_RX_SERIAL
)) {
789 featureClear(FEATURE_RX_PARALLEL_PWM
);
790 featureClear(FEATURE_RX_PPM
);
793 if (featureConfigured(FEATURE_RX_PARALLEL_PWM
)) {
794 #if defined(STM32F10X)
795 // rssi adc needs the same ports
796 featureClear(FEATURE_RSSI_ADC
);
797 // current meter needs the same ports
798 if (masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
) {
799 featureClear(FEATURE_CURRENT_METER
);
803 #if defined(STM32F10X) || defined(CHEBUZZ) || defined(STM32F3DISCOVERY)
804 // led strip needs the same ports
805 featureClear(FEATURE_LED_STRIP
);
808 // software serial needs free PWM ports
809 featureClear(FEATURE_SOFTSERIAL
);
813 #if defined(LED_STRIP) && (defined(USE_SOFTSERIAL1) || defined(USE_SOFTSERIAL2))
814 if (featureConfigured(FEATURE_SOFTSERIAL
) && (
816 #ifdef USE_SOFTSERIAL1
817 || (LED_STRIP_TIMER
== SOFTSERIAL_1_TIMER
)
819 #ifdef USE_SOFTSERIAL2
820 || (LED_STRIP_TIMER
== SOFTSERIAL_2_TIMER
)
823 // led strip needs the same timer as softserial
824 featureClear(FEATURE_LED_STRIP
);
828 #if defined(NAZE) && defined(SONAR)
829 if (featureConfigured(FEATURE_RX_PARALLEL_PWM
) && featureConfigured(FEATURE_SONAR
) && featureConfigured(FEATURE_CURRENT_METER
) && masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
) {
830 featureClear(FEATURE_CURRENT_METER
);
834 #if defined(OLIMEXINO) && defined(SONAR)
835 if (feature(FEATURE_SONAR
) && feature(FEATURE_CURRENT_METER
) && masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
) {
836 featureClear(FEATURE_CURRENT_METER
);
840 #if defined(CC3D) && defined(DISPLAY) && defined(USE_USART3)
841 if (doesConfigurationUsePort(SERIAL_PORT_USART3
) && feature(FEATURE_DISPLAY
)) {
842 featureClear(FEATURE_DISPLAY
);
847 // hardware supports serial port inversion, make users life easier for those that want to connect SBus RX's
848 masterConfig
.telemetryConfig
.telemetry_inversion
= 1;
852 /*#if defined(LED_STRIP) && defined(TRANSPONDER) // TODO - Add transponder feature
853 if ((WS2811_DMA_TC_FLAG == TRANSPONDER_DMA_TC_FLAG) && featureConfigured(FEATURE_TRANSPONDER) && featureConfigured(FEATURE_LED_STRIP)) {
854 featureClear(FEATURE_LED_STRIP);
859 #if defined(CC3D) && defined(SONAR) && defined(USE_SOFTSERIAL1) && defined(RSSI_ADC_GPIO)
861 if ((featureConfigured(FEATURE_SONAR
) + featureConfigured(FEATURE_SOFTSERIAL
) + featureConfigured(FEATURE_RSSI_ADC
)) > 1) {
862 featureClear(FEATURE_SONAR
);
863 featureClear(FEATURE_SOFTSERIAL
);
864 featureClear(FEATURE_RSSI_ADC
);
868 #if defined(COLIBRI_RACE)
869 masterConfig
.serialConfig
.portConfigs
[0].functionMask
= FUNCTION_MSP
;
870 if(featureConfigured(FEATURE_RX_PARALLEL_PWM
) || featureConfigured(FEATURE_RX_MSP
)) {
871 featureClear(FEATURE_RX_PARALLEL_PWM
);
872 featureClear(FEATURE_RX_MSP
);
873 featureSet(FEATURE_RX_PPM
);
875 if(featureConfigured(FEATURE_RX_SERIAL
)) {
876 masterConfig
.serialConfig
.portConfigs
[2].functionMask
= FUNCTION_RX_SERIAL
;
877 //masterConfig.rxConfig.serialrx_provider = SERIALRX_SBUS;
881 useRxConfig(&masterConfig
.rxConfig
);
883 serialConfig_t
*serialConfig
= &masterConfig
.serialConfig
;
885 if (!isSerialConfigValid(serialConfig
)) {
886 resetSerialConfig(serialConfig
);
890 void initEEPROM(void)
894 void readEEPROM(void)
897 if (!isEEPROMContentValid())
898 failureMode(FAILURE_INVALID_EEPROM_CONTENTS
);
903 memcpy(&masterConfig
, (char *) CONFIG_START_FLASH_ADDRESS
, sizeof(master_t
));
905 if (masterConfig
.current_profile_index
> MAX_PROFILE_COUNT
- 1) // sanity check
906 masterConfig
.current_profile_index
= 0;
908 setProfile(masterConfig
.current_profile_index
);
910 validateAndFixConfig();
916 void readEEPROMAndNotify(void)
918 // re-read written data
920 beeperConfirmationBeeps(1);
923 void writeEEPROM(void)
925 // Generate compile time error if the config does not fit in the reserved area of flash.
926 BUILD_BUG_ON(sizeof(master_t
) > FLASH_TO_RESERVE_FOR_CONFIG
);
928 FLASH_Status status
= 0;
930 int8_t attemptsRemaining
= 3;
934 // prepare checksum/version constants
935 masterConfig
.version
= EEPROM_CONF_VERSION
;
936 masterConfig
.size
= sizeof(master_t
);
937 masterConfig
.magic_be
= 0xBE;
938 masterConfig
.magic_ef
= 0xEF;
939 masterConfig
.chk
= 0; // erase checksum before recalculating
940 masterConfig
.chk
= calculateChecksum((const uint8_t *) &masterConfig
, sizeof(master_t
));
944 while (attemptsRemaining
--) {
946 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPERR
);
949 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPRTERR
);
951 for (wordOffset
= 0; wordOffset
< sizeof(master_t
); wordOffset
+= 4) {
952 if (wordOffset
% FLASH_PAGE_SIZE
== 0) {
953 status
= FLASH_ErasePage(CONFIG_START_FLASH_ADDRESS
+ wordOffset
);
954 if (status
!= FLASH_COMPLETE
) {
959 status
= FLASH_ProgramWord(CONFIG_START_FLASH_ADDRESS
+ wordOffset
,
960 *(uint32_t *) ((char *) &masterConfig
+ wordOffset
));
961 if (status
!= FLASH_COMPLETE
) {
965 if (status
== FLASH_COMPLETE
) {
971 // Flash write failed - just die now
972 if (status
!= FLASH_COMPLETE
|| !isEEPROMContentValid()) {
973 failureMode(FAILURE_FLASH_WRITE_FAILED
);
979 void ensureEEPROMContainsValidData(void)
981 if (isEEPROMContentValid()) {
988 void resetEEPROM(void)
994 void saveConfigAndNotify(void)
997 readEEPROMAndNotify();
1000 void changeProfile(uint8_t profileIndex
)
1002 masterConfig
.current_profile_index
= profileIndex
;
1005 beeperConfirmationBeeps(profileIndex
+ 1);
1008 void handleOneshotFeatureChangeOnRestart(void)
1010 // Shutdown PWM on all motors prior to soft restart
1013 // Apply additional delay when OneShot125 feature changed from on to off state
1014 if (feature(FEATURE_ONESHOT125
) && !featureConfigured(FEATURE_ONESHOT125
)) {
1015 delay(ONESHOT_FEATURE_CHANGED_DELAY_ON_BOOT_MS
);
1019 void latchActiveFeatures()
1021 activeFeaturesLatch
= masterConfig
.enabledFeatures
;
1024 bool featureConfigured(uint32_t mask
)
1026 return masterConfig
.enabledFeatures
& mask
;
1029 bool feature(uint32_t mask
)
1031 return activeFeaturesLatch
& mask
;
1034 void featureSet(uint32_t mask
)
1036 masterConfig
.enabledFeatures
|= mask
;
1039 void featureClear(uint32_t mask
)
1041 masterConfig
.enabledFeatures
&= ~(mask
);
1044 void featureClearAll()
1046 masterConfig
.enabledFeatures
= 0;
1049 uint32_t featureMask(void)
1051 return masterConfig
.enabledFeatures
;