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 "scheduler.h"
26 #include "common/axis.h"
27 #include "common/color.h"
28 #include "common/atomic.h"
29 #include "common/maths.h"
31 #include "drivers/nvic.h"
33 #include "drivers/sensor.h"
34 #include "drivers/system.h"
35 #include "drivers/gpio.h"
36 #include "drivers/light_led.h"
37 #include "drivers/sound_beeper.h"
38 #include "drivers/timer.h"
39 #include "drivers/serial.h"
40 #include "drivers/serial_softserial.h"
41 #include "drivers/serial_uart.h"
42 #include "drivers/accgyro.h"
43 #include "drivers/compass.h"
44 #include "drivers/pwm_mapping.h"
45 #include "drivers/pwm_rx.h"
46 #include "drivers/adc.h"
47 #include "drivers/bus_i2c.h"
48 #include "drivers/bus_spi.h"
49 #include "drivers/inverter.h"
50 #include "drivers/flash_m25p16.h"
51 #include "drivers/sonar_hcsr04.h"
52 #include "drivers/gyro_sync.h"
56 #include "io/serial.h"
57 #include "io/flashfs.h"
59 #include "io/escservo.h"
60 #include "io/rc_controls.h"
61 #include "io/gimbal.h"
62 #include "io/ledstrip.h"
63 #include "io/display.h"
65 #include "sensors/sensors.h"
66 #include "sensors/sonar.h"
67 #include "sensors/barometer.h"
68 #include "sensors/compass.h"
69 #include "sensors/acceleration.h"
70 #include "sensors/gyro.h"
71 #include "sensors/battery.h"
72 #include "sensors/boardalignment.h"
73 #include "sensors/initialisation.h"
75 #include "telemetry/telemetry.h"
76 #include "blackbox/blackbox.h"
78 #include "flight/pid.h"
79 #include "flight/imu.h"
80 #include "flight/mixer.h"
81 #include "flight/failsafe.h"
82 #include "flight/navigation_rewrite.h"
84 #include "config/runtime_config.h"
85 #include "config/config.h"
86 #include "config/config_profile.h"
87 #include "config/config_master.h"
89 #ifdef USE_HARDWARE_REVISION_DETECTION
90 #include "hardware_revision.h"
93 #include "build_config.h"
96 extern uint8_t motorControlEnable
;
98 #ifdef SOFTSERIAL_LOOPBACK
99 serialPort_t
*loopbackPort
;
102 void printfSupportInit(void);
103 void timerInit(void);
104 void telemetryInit(void);
105 void serialInit(serialConfig_t
*initialSerialConfig
, bool softserialEnabled
);
106 void mspInit(serialConfig_t
*serialConfig
);
107 void cliInit(serialConfig_t
*serialConfig
);
108 void failsafeInit(rxConfig_t
*intialRxConfig
, uint16_t deadband3d_throttle
);
109 pwmOutputConfiguration_t
*pwmInit(drv_pwm_config_t
*init
);
111 void mixerInit(mixerMode_e mixerMode
, motorMixer_t
*customMotorMixers
, servoMixer_t
*customServoMixers
);
113 void mixerInit(mixerMode_e mixerMode
, motorMixer_t
*customMotorMixers
);
115 void mixerUsePWMOutputConfiguration(pwmOutputConfiguration_t
*pwmOutputConfiguration
);
116 void rxInit(rxConfig_t
*rxConfig
, modeActivationCondition_t
*modeActivationConditions
);
117 void gpsPreInit(gpsConfig_t
*initialGpsConfig
);
118 void gpsInit(serialConfig_t
*serialConfig
, gpsConfig_t
*initialGpsConfig
);
120 void displayInit(rxConfig_t
*intialRxConfig
);
121 void ledStripInit(ledConfig_t
*ledConfigsToUse
, hsvColor_t
*colorsToUse
);
122 void spektrumBind(rxConfig_t
*rxConfig
);
123 const sonarHardware_t
*sonarGetHardwareConfiguration(batteryConfig_t
*batteryConfig
);
124 void sonarInit(const sonarHardware_t
*sonarHardware
);
127 // from system_stm32f30x.c
128 void SetSysClock(void);
131 // from system_stm32f10x.c
132 void SetSysClock(bool overclock
);
136 SYSTEM_STATE_INITIALISING
= 0,
137 SYSTEM_STATE_CONFIG_LOADED
= (1 << 0),
138 SYSTEM_STATE_SENSORS_READY
= (1 << 1),
139 SYSTEM_STATE_MOTORS_READY
= (1 << 2),
140 SYSTEM_STATE_READY
= (1 << 7)
143 static uint8_t systemState
= SYSTEM_STATE_INITIALISING
;
148 drv_pwm_config_t pwm_params
;
154 ensureEEPROMContainsValidData();
157 systemState
|= SYSTEM_STATE_CONFIG_LOADED
;
161 SCB
->CPACR
= (0x3 << (10*2)) | (0x3 << (11*2));
168 // Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers
169 // Configure the Flash Latency cycles and enable prefetch buffer
170 SetSysClock(masterConfig
.emf_avoidance
);
172 i2cSetOverclock(masterConfig
.i2c_overclock
);
174 #ifdef USE_HARDWARE_REVISION_DETECTION
175 detectHardwareRevision();
180 // Latch active features to be used for feature() in the remainder of init().
181 latchActiveFeatures();
186 if (feature(FEATURE_RX_SERIAL
)) {
187 switch (masterConfig
.rxConfig
.serialrx_provider
) {
188 case SERIALRX_SPEKTRUM1024
:
189 case SERIALRX_SPEKTRUM2048
:
190 // Spektrum satellite binding if enabled on startup.
191 // Must be called before that 100ms sleep so that we don't lose satellite's binding window after startup.
192 // The rest of Spektrum initialization will happen later - via spektrumInit()
193 spektrumBind(&masterConfig
.rxConfig
);
201 timerInit(); // timer must be initialized before any channel is allocated
203 serialInit(&masterConfig
.serialConfig
, feature(FEATURE_SOFTSERIAL
));
206 mixerInit(masterConfig
.mixerMode
, masterConfig
.customMotorMixer
, masterConfig
.customServoMixer
);
208 mixerInit(masterConfig
.mixerMode
, masterConfig
.customMotorMixer
);
211 memset(&pwm_params
, 0, sizeof(pwm_params
));
214 const sonarHardware_t
*sonarHardware
= NULL
;
216 if (feature(FEATURE_SONAR
)) {
217 sonarHardware
= sonarGetHardwareConfiguration(&masterConfig
.batteryConfig
);
218 sonarGPIOConfig_t sonarGPIOConfig
= {
220 .triggerPin
= sonarHardware
->echo_pin
,
221 .echoPin
= sonarHardware
->trigger_pin
,
223 pwm_params
.sonarGPIOConfig
= &sonarGPIOConfig
;
227 // when using airplane/wing mixer, servo/motor outputs are remapped
228 if (masterConfig
.mixerMode
== MIXER_AIRPLANE
|| masterConfig
.mixerMode
== MIXER_FLYING_WING
|| masterConfig
.mixerMode
== MIXER_CUSTOM_AIRPLANE
)
229 pwm_params
.airplane
= true;
231 pwm_params
.airplane
= false;
232 #if defined(USE_USART2) && defined(STM32F10X)
233 pwm_params
.useUART2
= doesConfigurationUsePort(SERIAL_PORT_USART2
);
236 pwm_params
.useUART3
= doesConfigurationUsePort(SERIAL_PORT_USART3
);
238 pwm_params
.useVbat
= feature(FEATURE_VBAT
);
239 pwm_params
.useSoftSerial
= feature(FEATURE_SOFTSERIAL
);
240 pwm_params
.useParallelPWM
= feature(FEATURE_RX_PARALLEL_PWM
);
241 pwm_params
.useRSSIADC
= feature(FEATURE_RSSI_ADC
);
242 pwm_params
.useCurrentMeterADC
= feature(FEATURE_CURRENT_METER
)
243 && masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
;
244 pwm_params
.useLEDStrip
= feature(FEATURE_LED_STRIP
);
245 pwm_params
.usePPM
= feature(FEATURE_RX_PPM
);
246 pwm_params
.useSerialRx
= feature(FEATURE_RX_SERIAL
);
248 pwm_params
.useSonar
= feature(FEATURE_SONAR
);
252 pwm_params
.useServos
= isMixerUsingServos();
253 pwm_params
.useChannelForwarding
= feature(FEATURE_CHANNEL_FORWARDING
);
254 pwm_params
.servoCenterPulse
= masterConfig
.escAndServoConfig
.servoCenterPulse
;
255 pwm_params
.servoPwmRate
= masterConfig
.servo_pwm_rate
;
258 pwm_params
.useOneshot
= feature(FEATURE_ONESHOT125
);
259 pwm_params
.motorPwmRate
= masterConfig
.motor_pwm_rate
;
260 pwm_params
.idlePulse
= masterConfig
.escAndServoConfig
.mincommand
;
261 if (feature(FEATURE_3D
))
262 pwm_params
.idlePulse
= masterConfig
.flight3DConfig
.neutral3d
;
263 if (pwm_params
.motorPwmRate
> 500)
264 pwm_params
.idlePulse
= 0; // brushed motors
266 pwmRxInit(masterConfig
.inputFilteringMode
);
268 pwmOutputConfiguration_t
*pwmOutputConfiguration
= pwmInit(&pwm_params
);
270 mixerUsePWMOutputConfiguration(pwmOutputConfiguration
);
272 if (!feature(FEATURE_ONESHOT125
))
273 motorControlEnable
= true;
275 systemState
|= SYSTEM_STATE_MOTORS_READY
;
278 beeperConfig_t beeperConfig
= {
279 .gpioPeripheral
= BEEP_PERIPHERAL
,
281 .gpioPort
= BEEP_GPIO
,
282 #ifdef BEEPER_INVERTED
283 .gpioMode
= Mode_Out_PP
,
286 .gpioMode
= Mode_Out_OD
,
291 if (hardwareRevision
>= NAZE32_REV5
) {
292 // naze rev4 and below used opendrain to PNP for buzzer. Rev5 and above use PP to NPN.
293 beeperConfig
.gpioMode
= Mode_Out_PP
;
294 beeperConfig
.isInverted
= true;
298 beeperInit(&beeperConfig
);
311 #ifdef USE_HARDWARE_REVISION_DETECTION
312 updateHardwareRevision();
316 if (hardwareRevision
== NAZE32_SP
) {
317 serialRemovePort(SERIAL_PORT_SOFTSERIAL2
);
319 serialRemovePort(SERIAL_PORT_USART3
);
323 #if defined(SPRACINGF3) && defined(SONAR) && defined(USE_SOFTSERIAL2)
324 if (feature(FEATURE_SONAR
) && feature(FEATURE_SOFTSERIAL
)) {
325 serialRemovePort(SERIAL_PORT_SOFTSERIAL2
);
332 if (hardwareRevision
!= NAZE32_SP
) {
335 if (!doesConfigurationUsePort(SERIAL_PORT_USART3
)) {
340 if (!doesConfigurationUsePort(SERIAL_PORT_USART3
)) {
349 drv_adc_config_t adc_params
;
351 adc_params
.enableVBat
= feature(FEATURE_VBAT
);
352 adc_params
.enableRSSI
= feature(FEATURE_RSSI_ADC
);
353 adc_params
.enableCurrentMeter
= feature(FEATURE_CURRENT_METER
);
354 adc_params
.enableExternal1
= false;
356 adc_params
.enableExternal1
= true;
359 // optional ADC5 input on rev.5 hardware
360 adc_params
.enableExternal1
= (hardwareRevision
>= NAZE32_REV5
);
363 adcInit(&adc_params
);
367 initBoardAlignment(&masterConfig
.boardAlignment
);
370 if (feature(FEATURE_DISPLAY
)) {
371 displayInit(&masterConfig
.rxConfig
);
376 if (feature(FEATURE_GPS
)) {
377 gpsPreInit(&masterConfig
.gpsConfig
);
381 if (!sensorsAutodetect(&masterConfig
.sensorAlignmentConfig
, masterConfig
.gyro_lpf
,
382 masterConfig
.acc_hardware
, masterConfig
.mag_hardware
, masterConfig
.baro_hardware
, currentProfile
->mag_declination
,
383 masterConfig
.looptime
, masterConfig
.gyroSync
, masterConfig
.gyroSyncDenominator
)) {
385 // if gyro was not detected due to whatever reason, we give up now.
386 failureMode(FAILURE_MISSING_ACC
);
389 systemState
|= SYSTEM_STATE_SENSORS_READY
;
393 for (i
= 0; i
< 10; i
++) {
405 if (sensors(SENSOR_MAG
))
411 mspInit(&masterConfig
.serialConfig
);
414 cliInit(&masterConfig
.serialConfig
);
417 failsafeInit(&masterConfig
.rxConfig
, masterConfig
.flight3DConfig
.deadband3d_throttle
);
419 rxInit(&masterConfig
.rxConfig
, currentProfile
->modeActivationConditions
);
422 if (feature(FEATURE_GPS
)) {
424 &masterConfig
.serialConfig
,
425 &masterConfig
.gpsConfig
432 &masterConfig
.navConfig
,
433 ¤tProfile
->pidProfile
,
434 ¤tProfile
->rcControlsConfig
,
435 &masterConfig
.rxConfig
,
436 &masterConfig
.escAndServoConfig
441 if (feature(FEATURE_SONAR
)) {
442 sonarInit(sonarHardware
);
447 ledStripInit(masterConfig
.ledConfigs
, masterConfig
.colors
);
449 if (feature(FEATURE_LED_STRIP
)) {
455 if (feature(FEATURE_TELEMETRY
)) {
462 if (hardwareRevision
== NAZE32_REV5
) {
465 #elif defined(USE_FLASH_M25P16)
476 if (masterConfig
.mixerMode
== MIXER_GIMBAL
) {
477 accSetCalibrationCycles(CALIBRATING_ACC_CYCLES
);
479 gyroSetCalibrationCycles(CALIBRATING_GYRO_CYCLES
);
481 baroSetCalibrationCycles(CALIBRATING_BARO_CYCLES
);
485 // TODO - not implemented yet
488 ENABLE_STATE(SMALL_ANGLE
);
489 DISABLE_ARMING_FLAG(PREVENT_ARMING
);
491 #ifdef SOFTSERIAL_LOOPBACK
492 // FIXME this is a hack, perhaps add a FUNCTION_LOOPBACK to support it properly
493 loopbackPort
= (serialPort_t
*)&(softSerialPorts
[0]);
494 if (!loopbackPort
->vTable
) {
495 loopbackPort
= openSoftSerial(0, NULL
, 19200, SERIAL_NOT_INVERTED
);
497 serialPrint(loopbackPort
, "LOOPBACK\r\n");
500 // Now that everything has powered up the voltage and cell count be determined.
502 if (feature(FEATURE_VBAT
| FEATURE_CURRENT_METER
))
503 batteryInit(&masterConfig
.batteryConfig
);
506 if (feature(FEATURE_DISPLAY
)) {
507 #ifdef USE_OLED_GPS_DEBUG_PAGE_ONLY
508 displayShowFixedPage(PAGE_GPS
);
510 displayResetPageCycling();
511 displayEnablePageCycling();
520 // Latch active features AGAIN since some may be modified by init().
521 latchActiveFeatures();
522 motorControlEnable
= true;
524 systemState
|= SYSTEM_STATE_READY
;
527 #ifdef SOFTSERIAL_LOOPBACK
528 void processLoopback(void) {
530 uint8_t bytesWaiting
;
531 while ((bytesWaiting
= serialRxBytesWaiting(loopbackPort
))) {
532 uint8_t b
= serialRead(loopbackPort
);
533 serialWrite(loopbackPort
, b
);
538 #define processLoopback()
544 /* Setup scheduler */
545 if (masterConfig
.gyroSync
) {
546 rescheduleTask(TASK_GYROPID
, targetLooptime
- INTERRUPT_WAIT_TIME
);
549 rescheduleTask(TASK_GYROPID
, targetLooptime
);
552 setTaskEnabled(TASK_GYROPID
, true);
553 setTaskEnabled(TASK_SERIAL
, true);
554 setTaskEnabled(TASK_BEEPER
, true);
555 setTaskEnabled(TASK_BATTERY
, feature(FEATURE_VBAT
) || feature(FEATURE_CURRENT_METER
));
556 setTaskEnabled(TASK_RX
, true);
558 setTaskEnabled(TASK_GPS
, feature(FEATURE_GPS
));
561 setTaskEnabled(TASK_COMPASS
, sensors(SENSOR_MAG
));
564 setTaskEnabled(TASK_BARO
, sensors(SENSOR_BARO
));
567 setTaskEnabled(TASK_SONAR
, sensors(SENSOR_SONAR
));
570 setTaskEnabled(TASK_DISPLAY
, feature(FEATURE_DISPLAY
));
573 setTaskEnabled(TASK_TELEMETRY
, feature(FEATURE_TELEMETRY
));
576 setTaskEnabled(TASK_LEDSTRIP
, feature(FEATURE_LED_STRIP
));
585 void HardFault_Handler(void)
587 // fall out of the sky
588 uint8_t requiredState
= SYSTEM_STATE_CONFIG_LOADED
| SYSTEM_STATE_MOTORS_READY
;
589 if ((systemState
& requiredState
) == requiredState
) {