Skip to content

Commit 7e8399a

Browse files
authored
Merge pull request #19 from ARMmbed/f/serial
Add serial write/read
2 parents b4c522c + 18dd3ec commit 7e8399a

File tree

11 files changed

+14726
-11731
lines changed

11 files changed

+14726
-11731
lines changed

binaries/k64f-blinky-green.bin

-4.98 KB
Binary file not shown.

binaries/k64f-blinky-red.bin

-4.98 KB
Binary file not shown.

binaries/microbit-say-green.hex

Lines changed: 4820 additions & 4073 deletions
Large diffs are not rendered by default.

binaries/microbit-say-red.hex

Lines changed: 4820 additions & 4073 deletions
Large diffs are not rendered by default.

binaries/nrf51dk-led-green.hex

Lines changed: 2461 additions & 1789 deletions
Large diffs are not rendered by default.

binaries/nrf51dk-led-red.hex

Lines changed: 2461 additions & 1789 deletions
Large diffs are not rendered by default.

examples/web.html

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en_GB">
33

44
<head>
5-
<title>WebUSB tester</title>
5+
<title>WebUSB demo</title>
66
<!-- Bootstrap CSS -->
77
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
88
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans">
@@ -27,13 +27,18 @@
2727
background-color: #fff;
2828
border: 2px solid #e7e9ec;
2929
border-radius: 6px;
30+
min-height: 100px;
3031
}
31-
32+
3233
p {
3334
margin: 0 0 15px;
3435
font-size: 18px;
3536
line-height: 1.8em;
3637
}
38+
39+
input {
40+
width: 100%;
41+
}
3742

3843
.container {
3944
max-width: 900px;
@@ -73,7 +78,7 @@
7378
</p>
7479

7580
<div class="row">
76-
<div class="col-md-4">
81+
<div class="col-md-5">
7782
<ol>
7883
<li>
7984
<p>
@@ -136,11 +141,38 @@
136141
<button onclick="printRegisters()" class="btn btn-info when-connected" disabled>Read Registers</button>
137142
</p>
138143
</li>
144+
<li>
145+
<p>
146+
Serial monitor:
147+
</p>
148+
<p>
149+
Baud Rate:
150+
<select id="baudRate" class="when-connected" disabled>
151+
<option value="9600">9600</option>
152+
<option value="14400">14400</option>
153+
<option value="19200">19200</option>
154+
<option value="28800">28800</option>
155+
<option value="38400">38400</option>
156+
<option value="57600">57600</option>
157+
<option value="115200">115200</option>
158+
</select>
159+
<div class="btn-group">
160+
<button onclick="startSerialMonitor()" class="btn btn-success when-connected" disabled>Start</button>
161+
<button onclick="stopSerialMonitor()" class="btn btn-danger when-connected" disabled>Stop</button>
162+
</div>
163+
</p>
164+
</li>
139165
</ol>
140166
</div>
141167

142-
<div class="col-md-8">
168+
<div class="col-md-7">
169+
Output:
143170
<pre id="logger"></pre>
171+
Serial monitor:
172+
<p>
173+
<input id="serialMonitorInput" class="when-connected" type="text" maxlength="63" placeholder="Write to a board" onchange="writeSerial()">
174+
</p>
175+
<pre id="serialMonitor"></pre>
144176
</div>
145177
</div>
146178
</div>
@@ -194,6 +226,15 @@
194226
document.getElementById("logger").innerHTML = "";
195227
}
196228

229+
function logSerial(data) {
230+
logger = document.getElementById("serialMonitor");
231+
logger.innerHTML = logger.innerHTML + data + "\n";
232+
}
233+
234+
function clearLogSerial() {
235+
document.getElementById("serialMonitor").innerHTML = "";
236+
}
237+
197238
/**
198239
* Snapshot the current state of the CPU. Reads all general-purpose registers, and returns them in an array.
199240
*/
@@ -208,6 +249,8 @@
208249
}
209250

210251
async function connect() {
252+
clearLog();
253+
clearLogSerial();
211254
this.hid = new DAPjs.HID(this.device);
212255

213256
log("Opening device.");
@@ -218,6 +261,7 @@
218261
log("Device opened.");
219262

220263
this.dapDevice = new DAPjs.DAP(this.hid);
264+
221265
const xhr = new XMLHttpRequest();
222266
xhr.responseType = "json";
223267
xhr.open("GET", "../flash_targets/flash_targets.json", true);
@@ -227,6 +271,7 @@
227271
let flashAlgorithm = new DAPjs.FlashAlgorithm(xhr.response, this.deviceCode);
228272
if (!flashAlgorithm.flashAlgo) return log("No flashing algorithm can be found for this board.");
229273

274+
this.serial = new DAPjs.Serial(dapDevice);
230275
this.target = new DAPjs.FlashTarget(this.dapDevice, flashAlgorithm);
231276

232277
log("Initialising device.");
@@ -296,6 +341,26 @@
296341
xhr.send();
297342
}
298343

