forked from phryniszak/jstlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (132 loc) · 4.28 KB
/
index.js
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
/*!
* Copyright(c) 2021 Pawel Hryniszak
*/
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
import * as rtt from "./modules/rtt.js";
import { Terminal } from "./modules/terminal.js";
rtt.init();
const logger = debug("stlink:index");
///////////////////////////////////////////////////////////////////////////////
// VARIABLES
let rttRunning = false;
const rttWriteArr = [];
// elements
const btnConnect = document.getElementById("open-stlink");
const btnRTT = document.getElementById("rtt");
const elUSBinfo = document.getElementById("info-usb");
const elCPUinfo = document.getElementById("info-cpu");
const elTick = document.getElementById("info-tick").firstElementChild;
const elTerminal = document.querySelector(".terminal");
const terminal = new Terminal(elTerminal);
document.elTick = elTick;
// document.elTick.children[1].setAttributeNS(null,"fill","#f00");
///////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
const resize = () => {
terminal.resize();
};
const initUI = () => {
resize();
updateUI();
// Caution: I am telling you this as a friend. It exists. It is a thing, but it is a hack. Please don't use it
window.scrollTo(0,1);
};
const updateUI = async () => {
let isOpened = rtt.isOpened();
let devices = await navigator.usb.getDevices();
// buttons
btnRTT.disabled = !isOpened;
if (!isOpened){
btnRTT.innerText = "Start RTT";
btnConnect.innerText = "Open STLINK"
rttRunning = false;
}else{
btnConnect.innerText = "Close STLINK"
}
// USB info
if (devices.length) {
elUSBinfo.innerText = isOpened ? `${devices[0].productName} opened` : `${devices[0].productName} connected`;
} else {
elUSBinfo.innerText = "---";
}
// STM32 info
elCPUinfo.innerText = isOpened ? `${rtt.getMCUstring()} 0x${rtt.status.RAM.address.toString(16)}:0x${rtt.status.RAM.size.toString(16)}` : "---";
// RTT tick
document.elTick.children[1].setAttributeNS(null, "fill", "white");
logger(`isOpened: ${isOpened} devices: ${devices.length}`);
};
const runRTT = async () => {
if (rttRunning) {
rttRunning = false;
btnRTT.innerText = "Start RTT";
return;
}
try{
await rtt.find();
rttRunning = true;
btnRTT.innerText = "Stop RTT";
updateUI();
}catch(e){
console.log(e);
}
if (rtt.status.aUp.length == 0 || rtt.status.aDown.length == 0) {
logger("RTT not found");
terminal.writeln("RTT not found.");
rttRunning = false;
return;
}
terminal.writeln(`Starting to read from rtt[0] channel name ${rtt.status.aUp[0].name}`);
terminal.write(`Size in ${rtt.status.aUp[0].SizeOfBuffer}`);
terminal.writeln(` size out ${rtt.status.aDown[0].SizeOfBuffer}`);
while (rttRunning) {
try{
let rttbuff = await rtt.read();
rttTick();
if (rttbuff.length) {
logger("rtt: ", rttbuff);
terminal.write(rttbuff);
}
if (rttWriteArr.length) {
await rtt.write(rttWriteArr);
}
}catch(e){
console.log(e);
// If error is thrown, it could be the target was disconnected/restarted, find the rtt block again
try{
await rtt.find();
}catch(e){
console.log(e);
}
}
}
};
const rttTick = () => {
document.elTick.children[1].setAttributeNS(null, "fill", "#" + Math.floor(Math.random()*16777215).toString(16).padStart(6, "0"));
};
///////////////////////////////////////////////////////////////////////////////
// EVENTS
window.onresize = resize;
navigator.usb.onconnect = updateUI;
navigator.usb.ondisconnect = updateUI;
// https://developer.mozilla.org/en-US/docs/Web/API/Document/readystatechange_event
window.addEventListener("load", initUI);
// click on open STLINK
btnConnect.onclick = async () => {
if (rtt.isOpened()){
// Stop RTT
if (rttRunning) runRTT();
// Close device
await rtt.close();
}else{
try{
await rtt.open();
}catch(e){
console.log(e);
}
}
updateUI();
};
terminal.addEventListener("key", async (ev) => {
rttWriteArr.push(ev.detail.ascii);
});
btnRTT.onclick = runRTT;