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 "build_config.h"
28 #include "common/axis.h"
29 #include "common/color.h"
30 #include "common/maths.h"
32 #include "drivers/system.h"
34 #include "drivers/sensor.h"
35 #include "drivers/accgyro.h"
36 #include "drivers/compass.h"
38 #include "drivers/serial.h"
39 #include "drivers/bus_i2c.h"
40 #include "drivers/gpio.h"
41 #include "drivers/timer.h"
42 #include "drivers/pwm_rx.h"
44 #include "flight/flight.h"
45 #include "flight/mixer.h"
46 #include "flight/failsafe.h"
47 #include "flight/navigation.h"
48 #include "flight/altitudehold.h"
52 #include "io/escservo.h"
53 #include "io/rc_controls.h"
55 #include "io/gimbal.h"
56 #include "io/serial.h"
57 #include "io/ledstrip.h"
58 #include "telemetry/telemetry.h"
60 #include "sensors/boardalignment.h"
61 #include "sensors/sensors.h"
62 #include "sensors/battery.h"
63 #include "sensors/sonar.h"
64 #include "sensors/acceleration.h"
65 #include "sensors/barometer.h"
66 #include "sensors/compass.h"
67 #include "sensors/gyro.h"
69 #include "config/runtime_config.h"
70 #include "config/config.h"
71 #include "config/config_profile.h"
72 #include "config/config_master.h"
76 #include "hardware_revision.h"
79 #include "serial_msp.h"
81 static serialPort_t
*mspSerialPort
;
83 extern uint16_t cycleTime
; // FIXME dependency on mw.c
84 extern uint16_t rssi
; // FIXME dependency on mw.c
85 extern int16_t debug
[4]; // FIXME dependency on mw.c
87 void useRcControlsConfig(modeActivationCondition_t
*modeActivationConditions
, escAndServoConfig_t
*escAndServoConfigToUse
, pidProfile_t
*pidProfileToUse
);
90 * MSP Guidelines, emphasis is used to clarify.
92 * Each FlightController (FC, Server) MUST change the API version when any MSP command is added, deleted, or changed.
94 * If you fork the FC source code and release your own version, you MUST change the Flight Controller Identifier.
96 * NEVER release a modified copy of this code that shares the same Flight controller IDENT and API version
97 * if the API doesn't match EXACTLY.
99 * Consumers of the API (API clients) SHOULD first attempt to get a response from the MSP_API_VERSION command.
100 * If no response is obtained then client MAY try the legacy MSP_IDENT command.
102 * API consumers should ALWAYS handle communication failures gracefully and attempt to continue
103 * without the information if possible. Clients MAY log/display a suitable message.
105 * API clients should NOT attempt any communication if they can't handle the returned API MAJOR VERSION.
107 * API clients SHOULD attempt communication if the API MINOR VERSION has increased from the time
108 * the API client was written and handle command failures gracefully. Clients MAY disable
109 * functionality that depends on the commands while still leaving other functionality intact.
110 * Clients SHOULD operate in READ-ONLY mode and SHOULD present a warning to the user to state
111 * that the newer API version may cause problems before using API commands that change FC state.
113 * It is for this reason that each MSP command should be specific as possible, such that changes
114 * to commands break as little functionality as possible.
116 * API client authors MAY use a compatibility matrix/table when determining if they can support
117 * a given command from a given flight controller at a given api version level.
119 * Developers MUST NOT create new MSP commands that do more than one thing.
121 * Failure to follow these guidelines will likely invoke the wrath of developers trying to write tools
122 * that use the API and the users of those tools.
125 #define MSP_PROTOCOL_VERSION 0
127 #define API_VERSION_MAJOR 1 // increment when major changes are made
128 #define API_VERSION_MINOR 5 // increment when any change is made, reset to zero when major changes are released after changing API_VERSION_MAJOR
130 #define API_VERSION_LENGTH 2
132 #define MULTIWII_IDENTIFIER "MWII";
133 #define CLEANFLIGHT_IDENTIFIER "CLFL"
134 #define BASEFLIGHT_IDENTIFIER "BAFL";
136 #define FLIGHT_CONTROLLER_IDENTIFIER_LENGTH 4
137 static const char *flightControllerIdentifier
= CLEANFLIGHT_IDENTIFIER
; // 4 UPPER CASE alpha numeric characters that identify the flight controller.
139 #define FLIGHT_CONTROLLER_VERSION_LENGTH 3
140 #define FLIGHT_CONTROLLER_VERSION_MASK 0xFFF
142 const char *boardIdentifier
= TARGET_BOARD_IDENTIFIER
;
143 #define BOARD_IDENTIFIER_LENGTH 4 // 4 UPPER CASE alpha numeric characters that identify the board being used.
144 #define BOARD_HARDWARE_REVISION_LENGTH 2
146 // These are baseflight specific flags but they are useless now since MW 2.3 uses the upper 4 bits for the navigation version.
147 #define CAP_PLATFORM_32BIT ((uint32_t)1 << 31)
148 #define CAP_BASEFLIGHT_CONFIG ((uint32_t)1 << 30)
150 // MW 2.3 stores NAVI_VERSION in the top 4 bits of the capability mask.
151 #define CAP_NAVI_VERSION_BIT_4_MSB ((uint32_t)1 << 31)
152 #define CAP_NAVI_VERSION_BIT_3 ((uint32_t)1 << 30)
153 #define CAP_NAVI_VERSION_BIT_2 ((uint32_t)1 << 29)
154 #define CAP_NAVI_VERSION_BIT_1_LSB ((uint32_t)1 << 28)
156 #define CAP_DYNBALANCE ((uint32_t)1 << 2)
157 #define CAP_FLAPS ((uint32_t)1 << 3)
158 #define CAP_NAVCAP ((uint32_t)1 << 4)
159 #define CAP_EXTAUX ((uint32_t)1 << 5)
161 #define MSP_API_VERSION 1 //out message
162 #define MSP_FC_VARIANT 2 //out message
163 #define MSP_FC_VERSION 3 //out message
164 #define MSP_BOARD_INFO 4 //out message
165 #define MSP_BUILD_INFO 5 //out message
168 // MSP commands for Cleanflight original features
170 #define MSP_CHANNEL_FORWARDING 32 //out message Returns channel forwarding settings
171 #define MSP_SET_CHANNEL_FORWARDING 33 //in message Channel forwarding settings
173 #define MSP_MODE_RANGES 34 //out message Returns all mode ranges
174 #define MSP_SET_MODE_RANGE 35 //in message Sets a single mode range
176 #define MSP_FEATURE 36
177 #define MSP_SET_FEATURE 37
179 #define MSP_BOARD_ALIGNMENT 38
180 #define MSP_SET_BOARD_ALIGNMENT 39
182 #define MSP_CURRENT_METER_CONFIG 40
183 #define MSP_SET_CURRENT_METER_CONFIG 41
186 #define MSP_SET_MIXER 43
188 #define MSP_RX_CONFIG 44
189 #define MSP_SET_RX_CONFIG 45
191 #define MSP_LED_COLORS 46
192 #define MSP_SET_LED_COLORS 47
194 #define MSP_LED_STRIP_CONFIG 48
195 #define MSP_SET_LED_STRIP_CONFIG 49
197 #define MSP_RSSI_CONFIG 50
198 #define MSP_SET_RSSI_CONFIG 51
200 #define MSP_ADJUSTMENT_RANGES 52
201 #define MSP_SET_ADJUSTMENT_RANGE 53
203 // private - only to be used by the configurator, the commands are likely to change
204 #define MSP_CF_SERIAL_CONFIG 54
205 #define MSP_SET_CF_SERIAL_CONFIG 55
207 #define MSP_VOLTAGE_METER_CONFIG 56
208 #define MSP_SET_VOLTAGE_METER_CONFIG 57
210 #define MSP_SONAR_ALTITUDE 58 //out message get sonar altitude [cm]
212 #define MSP_PID_CONTROLLER 59
213 #define MSP_SET_PID_CONTROLLER 60
215 // Baseflight MSP commands (if enabled they exist in Cleanflight)
217 #define MSP_RX_MAP 64 //out message get channel map (also returns number of channels total)
218 #define MSP_SET_RX_MAP 65 //in message set rx map, numchannels to set comes from MSP_RX_MAP
220 // FIXME - Provided for backwards compatibility with configurator code until configurator is updated.
221 // DEPRECATED - DO NOT USE "MSP_BF_CONFIG" and MSP_SET_BF_CONFIG. In Cleanflight, isolated commands already exist and should be used instead.
222 #define MSP_BF_CONFIG 66 //out message baseflight-specific settings that aren't covered elsewhere
223 #define MSP_SET_BF_CONFIG 67 //in message baseflight-specific settings save
225 #define MSP_REBOOT 68 //in message reboot settings
227 // DEPRECATED - Use MSP_BUILD_INFO instead
228 #define MSP_BF_BUILD_INFO 69 //out message build date as well as some space for future expansion
231 // Multwii original MSP commands
234 // DEPRECATED - See MSP_API_VERSION and MSP_MIXER
235 #define MSP_IDENT 100 //out message mixerMode + multiwii version + protocol version + capability variable
238 #define MSP_STATUS 101 //out message cycletime & errors_count & sensor present & box activation & current setting number
239 #define MSP_RAW_IMU 102 //out message 9 DOF
240 #define MSP_SERVO 103 //out message 8 servos
241 #define MSP_MOTOR 104 //out message 8 motors
242 #define MSP_RC 105 //out message 8 rc chan and more
243 #define MSP_RAW_GPS 106 //out message fix, numsat, lat, lon, alt, speed, ground course
244 #define MSP_COMP_GPS 107 //out message distance home, direction home
245 #define MSP_ATTITUDE 108 //out message 2 angles 1 heading
246 #define MSP_ALTITUDE 109 //out message altitude, variometer
247 #define MSP_ANALOG 110 //out message vbat, powermetersum, rssi if available on RX
248 #define MSP_RC_TUNING 111 //out message rc rate, rc expo, rollpitch rate, yaw rate, dyn throttle PID
249 #define MSP_PID 112 //out message P I D coeff (9 are used currently)
250 #define MSP_BOX 113 //out message BOX setup (number is dependant of your setup)
251 #define MSP_MISC 114 //out message powermeter trig
252 #define MSP_MOTOR_PINS 115 //out message which pins are in use for motors & servos, for GUI
253 #define MSP_BOXNAMES 116 //out message the aux switch names
254 #define MSP_PIDNAMES 117 //out message the PID names
255 #define MSP_WP 118 //out message get a WP, WP# is in the payload, returns (WP#, lat, lon, alt, flags) WP#0-home, WP#16-poshold
256 #define MSP_BOXIDS 119 //out message get the permanent IDs associated to BOXes
257 #define MSP_SERVO_CONF 120 //out message Servo settings
258 #define MSP_NAV_STATUS 121 //out message Returns navigation status
259 #define MSP_NAV_CONFIG 122 //out message Returns navigation parameters
261 #define MSP_SET_RAW_RC 200 //in message 8 rc chan
262 #define MSP_SET_RAW_GPS 201 //in message fix, numsat, lat, lon, alt, speed
263 #define MSP_SET_PID 202 //in message P I D coeff (9 are used currently)
264 #define MSP_SET_BOX 203 //in message BOX setup (number is dependant of your setup)
265 #define MSP_SET_RC_TUNING 204 //in message rc rate, rc expo, rollpitch rate, yaw rate, dyn throttle PID
266 #define MSP_ACC_CALIBRATION 205 //in message no param
267 #define MSP_MAG_CALIBRATION 206 //in message no param
268 #define MSP_SET_MISC 207 //in message powermeter trig + 8 free for future use
269 #define MSP_RESET_CONF 208 //in message no param
270 #define MSP_SET_WP 209 //in message sets a given WP (WP#,lat, lon, alt, flags)
271 #define MSP_SELECT_SETTING 210 //in message Select Setting Number (0-2)
272 #define MSP_SET_HEAD 211 //in message define a new heading hold direction
273 #define MSP_SET_SERVO_CONF 212 //in message Servo settings
274 #define MSP_SET_MOTOR 214 //in message PropBalance function
275 #define MSP_SET_NAV_CONFIG 215 //in message Sets nav config parameters - write to the eeprom
277 // #define MSP_BIND 240 //in message no param
279 #define MSP_EEPROM_WRITE 250 //in message no param
281 #define MSP_DEBUGMSG 253 //out message debug string buffer
282 #define MSP_DEBUG 254 //out message debug1,debug2,debug3,debug4
284 // Additional commands that are not compatible with MultiWii
285 #define MSP_UID 160 //out message Unique device ID
286 #define MSP_ACC_TRIM 240 //out message get acc angle trim values
287 #define MSP_SET_ACC_TRIM 239 //in message set acc angle trim values
288 #define MSP_GPSSVINFO 164 //out message get Signal Strength (only U-Blox)
290 #define INBUF_SIZE 64
292 typedef struct box_e
{
293 const uint8_t boxId
; // see boxId_e
294 const char *boxName
; // GUI-readable box name
295 const uint8_t permanentId
; //
299 static const box_t
const boxes
[CHECKBOX_ITEM_COUNT
+ 1] = {
300 { BOXARM
, "ARM;", 0 },
301 { BOXANGLE
, "ANGLE;", 1 },
302 { BOXHORIZON
, "HORIZON;", 2 },
303 { BOXBARO
, "BARO;", 3 },
304 //{ BOXVARIO, "VARIO;", 4 },
305 { BOXMAG
, "MAG;", 5 },
306 { BOXHEADFREE
, "HEADFREE;", 6 },
307 { BOXHEADADJ
, "HEADADJ;", 7 },
308 { BOXCAMSTAB
, "CAMSTAB;", 8 },
309 { BOXCAMTRIG
, "CAMTRIG;", 9 },
310 { BOXGPSHOME
, "GPS HOME;", 10 },
311 { BOXGPSHOLD
, "GPS HOLD;", 11 },
312 { BOXPASSTHRU
, "PASSTHRU;", 12 },
313 { BOXBEEPERON
, "BEEPER;", 13 },
314 { BOXLEDMAX
, "LEDMAX;", 14 },
315 { BOXLEDLOW
, "LEDLOW;", 15 },
316 { BOXLLIGHTS
, "LLIGHTS;", 16 },
317 { BOXCALIB
, "CALIB;", 17 },
318 { BOXGOV
, "GOVERNOR;", 18 },
319 { BOXOSD
, "OSD SW;", 19 },
320 { BOXTELEMETRY
, "TELEMETRY;", 20 },
321 { BOXAUTOTUNE
, "AUTOTUNE;", 21 },
322 { BOXSONAR
, "SONAR;", 22 },
323 { CHECKBOX_ITEM_COUNT
, NULL
, 0xFF }
326 // this is calculated at startup based on enabled features.
327 static uint8_t activeBoxIds
[CHECKBOX_ITEM_COUNT
];
328 // this is the number of filled indexes in above array
329 static uint8_t activeBoxIdCount
= 0;
331 extern int16_t motor_disarmed
[MAX_SUPPORTED_MOTORS
];
333 // cause reboot after MSP processing complete
334 static bool isRebootScheduled
= false;
336 static const char pidnames
[] =
363 typedef struct mspPort_s
{
369 uint8_t inBuf
[INBUF_SIZE
];
372 mspPortUsage_e mspPortUsage
;
375 static mspPort_t mspPorts
[MAX_MSP_PORT_COUNT
];
377 static mspPort_t
*currentPort
;
379 static void serialize32(uint32_t a
)
383 serialWrite(mspSerialPort
, t
);
384 currentPort
->checksum
^= t
;
386 serialWrite(mspSerialPort
, t
);
387 currentPort
->checksum
^= t
;
389 serialWrite(mspSerialPort
, t
);
390 currentPort
->checksum
^= t
;
392 serialWrite(mspSerialPort
, t
);
393 currentPort
->checksum
^= t
;
396 static void serialize16(int16_t a
)
400 serialWrite(mspSerialPort
, t
);
401 currentPort
->checksum
^= t
;
403 serialWrite(mspSerialPort
, t
);
404 currentPort
->checksum
^= t
;
407 static void serialize8(uint8_t a
)
409 serialWrite(mspSerialPort
, a
);
410 currentPort
->checksum
^= a
;
413 static uint8_t read8(void)
415 return currentPort
->inBuf
[currentPort
->indRX
++] & 0xff;
418 static uint16_t read16(void)
420 uint16_t t
= read8();
421 t
+= (uint16_t)read8() << 8;
425 static uint32_t read32(void)
427 uint32_t t
= read16();
428 t
+= (uint32_t)read16() << 16;
432 static void headSerialResponse(uint8_t err
, uint8_t s
)
436 serialize8(err
? '!' : '>');
437 currentPort
->checksum
= 0; // start calculating a new checksum
439 serialize8(currentPort
->cmdMSP
);
442 static void headSerialReply(uint8_t s
)
444 headSerialResponse(0, s
);
447 static void headSerialError(uint8_t s
)
449 headSerialResponse(1, s
);
452 static void tailSerialReply(void)
454 serialize8(currentPort
->checksum
);
457 static void s_struct(uint8_t *cb
, uint8_t siz
)
459 headSerialReply(siz
);
464 static void serializeNames(const char *s
)
471 static const box_t
*findBoxByActiveBoxId(uint8_t activeBoxId
)
474 const box_t
*candidate
;
475 for (boxIndex
= 0; boxIndex
< sizeof(boxes
) / sizeof(box_t
); boxIndex
++) {
476 candidate
= &boxes
[boxIndex
];
477 if (candidate
->boxId
== activeBoxId
) {
484 static const box_t
*findBoxByPermenantId(uint8_t permenantId
)
487 const box_t
*candidate
;
488 for (boxIndex
= 0; boxIndex
< sizeof(boxes
) / sizeof(box_t
); boxIndex
++) {
489 candidate
= &boxes
[boxIndex
];
490 if (candidate
->permanentId
== permenantId
) {
497 static void serializeBoxNamesReply(void)
499 int i
, activeBoxId
, j
, flag
= 1, count
= 0, len
;
503 // in first run of the loop, we grab total size of junk to be sent
504 // then come back and actually send it
505 for (i
= 0; i
< activeBoxIdCount
; i
++) {
506 activeBoxId
= activeBoxIds
[i
];
508 box
= findBoxByActiveBoxId(activeBoxId
);
513 len
= strlen(box
->boxName
);
517 for (j
= 0; j
< len
; j
++)
518 serialize8(box
->boxName
[j
]);
523 headSerialReply(count
);
529 static void resetMspPort(mspPort_t
*mspPortToReset
, serialPort_t
*serialPort
, mspPortUsage_e usage
)
531 memset(mspPortToReset
, 0, sizeof(mspPort_t
));
533 mspPortToReset
->port
= serialPort
;
534 mspPortToReset
->mspPortUsage
= usage
;
537 // This rate is chosen since softserial supports it.
538 #define MSP_FALLBACK_BAUDRATE 19200
540 void mspAllocateSerialPorts(serialConfig_t
*serialConfig
)
546 for (portIndex
= 0; portIndex
< MAX_MSP_PORT_COUNT
; portIndex
++) {
547 mspPort_t
*mspPort
= &mspPorts
[portIndex
];
548 if (mspPort
->mspPortUsage
!= UNUSED_PORT
) {
552 uint32_t baudRate
= serialConfig
->msp_baudrate
;
554 bool triedFallbackRate
= false;
557 port
= openSerialPort(FUNCTION_MSP
, NULL
, baudRate
, MODE_RXTX
, SERIAL_NOT_INVERTED
);
559 if (triedFallbackRate
) {
563 baudRate
= MSP_FALLBACK_BAUDRATE
;
564 triedFallbackRate
= true;
568 if (port
&& portIndex
< MAX_MSP_PORT_COUNT
) {
569 resetMspPort(mspPort
, port
, FOR_GENERAL_MSP
);
576 // XXX this function might help with adding support for MSP on more than one port, if not delete it.
577 const serialPortFunctionList_t
*serialPortFunctionList
= getSerialPortFunctionList();
578 UNUSED(serialPortFunctionList
);
581 void mspReleasePortIfAllocated(serialPort_t
*serialPort
)
584 for (portIndex
= 0; portIndex
< MAX_MSP_PORT_COUNT
; portIndex
++) {
585 mspPort_t
*candidateMspPort
= &mspPorts
[portIndex
];
586 if (candidateMspPort
->port
== serialPort
) {
587 endSerialPortFunction(serialPort
, FUNCTION_MSP
);
588 memset(candidateMspPort
, 0, sizeof(mspPort_t
));
593 void mspInit(serialConfig_t
*serialConfig
)
595 // calculate used boxes based on features and fill availableBoxes[] array
596 memset(activeBoxIds
, 0xFF, sizeof(activeBoxIds
));
598 activeBoxIdCount
= 0;
599 activeBoxIds
[activeBoxIdCount
++] = BOXARM
;
601 if (sensors(SENSOR_ACC
)) {
602 activeBoxIds
[activeBoxIdCount
++] = BOXANGLE
;
603 activeBoxIds
[activeBoxIdCount
++] = BOXHORIZON
;
606 if (sensors(SENSOR_BARO
)) {
607 activeBoxIds
[activeBoxIdCount
++] = BOXBARO
;
610 if (sensors(SENSOR_ACC
) || sensors(SENSOR_MAG
)) {
611 activeBoxIds
[activeBoxIdCount
++] = BOXMAG
;
612 activeBoxIds
[activeBoxIdCount
++] = BOXHEADFREE
;
613 activeBoxIds
[activeBoxIdCount
++] = BOXHEADADJ
;
616 if (feature(FEATURE_SERVO_TILT
))
617 activeBoxIds
[activeBoxIdCount
++] = BOXCAMSTAB
;
620 if (feature(FEATURE_GPS
)) {
621 activeBoxIds
[activeBoxIdCount
++] = BOXGPSHOME
;
622 activeBoxIds
[activeBoxIdCount
++] = BOXGPSHOLD
;
626 if (masterConfig
.mixerMode
== MIXER_FLYING_WING
|| masterConfig
.mixerMode
== MIXER_AIRPLANE
)
627 activeBoxIds
[activeBoxIdCount
++] = BOXPASSTHRU
;
629 activeBoxIds
[activeBoxIdCount
++] = BOXBEEPERON
;
632 if (feature(FEATURE_LED_STRIP
)) {
633 activeBoxIds
[activeBoxIdCount
++] = BOXLEDLOW
;
637 if (feature(FEATURE_INFLIGHT_ACC_CAL
))
638 activeBoxIds
[activeBoxIdCount
++] = BOXCALIB
;
640 activeBoxIds
[activeBoxIdCount
++] = BOXOSD
;
642 if (feature(FEATURE_TELEMETRY
&& masterConfig
.telemetryConfig
.telemetry_switch
))
643 activeBoxIds
[activeBoxIdCount
++] = BOXTELEMETRY
;
646 activeBoxIds
[activeBoxIdCount
++] = BOXAUTOTUNE
;
649 if (feature(FEATURE_SONAR
)){
650 activeBoxIds
[activeBoxIdCount
++] = BOXSONAR
;
653 memset(mspPorts
, 0x00, sizeof(mspPorts
));
654 mspAllocateSerialPorts(serialConfig
);
657 #define IS_ENABLED(mask) (mask == 0 ? 0 : 1)
659 static bool processOutCommand(uint8_t cmdMSP
)
661 uint32_t i
, tmp
, junk
;
665 int32_t lat
= 0, lon
= 0;
669 case MSP_API_VERSION
:
671 1 + // protocol version length
674 serialize8(MSP_PROTOCOL_VERSION
);
676 serialize8(API_VERSION_MAJOR
);
677 serialize8(API_VERSION_MINOR
);
681 headSerialReply(FLIGHT_CONTROLLER_IDENTIFIER_LENGTH
);
683 for (i
= 0; i
< FLIGHT_CONTROLLER_IDENTIFIER_LENGTH
; i
++) {
684 serialize8(flightControllerIdentifier
[i
]);
689 headSerialReply(FLIGHT_CONTROLLER_VERSION_LENGTH
);
691 serialize8(FC_VERSION_MAJOR
);
692 serialize8(FC_VERSION_MINOR
);
693 serialize8(FC_VERSION_PATCH_LEVEL
);
698 BOARD_IDENTIFIER_LENGTH
+
699 BOARD_HARDWARE_REVISION_LENGTH
701 for (i
= 0; i
< BOARD_IDENTIFIER_LENGTH
; i
++) {
702 serialize8(boardIdentifier
[i
]);
705 serialize16(hardwareRevision
);
707 serialize16(0); // No other build targets currently have hardware revision detection.
715 GIT_SHORT_REVISION_LENGTH
718 for (i
= 0; i
< BUILD_DATE_LENGTH
; i
++) {
719 serialize8(buildDate
[i
]);
721 for (i
= 0; i
< BUILD_TIME_LENGTH
; i
++) {
722 serialize8(buildTime
[i
]);
725 for (i
= 0; i
< GIT_SHORT_REVISION_LENGTH
; i
++) {
726 serialize8(shortGitRevision
[i
]);
730 // DEPRECATED - Use MSP_API_VERSION
733 serialize8(MW_VERSION
);
734 serialize8(masterConfig
.mixerMode
);
735 serialize8(MSP_PROTOCOL_VERSION
);
736 serialize32(CAP_DYNBALANCE
| (masterConfig
.airplaneConfig
.flaps_speed
? CAP_FLAPS
: 0)); // "capability"
741 serialize16(cycleTime
);
743 serialize16(i2cGetErrorCounter());
747 serialize16(sensors(SENSOR_ACC
) | sensors(SENSOR_BARO
) << 1 | sensors(SENSOR_MAG
) << 2 | sensors(SENSOR_GPS
) << 3 | sensors(SENSOR_SONAR
) << 4);
748 // Serialize the flags in the order we delivered them, ignoring BOXNAMES and BOXINDEXES
749 // Requires new Multiwii protocol version to fix
750 // It would be preferable to setting the enabled bits based on BOXINDEX.
752 tmp
= IS_ENABLED(FLIGHT_MODE(ANGLE_MODE
)) << BOXANGLE
|
753 IS_ENABLED(FLIGHT_MODE(HORIZON_MODE
)) << BOXHORIZON
|
754 IS_ENABLED(FLIGHT_MODE(BARO_MODE
)) << BOXBARO
|
755 IS_ENABLED(FLIGHT_MODE(MAG_MODE
)) << BOXMAG
|
756 IS_ENABLED(FLIGHT_MODE(HEADFREE_MODE
)) << BOXHEADFREE
|
757 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXHEADADJ
)) << BOXHEADADJ
|
758 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXCAMSTAB
)) << BOXCAMSTAB
|
759 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXCAMTRIG
)) << BOXCAMTRIG
|
760 IS_ENABLED(FLIGHT_MODE(GPS_HOME_MODE
)) << BOXGPSHOME
|
761 IS_ENABLED(FLIGHT_MODE(GPS_HOLD_MODE
)) << BOXGPSHOLD
|
762 IS_ENABLED(FLIGHT_MODE(PASSTHRU_MODE
)) << BOXPASSTHRU
|
763 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXBEEPERON
)) << BOXBEEPERON
|
764 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXLEDMAX
)) << BOXLEDMAX
|
765 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXLEDLOW
)) << BOXLEDLOW
|
766 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXLLIGHTS
)) << BOXLLIGHTS
|
767 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXCALIB
)) << BOXCALIB
|
768 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXGOV
)) << BOXGOV
|
769 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXOSD
)) << BOXOSD
|
770 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXTELEMETRY
)) << BOXTELEMETRY
|
771 IS_ENABLED(IS_RC_MODE_ACTIVE(BOXAUTOTUNE
)) << BOXAUTOTUNE
|
772 IS_ENABLED(FLIGHT_MODE(SONAR_MODE
)) << BOXSONAR
|
773 IS_ENABLED(ARMING_FLAG(ARMED
)) << BOXARM
;
774 for (i
= 0; i
< activeBoxIdCount
; i
++) {
775 int flag
= (tmp
& (1 << activeBoxIds
[i
]));
780 serialize8(masterConfig
.current_profile_index
);
784 // Hack due to choice of units for sensor data in multiwii
786 for (i
= 0; i
< 3; i
++)
787 serialize16(accSmooth
[i
] / 8);
789 for (i
= 0; i
< 3; i
++)
790 serialize16(accSmooth
[i
]);
792 for (i
= 0; i
< 3; i
++)
793 serialize16(gyroData
[i
]);
794 for (i
= 0; i
< 3; i
++)
795 serialize16(magADC
[i
]);
798 s_struct((uint8_t *)&servo
, 16);
802 for (i
= 0; i
< MAX_SUPPORTED_SERVOS
; i
++) {
803 serialize16(currentProfile
->servoConf
[i
].min
);
804 serialize16(currentProfile
->servoConf
[i
].max
);
805 serialize16(currentProfile
->servoConf
[i
].middle
);
806 serialize8(currentProfile
->servoConf
[i
].rate
);
809 case MSP_CHANNEL_FORWARDING
:
811 for (i
= 0; i
< MAX_SUPPORTED_SERVOS
; i
++) {
812 serialize8(currentProfile
->servoConf
[i
].forwardFromChannel
);
816 s_struct((uint8_t *)motor
, 16);
819 headSerialReply(2 * rxRuntimeConfig
.channelCount
);
820 for (i
= 0; i
< rxRuntimeConfig
.channelCount
; i
++)
821 serialize16(rcData
[i
]);
825 for (i
= 0; i
< 2; i
++)
826 serialize16(inclination
.raw
[i
]);
827 serialize16(heading
);
831 #if defined(BARO) || defined(SONAR)
832 serialize32(altitudeHoldGetEstimatedAltitude());
838 case MSP_SONAR_ALTITUDE
:
841 serialize32(sonarGetLatestAltitude());
848 serialize8((uint8_t)constrain(vbat
, 0, 255));
849 serialize16((uint16_t)constrain(mAhDrawn
, 0, 0xFFFF)); // milliamphours drawn from battery
851 if(masterConfig
.batteryConfig
.multiwiiCurrentMeterOutput
) {
852 serialize16((uint16_t)constrain(amperage
* 10, 0, 0xFFFF)); // send amperage in 0.001 A steps. Negative range is truncated to zero
854 serialize16((int16_t)constrain(amperage
, -0x8000, 0x7FFF)); // send amperage in 0.01 A steps, range is -320A to 320A
858 serialize8(currentControlRateProfile
->rcRate8
);
859 serialize8(currentControlRateProfile
->rcExpo8
);
860 serialize8(currentControlRateProfile
->rollPitchRate
);
861 serialize8(currentControlRateProfile
->yawRate
);
862 serialize8(currentControlRateProfile
->dynThrPID
);
863 serialize8(currentControlRateProfile
->thrMid8
);
864 serialize8(currentControlRateProfile
->thrExpo8
);
867 headSerialReply(3 * PID_ITEM_COUNT
);
868 if (IS_PID_CONTROLLER_FP_BASED(currentProfile
->pidProfile
.pidController
)) { // convert float stuff into uint8_t to keep backwards compatability with all 8-bit shit with new pid
869 for (i
= 0; i
< 3; i
++) {
870 serialize8(constrain(lrintf(currentProfile
->pidProfile
.P_f
[i
] * 10.0f
), 0, 250));
871 serialize8(constrain(lrintf(currentProfile
->pidProfile
.I_f
[i
] * 100.0f
), 0, 250));
872 serialize8(constrain(lrintf(currentProfile
->pidProfile
.D_f
[i
] * 1000.0f
), 0, 100));
874 for (i
= 3; i
< PID_ITEM_COUNT
; i
++) {
876 serialize8(constrain(lrintf(currentProfile
->pidProfile
.A_level
* 10.0f
), 0, 250));
877 serialize8(constrain(lrintf(currentProfile
->pidProfile
.H_level
* 10.0f
), 0, 250));
878 serialize8(constrain(lrintf(currentProfile
->pidProfile
.H_sensitivity
), 0, 250));
880 serialize8(currentProfile
->pidProfile
.P8
[i
]);
881 serialize8(currentProfile
->pidProfile
.I8
[i
]);
882 serialize8(currentProfile
->pidProfile
.D8
[i
]);
886 for (i
= 0; i
< PID_ITEM_COUNT
; i
++) {
887 serialize8(currentProfile
->pidProfile
.P8
[i
]);
888 serialize8(currentProfile
->pidProfile
.I8
[i
]);
889 serialize8(currentProfile
->pidProfile
.D8
[i
]);
894 headSerialReply(sizeof(pidnames
) - 1);
895 serializeNames(pidnames
);
897 case MSP_PID_CONTROLLER
:
899 serialize8(currentProfile
->pidProfile
.pidController
);
901 case MSP_MODE_RANGES
:
902 headSerialReply(4 * MAX_MODE_ACTIVATION_CONDITION_COUNT
);
903 for (i
= 0; i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
; i
++) {
904 modeActivationCondition_t
*mac
= ¤tProfile
->modeActivationConditions
[i
];
905 const box_t
*box
= &boxes
[mac
->modeId
];
906 serialize8(box
->permanentId
);
907 serialize8(mac
->auxChannelIndex
);
908 serialize8(mac
->range
.startStep
);
909 serialize8(mac
->range
.endStep
);
912 case MSP_ADJUSTMENT_RANGES
:
913 headSerialReply(MAX_ADJUSTMENT_RANGE_COUNT
* (
914 1 + // adjustment index/slot
915 1 + // aux channel index
918 1 + // adjustment function
919 1 // aux switch channel index
921 for (i
= 0; i
< MAX_ADJUSTMENT_RANGE_COUNT
; i
++) {
922 adjustmentRange_t
*adjRange
= ¤tProfile
->adjustmentRanges
[i
];
923 serialize8(adjRange
->adjustmentIndex
);
924 serialize8(adjRange
->auxChannelIndex
);
925 serialize8(adjRange
->range
.startStep
);
926 serialize8(adjRange
->range
.endStep
);
927 serialize8(adjRange
->adjustmentFunction
);
928 serialize8(adjRange
->auxSwitchChannelIndex
);
932 serializeBoxNamesReply();
935 headSerialReply(activeBoxIdCount
);
936 for (i
= 0; i
< activeBoxIdCount
; i
++) {
937 const box_t
*box
= findBoxByActiveBoxId(activeBoxIds
[i
]);
941 serialize8(box
->permanentId
);
945 headSerialReply(2 * 6 + 4 + 2 + 4);
946 serialize16(masterConfig
.rxConfig
.midrc
);
948 serialize16(masterConfig
.escAndServoConfig
.minthrottle
);
949 serialize16(masterConfig
.escAndServoConfig
.maxthrottle
);
950 serialize16(masterConfig
.escAndServoConfig
.mincommand
);
952 serialize16(currentProfile
->failsafeConfig
.failsafe_throttle
);
955 serialize8(masterConfig
.gpsConfig
.provider
); // gps_type
956 serialize8(0); // TODO gps_baudrate (an index, cleanflight uses a uint32_t
957 serialize8(masterConfig
.gpsConfig
.sbasMode
); // gps_ubx_sbas
959 serialize8(0); // gps_type
960 serialize8(0); // TODO gps_baudrate (an index, cleanflight uses a uint32_t
961 serialize8(0); // gps_ubx_sbas
963 serialize8(masterConfig
.batteryConfig
.multiwiiCurrentMeterOutput
);
964 serialize8(masterConfig
.rxConfig
.rssi_channel
);
967 serialize16(currentProfile
->mag_declination
/ 10);
969 serialize8(masterConfig
.batteryConfig
.vbatscale
);
970 serialize8(masterConfig
.batteryConfig
.vbatmincellvoltage
);
971 serialize8(masterConfig
.batteryConfig
.vbatmaxcellvoltage
);
972 serialize8(masterConfig
.batteryConfig
.vbatwarningcellvoltage
);
976 for (i
= 0; i
< 8; i
++)
982 serialize8(STATE(GPS_FIX
));
983 serialize8(GPS_numSat
);
984 serialize32(GPS_coord
[LAT
]);
985 serialize32(GPS_coord
[LON
]);
986 serialize16(GPS_altitude
);
987 serialize16(GPS_speed
);
988 serialize16(GPS_ground_course
);
992 serialize16(GPS_distanceToHome
);
993 serialize16(GPS_directionToHome
);
994 serialize8(GPS_update
& 1);
997 wp_no
= read8(); // get the wp number
1000 lat
= GPS_home
[LAT
];
1001 lon
= GPS_home
[LON
];
1002 } else if (wp_no
== 16) {
1003 lat
= GPS_hold
[LAT
];
1004 lon
= GPS_hold
[LON
];
1009 serialize32(AltHold
); // altitude (cm) will come here -- temporary implementation to test feature with apps
1010 serialize16(0); // heading will come here (deg)
1011 serialize16(0); // time to stay (ms) will come here
1012 serialize8(0); // nav flag will come here
1015 headSerialReply(1 + (GPS_numCh
* 4));
1016 serialize8(GPS_numCh
);
1017 for (i
= 0; i
< GPS_numCh
; i
++){
1018 serialize8(GPS_svinfo_chn
[i
]);
1019 serialize8(GPS_svinfo_svid
[i
]);
1020 serialize8(GPS_svinfo_quality
[i
]);
1021 serialize8(GPS_svinfo_cno
[i
]);
1027 // make use of this crap, output some useful QA statistics
1028 //debug[3] = ((hse_value / 1000000) * 1000) + (SystemCoreClock / 1000000); // XX0YY [crystal clock : core clock]
1029 for (i
= 0; i
< 4; i
++)
1030 serialize16(debug
[i
]); // 4 variables are here for general monitoring purpose
1033 // Additional commands that are not compatible with MultiWii
1036 serialize16(currentProfile
->accelerometerTrims
.values
.pitch
);
1037 serialize16(currentProfile
->accelerometerTrims
.values
.roll
);
1041 headSerialReply(12);
1042 serialize32(U_ID_0
);
1043 serialize32(U_ID_1
);
1044 serialize32(U_ID_2
);
1049 serialize32(featureMask());
1052 case MSP_BOARD_ALIGNMENT
:
1054 serialize16(masterConfig
.boardAlignment
.rollDegrees
);
1055 serialize16(masterConfig
.boardAlignment
.pitchDegrees
);
1056 serialize16(masterConfig
.boardAlignment
.yawDegrees
);
1059 case MSP_VOLTAGE_METER_CONFIG
:
1061 serialize8(masterConfig
.batteryConfig
.vbatscale
);
1062 serialize8(masterConfig
.batteryConfig
.vbatmincellvoltage
);
1063 serialize8(masterConfig
.batteryConfig
.vbatmaxcellvoltage
);
1064 serialize8(masterConfig
.batteryConfig
.vbatwarningcellvoltage
);
1067 case MSP_CURRENT_METER_CONFIG
:
1069 serialize16(masterConfig
.batteryConfig
.currentMeterScale
);
1070 serialize16(masterConfig
.batteryConfig
.currentMeterOffset
);
1071 serialize8(masterConfig
.batteryConfig
.currentMeterType
);
1072 serialize16(masterConfig
.batteryConfig
.batteryCapacity
);
1077 serialize8(masterConfig
.mixerMode
);
1082 serialize8(masterConfig
.rxConfig
.serialrx_provider
);
1083 serialize16(masterConfig
.rxConfig
.maxcheck
);
1084 serialize16(masterConfig
.rxConfig
.midrc
);
1085 serialize16(masterConfig
.rxConfig
.mincheck
);
1086 serialize8(masterConfig
.rxConfig
.spektrum_sat_bind
);
1089 case MSP_RSSI_CONFIG
:
1091 serialize8(masterConfig
.rxConfig
.rssi_channel
);
1095 headSerialReply(MAX_MAPPABLE_RX_INPUTS
);
1096 for (i
= 0; i
< MAX_MAPPABLE_RX_INPUTS
; i
++)
1097 serialize8(masterConfig
.rxConfig
.rcmap
[i
]);
1101 headSerialReply(1 + 4 + 1 + 2 + 2 + 2 + 2 + 2);
1102 serialize8(masterConfig
.mixerMode
);
1104 serialize32(featureMask());
1106 serialize8(masterConfig
.rxConfig
.serialrx_provider
);
1108 serialize16(masterConfig
.boardAlignment
.rollDegrees
);
1109 serialize16(masterConfig
.boardAlignment
.pitchDegrees
);
1110 serialize16(masterConfig
.boardAlignment
.yawDegrees
);
1112 serialize16(masterConfig
.batteryConfig
.currentMeterScale
);
1113 serialize16(masterConfig
.batteryConfig
.currentMeterOffset
);
1116 case MSP_CF_SERIAL_CONFIG
:
1118 ((sizeof(uint8_t) * 2) * SERIAL_PORT_COUNT
) +
1119 (sizeof(uint32_t) * 4)
1121 for (i
= 0; i
< SERIAL_PORT_COUNT
; i
++) {
1122 serialize8(serialPortConstraints
[i
].identifier
);
1123 serialize8(masterConfig
.serialConfig
.serial_port_scenario
[i
]);
1125 serialize32(masterConfig
.serialConfig
.msp_baudrate
);
1126 serialize32(masterConfig
.serialConfig
.cli_baudrate
);
1127 serialize32(masterConfig
.serialConfig
.gps_baudrate
);
1128 serialize32(masterConfig
.serialConfig
.gps_passthrough_baudrate
);
1132 case MSP_LED_COLORS
:
1133 headSerialReply(CONFIGURABLE_COLOR_COUNT
* 4);
1134 for (i
= 0; i
< CONFIGURABLE_COLOR_COUNT
; i
++) {
1135 hsvColor_t
*color
= &masterConfig
.colors
[i
];
1136 serialize16(color
->h
);
1137 serialize8(color
->s
);
1138 serialize8(color
->v
);
1142 case MSP_LED_STRIP_CONFIG
:
1143 headSerialReply(MAX_LED_STRIP_LENGTH
* 7);
1144 for (i
= 0; i
< MAX_LED_STRIP_LENGTH
; i
++) {
1145 ledConfig_t
*ledConfig
= &masterConfig
.ledConfigs
[i
];
1146 serialize16((ledConfig
->flags
& LED_DIRECTION_MASK
) >> LED_DIRECTION_BIT_OFFSET
);
1147 serialize16((ledConfig
->flags
& LED_FUNCTION_MASK
) >> LED_FUNCTION_BIT_OFFSET
);
1148 serialize8(GET_LED_X(ledConfig
));
1149 serialize8(GET_LED_Y(ledConfig
));
1150 serialize8(ledConfig
->color
);
1154 case MSP_BF_BUILD_INFO
:
1155 headSerialReply(11 + 4 + 4);
1156 for (i
= 0; i
< 11; i
++)
1157 serialize8(buildDate
[i
]); // MMM DD YYYY as ascii, MMM = Jan/Feb... etc
1158 serialize32(0); // future exp
1159 serialize32(0); // future exp
1168 static bool processInCommand(void)
1174 int32_t lat
= 0, lon
= 0, alt
= 0;
1177 switch (currentPort
->cmdMSP
) {
1178 case MSP_SELECT_SETTING
:
1179 if (!ARMING_FLAG(ARMED
)) {
1180 masterConfig
.current_profile_index
= read8();
1181 if (masterConfig
.current_profile_index
> 2) {
1182 masterConfig
.current_profile_index
= 0;
1191 case MSP_SET_RAW_RC
:
1193 uint8_t channelCount
= currentPort
->dataSize
/ sizeof(uint16_t);
1194 if (channelCount
> MAX_SUPPORTED_RC_CHANNEL_COUNT
) {
1197 for (i
= 0; i
< channelCount
; i
++)
1198 rcData
[i
] = read16();
1199 rxMspFrameRecieve();
1203 case MSP_SET_ACC_TRIM
:
1204 currentProfile
->accelerometerTrims
.values
.pitch
= read16();
1205 currentProfile
->accelerometerTrims
.values
.roll
= read16();
1207 case MSP_SET_PID_CONTROLLER
:
1208 currentProfile
->pidProfile
.pidController
= read8();
1209 setPIDController(currentProfile
->pidProfile
.pidController
);
1212 if (IS_PID_CONTROLLER_FP_BASED(currentProfile
->pidProfile
.pidController
)) {
1213 for (i
= 0; i
< 3; i
++) {
1214 currentProfile
->pidProfile
.P_f
[i
] = (float)read8() / 10.0f
;
1215 currentProfile
->pidProfile
.I_f
[i
] = (float)read8() / 100.0f
;
1216 currentProfile
->pidProfile
.D_f
[i
] = (float)read8() / 1000.0f
;
1218 for (i
= 3; i
< PID_ITEM_COUNT
; i
++) {
1219 if (i
== PIDLEVEL
) {
1220 currentProfile
->pidProfile
.A_level
= (float)read8() / 10.0f
;
1221 currentProfile
->pidProfile
.H_level
= (float)read8() / 10.0f
;
1222 currentProfile
->pidProfile
.H_sensitivity
= read8();
1224 currentProfile
->pidProfile
.P8
[i
] = read8();
1225 currentProfile
->pidProfile
.I8
[i
] = read8();
1226 currentProfile
->pidProfile
.D8
[i
] = read8();
1230 for (i
= 0; i
< PID_ITEM_COUNT
; i
++) {
1231 currentProfile
->pidProfile
.P8
[i
] = read8();
1232 currentProfile
->pidProfile
.I8
[i
] = read8();
1233 currentProfile
->pidProfile
.D8
[i
] = read8();
1237 case MSP_SET_MODE_RANGE
:
1239 if (i
< MAX_MODE_ACTIVATION_CONDITION_COUNT
) {
1240 modeActivationCondition_t
*mac
= ¤tProfile
->modeActivationConditions
[i
];
1242 const box_t
*box
= findBoxByPermenantId(i
);
1244 mac
->modeId
= box
->boxId
;
1245 mac
->auxChannelIndex
= read8();
1246 mac
->range
.startStep
= read8();
1247 mac
->range
.endStep
= read8();
1249 useRcControlsConfig(currentProfile
->modeActivationConditions
, &masterConfig
.escAndServoConfig
, ¤tProfile
->pidProfile
);
1257 case MSP_SET_ADJUSTMENT_RANGE
:
1259 if (i
< MAX_ADJUSTMENT_RANGE_COUNT
) {
1260 adjustmentRange_t
*adjRange
= ¤tProfile
->adjustmentRanges
[i
];
1262 if (i
< MAX_SIMULTANEOUS_ADJUSTMENT_COUNT
) {
1263 adjRange
->adjustmentIndex
= i
;
1264 adjRange
->auxChannelIndex
= read8();
1265 adjRange
->range
.startStep
= read8();
1266 adjRange
->range
.endStep
= read8();
1267 adjRange
->adjustmentFunction
= read8();
1268 adjRange
->auxSwitchChannelIndex
= read8();
1277 case MSP_SET_RC_TUNING
:
1278 currentControlRateProfile
->rcRate8
= read8();
1279 currentControlRateProfile
->rcExpo8
= read8();
1280 currentControlRateProfile
->rollPitchRate
= read8();
1281 currentControlRateProfile
->yawRate
= read8();
1282 currentControlRateProfile
->dynThrPID
= read8();
1283 currentControlRateProfile
->thrMid8
= read8();
1284 currentControlRateProfile
->thrExpo8
= read8();
1288 if (tmp
< 1600 && tmp
> 1400)
1289 masterConfig
.rxConfig
.midrc
= tmp
;
1291 masterConfig
.escAndServoConfig
.minthrottle
= read16();
1292 masterConfig
.escAndServoConfig
.maxthrottle
= read16();
1293 masterConfig
.escAndServoConfig
.mincommand
= read16();
1295 currentProfile
->failsafeConfig
.failsafe_throttle
= read16();
1298 masterConfig
.gpsConfig
.provider
= read8(); // gps_type
1299 read8(); // gps_baudrate
1300 masterConfig
.gpsConfig
.sbasMode
= read8(); // gps_ubx_sbas
1302 read8(); // gps_type
1303 read8(); // gps_baudrate
1304 read8(); // gps_ubx_sbas
1306 masterConfig
.batteryConfig
.multiwiiCurrentMeterOutput
= read8();
1307 masterConfig
.rxConfig
.rssi_channel
= read8();
1310 currentProfile
->mag_declination
= read16() * 10;
1312 masterConfig
.batteryConfig
.vbatscale
= read8(); // actual vbatscale as intended
1313 masterConfig
.batteryConfig
.vbatmincellvoltage
= read8(); // vbatlevel_warn1 in MWC2.3 GUI
1314 masterConfig
.batteryConfig
.vbatmaxcellvoltage
= read8(); // vbatlevel_warn2 in MWC2.3 GUI
1315 masterConfig
.batteryConfig
.vbatwarningcellvoltage
= read8(); // vbatlevel when buzzer starts to alert
1318 for (i
= 0; i
< 8; i
++) // FIXME should this use MAX_MOTORS or MAX_SUPPORTED_MOTORS instead of 8
1319 motor_disarmed
[i
] = read16();
1321 case MSP_SET_SERVO_CONF
:
1322 for (i
= 0; i
< MAX_SUPPORTED_SERVOS
; i
++) {
1323 currentProfile
->servoConf
[i
].min
= read16();
1324 currentProfile
->servoConf
[i
].max
= read16();
1325 // provide temporary support for old clients that try and send a channel index instead of a servo middle
1326 uint16_t potentialServoMiddleOrChannelToForward
= read16();
1327 if (potentialServoMiddleOrChannelToForward
< MAX_SUPPORTED_SERVOS
) {
1328 currentProfile
->servoConf
[i
].forwardFromChannel
= potentialServoMiddleOrChannelToForward
;
1330 if (potentialServoMiddleOrChannelToForward
>= PWM_RANGE_MIN
&& potentialServoMiddleOrChannelToForward
<= PWM_RANGE_MAX
) {
1331 currentProfile
->servoConf
[i
].middle
= potentialServoMiddleOrChannelToForward
;
1333 currentProfile
->servoConf
[i
].rate
= read8();
1336 case MSP_SET_CHANNEL_FORWARDING
:
1337 for (i
= 0; i
< MAX_SUPPORTED_SERVOS
; i
++) {
1338 currentProfile
->servoConf
[i
].forwardFromChannel
= read8();
1341 case MSP_RESET_CONF
:
1342 if (!ARMING_FLAG(ARMED
)) {
1347 case MSP_ACC_CALIBRATION
:
1348 if (!ARMING_FLAG(ARMED
))
1349 accSetCalibrationCycles(CALIBRATING_ACC_CYCLES
);
1351 case MSP_MAG_CALIBRATION
:
1352 if (!ARMING_FLAG(ARMED
))
1353 ENABLE_STATE(CALIBRATE_MAG
);
1355 case MSP_EEPROM_WRITE
:
1356 if (ARMING_FLAG(ARMED
)) {
1364 case MSP_SET_RAW_GPS
:
1366 ENABLE_STATE(GPS_FIX
);
1368 DISABLE_STATE(GPS_FIX
);
1370 GPS_numSat
= read8();
1371 GPS_coord
[LAT
] = read32();
1372 GPS_coord
[LON
] = read32();
1373 GPS_altitude
= read16();
1374 GPS_speed
= read16();
1375 GPS_update
|= 2; // New data signalisation to GPS functions // FIXME Magic Numbers
1378 wp_no
= read8(); //get the wp number
1381 alt
= read32(); // to set altitude (cm)
1382 read16(); // future: to set heading (deg)
1383 read16(); // future: to set time to stay (ms)
1384 read8(); // future: to set nav flag
1386 GPS_home
[LAT
] = lat
;
1387 GPS_home
[LON
] = lon
;
1388 DISABLE_FLIGHT_MODE(GPS_HOME_MODE
); // with this flag, GPS_set_next_wp will be called in the next loop -- OK with SERIAL GPS / OK with I2C GPS
1389 ENABLE_STATE(GPS_FIX_HOME
);
1391 AltHold
= alt
; // temporary implementation to test feature with apps
1392 } else if (wp_no
== 16) { // OK with SERIAL GPS -- NOK for I2C GPS / needs more code dev in order to inject GPS coord inside I2C GPS
1393 GPS_hold
[LAT
] = lat
;
1394 GPS_hold
[LON
] = lon
;
1396 AltHold
= alt
; // temporary implementation to test feature with apps
1397 nav_mode
= NAV_MODE_WP
;
1398 GPS_set_next_wp(&GPS_hold
[LAT
], &GPS_hold
[LON
]);
1402 case MSP_SET_FEATURE
:
1404 featureSet(read32()); // features bitmap
1407 case MSP_SET_BOARD_ALIGNMENT
:
1408 masterConfig
.boardAlignment
.rollDegrees
= read16();
1409 masterConfig
.boardAlignment
.pitchDegrees
= read16();
1410 masterConfig
.boardAlignment
.yawDegrees
= read16();
1413 case MSP_SET_VOLTAGE_METER_CONFIG
:
1414 masterConfig
.batteryConfig
.vbatscale
= read8(); // actual vbatscale as intended
1415 masterConfig
.batteryConfig
.vbatmincellvoltage
= read8(); // vbatlevel_warn1 in MWC2.3 GUI
1416 masterConfig
.batteryConfig
.vbatmaxcellvoltage
= read8(); // vbatlevel_warn2 in MWC2.3 GUI
1417 masterConfig
.batteryConfig
.vbatwarningcellvoltage
= read8(); // vbatlevel when buzzer starts to alert
1420 case MSP_SET_CURRENT_METER_CONFIG
:
1421 masterConfig
.batteryConfig
.currentMeterScale
= read16();
1422 masterConfig
.batteryConfig
.currentMeterOffset
= read16();
1423 masterConfig
.batteryConfig
.currentMeterType
= read8();
1424 masterConfig
.batteryConfig
.batteryCapacity
= read16();
1427 #ifndef USE_QUAD_MIXER_ONLY
1429 masterConfig
.mixerMode
= read8();
1433 case MSP_SET_RX_CONFIG
:
1434 masterConfig
.rxConfig
.serialrx_provider
= read8();
1435 masterConfig
.rxConfig
.maxcheck
= read16();
1436 masterConfig
.rxConfig
.midrc
= read16();
1437 masterConfig
.rxConfig
.mincheck
= read16();
1438 masterConfig
.rxConfig
.spektrum_sat_bind
= read8();
1441 case MSP_SET_RSSI_CONFIG
:
1442 masterConfig
.rxConfig
.rssi_channel
= read8();
1445 case MSP_SET_RX_MAP
:
1446 for (i
= 0; i
< MAX_MAPPABLE_RX_INPUTS
; i
++) {
1447 masterConfig
.rxConfig
.rcmap
[i
] = read8();
1451 case MSP_SET_BF_CONFIG
:
1453 #ifdef USE_QUAD_MIXER_ONLY
1454 read8(); // mixerMode ignored
1456 masterConfig
.mixerMode
= read8(); // mixerMode
1460 featureSet(read32()); // features bitmap
1462 masterConfig
.rxConfig
.serialrx_provider
= read8(); // serialrx_type
1464 masterConfig
.boardAlignment
.rollDegrees
= read16(); // board_align_roll
1465 masterConfig
.boardAlignment
.pitchDegrees
= read16(); // board_align_pitch
1466 masterConfig
.boardAlignment
.yawDegrees
= read16(); // board_align_yaw
1468 masterConfig
.batteryConfig
.currentMeterScale
= read16();
1469 masterConfig
.batteryConfig
.currentMeterOffset
= read16();
1472 case MSP_SET_CF_SERIAL_CONFIG
:
1474 uint8_t baudRateSize
= (sizeof(uint32_t) * 4);
1475 uint8_t serialPortCount
= currentPort
->dataSize
- baudRateSize
;
1476 if (serialPortCount
!= SERIAL_PORT_COUNT
) {
1480 for (i
= 0; i
< SERIAL_PORT_COUNT
; i
++) {
1481 masterConfig
.serialConfig
.serial_port_scenario
[i
] = read8();
1483 masterConfig
.serialConfig
.msp_baudrate
= read32();
1484 masterConfig
.serialConfig
.cli_baudrate
= read32();
1485 masterConfig
.serialConfig
.gps_baudrate
= read32();
1486 masterConfig
.serialConfig
.gps_passthrough_baudrate
= read32();
1491 case MSP_SET_LED_COLORS
:
1492 for (i
= 0; i
< CONFIGURABLE_COLOR_COUNT
; i
++) {
1493 hsvColor_t
*color
= &masterConfig
.colors
[i
];
1494 color
->h
= read16();
1500 case MSP_SET_LED_STRIP_CONFIG
:
1503 if (i
>= MAX_LED_STRIP_LENGTH
|| currentPort
->dataSize
!= (1 + 7)) {
1507 ledConfig_t
*ledConfig
= &masterConfig
.ledConfigs
[i
];
1509 // currently we're storing directions and functions in a uint16 (flags)
1510 // the msp uses 2 x uint16_t to cater for future expansion
1512 ledConfig
->flags
= (mask
<< LED_DIRECTION_BIT_OFFSET
) & LED_DIRECTION_MASK
;
1515 ledConfig
->flags
|= (mask
<< LED_FUNCTION_BIT_OFFSET
) & LED_FUNCTION_MASK
;
1518 ledConfig
->xy
= CALCULATE_LED_X(mask
);
1521 ledConfig
->xy
|= CALCULATE_LED_Y(mask
);
1523 ledConfig
->color
= read8();
1525 reevalulateLedConfig();
1530 isRebootScheduled
= true;
1534 // we do not know how to handle the (valid) message, indicate error MSP $M!
1541 static void mspProcessPort(void)
1545 while (serialTotalBytesWaiting(mspSerialPort
)) {
1546 c
= serialRead(mspSerialPort
);
1548 if (currentPort
->c_state
== IDLE
) {
1549 currentPort
->c_state
= (c
== '$') ? HEADER_START
: IDLE
;
1550 if (currentPort
->c_state
== IDLE
&& !ARMING_FLAG(ARMED
))
1551 evaluateOtherData(c
); // if not armed evaluate all other incoming serial data
1552 } else if (currentPort
->c_state
== HEADER_START
) {
1553 currentPort
->c_state
= (c
== 'M') ? HEADER_M
: IDLE
;
1554 } else if (currentPort
->c_state
== HEADER_M
) {
1555 currentPort
->c_state
= (c
== '<') ? HEADER_ARROW
: IDLE
;
1556 } else if (currentPort
->c_state
== HEADER_ARROW
) {
1557 if (c
> INBUF_SIZE
) { // now we are expecting the payload size
1558 currentPort
->c_state
= IDLE
;
1561 currentPort
->dataSize
= c
;
1562 currentPort
->offset
= 0;
1563 currentPort
->checksum
= 0;
1564 currentPort
->indRX
= 0;
1565 currentPort
->checksum
^= c
;
1566 currentPort
->c_state
= HEADER_SIZE
; // the command is to follow
1567 } else if (currentPort
->c_state
== HEADER_SIZE
) {
1568 currentPort
->cmdMSP
= c
;
1569 currentPort
->checksum
^= c
;
1570 currentPort
->c_state
= HEADER_CMD
;
1571 } else if (currentPort
->c_state
== HEADER_CMD
&& currentPort
->offset
< currentPort
->dataSize
) {
1572 currentPort
->checksum
^= c
;
1573 currentPort
->inBuf
[currentPort
->offset
++] = c
;
1574 } else if (currentPort
->c_state
== HEADER_CMD
&& currentPort
->offset
>= currentPort
->dataSize
) {
1575 if (currentPort
->checksum
== c
) { // compare calculated and transferred checksum
1576 // we got a valid packet, evaluate it
1577 if (!(processOutCommand(currentPort
->cmdMSP
) || processInCommand())) {
1582 currentPort
->c_state
= IDLE
;
1583 break; // process one command so as not to block.
1588 void setCurrentPort(mspPort_t
*port
)
1591 mspSerialPort
= currentPort
->port
;
1594 void mspProcess(void)
1597 mspPort_t
*candidatePort
;
1599 for (portIndex
= 0; portIndex
< MAX_MSP_PORT_COUNT
; portIndex
++) {
1600 candidatePort
= &mspPorts
[portIndex
];
1601 if (candidatePort
->mspPortUsage
!= FOR_GENERAL_MSP
) {
1605 setCurrentPort(candidatePort
);
1608 if (isRebootScheduled
) {
1609 // pause a little while to allow response to be sent
1610 while (!isSerialTransmitBufferEmpty(candidatePort
->port
)) {
1618 static const uint8_t mspTelemetryCommandSequence
[] = {
1619 MSP_BOXNAMES
, // repeat boxnames, in case the first transmission was lost or never received.
1631 #define MSP_TELEMETRY_COMMAND_SEQUENCE_ENTRY_COUNT (sizeof(mspTelemetryCommandSequence) / sizeof(mspTelemetryCommandSequence[0]))
1633 static mspPort_t
*mspTelemetryPort
= NULL
;
1635 void mspSetTelemetryPort(serialPort_t
*serialPort
)
1638 mspPort_t
*candidatePort
= NULL
;
1639 mspPort_t
*matchedPort
= NULL
;
1641 // find existing telemetry port
1642 for (portIndex
= 0; portIndex
< MAX_MSP_PORT_COUNT
; portIndex
++) {
1643 candidatePort
= &mspPorts
[portIndex
];
1644 if (candidatePort
->mspPortUsage
== FOR_TELEMETRY
) {
1645 matchedPort
= candidatePort
;
1652 for (portIndex
= 0; portIndex
< MAX_MSP_PORT_COUNT
; portIndex
++) {
1653 candidatePort
= &mspPorts
[portIndex
];
1654 if (candidatePort
->mspPortUsage
== UNUSED_PORT
) {
1655 matchedPort
= candidatePort
;
1660 mspTelemetryPort
= matchedPort
;
1661 if (!mspTelemetryPort
) {
1665 resetMspPort(mspTelemetryPort
, serialPort
, FOR_TELEMETRY
);
1668 void sendMspTelemetry(void)
1670 static uint32_t sequenceIndex
= 0;
1672 if (!mspTelemetryPort
) {
1676 setCurrentPort(mspTelemetryPort
);
1678 processOutCommand(mspTelemetryCommandSequence
[sequenceIndex
]);
1682 if (sequenceIndex
>= MSP_TELEMETRY_COMMAND_SEQUENCE_ENTRY_COUNT
) {