Skip to content

Commit 81addf6

Browse files
committed
Refactor serial.
Add example binaries.
1 parent f55c738 commit 81addf6

File tree

11 files changed

+14630
-11783
lines changed

11 files changed

+14630
-11783
lines changed

binaries/k64f-blinky-green.bin

3.09 KB
Binary file not shown.

binaries/k64f-blinky-red.bin

3.09 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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@
261261
log("Device opened.");
262262

263263
this.dapDevice = new DAPjs.DAP(this.hid);
264+
this.serial = new DAPjs.Serial(dapDevice);
264265
this.target = new DAPjs.FlashTarget(this.dapDevice, DAPjs.FlashTargets.get(this.deviceCode));
265266

266267
log("Initialising device.");
@@ -333,20 +334,20 @@
333334
async function startSerialMonitor() {
334335
var elem = document.getElementById("baudRate");
335336
var baudRate = elem.options[elem.selectedIndex].value;
336-
await this.dapDevice.initializeSerialMonitor(baudRate);
337-
this.dapDevice.startSerialMonitor((data) => {
337+
await this.serial.initialize(baudRate);
338+
this.serial.start((data) => {
338339
logSerial(data);
339340
});
340341
}
341342

342343
function writeSerial() {
343344
var elem = document.getElementById("serialMonitorInput");
344-
this.dapDevice.writeSerialData(elem.value);
345+
this.serial.write(elem.value);
345346
elem.value = "";
346347
}
347348

348349
function stopSerialMonitor() {
349-
this.dapDevice.stopSerialMonitor();
350+
this.serial.stop();
350351
clearLogSerial();
351352
}
352353

src/dap/dap.ts

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export class DAP {
1010

1111
private dpSelect: number;
1212
private csw: number;
13-
private serialTimer: any;
1413
// private idcode: number;
1514

1615
constructor(private device: IHID) {
@@ -23,48 +22,6 @@ export class DAP {
2322
await this.init();
2423
}
2524

26-
public async initializeSerialMonitor(baudRate: number) {
27-
const serialConfig: number[] = [];
28-
addInt32(serialConfig, baudRate);
29-
// serialConfig.push("8".charCodeAt(0)); // data bits
30-
// serialConfig.push("0".charCodeAt(0)); // parity
31-
// serialConfig.push("1".charCodeAt(0)); // stop bits
32-
// serialConfig.push("0".charCodeAt(0)); // flow control
33-
// console.log(serialConfig);
34-
await this.dap.initializeSerial(serialConfig);
35-
}
36-
37-
public async startSerialMonitor(responseCallback?: (serialData: string) => void) {
38-
this.serialTimer = setInterval(async () => {
39-
let serialData = await this.dap.getSerialData();
40-
if (serialData.byteLength > 0) {
41-
serialData = serialData.subarray(1);
42-
const emptyResponse = serialData.every((c: any) => {
43-
return c === 0;
44-
});
45-
if (!emptyResponse) {
46-
const data = Buffer.from(serialData.buffer).toString("utf8").substring(1);
47-
responseCallback(data);
48-
}
49-
}
50-
}, 200);
51-
}
52-
53-
public async writeSerialData(data: string) {
54-
let arrayData = [];
55-
if (data || data !== "") {
56-
arrayData = data.split("").map((e: any) => e.charCodeAt());
57-
}
58-
return await this.dap.writeSerialData(arrayData);
59-
}
60-
61-
public async stopSerialMonitor() {
62-
if (this.serialTimer) {
63-
clearInterval(this.serialTimer);
64-
this.serialTimer = null;
65-
}
66-
}
67-
6825
public async init() {
6926
await this.dap.connect();
7027
await this.readDp(Reg.IDCODE);
@@ -224,4 +181,16 @@ export class DAP {
224181

225182
return buf;
226183
}
184+
185+
public async initializeSerial(data: number[]) {
186+
return this.dap.cmdNums(DapCmd.DAP_VENDOR1, data);
187+
}
188+
189+
public async readSerial() {
190+
return this.dap.cmdNums(DapCmd.DAP_VENDOR2, []);
191+
}
192+
193+
public async writeSerial(data: number[]) {
194+
return this.dap.cmdNums(DapCmd.DAP_VENDOR3, data);
195+
}
227196
}

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 {FlashTargets, FlashTarget} from "./targets/FlashTarget";
67
export {FlashProgram} from "./targets/FlashProgram";

src/serial/serial.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 stop() {
37+
if (this.timer) {
38+
clearInterval(this.timer);
39+
this.timer = null;
40+
}
41+
}
42+
43+
public async write(data: string) {
44+
let arrayData = [];
45+
if (data || data !== "") {
46+
arrayData = data.split("").map((e: any) => e.charCodeAt());
47+
}
48+
return await this.dap.writeSerial(arrayData);
49+
}
50+
}

0 commit comments

Comments
 (0)