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/>.
25 #include "common/axis.h"
26 #include "common/color.h"
27 #include "common/atomic.h"
28 #include "drivers/nvic.h"
30 #include "drivers/system.h"
31 #include "drivers/gpio.h"
32 #include "drivers/light_led.h"
33 #include "drivers/sound_beeper.h"
34 #include "drivers/timer.h"
35 #include "drivers/serial.h"
36 #include "drivers/serial_softserial.h"
37 #include "drivers/serial_uart.h"
38 #include "drivers/accgyro.h"
39 #include "drivers/pwm_mapping.h"
40 #include "drivers/pwm_rx.h"
41 #include "drivers/adc.h"
42 #include "drivers/bus_i2c.h"
43 #include "drivers/bus_spi.h"
44 #include "drivers/inverter.h"
46 #include "flight/flight.h"
47 #include "flight/mixer.h"
49 #include "io/serial.h"
50 #include "flight/failsafe.h"
51 #include "flight/navigation.h"
55 #include "io/escservo.h"
56 #include "io/rc_controls.h"
57 #include "io/gimbal.h"
58 #include "io/ledstrip.h"
59 #include "io/display.h"
60 #include "sensors/sensors.h"
61 #include "sensors/sonar.h"
62 #include "sensors/barometer.h"
63 #include "sensors/compass.h"
64 #include "sensors/acceleration.h"
65 #include "sensors/gyro.h"
66 #include "telemetry/telemetry.h"
67 #include "sensors/battery.h"
68 #include "sensors/boardalignment.h"
69 #include "config/runtime_config.h"
70 #include "config/config.h"
71 #include "config/config_profile.h"
72 #include "config/config_master.h"
75 #include "target/NAZE/hardware_revision.h"
78 #include "build_config.h"
80 #ifdef DEBUG_SECTION_TIMES
81 uint32_t sectionTimes
[2][4];
83 extern uint32_t previousTime
;
85 #ifdef SOFTSERIAL_LOOPBACK
86 serialPort_t
*loopbackPort
;
91 void initPrintfSupport(void);
93 void initTelemetry(void);
94 void serialInit(serialConfig_t
*initialSerialConfig
);
95 failsafe_t
* failsafeInit(rxConfig_t
*intialRxConfig
);
96 pwmOutputConfiguration_t
*pwmInit(drv_pwm_config_t
*init
);
97 void mixerInit(MultiType mixerConfiguration
, motorMixer_t
*customMixers
);
98 void mixerUsePWMOutputConfiguration(pwmOutputConfiguration_t
*pwmOutputConfiguration
);
99 void rxInit(rxConfig_t
*rxConfig
, failsafe_t
*failsafe
);
100 void beepcodeInit(failsafe_t
*initialFailsafe
);
101 void gpsInit(serialConfig_t
*serialConfig
, gpsConfig_t
*initialGpsConfig
);
102 void navigationInit(gpsProfile_t
*initialGpsProfile
, pidProfile_t
*pidProfile
);
103 bool sensorsAutodetect(sensorAlignmentConfig_t
*sensorAlignmentConfig
, uint16_t gyroLpf
, uint8_t accHardwareToUse
, int16_t magDeclinationFromConfig
);
105 void displayInit(rxConfig_t
*intialRxConfig
);
106 void ledStripInit(ledConfig_t
*ledConfigsToUse
, hsvColor_t
*colorsToUse
, failsafe_t
* failsafeToUse
);
108 void spektrumBind(rxConfig_t
*rxConfig
);
111 // from system_stm32f30x.c
112 void SetSysClock(void);
115 // from system_stm32f10x.c
116 void SetSysClock(bool overclock
);
122 drv_pwm_config_t pwm_params
;
123 drv_adc_config_t adc_params
;
124 bool sensorsOK
= false;
130 ensureEEPROMContainsValidData();
135 SCB
->CPACR
= (0x3 << (10*2)) | (0x3 << (11*2));
142 // Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers
143 // Configure the Flash Latency cycles and enable prefetch buffer
144 SetSysClock(masterConfig
.emf_avoidance
);
148 detectHardwareRevision();
153 // Spektrum sattelite bind - ported from Baseflight
154 if (feature(FEATURE_RX_MSP
)) {
155 switch (masterConfig
.rxConfig
.serialrx_provider
) {
156 case SERIALRX_SPEKTRUM1024
:
157 case SERIALRX_SPEKTRUM2048
:
158 // Spektrum satellite binding if enabled on startup.
159 // Must be called before that 100ms sleep so that we don't lose satellite's binding window after startup.
160 // The rest of Spektrum initialization will happen later - via spektrumInit()
161 spektrumBind(&masterConfig
.rxConfig
);
168 timerInit(); // timer must be initialized before any channel is allocated
173 beeperConfig_t beeperConfig
= {
174 .gpioMode
= Mode_Out_OD
,
176 .gpioPort
= BEEP_GPIO
,
177 .gpioPeripheral
= BEEP_PERIPHERAL
,
181 if (hardwareRevision
>= NAZE32_REV5
) {
182 // naze rev4 and below used opendrain to PNP for buzzer. Rev5 and above use PP to NPN.
183 beeperConfig
.gpioMode
= Mode_Out_PP
;
184 beeperConfig
.isInverted
= true;
188 beeperInit(&beeperConfig
);
202 updateHardwareRevision();
207 if (hardwareRevision
!= NAZE32_SP
) {
211 // Configure the rest of the stuff
217 adc_params
.enableRSSI
= feature(FEATURE_RSSI_ADC
);
218 adc_params
.enableCurrentMeter
= feature(FEATURE_CURRENT_METER
);
219 adc_params
.enableExternal1
= false;
221 adc_params
.enableExternal1
= true;
224 // optional ADC5 input on rev.5 hardware
225 adc_params
.enableExternal1
= (hardwareRevision
>= NAZE32_REV5
);
228 adcInit(&adc_params
);
232 initBoardAlignment(&masterConfig
.boardAlignment
);
235 if (feature(FEATURE_DISPLAY
)) {
236 displayInit(&masterConfig
.rxConfig
);
240 // We have these sensors; SENSORS_SET defined in board.h depending on hardware platform
241 sensorsSet(SENSORS_SET
);
242 // drop out any sensors that don't seem to work, init all the others. halt if gyro is dead.
243 sensorsOK
= sensorsAutodetect(&masterConfig
.sensorAlignmentConfig
, masterConfig
.gyro_lpf
, masterConfig
.acc_hardware
, currentProfile
->mag_declination
);
245 // if gyro was not detected due to whatever reason, we give up now.
251 for (i
= 0; i
< 10; i
++) {
263 mixerInit(masterConfig
.mixerConfiguration
, masterConfig
.customMixer
);
266 if (sensors(SENSOR_MAG
))
270 serialInit(&masterConfig
.serialConfig
);
272 memset(&pwm_params
, 0, sizeof(pwm_params
));
273 // when using airplane/wing mixer, servo/motor outputs are remapped
274 if (masterConfig
.mixerConfiguration
== MULTITYPE_AIRPLANE
|| masterConfig
.mixerConfiguration
== MULTITYPE_FLYING_WING
)
275 pwm_params
.airplane
= true;
277 pwm_params
.airplane
= false;
278 #if defined(SERIAL_PORT_USART2) && defined(STM32F10X)
279 pwm_params
.useUART2
= doesConfigurationUsePort(SERIAL_PORT_USART2
);
281 pwm_params
.useVbat
= feature(FEATURE_VBAT
);
282 pwm_params
.useSoftSerial
= feature(FEATURE_SOFTSERIAL
);
283 pwm_params
.useParallelPWM
= feature(FEATURE_RX_PARALLEL_PWM
);
284 pwm_params
.useRSSIADC
= feature(FEATURE_RSSI_ADC
);
285 pwm_params
.useCurrentMeterADC
= feature(FEATURE_CURRENT_METER
);
286 pwm_params
.useLEDStrip
= feature(FEATURE_LED_STRIP
);
287 pwm_params
.usePPM
= feature(FEATURE_RX_PPM
);
288 pwm_params
.useOneshot
= feature(FEATURE_ONESHOT125
);
289 pwm_params
.useServos
= isMixerUsingServos();
290 pwm_params
.extraServos
= currentProfile
->gimbalConfig
.gimbal_flags
& GIMBAL_FORWARDAUX
;
291 pwm_params
.motorPwmRate
= masterConfig
.motor_pwm_rate
;
292 pwm_params
.servoPwmRate
= masterConfig
.servo_pwm_rate
;
293 pwm_params
.idlePulse
= PULSE_1MS
; // standard PWM for brushless ESC (default, overridden below)
294 if (feature(FEATURE_3D
))
295 pwm_params
.idlePulse
= masterConfig
.flight3DConfig
.neutral3d
;
296 if (pwm_params
.motorPwmRate
> 500)
297 pwm_params
.idlePulse
= 0; // brushed motors
298 pwm_params
.servoCenterPulse
= masterConfig
.rxConfig
.midrc
;
300 pwmRxInit(masterConfig
.inputFilteringMode
);
302 pwmOutputConfiguration_t
*pwmOutputConfiguration
= pwmInit(&pwm_params
);
304 mixerUsePWMOutputConfiguration(pwmOutputConfiguration
);
306 failsafe
= failsafeInit(&masterConfig
.rxConfig
);
307 beepcodeInit(failsafe
);
308 rxInit(&masterConfig
.rxConfig
, failsafe
);
311 if (feature(FEATURE_GPS
)) {
313 &masterConfig
.serialConfig
,
314 &masterConfig
.gpsConfig
317 ¤tProfile
->gpsProfile
,
318 ¤tProfile
->pidProfile
324 if (feature(FEATURE_SONAR
)) {
330 ledStripInit(masterConfig
.ledConfigs
, masterConfig
.colors
, failsafe
);
332 if (feature(FEATURE_LED_STRIP
)) {
338 if (feature(FEATURE_TELEMETRY
))
342 previousTime
= micros();
344 if (masterConfig
.mixerConfiguration
== MULTITYPE_GIMBAL
) {
345 accSetCalibrationCycles(CALIBRATING_ACC_CYCLES
);
347 gyroSetCalibrationCycles(CALIBRATING_GYRO_CYCLES
);
349 baroSetCalibrationCycles(CALIBRATING_BARO_CYCLES
);
353 // TODO - not implemented yet
356 ENABLE_STATE(SMALL_ANGLE
);
357 DISABLE_ARMING_FLAG(PREVENT_ARMING
);
359 #ifdef SOFTSERIAL_LOOPBACK
360 // FIXME this is a hack, perhaps add a FUNCTION_LOOPBACK to support it properly
361 loopbackPort
= (serialPort_t
*)&(softSerialPorts
[0]);
362 if (!loopbackPort
->vTable
) {
363 loopbackPort
= openSoftSerial(0, NULL
, 19200, SERIAL_NOT_INVERTED
);
365 serialPrint(loopbackPort
, "LOOPBACK\r\n");
368 // Now that everything has powered up the voltage and cell count be determined.
370 // Check battery type/voltage
371 if (feature(FEATURE_VBAT
))
372 batteryInit(&masterConfig
.batteryConfig
);
375 if (feature(FEATURE_DISPLAY
)) {
376 #ifdef USE_OLED_GPS_DEBUG_PAGE_ONLY
377 displayShowFixedPage(PAGE_GPS
);
379 displayEnablePageCycling();
385 #ifdef SOFTSERIAL_LOOPBACK
386 void processLoopback(void) {
388 uint8_t bytesWaiting
;
389 while ((bytesWaiting
= serialTotalBytesWaiting(loopbackPort
))) {
390 uint8_t b
= serialRead(loopbackPort
);
391 serialWrite(loopbackPort
, b
);
396 #define processLoopback()
408 void HardFault_Handler(void)
410 // fall out of the sky
411 writeAllMotors(masterConfig
.escAndServoConfig
.mincommand
);