-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathprogram_radio.py
More file actions
217 lines (180 loc) · 5.53 KB
/
program_radio.py
File metadata and controls
217 lines (180 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import serial.tools.list_ports
import time
import esptool
import sys
from xmodem import XMODEM
def flash_esp_api(
port,
baud,
chip,
flash_args,
):
"""
flash_args:
[
(0x1000, "bootloader.bin"),
(0x8000, "partition-table.bin"),
(0x10000, "app.bin"),
]
"""
# 等价于命令行参数
argv = [
"--chip", chip,
"--port", port,
"--baud", str(baud),
"write-flash",
]
for addr, path in flash_args:
argv.append(hex(addr))
argv.append(path)
# esptool 内部就是解析 sys.argv
old_argv = sys.argv
try:
sys.argv = ["esptool.py"] + argv
esptool.main()
except:
return False
finally:
sys.argv = old_argv
return True
class radio_class(object):
def __init__(self):
self.ser = ''
self.status = 0
self.com = ''
def find_word_in_string(self, s, word):
try:
words = s.split()
except:
return 0
for w in words:
if w == word:
return 1
return 0
def serial_tx(self, str):
self.ser.write(str.encode('utf-8'))
def serial_rx(self, sleep_sec):
data_received = b''
time.sleep(sleep_sec)
while self.ser.in_waiting:
data_received = self.ser.read(self.ser.in_waiting)
return data_received.decode('utf-8')
def getc(self, size, timeout=1):
return self.ser.read(size)
def putc(self, data, timeout=1):
return self.ser.write(data)
def search_stm32_port(self):
ports = list(serial.tools.list_ports.comports())
for i in range(0, len(ports)):
if (self.find_word_in_string(ports[i].description, 'STMicroelectronics')):
self.com = ports[i].name
time.sleep(1)
try:
self.ser = serial.Serial(self.com, 115200, timeout=2)
return 1
except:
return 0
return 0
def search_elrs_port(self):
ports = list(serial.tools.list_ports.comports())
for i in range(0, len(ports)):
if (self.find_word_in_string(ports[i].description, 'CH340')):
self.com = ports[i].name
time.sleep(1)
self.ser = serial.Serial(self.com, 115200, timeout=2)
return 1
return 0
def unzip_radio_firmware(self, file_path, dest_folder):
import zipfile
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(dest_folder)
def radio_is_active(self):
if self.search_stm32_port() == 0:
return 0
cmd = "ATPING\r\n"
self.ser.write(cmd.encode('utf-8'))
rx = self.serial_rx(1)
self.ser.close()
if self.find_word_in_string(rx, "HDZero"):
return 1
else:
return 2
def program_stm32(self):
# enter bootloader mode
if self.search_stm32_port() == 0:
return False
cmd = "ATPROG\r\n"
self.ser.write(cmd.encode('utf-8'))
self.ser.write(cmd.encode('utf-8'))
self.ser.write(cmd.encode('utf-8'))
time.sleep(0.5)
self.ser.close()
time.sleep(0.5)
# start programming
if self.search_stm32_port() == 0:
return False
time.sleep(0.2)
cmd = "XXX"
self.ser.write(cmd.encode('utf-8'))
self.ser.write(cmd.encode('utf-8'))
self.ser.write(cmd.encode('utf-8'))
time.sleep(0.5)
self.serial_rx(0)
try:
stream = open("resource/hdzero_radio_stm32.bin", "rb")
modem = XMODEM(self.getc, self.putc)
modem.send(stream)
except:
return False
cmd = "ccc"
self.ser.write(cmd.encode('utf-8'))
self.ser.write(cmd.encode('utf-8'))
self.ser.write(cmd.encode('utf-8'))
time.sleep(0.5)
self.ser.close()
return True
def program_elrs_tx(self):
if self.search_stm32_port() == 0:
return False
cmd = "ATPGTX\r\n"
self.ser.write(cmd.encode('utf-8'))
time.sleep(0.2)
self.ser.close()
time.sleep(1)
if self.search_elrs_port() == 0:
return False
self.ser.close()
return flash_esp_api(
port=self.ser.port,
baud=460800,
chip="esp32",
flash_args=[
(0x1000, "resource/elrs_tx/bootloader.bin"),
(0x8000, "resource/elrs_tx/partitions.bin"),
(0xe000, "resource/elrs_tx/boot_app0.bin"),
(0x10000, "resource/elrs_tx/firmware.bin"),
],
)
def program_elrs_backpack(self):
if self.search_stm32_port() == 0:
return False
cmd = "ATPGBP\r\n"
self.ser.write(cmd.encode('utf-8'))
time.sleep(0.2)
self.ser.close()
time.sleep(1)
if self.search_elrs_port() == 0:
return False
self.ser.close()
return flash_esp_api(
port=self.ser.port,
baud=460800,
chip="esp32c3",
flash_args=[
(0x0000, "resource/elrs_backpack/bootloader.bin"),
(0x8000, "resource/elrs_backpack/partitions.bin"),
(0xe000, "resource/elrs_backpack/boot_app0.bin"),
(0x10000, "resource/elrs_backpack/firmware.bin"),
],
)
my_radio = radio_class()