-
Notifications
You must be signed in to change notification settings - Fork 2
/
rtt.py
executable file
·103 lines (84 loc) · 2.61 KB
/
rtt.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
#!/usr/bin/env python3
import subprocess
import time
import os
import psutil
import signal
import sys
LOG_FILE = "/tmp/rtt_output.log"
DEVICE = "NRF52833_XXAA"
INTERFACE = "SWD"
SPEED = 4000
CHANNEL = 0
TIMEOUT = 5 # Timeout in seconds to detect inactivity
rtt_process = None
if not os.path.exists(LOG_FILE):
with open(LOG_FILE, "w"):
pass
else:
with open(LOG_FILE, "w") as f:
f.truncate(0)
def kill_existing_jlink_rtt_logger():
for proc in psutil.process_iter(attrs=['pid', 'name']):
if proc.info['name'] == 'JLinkRTTLoggerEx':
proc.kill()
time.sleep(1)
def start_rtt_logger():
command = [
"JLinkRTTLogger",
"-Device", DEVICE,
"-If", INTERFACE,
"-Speed", str(SPEED),
"-RTTChannel", str(CHANNEL),
LOG_FILE
]
with open(os.devnull, 'w') as devnull:
return subprocess.Popen(command, stdout=devnull, stderr=devnull), time.time()
def monitor_log_file(log_file, start_time, timeout):
last_size = 0
while True:
if os.path.exists(log_file):
current_size = os.path.getsize(log_file)
if current_size != last_size:
with open(log_file, "r") as f:
f.seek(last_size)
new_data = f.read()
if new_data:
# Filter out unwanted lines
filtered_data = "\n".join(
line for line in new_data.splitlines()
if "Booting Zephyr OS build" not in line
) + "\n"
print(filtered_data, end="")
last_size = current_size
start_time = time.time()
elif time.time() - start_time > timeout:
break
time.sleep(1)
return False
def signal_handler(sig, frame):
if rtt_process:
rtt_process.terminate()
rtt_process.wait()
sys.exit(0)
def main():
global rtt_process
# Register signal handlers
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
while True:
kill_existing_jlink_rtt_logger()
# Start RTT Logger
rtt_process, start_time = start_rtt_logger()
try:
# Monitor log file for timeout
monitor_log_file(LOG_FILE, start_time, TIMEOUT)
except KeyboardInterrupt:
print("Keyboard Interrupt")
break
finally:
# Terminate RTT Logger process if running
rtt_process.terminate()
rtt_process.wait()
if __name__ == "__main__":
main()