@@ -628,14 +628,6 @@ bool AP_BLHeli::BL_SendBuf(const uint8_t *buf, uint16_t len)
628
628
if (serial_start_ms == 0 ) {
629
629
serial_start_ms = AP_HAL::millis ();
630
630
}
631
- uint32_t now = AP_HAL::millis ();
632
- if (serial_start_ms == 0 || now - serial_start_ms < 1000 ) {
633
- /*
634
- we've just started the interface. We want it idle for at
635
- least 1 second before we start sending serial data.
636
- */
637
- hal.scheduler ->delay (1100 );
638
- }
639
631
memcpy (blheli.buf , buf, len);
640
632
uint16_t crc = BL_CRC (buf, len);
641
633
blheli.buf [len] = crc;
@@ -750,18 +742,14 @@ bool AP_BLHeli::BL_ReadA(uint8_t cmd, uint8_t *buf, uint16_t n)
750
742
*/
751
743
bool AP_BLHeli::BL_ConnectEx (void )
752
744
{
753
- if (blheli.connected [blheli.chan ] != 0 ) {
754
- debug (" Using cached interface 0x%x for %u" , blheli.interface_mode [blheli.chan ], blheli.chan );
755
- return true ;
756
- }
757
745
debug (" BL_ConnectEx %u/%u at %u" , blheli.chan , num_motors, motor_map[blheli.chan ]);
758
746
setDisconnected ();
759
747
const uint8_t BootInit[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0x0D ,' B' ,' L' ,' H' ,' e' ,' l' ,' i' ,0xF4 ,0x7D };
760
748
if (!BL_SendBuf (BootInit, 21 )) {
761
749
return false ;
762
750
}
763
751
764
- uint8_t BootInfo[8 ];
752
+ uint8_t BootInfo[9 ];
765
753
if (!BL_ReadBuf (BootInfo, 8 )) {
766
754
return false ;
767
755
}
@@ -927,9 +915,13 @@ void AP_BLHeli::blheli_process_command(void)
927
915
switch (blheli.command ) {
928
916
case cmd_InterfaceTestAlive: {
929
917
debug (" cmd_InterfaceTestAlive" );
930
- BL_SendCMDKeepAlive ();
931
- if (blheli.ack != ACK_OK) {
932
- setDisconnected ();
918
+ if (!isMcuConnected ()) {
919
+ blheli.ack = ACK_D_GENERAL_ERROR;
920
+ } else {
921
+ BL_SendCMDKeepAlive ();
922
+ if (blheli.ack != ACK_OK) {
923
+ setDisconnected ();
924
+ }
933
925
}
934
926
uint8_t b = 0 ;
935
927
blheli_send_reply (&b, 1 );
@@ -997,21 +989,37 @@ void AP_BLHeli::blheli_process_command(void)
997
989
}
998
990
999
991
case cmd_DeviceInitFlash: {
992
+ uint8_t chan = blheli.buf [0 ];
993
+
1000
994
debug (" cmd_DeviceInitFlash(%u)" , unsigned (blheli.buf [0 ]));
1001
995
if (blheli.buf [0 ] >= num_motors) {
1002
996
debug (" bad channel %u" , blheli.buf [0 ]);
1003
997
blheli.ack = ACK_I_INVALID_CHANNEL;
1004
- blheli_send_reply (&blheli. buf [ 0 ] , 1 );
998
+ blheli_send_reply (&chan , 1 );
1005
999
break ;
1006
1000
}
1007
- blheli.chan = blheli.buf [0 ];
1008
- blheli.ack = ACK_OK;
1009
- BL_ConnectEx ();
1010
- uint8_t buf[4 ] = {blheli.deviceInfo [blheli.chan ][0 ],
1011
- blheli.deviceInfo [blheli.chan ][1 ],
1012
- blheli.deviceInfo [blheli.chan ][2 ],
1013
- blheli.deviceInfo [blheli.chan ][3 ]}; // device ID
1014
- blheli_send_reply (buf, sizeof (buf));
1001
+ // betaflight tries three times to connect, this avoids the need to wait some arbitrary
1002
+ // period for the interface to be up.
1003
+ bool failed = true ;
1004
+ for (uint8_t i = 0 ; i<3 ; i++) {
1005
+ blheli.chan = chan;
1006
+ blheli.ack = ACK_OK;
1007
+ if (BL_ConnectEx ()) {
1008
+ uint8_t buf[4 ] = {blheli.deviceInfo [blheli.chan ][0 ],
1009
+ blheli.deviceInfo [blheli.chan ][1 ],
1010
+ blheli.deviceInfo [blheli.chan ][2 ],
1011
+ blheli.deviceInfo [blheli.chan ][3 ]}; // device ID
1012
+ blheli_send_reply (buf, sizeof (buf));
1013
+ failed = false ;
1014
+ break ;
1015
+ }
1016
+ }
1017
+
1018
+ if (failed) {
1019
+ blheli.ack = ACK_D_GENERAL_ERROR;
1020
+ blheli_send_reply (&chan, 1 );
1021
+ setDisconnected ();
1022
+ }
1015
1023
break ;
1016
1024
}
1017
1025
0 commit comments