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/sensor.h"
31 #include "drivers/system.h"
32 #include "drivers/gpio.h"
33 #include "drivers/light_led.h"
34 #include "drivers/sound_beeper.h"
35 #include "drivers/timer.h"
36 #include "drivers/serial.h"
37 #include "drivers/serial_softserial.h"
38 #include "drivers/serial_uart.h"
39 #include "drivers/accgyro.h"
40 #include "drivers/compass.h"
41 #include "drivers/pwm_mapping.h"
42 #include "drivers/pwm_rx.h"
43 #include "drivers/adc.h"
44 #include "drivers/bus_i2c.h"
45 #include "drivers/bus_spi.h"
46 #include "drivers/inverter.h"
48 #include "flight/flight.h"
49 #include "flight/mixer.h"
51 #include "io/serial.h"
52 #include "flight/failsafe.h"
53 #include "flight/navigation.h"
57 #include "io/escservo.h"
58 #include "io/rc_controls.h"
59 #include "io/gimbal.h"
60 #include "io/ledstrip.h"
61 #include "io/display.h"
62 #include "sensors/sensors.h"
63 #include "sensors/sonar.h"
64 #include "sensors/barometer.h"
65 #include "sensors/compass.h"
66 #include "sensors/acceleration.h"
67 #include "sensors/gyro.h"
68 #include "telemetry/telemetry.h"
69 #include "blackbox/blackbox.h"
70 #include "sensors/battery.h"
71 #include "sensors/boardalignment.h"
72 #include "config/runtime_config.h"
73 #include "config/config.h"
74 #include "config/config_profile.h"
75 #include "config/config_master.h"
77 #ifdef USE_HARDWARE_REVISION_DETECTION
78 #include "hardware_revision.h"
81 #include "build_config.h"
83 #ifdef DEBUG_SECTION_TIMES
84 uint32_t sectionTimes
[2][4];
86 extern uint32_t previousTime
;
88 #ifdef SOFTSERIAL_LOOPBACK
89 serialPort_t
*loopbackPort
;
94 void initPrintfSupport(void);
96 void initTelemetry(void);
97 void serialInit(serialConfig_t
*initialSerialConfig
);
98 failsafe_t
* failsafeInit(rxConfig_t
*intialRxConfig
);
99 pwmOutputConfiguration_t
*pwmInit(drv_pwm_config_t
*init
);
100 void mixerInit(mixerMode_e mixerMode
, motorMixer_t
*customMixers
);
101 void mixerUsePWMOutputConfiguration(pwmOutputConfiguration_t
*pwmOutputConfiguration
);
102 void rxInit(rxConfig_t
*rxConfig
, failsafe_t
*failsafe
);
103 void beepcodeInit(failsafe_t
*initialFailsafe
);
104 void gpsInit(serialConfig_t
*serialConfig
, gpsConfig_t
*initialGpsConfig
);
105 void navigationInit(gpsProfile_t
*initialGpsProfile
, pidProfile_t
*pidProfile
);
106 bool sensorsAutodetect(sensorAlignmentConfig_t
*sensorAlignmentConfig
, uint16_t gyroLpf
, uint8_t accHardwareToUse
, int8_t magHardwareToUse
, int16_t magDeclinationFromConfig
);
108 void displayInit(rxConfig_t
*intialRxConfig
);
109 void ledStripInit(ledConfig_t
*ledConfigsToUse
, hsvColor_t
*colorsToUse
, failsafe_t
* failsafeToUse
);
111 void spektrumBind(rxConfig_t
*rxConfig
);
114 // from system_stm32f30x.c
115 void SetSysClock(void);
118 // from system_stm32f10x.c
119 void SetSysClock(bool overclock
);
125 drv_pwm_config_t pwm_params
;
126 bool sensorsOK
= false;
132 ensureEEPROMContainsValidData();
137 SCB
->CPACR
= (0x3 << (10*2)) | (0x3 << (11*2));
144 // Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers
145 // Configure the Flash Latency cycles and enable prefetch buffer
146 SetSysClock(masterConfig
.emf_avoidance
);
149 #ifdef USE_HARDWARE_REVISION_DETECTION
150 detectHardwareRevision();
158 if (feature(FEATURE_RX_SERIAL
)) {
159 switch (masterConfig
.rxConfig
.serialrx_provider
) {
160 case SERIALRX_SPEKTRUM1024
:
161 case SERIALRX_SPEKTRUM2048
:
162 // Spektrum satellite binding if enabled on startup.
163 // Must be called before that 100ms sleep so that we don't lose satellite's binding window after startup.
164 // The rest of Spektrum initialization will happen later - via spektrumInit()
165 spektrumBind(&masterConfig
.rxConfig
);
173 timerInit(); // timer must be initialized before any channel is allocated
176 beeperConfig_t beeperConfig
= {
177 .gpioMode
= Mode_Out_OD
,
179 .gpioPort
= BEEP_GPIO
,
180 .gpioPeripheral
= BEEP_PERIPHERAL
,
184 if (hardwareRevision
>= NAZE32_REV5
) {
185 // naze rev4 and below used opendrain to PNP for buzzer. Rev5 and above use PP to NPN.
186 beeperConfig
.gpioMode
= Mode_Out_PP
;
187 beeperConfig
.isInverted
= true;
191 beeperInit(&beeperConfig
);
204 #ifdef USE_HARDWARE_REVISION_DETECTION
205 updateHardwareRevision();
210 if (hardwareRevision
!= NAZE32_SP
) {
214 if (!doesConfigurationUsePort(SERIAL_PORT_USART3
)) {
223 drv_adc_config_t adc_params
;
225 adc_params
.enableRSSI
= feature(FEATURE_RSSI_ADC
);
226 adc_params
.enableCurrentMeter
= feature(FEATURE_CURRENT_METER
);
227 adc_params
.enableExternal1
= false;
229 adc_params
.enableExternal1
= true;
232 // optional ADC5 input on rev.5 hardware
233 adc_params
.enableExternal1
= (hardwareRevision
>= NAZE32_REV5
);
236 adcInit(&adc_params
);
240 initBoardAlignment(&masterConfig
.boardAlignment
);
243 if (feature(FEATURE_DISPLAY
)) {
244 displayInit(&masterConfig
.rxConfig
);
248 // We have these sensors; SENSORS_SET defined in board.h depending on hardware platform
249 sensorsSet(SENSORS_SET
);
250 // drop out any sensors that don't seem to work, init all the others. halt if gyro is dead.
251 sensorsOK
= sensorsAutodetect(&masterConfig
.sensorAlignmentConfig
, masterConfig
.gyro_lpf
, masterConfig
.acc_hardware
, masterConfig
.mag_hardware
, currentProfile
->mag_declination
);
253 // if gyro was not detected due to whatever reason, we give up now.
259 for (i
= 0; i
< 10; i
++) {
271 mixerInit(masterConfig
.mixerMode
, masterConfig
.customMixer
);
274 if (sensors(SENSOR_MAG
))
278 serialInit(&masterConfig
.serialConfig
);
280 memset(&pwm_params
, 0, sizeof(pwm_params
));
281 // when using airplane/wing mixer, servo/motor outputs are remapped
282 if (masterConfig
.mixerMode
== MIXER_AIRPLANE
|| masterConfig
.mixerMode
== MIXER_FLYING_WING
)
283 pwm_params
.airplane
= true;
285 pwm_params
.airplane
= false;
286 #if defined(SERIAL_PORT_USART2) && defined(STM32F10X)
287 pwm_params
.useUART2
= doesConfigurationUsePort(SERIAL_PORT_USART2
);
289 pwm_params
.useVbat
= feature(FEATURE_VBAT
);
290 pwm_params
.useSoftSerial
= feature(FEATURE_SOFTSERIAL
);
291 pwm_params
.useParallelPWM
= feature(FEATURE_RX_PARALLEL_PWM
);
292 pwm_params
.useRSSIADC
= feature(FEATURE_RSSI_ADC
);
293 pwm_params
.useCurrentMeterADC
= feature(FEATURE_CURRENT_METER
)
294 && masterConfig
.batteryConfig
.currentMeterType
== CURRENT_SENSOR_ADC
;
295 pwm_params
.useLEDStrip
= feature(FEATURE_LED_STRIP
);
296 pwm_params
.usePPM
= feature(FEATURE_RX_PPM
);
297 pwm_params
.useOneshot
= feature(FEATURE_ONESHOT125
);
298 pwm_params
.useSerialRx
= feature(FEATURE_RX_SERIAL
);
299 pwm_params
.useServos
= isMixerUsingServos();
300 pwm_params
.extraServos
= currentProfile
->gimbalConfig
.gimbal_flags
& GIMBAL_FORWARDAUX
;
301 pwm_params
.motorPwmRate
= masterConfig
.motor_pwm_rate
;
302 pwm_params
.servoPwmRate
= masterConfig
.servo_pwm_rate
;
303 pwm_params
.idlePulse
= PULSE_1MS
; // standard PWM for brushless ESC (default, overridden below)
304 if (feature(FEATURE_3D
))
305 pwm_params
.idlePulse
= masterConfig
.flight3DConfig
.neutral3d
;
306 if (pwm_params
.motorPwmRate
> 500)
307 pwm_params
.idlePulse
= 0; // brushed motors
308 pwm_params
.servoCenterPulse
= masterConfig
.escAndServoConfig
.servoCenterPulse
;
310 pwmRxInit(masterConfig
.inputFilteringMode
);
312 pwmOutputConfiguration_t
*pwmOutputConfiguration
= pwmInit(&pwm_params
);
314 mixerUsePWMOutputConfiguration(pwmOutputConfiguration
);
316 failsafe
= failsafeInit(&masterConfig
.rxConfig
);
317 beepcodeInit(failsafe
);
318 rxInit(&masterConfig
.rxConfig
, failsafe
);
321 if (feature(FEATURE_GPS
)) {
323 &masterConfig
.serialConfig
,
324 &masterConfig
.gpsConfig
327 ¤tProfile
->gpsProfile
,
328 ¤tProfile
->pidProfile
334 if (feature(FEATURE_SONAR
)) {
340 ledStripInit(masterConfig
.ledConfigs
, masterConfig
.colors
, failsafe
);
342 if (feature(FEATURE_LED_STRIP
)) {
348 if (feature(FEATURE_TELEMETRY
))
356 previousTime
= micros();
358 if (masterConfig
.mixerMode
== MIXER_GIMBAL
) {
359 accSetCalibrationCycles(CALIBRATING_ACC_CYCLES
);
361 gyroSetCalibrationCycles(CALIBRATING_GYRO_CYCLES
);
363 baroSetCalibrationCycles(CALIBRATING_BARO_CYCLES
);
367 // TODO - not implemented yet
370 ENABLE_STATE(SMALL_ANGLE
);
371 DISABLE_ARMING_FLAG(PREVENT_ARMING
);
373 #ifdef SOFTSERIAL_LOOPBACK
374 // FIXME this is a hack, perhaps add a FUNCTION_LOOPBACK to support it properly
375 loopbackPort
= (serialPort_t
*)&(softSerialPorts
[0]);
376 if (!loopbackPort
->vTable
) {
377 loopbackPort
= openSoftSerial(0, NULL
, 19200, SERIAL_NOT_INVERTED
);
379 serialPrint(loopbackPort
, "LOOPBACK\r\n");
382 // Now that everything has powered up the voltage and cell count be determined.
384 // Check battery type/voltage
385 if (feature(FEATURE_VBAT
))
386 batteryInit(&masterConfig
.batteryConfig
);
389 if (feature(FEATURE_DISPLAY
)) {
390 #ifdef USE_OLED_GPS_DEBUG_PAGE_ONLY
391 displayShowFixedPage(PAGE_GPS
);
393 displayEnablePageCycling();
403 #ifdef SOFTSERIAL_LOOPBACK
404 void processLoopback(void) {
406 uint8_t bytesWaiting
;
407 while ((bytesWaiting
= serialTotalBytesWaiting(loopbackPort
))) {
408 uint8_t b
= serialRead(loopbackPort
);
409 serialWrite(loopbackPort
, b
);
414 #define processLoopback()
426 void HardFault_Handler(void)
428 // fall out of the sky
429 writeAllMotors(masterConfig
.escAndServoConfig
.mincommand
);