344+
async function startSerialMonitor() {
345+
var elem = document.getElementById("baudRate");
346+
var baudRate = elem.options[elem.selectedIndex].value;
347+
await this.serial.initialize(baudRate);
348+
this.serial.start((data) => {
349+
logSerial(data);
350+
});
351+
}
352+
353+
function writeSerial() {
354+
var elem = document.getElementById("serialMonitorInput");
355+
this.serial.write(elem.value);
356+
elem.value = "";
357+
}
358+
359+
function stopSerialMonitor() {
360+
this.serial.stop();
361+
clearLogSerial();
362+
}
363+
299364
async function selectBoard() {
300365
this.device = await navigator.usb.requestDevice({ filters: [{vendorId: 0x0d28}]});
301366
this.deviceCode = this.device.serialNumber.slice(0, 4);
@@ -309,6 +374,18 @@
309374
this.flashProgressBar = document.getElementById('flash-progress');
310375
this.flashProgressBarContainer = document.getElementById('progress-container');
311376
this.selector = new DAPjs.PlatformSelector();
377+
navigator.usb.addEventListener('disconnect', disconnectEvent => {
378+
if (this.device && this.device.serialNumber == disconnectEvent.device.serialNumber) {
379+
this.device = null;
380+
const elements = Array.from(document.querySelectorAll(".when-connected"));
381+
for (const elem of elements) {
382+
elem.disabled = true;
383+
}
384+
clearLog();
385+
log("Device disconnected");
386+
clearLogSerial();
387+
}
388+
});
312389
};
313390
</script>
314391
</body>

src/dap/dap.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ApReg, DapRegisters, DapVal, Reg} from "./constants";
1+
import {ApReg, DapVal, Reg, DapRegisters} from "./constants";
22
import {PreparedDapCommand} from "./prepared";
33

44
import {CMSISDAP, DapCmd} from "../transport/cmsis_dap";
@@ -24,7 +24,6 @@ export class DAP {
2424

2525
public async init() {
2626
await this.dap.connect();
27-
2827
await this.readDp(Reg.IDCODE);
2928
// const n = await this.readDp(Reg.IDCODE);
3029
// this.idcode = n;
@@ -182,4 +181,20 @@ export class DAP {
182181

183182
return buf;
184183
}
184+
185+
public async readSerialSettings() {
186+
return this.dap.cmdNums(DapCmd.DAP_VENDOR1, []);
187+
}
188+
189+
public async initializeSerial(data: number[]) {
190+
return this.dap.cmdNums(DapCmd.DAP_VENDOR2, data);
191+
}
192+
193+
public async readSerial() {
194+
return this.dap.cmdNums(DapCmd.DAP_VENDOR3, []);
195+
}
196+
197+
public async writeSerial(data: number[]) {
198+
return this.dap.cmdNums(DapCmd.DAP_VENDOR4, data);
199+
}
185200
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {CortexM} from "./cortex/cortex";
22
export {CortexReg, CortexSpecialReg, CoreState, CoreNames, ISANames} from "./cortex/constants";
33
export {DAP} from "./dap/dap";
4+
export {Serial} from "./serial/serial";
45

56
export {FlashTarget} from "./targets/FlashTarget";
67
export {FlashProgram} from "./targets/FlashProgram";

src/serial/serial.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {DAP} from "../dap/dap";
2+
import {addInt32} from "../util";
3+
4+
export class Serial {
5+
private dap: DAP;
6+
private timer: any;
7+
private delay: number;
8+
9+
constructor(dap: DAP) {
10+
this.dap = dap;
11+
}
12+
13+
public async initialize(baudRate: number, delay = 200) {
14+
this.delay = delay;
15+
const serialConfig: number[] = [];
16+
addInt32(serialConfig, baudRate);
17+
await this.dap.initializeSerial(serialConfig);
18+
}
19+
20+
public async start(responseCallback?: (serialData: string) => void) {
21+
this.timer = setInterval(async () => {
22+
let serialData = await this.dap.readSerial();
23+
if (serialData.byteLength > 0) {
24+
serialData = serialData.subarray(1);
25+
const emptyResponse = serialData.every((c: any) => {
26+
return c === 0;
27+
});
28+
if (!emptyResponse) {
29+
const data = Buffer.from(serialData.buffer).toString("utf8").substring(1);
30+
responseCallback(data);
31+
}
32+
}
33+
}, this.delay);
34+
}
35+
36+
public async getSerialSettings() {
37+
return this.dap.readSerialSettings();
38+
}
39+
40+
public async stop() {
41+
if (this.timer) {
42+
clearInterval(this.timer);
43+
this.timer = null;
44+
}
45+
}
46+
47+
public async write(data: string) {
48+
let arrayData = [];
49+
if (data || data !== "") {
50+
arrayData = data.split("").map((e: any) => e.charCodeAt());
51+
}
52+
return await this.dap.writeSerial(arrayData);
53+
}
54+
}

0 commit comments

Comments
 (0)