1 import serial
, time
, sys
4 import external
.streamexpect
as streamexpect
7 def dbg_print(line
=''):
8 sys
.stdout
.write(line
+ '\n')
12 def etx_passthrough_init(port
, requestedBaudrate
):
14 dbg_print("======== PASSTHROUGH INIT ========")
15 dbg_print(" Trying to initialize %s @ %s" % (port
, requestedBaudrate
))
17 s
= serial
.Serial(port
=port
, baudrate
=requestedBaudrate
,
18 bytesize
=8, parity
='N', stopbits
=1,
19 timeout
=1, xonxoff
=0, rtscts
=0)
21 with streamexpect
.wrap(s
) as rl
:
23 rl
.write(b
"set pulses 0\n")
24 rl
.expect_bytes(b
"set: ", timeout
=1.0)
25 rl
.expect_bytes(b
"> ", timeout
=1.0)
26 rl
.write(b
"set rfmod 0 power off\n")
27 rl
.expect_bytes(b
"set: ", timeout
=1.0)
28 rl
.expect_bytes(b
"> ", timeout
=1.0)
30 rl
.write(b
"set rfmod 0 bootpin 1\n")
31 rl
.expect_bytes(b
"set: ", timeout
=1.0)
32 rl
.expect_bytes(b
"> ", timeout
=1.0)
34 rl
.write(b
"set rfmod 0 power on\n")
35 rl
.expect_bytes(b
"set: ", timeout
=1.0)
36 rl
.expect_bytes(b
"> ", timeout
=1.0)
38 rl
.write(b
"set rfmod 0 bootpin 0\n")
39 rl
.expect_bytes(b
"set: ", timeout
=1.0)
40 rl
.expect_bytes(b
"> ", timeout
=1.0)
42 cmd
= "serialpassthrough rfmod 0 %s" % requestedBaudrate
44 dbg_print("Enabling serial passthrough...")
45 dbg_print(" CMD: '%s'" % cmd
)
46 rl
.write(cmd
.encode("utf-8"))
51 dbg_print("======== PASSTHROUGH DONE ========")
53 def init_passthrough(source
, target
, env
):
54 env
.AutodetectUploadPort([env
])
55 port
= env
['UPLOAD_PORT']
56 etx_passthrough_init(port
, env
['UPLOAD_SPEED'])
58 if __name__
== '__main__':
59 parser
= argparse
.ArgumentParser(
60 description
="Initialize EdgeTX passthrough to internal module")
61 parser
.add_argument("-b", "--baud", type=int, default
=460800,
62 help="Baud rate for passthrough communication")
63 parser
.add_argument("-p", "--port", type=str,
64 help="Override serial port autodetection and use PORT")
65 args
= parser
.parse_args()
67 if (args
.port
== None):
68 args
.port
= serials_find
.get_serial_port()
70 etx_passthrough_init(args
.port
, args
.baud
)