forked from mightymos/OnbrightFlasher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashScript.py
275 lines (231 loc) · 11.1 KB
/
flashScript.py
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import sys
import subprocess
import re
import time
import os
import logging
# Check and install pyserial
try:
import serial.tools.list_ports
except ImportError:
python_executable = sys.executable
subprocess.check_call([python_executable, "-m", "pip", "install", "pyserial"])
import serial.tools.list_ports
class StateMachine:
def __init__(self):
# Initialize variables
self.selected_port = None
self.ser = None
self.selected_file = None
self.states = [
self.select_port,
self.open_serial,
self.list_and_select_files,
self.check_ready,
self.handshake,
self.connect_to_OBS38S003,
self.erase,
self.set_fuse,
self.send_file,
self.reset_mcu
]
self.current_state = 0
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler('flashScript.log')
]
)
self.logger = logging.getLogger(__name__)
def run(self):
try:
while self.current_state < len(self.states):
state_function = self.states[self.current_state]
success = state_function()
if not success:
self.logger.error(f"State {self.current_state} failed. Returning to handshake state.")
self.current_state = 3 # Set to index of the 'check_ready' state
else:
self.current_state += 1
except serial.SerialException as e:
self.logger.error(f"Error opening or closing serial connection: {e}")
finally:
if self.ser and self.ser.isOpen():
self.ser.close()
print("Serial connection closed.")
def select_port(self):
available_ports = list(serial.tools.list_ports.comports())
if not available_ports:
self.logger.error("No COM ports available.")
return False
print("Available COM ports:")
for i, port_info in enumerate(available_ports, start=1):
print(f"{i}. {port_info}")
while True:
try:
selection = int(input("Select a COM port (enter the number): "))
if 1 <= selection <= len(available_ports):
self.selected_port = available_ports[selection - 1].device
return True
else:
print("Invalid selection. Please enter a valid number.")
except ValueError:
self.logger.error("Invalid input. Please enter a valid number.")
def open_serial(self):
try:
self.ser = serial.Serial(self.selected_port, baudrate=115200, timeout=1,
bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
rtscts=False, dsrdtr=False, xonxoff=False, write_timeout=None,
inter_byte_timeout=None, exclusive=None)
self.logger.info(f"Serial connection opened on {self.selected_port}")
return True
except serial.SerialException as e:
self.logger.error(f"Error opening serial connection: {e}")
return False
def list_and_select_files(self):
current_directory = os.getcwd()
hex_files = [file for file in os.listdir(current_directory) if file.lower().endswith(('.hex', '.ihx'))]
if not hex_files:
print("No hex or ihx files found in the current directory.")
return False
print("Select a file by number:")
for i, file in enumerate(hex_files, start=1):
print(f"{i}. {file}")
while True:
try:
user_input = int(input("Enter the number of the file you want to select: "))
if 1 <= user_input <= len(hex_files):
self.selected_file = os.path.join(current_directory, hex_files[user_input - 1])
self.logger.info(f"You selected: {self.selected_file}")
return True
else:
print("Invalid input. Please enter a valid number.")
except ValueError:
self.logger.error("Invalid input. Please enter a valid number.")
def check_ready(self):
return self.check_if_ready(expected_data=None)
def check_if_ready(self, timeout=10, expected_data=None):
start_time = time.time()
try:
while True:
data = self.ser.readline().decode('utf-8').strip()
self.logger.debug(f'data = {data} and expected_data = {expected_data}')
if data:
self.logger.info(data)
if expected_data and expected_data in data:
return True
if expected_data is None and not data:
return True
if data == "Status: 2":
self.logger.error(f"Something went wrong. Process ended with {data}")
return False
if data == "Can try command [signature] or [idle] then [handshake] to retry":
self.logger.error(f"Something went wrong. Process ended with {data}")
return False
if time.time() - start_time > timeout:
self.logger.error(f"Timeout reached. No valid response within {timeout} seconds. Last response was: {data}")
return False
except serial.SerialException as e:
self.logger.error(f"Error during check_if_ready: {e}")
return False
def send_command(self, command, expected_response, max_retries=20, retry_interval=0.5, write_delay=0.1, resend_interval=5):
try:
retries = 0
while retries < max_retries:
if retries == 0 or retries % resend_interval == 0:
self.ser.write(f"{command}\r\n".encode('utf-8'))
time.sleep(write_delay)
response = self.ser.readline().decode('utf-8').strip()
self.logger.info(response)
if response == expected_response:
return True
self.logger.debug(f'send {command} expects {expected_response} but gets {response} retries:{retries}/{max_retries}')
retries += 1
time.sleep(retry_interval)
return False
except serial.SerialException as e:
self.logger.error(f"Error during send_command: {e}")
return False
def handshake(self):
try:
if self.send_command("handshake", "handshake"):
if self.check_if_ready(timeout=30, expected_data="Status: 0"):
return True
except serial.SerialException as e:
self.logger.error(f"Error during perform_handshake: {e}")
return False
def connect_to_OBS38S003(self):
try:
if self.check_if_ready(timeout=5, expected_data="Chip read: 0xA"):
if self.check_if_ready(timeout=5, expected_data="Returning to idle state..."):
return True
except serial.SerialException as e:
self.logger.error(f"Error during connect_to_OBS38S003: {e}")
return False
def erase(self):
try:
if self.send_command("erase", "Erasing chip..."):
if self.check_if_ready(timeout=10, expected_data="Chip erase successful"):
return True
except Exception as e:
self.logger.error(f"Error during erase: {e}")
return False
def set_fuse(self):
try:
if self.send_command("setfuse 18 249", "Set configuration byte..."):
if self.check_if_ready(timeout=5, expected_data="Status: 0"):
if self.check_if_ready(timeout=5, expected_data="Wrote configuration byte"):
return True
except Exception as e:
self.logger.error(f"Error during set_fuse: {e}")
return False
def send_file(self):
try:
if self.check_if_ready(timeout=1):
with open(self.selected_file, 'r') as file:
lines = file.readlines()
for i, line in enumerate(lines):
self.ser.write(line.encode('utf-8'))
response = self.ser.readline().decode('utf-8').strip()
self.logger.info(response)
if not response.strip() == line.strip():
self.logger.error("Error: Unexpected response or mismatch with sent line.")
self.logger.error(f"Sent line representation: {repr(line.strip())}")
self.logger.error(f"Response representation: {repr(response)}")
return False
if not i == len(lines) - 1:
response = self.ser.readline().decode('utf-8').strip()
self.logger.info(response)
expected = "Write successful"
if not response.strip() == expected:
self.logger.error("Error: Unexpected response or mismatch with sent line.")
self.logger.error(f"Sent line representation: {repr(line.strip())}")
self.logger.error(f"Response representation: {repr(response)}")
return False
response = self.ser.readline().decode('utf-8').strip()
self.logger.info(response)
pattern = re.compile(r'Wrote (\d+) bytes')
match = pattern.match(response)
if not match:
self.logger.error("Error: Unexpected response or mismatch with expected pattern.")
self.logger.error(f"Sent line representation: {repr(line.strip())}")
self.logger.error(f"Response representation: {repr(response)}")
return False
return True
except Exception as e:
self.logger.error(f"Error sending file content: {e}")
return False
def reset_mcu(self):
try:
if self.send_command("mcureset", "MCU reset..."):
if self.check_if_ready(timeout=1):
return True
except Exception as e:
self.logger.error(f"Error during reset_mcu: {e}")
return False
if __name__ == "__main__":
state_machine = StateMachine()
state_machine.run()