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 "blackbox/blackbox.h"
26 #include "common/axis.h"
27 #include "common/color.h"
28 #include "common/maths.h"
29 #include "common/printf.h"
31 #include "config/config_eeprom.h"
32 #include "config/feature.h"
33 #include "config/parameter_group.h"
34 #include "config/parameter_group_ids.h"
37 #include "cms/cms_types.h"
39 #include "drivers/nvic.h"
40 #include "drivers/sensor.h"
41 #include "drivers/system.h"
42 #include "drivers/time.h"
43 #include "drivers/dma.h"
44 #include "drivers/io.h"
45 #include "drivers/light_led.h"
46 #include "drivers/sound_beeper.h"
47 #include "drivers/timer.h"
48 #include "drivers/serial.h"
49 #include "drivers/serial_softserial.h"
50 #include "drivers/serial_uart.h"
51 #include "drivers/accgyro/accgyro.h"
52 #include "drivers/compass/compass.h"
53 #include "drivers/pwm_esc_detect.h"
54 #include "drivers/rx_pwm.h"
55 #include "drivers/pwm_output.h"
56 #include "drivers/adc.h"
57 #include "drivers/bus.h"
58 #include "drivers/bus_i2c.h"
59 #include "drivers/bus_spi.h"
60 #include "drivers/buttons.h"
61 #include "drivers/inverter.h"
62 #include "drivers/flash_m25p16.h"
63 #include "drivers/sonar_hcsr04.h"
64 #include "drivers/sdcard.h"
65 #include "drivers/usb_io.h"
66 #include "drivers/transponder_ir.h"
67 #include "drivers/exti.h"
68 #include "drivers/max7456.h"
69 #include "drivers/vtx_rtc6705.h"
70 #include "drivers/vtx_common.h"
72 #include "fc/config.h"
73 #include "fc/fc_init.h"
74 #include "fc/fc_msp.h"
75 #include "fc/fc_tasks.h"
76 #include "fc/rc_controls.h"
77 #include "fc/runtime_config.h"
80 #include "msp/msp_serial.h"
83 #include "rx/spektrum.h"
85 #include "io/beeper.h"
86 #include "io/displayport_max7456.h"
87 #include "io/serial.h"
88 #include "io/flashfs.h"
90 #include "io/motors.h"
91 #include "io/servos.h"
92 #include "io/gimbal.h"
93 #include "io/ledstrip.h"
94 #include "io/dashboard.h"
95 #include "io/asyncfatfs/asyncfatfs.h"
96 #include "io/transponder_ir.h"
98 #include "io/osd_slave.h"
99 #include "io/displayport_msp.h"
100 #include "io/vtx_rtc6705.h"
101 #include "io/vtx_control.h"
102 #include "io/vtx_smartaudio.h"
103 #include "io/vtx_tramp.h"
105 #include "scheduler/scheduler.h"
107 #include "sensors/acceleration.h"
108 #include "sensors/barometer.h"
109 #include "sensors/battery.h"
110 #include "sensors/boardalignment.h"
111 #include "sensors/compass.h"
112 #include "sensors/esc_sensor.h"
113 #include "sensors/gyro.h"
114 #include "sensors/initialisation.h"
115 #include "sensors/sensors.h"
116 #include "sensors/sonar.h"
118 #include "telemetry/telemetry.h"
120 #include "flight/failsafe.h"
121 #include "flight/imu.h"
122 #include "flight/mixer.h"
123 #include "flight/navigation.h"
124 #include "flight/pid.h"
125 #include "flight/servos.h"
128 #ifdef USE_HARDWARE_REVISION_DETECTION
129 #include "hardware_revision.h"
132 #include "build/build_config.h"
133 #include "build/debug.h"
135 #ifdef TARGET_PREINIT
136 void targetPreInit(void);
139 extern uint8_t motorControlEnable
;
141 #ifdef SOFTSERIAL_LOOPBACK
142 serialPort_t
*loopbackPort
;
145 uint8_t systemState
= SYSTEM_STATE_INITIALISING
;
147 void processLoopback(void)
149 #ifdef SOFTSERIAL_LOOPBACK
151 uint8_t bytesWaiting
;
152 while ((bytesWaiting
= serialRxBytesWaiting(loopbackPort
))) {
153 uint8_t b
= serialRead(loopbackPort
);
154 serialWrite(loopbackPort
, b
);
162 bool canUpdateVTX(void)
164 #if defined(MAX7456_SPI_INSTANCE) && defined(RTC6705_SPI_INSTANCE) && defined(SPI_SHARED_MAX7456_AND_RTC6705)
165 if (feature(FEATURE_OSD
)) {
166 return !max7456DmaInProgress();
173 #ifdef BUS_SWITCH_PIN
174 void busSwitchInit(void)
176 static IO_t busSwitchResetPin
= IO_NONE
;
178 busSwitchResetPin
= IOGetByTag(IO_TAG(BUS_SWITCH_PIN
));
179 IOInit(busSwitchResetPin
, OWNER_SYSTEM
, 0);
180 IOConfigGPIO(busSwitchResetPin
, IOCFG_OUT_PP
);
183 IOLo(busSwitchResetPin
);
189 #ifdef USE_HAL_DRIVER
197 // initialize IO (needed for all IO operations)
200 #ifdef USE_HARDWARE_REVISION_DETECTION
201 detectHardwareRevision();
204 #ifdef BRUSHED_ESC_AUTODETECT
210 ensureEEPROMContainsValidData();
213 systemState
|= SYSTEM_STATE_CONFIG_LOADED
;
215 //i2cSetOverclock(masterConfig.i2c_overclock);
217 debugMode
= systemConfig()->debug_mode
;
219 // Latch active features to be used for feature() in the remainder of init().
220 latchActiveFeatures();
222 #ifdef TARGET_PREINIT
226 #if !defined(UNIT_TEST) && !defined(USE_FAKE_LED)
227 ledInit(statusLedConfig());
239 // Check status of bind plug and exit if not active
240 delayMicroseconds(10); // allow configuration to settle
242 if (!isMPUSoftReset()) {
243 #if defined(BUTTON_A_PIN) && defined(BUTTON_B_PIN)
244 // two buttons required
245 uint8_t secondsRemaining
= 5;
246 bool bothButtonsHeld
;
248 bothButtonsHeld
= buttonAPressed() && buttonBPressed();
249 if (bothButtonsHeld
) {
250 if (--secondsRemaining
== 0) {
257 } while (bothButtonsHeld
);
262 #ifdef SPEKTRUM_BIND_PIN
263 if (feature(FEATURE_RX_SERIAL
)) {
264 switch (rxConfig()->serialrx_provider
) {
265 case SERIALRX_SPEKTRUM1024
:
266 case SERIALRX_SPEKTRUM2048
:
267 // Spektrum satellite binding if enabled on startup.
268 // Must be called before that 100ms sleep so that we don't lose satellite's binding window after startup.
269 // The rest of Spektrum initialization will happen later - via spektrumInit()
270 spektrumBind(rxConfigMutable());
278 timerInit(); // timer must be initialized before any channel is allocated
280 #ifdef BUS_SWITCH_PIN
284 #if defined(USE_UART) && !defined(SITL)
285 uartPinConfigure(serialPinConfig());
288 #if defined(AVOID_UART1_FOR_PWM_PPM)
289 serialInit(feature(FEATURE_SOFTSERIAL
),
290 feature(FEATURE_RX_PPM
) || feature(FEATURE_RX_PARALLEL_PWM
) ? SERIAL_PORT_USART1
: SERIAL_PORT_NONE
);
291 #elif defined(AVOID_UART2_FOR_PWM_PPM)
292 serialInit(feature(FEATURE_SOFTSERIAL
),
293 feature(FEATURE_RX_PPM
) || feature(FEATURE_RX_PARALLEL_PWM
) ? SERIAL_PORT_USART2
: SERIAL_PORT_NONE
);
294 #elif defined(AVOID_UART3_FOR_PWM_PPM)
295 serialInit(feature(FEATURE_SOFTSERIAL
),
296 feature(FEATURE_RX_PPM
) || feature(FEATURE_RX_PARALLEL_PWM
) ? SERIAL_PORT_USART3
: SERIAL_PORT_NONE
);
298 serialInit(feature(FEATURE_SOFTSERIAL
), SERIAL_PORT_NONE
);
301 mixerInit(mixerConfig()->mixerMode
);
306 uint16_t idlePulse
= motorConfig()->mincommand
;
307 if (feature(FEATURE_3D
)) {
308 idlePulse
= flight3DConfig()->neutral3d
;
311 if (motorConfig()->dev
.motorPwmProtocol
== PWM_TYPE_BRUSHED
) {
312 featureClear(FEATURE_3D
);
313 idlePulse
= 0; // brushed motors
316 mixerConfigureOutput();
317 motorDevInit(&motorConfig()->dev
, idlePulse
, getMotorCount());
320 servoConfigureOutput();
321 if (isMixerUsingServos()) {
322 //pwm_params.useChannelForwarding = feature(FEATURE_CHANNEL_FORWARDING);
323 servoDevInit(&servoConfig()->dev
);
329 else if (feature(FEATURE_RX_PPM
)) {
330 ppmRxInit(ppmConfig(), motorConfig()->dev
.motorPwmProtocol
);
334 else if (feature(FEATURE_RX_PARALLEL_PWM
)) {
335 pwmRxInit(pwmConfig());
339 systemState
|= SYSTEM_STATE_MOTORS_READY
;
342 beeperInit(beeperDevConfig());
344 /* temp until PGs are implemented. */
345 #if defined(USE_INVERTER) && !defined(SITL)
346 initInverters(serialPinConfig());
349 #ifdef TARGET_BUS_INIT
354 #ifdef USE_SPI_DEVICE_1
357 #ifdef USE_SPI_DEVICE_2
360 #ifdef USE_SPI_DEVICE_3
363 #ifdef USE_SPI_DEVICE_4
369 i2cHardwareConfigure();
371 // Note: Unlike UARTs which are configured when client is present,
372 // I2C buses are initialized unconditionally if they are configured.
374 #ifdef USE_I2C_DEVICE_1
377 #ifdef USE_I2C_DEVICE_2
380 #ifdef USE_I2C_DEVICE_3
383 #ifdef USE_I2C_DEVICE_4
388 #endif /* TARGET_BUS_INIT */
390 #ifdef USE_HARDWARE_REVISION_DETECTION
391 updateHardwareRevision();
398 #if defined(SONAR_SOFTSERIAL2_EXCLUSIVE) && defined(SONAR) && defined(USE_SOFTSERIAL2)
399 if (feature(FEATURE_SONAR
) && feature(FEATURE_SOFTSERIAL
)) {
400 serialRemovePort(SERIAL_PORT_SOFTSERIAL2
);
404 #if defined(SONAR_SOFTSERIAL1_EXCLUSIVE) && defined(SONAR) && defined(USE_SOFTSERIAL1)
405 if (feature(FEATURE_SONAR
) && feature(FEATURE_SOFTSERIAL
)) {
406 serialRemovePort(SERIAL_PORT_SOFTSERIAL1
);
411 adcConfigMutable()->vbat
.enabled
= (batteryConfig()->voltageMeterSource
== VOLTAGE_METER_ADC
);
412 adcConfigMutable()->current
.enabled
= (batteryConfig()->currentMeterSource
== CURRENT_METER_ADC
);
414 adcConfigMutable()->rssi
.enabled
= feature(FEATURE_RSSI_ADC
);
415 adcInit(adcConfig());
418 initBoardAlignment(boardAlignment());
420 if (!sensorsAutodetect()) {
421 // if gyro was not detected due to whatever reason, we give up now.
422 failureMode(FAILURE_MISSING_ACC
);
425 systemState
|= SYSTEM_STATE_SENSORS_READY
;
431 for (int i
= 0; i
< 10; i
++) {
435 if (!(getBeeperOffMask() & (1 << (BEEPER_SYSTEM_INIT
- 1)))) BEEP_ON
;
442 // gyro.targetLooptime set in sensorsAutodetect(), so we are ready to call pidInit()
443 pidInit(currentPidProfile
);
451 cliInit(serialConfig());
459 * CMS, display devices and OSD
465 #if (defined(OSD) || (defined(USE_MSP_DISPLAYPORT) && defined(CMS)) || defined(USE_OSD_SLAVE))
466 displayPort_t
*osdDisplayPort
= NULL
;
469 #if defined(OSD) && !defined(USE_OSD_SLAVE)
470 //The OSD need to be initialised after GYRO to avoid GYRO initialisation failure on some targets
472 if (feature(FEATURE_OSD
)) {
473 #if defined(USE_MAX7456)
474 // If there is a max7456 chip for the OSD then use it
475 osdDisplayPort
= max7456DisplayPortInit(vcdProfile());
476 #elif defined(USE_OSD_OVER_MSP_DISPLAYPORT) // OSD over MSP; not supported (yet)
477 osdDisplayPort
= displayPortMspInit();
479 // osdInit will register with CMS by itself.
480 osdInit(osdDisplayPort
);
484 #if defined(USE_OSD_SLAVE) && !defined(OSD)
485 #if defined(USE_MAX7456)
486 // If there is a max7456 chip for the OSD then use it
487 osdDisplayPort
= max7456DisplayPortInit(vcdProfile());
488 // osdInit will register with CMS by itself.
489 osdSlaveInit(osdDisplayPort
);
493 #if defined(CMS) && defined(USE_MSP_DISPLAYPORT)
494 // If BFOSD is not active, then register MSP_DISPLAYPORT as a CMS device.
496 cmsDisplayPortRegister(displayPortMspInit());
500 // Dashbord will register with CMS by itself.
501 if (feature(FEATURE_DASHBOARD
)) {
508 if (feature(FEATURE_GPS
)) {
517 if (feature(FEATURE_LED_STRIP
)) {
523 if (feature(FEATURE_TELEMETRY
)) {
528 #ifdef USE_ESC_SENSOR
529 if (feature(FEATURE_ESC_SENSOR
)) {
534 #ifdef USB_DETECT_PIN
535 usbCableDetectInit();
539 if (feature(FEATURE_TRANSPONDER
)) {
541 transponderStartRepeating();
542 systemState
|= SYSTEM_STATE_TRANSPONDER_ENABLED
;
547 #if defined(USE_FLASH_M25P16)
548 m25p16_init(flashConfig());
554 if (blackboxConfig()->device
== BLACKBOX_DEVICE_SDCARD
) {
555 sdcardInsertionDetectInit();
556 sdcard_init(sdcardConfig()->useDma
);
565 if (mixerConfig()->mixerMode
== MIXER_GIMBAL
) {
566 accSetCalibrationCycles(CALIBRATING_ACC_CYCLES
);
568 gyroStartCalibration();
570 baroSetCalibrationCycles(CALIBRATING_BARO_CYCLES
);
578 #ifdef VTX_SMARTAUDIO
587 #ifdef VTX_RTC6705OPTIONAL
588 if (!vtxCommonDeviceRegistered()) // external VTX takes precedence when configured.
595 #endif // VTX_CONTROL
598 // TODO - not implemented yet
601 ENABLE_STATE(SMALL_ANGLE
);
602 DISABLE_ARMING_FLAG(PREVENT_ARMING
);
604 #ifdef SOFTSERIAL_LOOPBACK
605 // FIXME this is a hack, perhaps add a FUNCTION_LOOPBACK to support it properly
606 loopbackPort
= (serialPort_t
*)&(softSerialPorts
[0]);
607 if (!loopbackPort
->vTable
) {
608 loopbackPort
= openSoftSerial(0, NULL
, 19200, SERIAL_NOT_INVERTED
);
610 serialPrint(loopbackPort
, "LOOPBACK\r\n");
613 batteryInit(); // always needs doing, regardless of features.
616 if (feature(FEATURE_DASHBOARD
)) {
617 #ifdef USE_OLED_GPS_DEBUG_PAGE_ONLY
618 dashboardShowFixedPage(PAGE_GPS
);
620 dashboardResetPageCycling();
621 dashboardEnablePageCycling();
630 // Latch active features AGAIN since some may be modified by init().
631 latchActiveFeatures();
632 motorControlEnable
= true;
639 systemState
|= SYSTEM_STATE_READY
;