From 3e285b0c057e58818377150d51affe0523bc002e Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Fri, 21 Jun 2024 23:57:48 +0200 Subject: [PATCH 1/2] Add CRSF bind command. Also add MSP message to trigger bind. Bind commands should work for srxl2 and crsf --- src/main/fc/cli.c | 7 +++++++ src/main/fc/fc_msp.c | 25 ++++++++++++++++++++++++- src/main/msp/msp_protocol_v2_common.h | 1 + src/main/rx/crsf.c | 7 +++++++ src/main/rx/crsf.h | 11 +++++++++++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/fc/cli.c b/src/main/fc/cli.c index 7b9c1329884..0d54cab4398 100644 --- a/src/main/fc/cli.c +++ b/src/main/fc/cli.c @@ -104,6 +104,7 @@ bool cliMode = false; #include "rx/rx.h" #include "rx/spektrum.h" #include "rx/srxl2.h" +#include "rx/crsf.h" #include "scheduler/scheduler.h" @@ -3224,6 +3225,12 @@ void cliRxBind(char *cmdline){ srxl2Bind(); cliPrint("Binding SRXL2 receiver..."); break; +#endif +#if defined(USE_SERIALRX_CRSF) + case SERIALRX_CRSF: + crsfBind(); + cliPrint("Binding CRSF receiver..."); + break; #endif } } diff --git a/src/main/fc/fc_msp.c b/src/main/fc/fc_msp.c index 6362901e45b..acbf0c2dcb4 100644 --- a/src/main/fc/fc_msp.c +++ b/src/main/fc/fc_msp.c @@ -110,6 +110,8 @@ #include "rx/rx.h" #include "rx/msp.h" +#include "rx/srxl2.h" +#include "rx/crsf.h" #include "scheduler/scheduler.h" @@ -1030,6 +1032,7 @@ static bool mspFcProcessOutCommand(uint16_t cmdMSP, sbuf_t *dst, mspPostProcessF case MSP_MIXER: sbufWriteU8(dst, 3); // mixerMode no longer supported, send 3 (QuadX) as fallback break; + case MSP_RX_CONFIG: sbufWriteU8(dst, rxConfig()->serialrx_provider); @@ -2887,7 +2890,7 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src) } else return MSP_RESULT_ERROR; break; - + case MSP_SET_FAILSAFE_CONFIG: if (dataSize == 20) { failsafeConfigMutable()->failsafe_delay = sbufReadU8(src); @@ -3352,6 +3355,26 @@ static mspResult_e mspFcProcessInCommand(uint16_t cmdMSP, sbuf_t *src) break; + case MSP2_BETAFLIGHT_BIND: + if (rxConfig()->receiverType == RX_TYPE_SERIAL) { + switch (rxConfig()->serialrx_provider) { + default: + return MSP_RESULT_ERROR; + #if defined(USE_SERIALRX_SRXL2) + case SERIALRX_SRXL2: + srxl2Bind(); + break; + #endif + #if defined(USE_SERIALRX_CRSF) + case SERIALRX_CRSF: + crsfBind(); + break; + #endif + } + } else { + return MSP_RESULT_ERROR; + } + break; default: return MSP_RESULT_ERROR; diff --git a/src/main/msp/msp_protocol_v2_common.h b/src/main/msp/msp_protocol_v2_common.h index 82b7f754079..e778a1808c9 100644 --- a/src/main/msp/msp_protocol_v2_common.h +++ b/src/main/msp/msp_protocol_v2_common.h @@ -33,3 +33,4 @@ #define MSP2_COMMON_SET_RADAR_POS 0x100B //SET radar position information #define MSP2_COMMON_SET_RADAR_ITD 0x100C //SET radar information to display +#define MSP2_BETAFLIGHT_BIND 0x3000 diff --git a/src/main/rx/crsf.c b/src/main/rx/crsf.c index 85820f7fa20..f325859a8a0 100755 --- a/src/main/rx/crsf.c +++ b/src/main/rx/crsf.c @@ -331,4 +331,11 @@ bool crsfRxIsActive(void) { return serialPort != NULL; } + + +void crsfBind(void) +{ + +} + #endif diff --git a/src/main/rx/crsf.h b/src/main/rx/crsf.h index f3bc7933494..69777a0390c 100755 --- a/src/main/rx/crsf.h +++ b/src/main/rx/crsf.h @@ -104,6 +104,15 @@ typedef enum { CRSF_FRAMETYPE_DISPLAYPORT_CMD = 0x7D, // displayport control command } crsfFrameType_e; +enum { + CRSF_COMMAND_SUBCMD_RX = 0x10, // receiver command + CRSF_COMMAND_SUBCMD_GENERAL = 0x0A, // general command +}; + +enum { + CRSF_COMMAND_SUBCMD_RX_BIND = 0x01, // bind command +}; + typedef struct crsfFrameDef_s { uint8_t deviceAddress; uint8_t frameLength; @@ -124,3 +133,5 @@ struct rxConfig_s; struct rxRuntimeConfig_s; bool crsfRxInit(const struct rxConfig_s *initialRxConfig, struct rxRuntimeConfig_s *rxRuntimeConfig); bool crsfRxIsActive(void); + +void crsfBind(void); From a33e13aff9268462beba563f9b1c541ed840d8b4 Mon Sep 17 00:00:00 2001 From: Marcelo Bezerra <23555060+mmosca@users.noreply.github.com> Date: Sat, 22 Jun 2024 00:08:39 +0200 Subject: [PATCH 2/2] Almost forgot to commit the important bits :) --- src/main/rx/crsf.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/rx/crsf.c b/src/main/rx/crsf.c index f325859a8a0..a791b2e9dd8 100755 --- a/src/main/rx/crsf.c +++ b/src/main/rx/crsf.c @@ -335,7 +335,20 @@ bool crsfRxIsActive(void) void crsfBind(void) { - + if (serialPort != NULL) { + uint8_t bindFrame[] = { + CRSF_SYNC_BYTE, + 0x07, // frame length + CRSF_FRAMETYPE_COMMAND, + CRSF_ADDRESS_CRSF_RECEIVER, + CRSF_ADDRESS_FLIGHT_CONTROLLER, + CRSF_COMMAND_SUBCMD_RX, + CRSF_COMMAND_SUBCMD_RX_BIND, + 0x9E, // Command CRC8 + 0xE8, // Packet CRC8 + }; + serialWriteBuf(serialPort, bindFrame, 9); + } } #endif