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/build_config.h"
31 #include "build/debug.h"
32 #include "build/debug_pin.h"
35 #include "cms/cms_types.h"
37 #include "common/axis.h"
38 #include "common/color.h"
39 #include "common/maths.h"
40 #include "common/printf_serial.h"
42 #include "config/config.h"
43 #include "config/config_eeprom.h"
44 #include "config/feature.h"
46 #include "drivers/accgyro/accgyro.h"
47 #include "drivers/adc.h"
48 #include "drivers/bus.h"
49 #include "drivers/bus_i2c.h"
50 #include "drivers/bus_quadspi.h"
51 #include "drivers/bus_spi.h"
52 #include "drivers/buttons.h"
53 #include "drivers/camera_control.h"
54 #include "drivers/compass/compass.h"
55 #include "drivers/dma.h"
56 #include "drivers/exti.h"
57 #include "drivers/flash.h"
58 #include "drivers/inverter.h"
59 #include "drivers/io.h"
60 #include "drivers/light_led.h"
61 #include "drivers/mco.h"
62 #include "drivers/nvic.h"
63 #include "drivers/persistent.h"
64 #include "drivers/pin_pull_up_down.h"
65 #include "drivers/pwm_esc_detect.h"
66 #include "drivers/pwm_output.h"
67 #include "drivers/rx/rx_pwm.h"
68 #include "drivers/sensor.h"
69 #include "drivers/serial.h"
70 #include "drivers/serial_softserial.h"
71 #include "drivers/serial_uart.h"
72 #include "drivers/sdcard.h"
73 #include "drivers/sdio.h"
74 #include "drivers/sound_beeper.h"
75 #include "drivers/system.h"
76 #include "drivers/time.h"
77 #include "drivers/timer.h"
78 #include "drivers/transponder_ir.h"
79 #include "drivers/usb_io.h"
81 #include "drivers/usb_msc.h"
83 #include "drivers/vtx_common.h"
84 #include "drivers/vtx_rtc6705.h"
85 #include "drivers/vtx_table.h"
87 #include "fc/board_info.h"
88 #include "fc/dispatch.h"
90 #include "fc/rc_controls.h"
91 #include "fc/runtime_config.h"
95 #include "flight/failsafe.h"
96 #include "flight/imu.h"
97 #include "flight/mixer.h"
98 #include "flight/pid.h"
99 #include "flight/pid_init.h"
100 #include "flight/servos.h"
102 #include "io/asyncfatfs/asyncfatfs.h"
103 #include "io/beeper.h"
104 #include "io/dashboard.h"
105 #include "io/displayport_frsky_osd.h"
106 #include "io/displayport_max7456.h"
107 #include "io/displayport_msp.h"
108 #include "io/flashfs.h"
109 #include "io/gimbal.h"
111 #include "io/ledstrip.h"
112 #include "io/pidaudio.h"
113 #include "io/piniobox.h"
114 #include "io/rcdevice_cam.h"
115 #include "io/serial.h"
116 #include "io/servos.h"
117 #include "io/transponder_ir.h"
119 #include "io/vtx_control.h"
120 #include "io/vtx_rtc6705.h"
121 #include "io/vtx_smartaudio.h"
122 #include "io/vtx_tramp.h"
124 #include "msc/emfat_file.h"
125 #ifdef USE_PERSISTENT_MSC_RTC
126 #include "msc/usbd_storage.h"
130 #include "msp/msp_serial.h"
135 #include "pg/beeper.h"
136 #include "pg/beeper_dev.h"
137 #include "pg/bus_i2c.h"
138 #include "pg/bus_spi.h"
139 #include "pg/bus_quadspi.h"
140 #include "pg/flash.h"
142 #include "pg/motor.h"
143 #include "pg/pinio.h"
144 #include "pg/piniobox.h"
145 #include "pg/pin_pull_up_down.h"
148 #include "pg/rx_pwm.h"
149 #include "pg/rx_spi.h"
150 #include "pg/sdcard.h"
152 #include "pg/vtx_io.h"
155 #include "rx/spektrum.h"
157 #include "scheduler/scheduler.h"
159 #include "sensors/acceleration.h"
160 #include "sensors/barometer.h"
161 #include "sensors/battery.h"
162 #include "sensors/boardalignment.h"
163 #include "sensors/compass.h"
164 #include "sensors/esc_sensor.h"
165 #include "sensors/gyro.h"
166 #include "sensors/gyro_init.h"
167 #include "sensors/initialisation.h"
169 #include "telemetry/telemetry.h"
171 #ifdef USE_HARDWARE_REVISION_DETECTION
172 #include "hardware_revision.h"
175 #ifdef TARGET_PREINIT
176 void targetPreInit(void);
179 #ifdef SOFTSERIAL_LOOPBACK
180 serialPort_t
*loopbackPort
;
183 uint8_t systemState
= SYSTEM_STATE_INITIALISING
;
185 void processLoopback(void)
187 #ifdef SOFTSERIAL_LOOPBACK
189 uint8_t bytesWaiting
;
190 while ((bytesWaiting
= serialRxBytesWaiting(loopbackPort
))) {
191 uint8_t b
= serialRead(loopbackPort
);
192 serialWrite(loopbackPort
, b
);
198 #ifdef BUS_SWITCH_PIN
199 void busSwitchInit(void)
201 static IO_t busSwitchResetPin
= IO_NONE
;
203 busSwitchResetPin
= IOGetByTag(IO_TAG(BUS_SWITCH_PIN
));
204 IOInit(busSwitchResetPin
, OWNER_SYSTEM
, 0);
205 IOConfigGPIO(busSwitchResetPin
, IOCFG_OUT_PP
);
208 IOLo(busSwitchResetPin
);
213 static void configureSPIAndQuadSPI(void)
216 spiPinConfigure(spiPinConfig(0));
224 #ifdef USE_SPI_DEVICE_1
227 #ifdef USE_SPI_DEVICE_2
230 #ifdef USE_SPI_DEVICE_3
233 #ifdef USE_SPI_DEVICE_4
236 #ifdef USE_SPI_DEVICE_5
239 #ifdef USE_SPI_DEVICE_6
245 quadSpiPinConfigure(quadSpiConfig(0));
247 #ifdef USE_QUADSPI_DEVICE_1
248 quadSpiInit(QUADSPIDEV_1
);
250 #endif // USE_QUAD_SPI
254 static void sdCardAndFSInit()
256 sdcard_init(sdcardConfig());
261 static void swdPinsInit(void)
263 IO_t io
= IOGetByTag(DEFIO_TAG_E(PA13
)); // SWDIO
264 if (IOGetOwner(io
) == OWNER_FREE
) {
265 IOInit(io
, OWNER_SWD
, 0);
267 io
= IOGetByTag(DEFIO_TAG_E(PA14
)); // SWCLK
268 if (IOGetOwner(io
) == OWNER_FREE
) {
269 IOInit(io
, OWNER_SWD
, 0);
275 #ifdef SERIAL_PORT_COUNT
281 // initialize IO (needed for all IO operations)
284 #ifdef USE_HARDWARE_REVISION_DETECTION
285 detectHardwareRevision();
288 #if defined(USE_TARGET_CONFIG)
289 // Call once before the config is loaded for any target specific configuration required to support loading the config
290 targetConfiguration();
293 #ifdef USE_BRUSHED_ESC_AUTODETECT
294 // Opportunistically use the first motor pin of the default configuration for detection.
295 // We are doing this as with some boards, timing seems to be important, and the later detection will fail.
296 ioTag_t motorIoTag
= timerioTagGetByUsage(TIM_USE_MOTOR
, 0);
299 detectBrushedESC(motorIoTag
);
304 FLASH_INIT_ATTEMPTED
= (1 << 0),
305 SD_INIT_ATTEMPTED
= (1 << 1),
306 SPI_AND_QSPI_INIT_ATTEMPTED
= (1 << 2),
308 uint8_t initFlags
= 0;
310 #ifdef CONFIG_IN_SDCARD
313 // Config in sdcard presents an issue with pin configuration since the pin and sdcard configs for the
314 // sdcard are in the config which is on the sdcard which we can't read yet!
316 // FIXME We need to add configuration somewhere, e.g. bootloader image or reserved flash area, that can be read by the firmware.
317 // it's currently possible for the firmware resource allocation to be wrong after the config is loaded if the user changes the settings.
318 // This would cause undefined behaviour once the config is loaded. so for now, users must NOT change sdio/spi configs needed for
319 // the system to boot and/or to save the config.
321 // note that target specific SDCARD/SDIO/SPI/QUADSPI configs are
322 // also not supported in USE_TARGET_CONFIG/targetConfigure() when using CONFIG_IN_SDCARD.
326 // IMPORTANT: all default flash and pin configurations must be valid for the target after pgResetAll() is called.
327 // Target designers must ensure other devices connected the same SPI/QUADSPI interface as the flash chip do not
328 // cause communication issues with the flash chip. e.g. use external pullups on SPI/QUADSPI CS lines.
331 #ifdef TARGET_BUS_INIT
332 #error "CONFIG_IN_SDCARD and TARGET_BUS_INIT are mutually exclusive"
337 #ifdef USE_SDCARD_SPI
338 configureSPIAndQuadSPI();
339 initFlags
|= SPI_AND_QSPI_INIT_ATTEMPTED
;
343 initFlags
|= SD_INIT_ATTEMPTED
;
345 if (!sdcard_isInserted()) {
346 failureMode(FAILURE_SDCARD_REQUIRED
);
349 while (afatfs_getFilesystemState() != AFATFS_FILESYSTEM_STATE_READY
) {
352 if (afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_FATAL
) {
353 failureMode(FAILURE_SDCARD_INITIALISATION_FAILED
);
357 #endif // CONFIG_IN_SDCARD
359 #ifdef CONFIG_IN_EXTERNAL_FLASH
361 // Config on external flash presents an issue with pin configuration since the pin and flash configs for the
362 // external flash are in the config which is on a chip which we can't read yet!
364 // FIXME We need to add configuration somewhere, e.g. bootloader image or reserved flash area, that can be read by the firmware.
365 // it's currently possible for the firmware resource allocation to be wrong after the config is loaded if the user changes the settings.
366 // This would cause undefined behaviour once the config is loaded. so for now, users must NOT change flash/pin configs needed for
367 // the system to boot and/or to save the config.
369 // note that target specific FLASH/SPI/QUADSPI configs are
370 // also not supported in USE_TARGET_CONFIG/targetConfigure() when using CONFIG_IN_EXTERNAL_FLASH.
374 // IMPORTANT: all default flash and pin configurations must be valid for the target after pgResetAll() is called.
375 // Target designers must ensure other devices connected the same SPI/QUADSPI interface as the flash chip do not
376 // cause communication issues with the flash chip. e.g. use external pullups on SPI/QUADSPI CS lines.
380 #ifdef TARGET_BUS_INIT
381 #error "CONFIG_IN_EXTERNAL_FLASH and TARGET_BUS_INIT are mutually exclusive"
384 configureSPIAndQuadSPI();
385 initFlags
|= SPI_AND_QSPI_INIT_ATTEMPTED
;
388 #ifndef USE_FLASH_CHIP
389 #error "CONFIG_IN_EXTERNAL_FLASH requires USE_FLASH_CHIP to be defined."
392 bool haveFlash
= flashInit(flashConfig());
395 failureMode(FAILURE_EXTERNAL_FLASH_INIT_FAILED
);
397 initFlags
|= FLASH_INIT_ATTEMPTED
;
399 #endif // CONFIG_IN_EXTERNAL_FLASH
403 ensureEEPROMStructureIsValid();
405 bool readSuccess
= readEEPROM();
407 #if defined(USE_BOARD_INFO)
408 initBoardInformation();
411 if (!readSuccess
|| !isEEPROMVersionValid() || strncasecmp(systemConfig()->boardIdentifier
, TARGET_BOARD_IDENTIFIER
, sizeof(TARGET_BOARD_IDENTIFIER
))) {
415 systemState
|= SYSTEM_STATE_CONFIG_LOADED
;
422 // Ensure the SD card is initialised before the USB MSC starts to avoid a race condition
423 #if !defined(CONFIG_IN_SDCARD) && defined(STM32H7) && defined(USE_SDCARD_SDIO) // H7 only for now, likely should be applied to F4/F7 too
426 initFlags
|= SD_INIT_ATTEMPTED
;
431 #ifdef USE_BRUSHED_ESC_AUTODETECT
432 // Now detect again with the actually configured pin for motor 1, if it is not the default pin.
433 ioTag_t configuredMotorIoTag
= motorConfig()->dev
.ioTags
[0];
435 if (configuredMotorIoTag
&& configuredMotorIoTag
!= motorIoTag
) {
436 detectBrushedESC(configuredMotorIoTag
);
440 debugMode
= systemConfig()->debug_mode
;
442 #ifdef TARGET_PREINIT
446 #if !defined(USE_FAKE_LED)
447 ledInit(statusLedConfig());
455 #if defined(USE_BUTTONS)
459 delayMicroseconds(10); // allow configuration to settle // XXX Could be removed, too?
461 // Allow EEPROM reset with two-button-press without power cycling in DEBUG build
463 #define EEPROM_RESET_PRECONDITION true
465 #define EEPROM_RESET_PRECONDITION (!isMPUSoftReset())
468 if (EEPROM_RESET_PRECONDITION
) {
469 #if defined(BUTTON_A_PIN) && defined(BUTTON_B_PIN)
470 // two buttons required
471 uint8_t secondsRemaining
= 5;
472 bool bothButtonsHeld
;
474 bothButtonsHeld
= buttonAPressed() && buttonBPressed();
475 if (bothButtonsHeld
) {
476 if (--secondsRemaining
== 0) {
478 #ifdef USE_PERSISTENT_OBJECTS
479 persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON
, RESET_NONE
);
486 } while (bothButtonsHeld
);
490 #undef EEPROM_RESET_PRECONDITION
492 #endif // USE_BUTTONS
494 // Note that spektrumBind checks if a call is immediately after
495 // hard reset (including power cycle), so it should be called before
496 // systemClockSetHSEValue and OverclockRebootIfNecessary, as these
497 // may cause soft reset which will prevent spektrumBind not to execute
498 // the bind procedure.
500 #if defined(USE_SPEKTRUM_BIND)
501 if (featureIsEnabled(FEATURE_RX_SERIAL
)) {
502 switch (rxConfig()->serialrx_provider
) {
503 case SERIALRX_SPEKTRUM1024
:
504 case SERIALRX_SPEKTRUM2048
:
506 // Spektrum satellite binding if enabled on startup.
507 // Must be called before that 100ms sleep so that we don't lose satellite's binding window after startup.
508 // The rest of Spektrum initialization will happen later - via spektrumInit()
509 spektrumBind(rxConfigMutable());
515 #if defined(STM32F4) || defined(STM32G4)
516 // F4 has non-8MHz boards
517 // G4 for Betaflight allow 24 or 27MHz oscillator
518 systemClockSetHSEValue(systemConfig()->hseMhz
* 1000000U);
522 OverclockRebootIfNecessary(systemConfig()->cpu_overclock
);
525 // Configure MCO output after config is stable
527 // Note that mcoConfigure must be augmented with an additional argument to
528 // indicate which device instance to configure when MCO and MCO2 are both supported
530 #if defined(STM32F4) || defined(STM32F7)
531 // F4 and F7 support MCO on PA8 and MCO2 on PC9, but only MCO2 is supported for now
532 mcoConfigure(MCODEV_2
, mcoConfig(MCODEV_2
));
533 #elif defined(STM32G4)
534 // G4 only supports one MCO on PA8
535 mcoConfigure(MCODEV_1
, mcoConfig(MCODEV_1
));
537 #error Unsupported MCU
542 timerInit(); // timer must be initialized before any channel is allocated
545 #ifdef BUS_SWITCH_PIN
549 #if defined(USE_UART) && !defined(SIMULATOR_BUILD)
550 uartPinConfigure(serialPinConfig());
553 #if defined(AVOID_UART1_FOR_PWM_PPM)
554 serialInit(featureIsEnabled(FEATURE_SOFTSERIAL
),
555 featureIsEnabled(FEATURE_RX_PPM
) || featureIsEnabled(FEATURE_RX_PARALLEL_PWM
) ? SERIAL_PORT_USART1
: SERIAL_PORT_NONE
);
556 #elif defined(AVOID_UART2_FOR_PWM_PPM)
557 serialInit(featureIsEnabled(FEATURE_SOFTSERIAL
),
558 featureIsEnabled(FEATURE_RX_PPM
) || featureIsEnabled(FEATURE_RX_PARALLEL_PWM
) ? SERIAL_PORT_USART2
: SERIAL_PORT_NONE
);
559 #elif defined(AVOID_UART3_FOR_PWM_PPM)
560 serialInit(featureIsEnabled(FEATURE_SOFTSERIAL
),
561 featureIsEnabled(FEATURE_RX_PPM
) || featureIsEnabled(FEATURE_RX_PARALLEL_PWM
) ? SERIAL_PORT_USART3
: SERIAL_PORT_NONE
);
563 serialInit(featureIsEnabled(FEATURE_SOFTSERIAL
), SERIAL_PORT_NONE
);
566 mixerInit(mixerConfig()->mixerMode
);
568 uint16_t idlePulse
= motorConfig()->mincommand
;
569 if (featureIsEnabled(FEATURE_3D
)) {
570 idlePulse
= flight3DConfig()->neutral3d
;
572 if (motorConfig()->dev
.motorPwmProtocol
== PWM_TYPE_BRUSHED
) {
573 idlePulse
= 0; // brushed motors
576 /* Motors needs to be initialized soon as posible because hardware initialization
577 * may send spurious pulses to esc's causing their early initialization. Also ppm
578 * receiver may share timer with motors so motors MUST be initialized here. */
579 motorDevInit(&motorConfig()->dev
, idlePulse
, getMotorCount());
580 systemState
|= SYSTEM_STATE_MOTORS_READY
;
587 else if (featureIsEnabled(FEATURE_RX_PPM
)) {
588 ppmRxInit(ppmConfig());
592 else if (featureIsEnabled(FEATURE_RX_PARALLEL_PWM
)) {
593 pwmRxInit(pwmConfig());
598 beeperInit(beeperDevConfig());
600 /* temp until PGs are implemented. */
601 #if defined(USE_INVERTER) && !defined(SIMULATOR_BUILD)
602 initInverters(serialPinConfig());
606 #ifdef TARGET_BUS_INIT
611 // Depending on compilation options SPI/QSPI initialisation may already be done.
612 if (!(initFlags
& SPI_AND_QSPI_INIT_ATTEMPTED
)) {
613 configureSPIAndQuadSPI();
614 initFlags
|= SPI_AND_QSPI_INIT_ATTEMPTED
;
618 /* MSC mode will start after init, but will not allow scheduler to run,
619 * so there is no bottleneck in reading and writing data */
621 if (mscCheckBootAndReset() || mscCheckButton()) {
622 ledInit(statusLedConfig());
624 #if defined(USE_FLASHFS)
625 // If the blackbox device is onboard flash, then initialize and scan
626 // it to identify the log files *before* starting the USB device to
627 // prevent timeouts of the mass storage device.
628 if (blackboxConfig()->device
== BLACKBOX_DEVICE_FLASH
) {
632 // There's no more initialisation to be done, so enable DMA where possible for SPI
636 if (mscStart() == 0) {
639 systemResetFromMsc();
644 #ifdef USE_PERSISTENT_MSC_RTC
645 // if we didn't enter MSC mode then clear the persistent RTC value
646 persistentObjectWrite(PERSISTENT_OBJECT_RTC_HIGH
, 0);
647 persistentObjectWrite(PERSISTENT_OBJECT_RTC_LOW
, 0);
651 i2cHardwareConfigure(i2cConfig(0));
653 // Note: Unlike UARTs which are configured when client is present,
654 // I2C buses are initialized unconditionally if they are configured.
656 #ifdef USE_I2C_DEVICE_1
659 #ifdef USE_I2C_DEVICE_2
662 #ifdef USE_I2C_DEVICE_3
665 #ifdef USE_I2C_DEVICE_4
670 #endif // TARGET_BUS_INIT
672 #ifdef USE_HARDWARE_REVISION_DETECTION
673 updateHardwareRevision();
676 #ifdef USE_VTX_RTC6705
677 bool useRTC6705
= rtc6705IOInit(vtxIOConfig());
680 #ifdef USE_CAMERA_CONTROL
685 adcInit(adcConfig());
688 initBoardAlignment(boardAlignment());
690 if (!sensorsAutodetect()) {
691 // if gyro was not detected due to whatever reason, notify and don't arm.
693 #if defined(USE_UNIFIED_TARGET)
694 && isSystemConfigured()
697 indicateFailure(FAILURE_MISSING_ACC
, 2);
699 setArmingDisabled(ARMING_DISABLED_NO_GYRO
);
702 systemState
|= SYSTEM_STATE_SENSORS_READY
;
704 // Set the targetLooptime based on the detected gyro sampleRateHz and pid_process_denom
705 gyroSetTargetLooptime(pidConfig()->pid_process_denom
);
707 // Validate and correct the gyro config or PID loop time if needed
708 validateAndFixGyroConfig();
710 // Now reset the targetLooptime as it's possible for the validation to change the pid_process_denom
711 gyroSetTargetLooptime(pidConfig()->pid_process_denom
);
713 // Finally initialize the gyro filtering
716 pidInit(currentPidProfile
);
726 if (isMixerUsingServos()) {
727 //pwm_params.useChannelForwarding = featureIsEnabled(FEATURE_CHANNEL_FORWARDING);
728 servoDevInit(&servoConfig()->dev
);
734 pinioInit(pinioConfig());
737 #ifdef USE_PIN_PULL_UP_DOWN
738 pinPullupPulldownInit();
742 pinioBoxInit(pinioBoxConfig());
749 for (int i
= 0; i
< 10; i
++) {
752 #if defined(USE_BEEPER)
754 if (!(beeperConfig()->beeper_off_flags
& BEEPER_GET_FLAG(BEEPER_SYSTEM_INIT
))) {
773 if (featureIsEnabled(FEATURE_GPS
)) {
781 if (featureIsEnabled(FEATURE_LED_STRIP
)) {
787 if (featureIsEnabled(FEATURE_TELEMETRY
)) {
792 #ifdef USE_ESC_SENSOR
793 if (featureIsEnabled(FEATURE_ESC_SENSOR
)) {
798 #ifdef USE_USB_DETECT
799 usbCableDetectInit();
802 #ifdef USE_TRANSPONDER
803 if (featureIsEnabled(FEATURE_TRANSPONDER
)) {
805 transponderStartRepeating();
806 systemState
|= SYSTEM_STATE_TRANSPONDER_ENABLED
;
810 #ifdef USE_FLASH_CHIP
811 if (!(initFlags
& FLASH_INIT_ATTEMPTED
)) {
812 flashInit(flashConfig());
813 initFlags
|= FLASH_INIT_ATTEMPTED
;
822 if (blackboxConfig()->device
== BLACKBOX_DEVICE_SDCARD
) {
823 if (sdcardConfig()->mode
) {
824 if (!(initFlags
& SD_INIT_ATTEMPTED
)) {
825 initFlags
|= SD_INIT_ATTEMPTED
;
835 if (mixerConfig()->mixerMode
== MIXER_GIMBAL
) {
836 accStartCalibration();
839 gyroStartCalibration(false);
841 baroStartCalibration();
844 #if defined(USE_VTX_COMMON) || defined(USE_VTX_CONTROL)
848 #ifdef USE_VTX_CONTROL
851 #if defined(USE_VTX_COMMON)
855 #ifdef USE_VTX_SMARTAUDIO
863 #ifdef USE_VTX_RTC6705
864 if (!vtxCommonDevice() && useRTC6705
) { // external VTX takes precedence when configured.
869 #endif // VTX_CONTROL
873 // TODO - not implemented yet
877 #ifdef SOFTSERIAL_LOOPBACK
878 // FIXME this is a hack, perhaps add a FUNCTION_LOOPBACK to support it properly
879 loopbackPort
= (serialPort_t
*)&(softSerialPorts
[0]);
880 if (!loopbackPort
->vTable
) {
881 loopbackPort
= openSoftSerial(0, NULL
, 19200, SERIAL_NOT_INVERTED
);
883 serialPrint(loopbackPort
, "LOOPBACK\r\n");
886 batteryInit(); // always needs doing, regardless of features.
890 #endif // USE_RCDEVICE
892 #ifdef USE_PERSISTENT_STATS
901 * CMS, display devices and OSD
907 #if (defined(USE_OSD) || (defined(USE_MSP_DISPLAYPORT) && defined(USE_CMS)))
908 displayPort_t
*osdDisplayPort
= NULL
;
909 osdDisplayPortDevice_e osdDisplayPortDevice
= OSD_DISPLAYPORT_DEVICE_NONE
;
913 //The OSD need to be initialised after GYRO to avoid GYRO initialisation failure on some targets
915 if (featureIsEnabled(FEATURE_OSD
)) {
916 osdDisplayPortDevice_e device
= osdConfig()->displayPortDevice
;
920 case OSD_DISPLAYPORT_DEVICE_AUTO
:
923 #if defined(USE_FRSKYOSD)
924 // Test OSD_DISPLAYPORT_DEVICE_FRSKYOSD first, since an FC could
925 // have a builtin MAX7456 but also an FRSKYOSD connected to an
927 case OSD_DISPLAYPORT_DEVICE_FRSKYOSD
:
928 osdDisplayPort
= frskyOsdDisplayPortInit(vcdProfile()->video_system
);
929 if (osdDisplayPort
|| device
== OSD_DISPLAYPORT_DEVICE_FRSKYOSD
) {
930 osdDisplayPortDevice
= OSD_DISPLAYPORT_DEVICE_FRSKYOSD
;
936 #if defined(USE_MAX7456)
937 case OSD_DISPLAYPORT_DEVICE_MAX7456
:
938 // If there is a max7456 chip for the OSD configured and detectd then use it.
939 if (max7456DisplayPortInit(vcdProfile(), &osdDisplayPort
) || device
== OSD_DISPLAYPORT_DEVICE_MAX7456
) {
940 osdDisplayPortDevice
= OSD_DISPLAYPORT_DEVICE_MAX7456
;
946 #if defined(USE_CMS) && defined(USE_MSP_DISPLAYPORT) && defined(USE_OSD_OVER_MSP_DISPLAYPORT)
947 case OSD_DISPLAYPORT_DEVICE_MSP
:
948 osdDisplayPort
= displayPortMspInit();
949 if (osdDisplayPort
|| device
== OSD_DISPLAYPORT_DEVICE_MSP
) {
950 osdDisplayPortDevice
= OSD_DISPLAYPORT_DEVICE_MSP
;
956 // Other device cases can be added here
958 case OSD_DISPLAYPORT_DEVICE_NONE
:
963 // osdInit will register with CMS by itself.
964 osdInit(osdDisplayPort
, osdDisplayPortDevice
);
966 if (osdDisplayPortDevice
== OSD_DISPLAYPORT_DEVICE_NONE
) {
967 featureDisableImmediate(FEATURE_OSD
);
972 #if defined(USE_CMS) && defined(USE_MSP_DISPLAYPORT)
973 // If BFOSD is not active, then register MSP_DISPLAYPORT as a CMS device.
974 if (!osdDisplayPort
) {
975 cmsDisplayPortRegister(displayPortMspInit());
980 // Dashbord will register with CMS by itself.
981 if (featureIsEnabled(FEATURE_DASHBOARD
)) {
983 #ifdef USE_OLED_GPS_DEBUG_PAGE_ONLY
984 dashboardShowFixedPage(PAGE_GPS
);
986 dashboardResetPageCycling();
987 dashboardEnablePageCycling();
992 setArmingDisabled(ARMING_DISABLED_BOOT_GRACE_TIME
);
1000 // Attempt to enable DMA on all SPI busses
1010 systemState
|= SYSTEM_STATE_READY
;