11import serial , time , sys , re
22import argparse
3- from xmodem import XMODEM
43import serials_find
54import SerialHelper
5+ import bootloader
66
77SCRIPT_DEBUG = 0
88
@@ -25,7 +25,7 @@ def _validate_serialrx(rl, config, expected):
2525 expected = [expected ]
2626 rl .set_delimiters (["# " ])
2727 rl .clear ()
28- rl .write ("get serialrx_%s\r \n " % config )
28+ rl .write_str ("get serialrx_%s" % config )
2929 line = rl .read_line (1. ).strip ()
3030 for key in expected :
3131 key = " = %s" % key
@@ -35,7 +35,7 @@ def _validate_serialrx(rl, config, expected):
3535 return found
3636
3737
38- def bf_passthrough_init (port , requestedBaudrate , half_duplex = False , reset_to_bl = False ):
38+ def bf_passthrough_init (port , requestedBaudrate , half_duplex = False ):
3939 debug = SCRIPT_DEBUG
4040
4141 sys .stdout .flush ()
@@ -49,7 +49,7 @@ def bf_passthrough_init(port, requestedBaudrate, half_duplex=False, reset_to_bl=
4949 rl = SerialHelper .SerialHelper (s , 3. , ['CCC' , "# " ])
5050 rl .clear ()
5151 # Send start command '#'
52- rl .write ("#\r \n " , half_duplex )
52+ rl .write_str ("#" , half_duplex )
5353 start = rl .read_line (2. ).strip ()
5454 #dbg_print("BF INIT: '%s'" % start.replace("\r", ""))
5555 if "CCC" in start :
@@ -79,7 +79,7 @@ def bf_passthrough_init(port, requestedBaudrate, half_duplex=False, reset_to_bl=
7979
8080 rl .set_delimiters (["\n " ])
8181 rl .clear ()
82- rl .write ("serial\r \n " )
82+ rl .write_str ("serial" )
8383
8484 while True :
8585 line = rl .read_line ().strip ()
@@ -104,39 +104,51 @@ def bf_passthrough_init(port, requestedBaudrate, half_duplex=False, reset_to_bl=
104104
105105 dbg_print ("Enabling serial passthrough..." )
106106 dbg_print (" CMD: '%s'" % cmd )
107- rl .write (cmd + '\n ' )
108-
107+ rl .write_str (cmd )
108+ time .sleep (.2 )
109+ s .close ()
109110 dbg_print ("======== PASSTHROUGH DONE ========" )
110111
111- if (reset_to_bl ):
112- dbg_print ("======== RESET TO BOOTLOADER ========" )
113- if half_duplex == True :
114- BootloaderInitSeq1 = bytes ([0x89 ,0x04 ,0x32 ,0x62 ,0x6c ,0x0A ]) # GHST
115- dbg_print (" Using GHST (half duplex)!\n " )
116- else :
117- BootloaderInitSeq1 = bytes ([0xEC ,0x04 ,0x32 ,0x62 ,0x6c ,0x0A ]) # CRSF
118-
119- s .write (BootloaderInitSeq1 )
120112
121- time .sleep (.2 )
113+ def reset_to_bootloader (args ):
114+ dbg_print ("======== RESET TO BOOTLOADER ========" )
115+ s = serial .Serial (port = args .port , baudrate = args .baud ,
116+ bytesize = 8 , parity = 'N' , stopbits = 1 ,
117+ timeout = 1 , xonxoff = 0 , rtscts = 0 )
118+ if args .half_duplex :
119+ BootloaderInitSeq = bootloader .get_init_seq ('GHST' , args .type )
120+ dbg_print (" * Using half duplex (GHST)" )
121+ else :
122+ BootloaderInitSeq = bootloader .get_init_seq ('CRSF' , args .type )
123+ dbg_print (" * Using full duplex (CFSF)" )
124+ s .write (BootloaderInitSeq )
125+ s .flush ()
126+ time .sleep (.5 )
122127 s .close ()
123128
124129
125130if __name__ == '__main__' :
126- parser = argparse .ArgumentParser (description = "Initialize BetaFlight passthrough and optionally send a reboot comamnd sequence" )
131+ parser = argparse .ArgumentParser (
132+ description = "Initialize BetaFlight passthrough and optionally send a reboot comamnd sequence" )
127133 parser .add_argument ("-b" , "--baud" , type = int , default = 420000 ,
128134 help = "Baud rate for passthrough communication" )
129135 parser .add_argument ("-p" , "--port" , type = str ,
130136 help = "Override serial port autodetection and use PORT" )
131137 parser .add_argument ("-nr" , "--no-reset" , action = "store_false" ,
132- dest = "reset_to_bl" ,
133- help = "Do not send reset_to_bootloader command sequence" )
138+ dest = "reset_to_bl" , help = "Do not send reset_to_bootloader command sequence" )
139+ parser .add_argument ("-hd" , "--half-duplex" , action = "store_true" ,
140+ dest = "half_duplex" , help = "Use half duplex mode" )
141+ parser .add_argument ("-t" , "--type" , type = str , default = "ESP82" ,
142+ help = "Defines flash target type which is sent to target in reboot command" )
134143 args = parser .parse_args ()
135144
136145 if (args .port == None ):
137146 args .port = serials_find .get_serial_port ()
138147
139148 try :
140- bf_passthrough_init (args .port , args .baud , reset_to_bl = args . reset_to_bl )
149+ bf_passthrough_init (args .port , args .baud )
141150 except PassthroughEnabled as err :
142151 dbg_print (str (err ))
152+
153+ if args .reset_to_bl :
154+ reset_to_bootloader (args )
0 commit comments