Skip to content

Commit 7885600

Browse files
committed
Clear errors in the abort register after any error
1 parent f50910f commit 7885600

File tree

6 files changed

+235
-151
lines changed

6 files changed

+235
-151
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
],
3636
"scripts": {
3737
"build": "rollup -c && typedoc src",
38-
"watch": "rollup -c rollup.dev.config.js -w"
38+
"watch": "rollup -c -w",
39+
"watch:web": "rollup -c rollup.dev.config.js -w"
3940
},
4041
"engines": {
4142
"node": ">=8.14.0"

rollup.config.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
import del from "rollup-plugin-delete";
2-
import tslint from "rollup-plugin-tslint";
3-
import builtins from "rollup-plugin-node-builtins";
4-
import typescript from "rollup-plugin-typescript2";
5-
import { terser } from "rollup-plugin-terser";
6-
import sourceMaps from "rollup-plugin-sourcemaps"
1+
import del from 'rollup-plugin-delete';
2+
import tslint from 'rollup-plugin-tslint';
3+
import builtins from 'rollup-plugin-node-builtins';
4+
import typescript from 'rollup-plugin-typescript2';
5+
import { terser } from 'rollup-plugin-terser';
6+
import sourceMaps from 'rollup-plugin-sourcemaps'
77

8-
const name = "DAPjs";
8+
const name = 'DAPjs';
99
const pkg = require('./package.json')
10+
const watch = process.env.ROLLUP_WATCH;
1011

1112
export default {
12-
input: "src/index.ts",
13+
input: 'src/index.ts',
1314
output: [
1415
{
1516
file: pkg.main,
16-
format: "umd",
17+
format: 'umd',
1718
sourcemap: true,
1819
name
1920
},
2021
{
2122
file: pkg.module,
22-
format: "esm",
23+
format: 'esm',
2324
sourcemap: true
2425
}
2526
],
2627
plugins: [
27-
del({
28+
!watch && del({
2829
targets: [
29-
"dist/*",
30-
"types/*"
30+
'dist/*',
31+
'types/*'
3132
]
3233
}),
3334
tslint({

rollup.dev.config.js renamed to rollup.web.config.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
import del from "rollup-plugin-delete";
2-
import tslint from "rollup-plugin-tslint";
3-
import builtins from "rollup-plugin-node-builtins";
4-
import typescript from "rollup-plugin-typescript2";
5-
import { terser } from "rollup-plugin-terser";
6-
import sourceMaps from "rollup-plugin-sourcemaps"
7-
import serve from "rollup-plugin-serve";
8-
import livereload from "rollup-plugin-livereload";
1+
import del from 'rollup-plugin-delete';
2+
import tslint from 'rollup-plugin-tslint';
3+
import builtins from 'rollup-plugin-node-builtins';
4+
import typescript from 'rollup-plugin-typescript2';
5+
import { terser } from 'rollup-plugin-terser';
6+
import sourceMaps from 'rollup-plugin-sourcemaps'
7+
import serve from 'rollup-plugin-serve';
8+
import livereload from 'rollup-plugin-livereload';
99

10-
const name = "DAPjs";
10+
const name = 'DAPjs';
1111
const pkg = require('./package.json')
1212
const watch = process.env.ROLLUP_WATCH;
1313

1414
export default {
15-
input: "src/index.ts",
15+
input: 'src/index.ts',
1616
output: [
1717
{
1818
file: pkg.main,
19-
format: "umd",
19+
format: 'umd',
2020
sourcemap: true,
2121
name
2222
},
2323
{
2424
file: pkg.module,
25-
format: "esm",
25+
format: 'esm',
2626
sourcemap: true
2727
}
2828
],
2929
plugins: [
3030
!watch && del({
3131
targets: [
32-
"dist/*",
33-
"types/*"
32+
'dist/*',
33+
'types/*'
3434
]
3535
}),
3636
tslint({
@@ -43,9 +43,9 @@ export default {
4343
terser(),
4444
sourceMaps(),
4545
watch && serve({
46-
contentBase: ".",
46+
contentBase: '.',
4747
open: true,
48-
openPage: "/examples/daplink-flash/web.html",
48+
openPage: '/examples/daplink-flash/web.html',
4949
}),
5050
watch && livereload()
5151
]

src/dap/adi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export class ADI implements DAP {
322322
* @returns Promise of register data
323323
*/
324324
public async readMem32(register: number): Promise<number> {
325-
const result = await this.proxy.transfer(this.readMem32Command(register));
325+
const result = await this.proxy.transfer(this.readMem32Command(register));
326326
return result[0];
327327
}
328328

src/daplink/index.ts

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ export class DAPLink extends CmsisDAP {
127127
data.set([page.byteLength]);
128128
data.set(new Uint8Array(page), 1);
129129

130-
await this.send(DAPLinkFlash.WRITE, data);
130+
try {
131+
await this.send(DAPLinkFlash.WRITE, data);
132+
} catch (error) {
133+
await this.clearAbort();
134+
throw error;
135+
}
136+
131137
this.emit(DAPLink.EVENT_PROGRESS, offset / buffer.byteLength);
132138
if (end < buffer.byteLength) {
133139
return this.writeBuffer(buffer, pageSize, end);
@@ -148,32 +154,42 @@ export class DAPLink extends CmsisDAP {
148154
const arrayBuffer = isView(buffer) ? buffer.buffer : buffer;
149155
const streamType = this.isBufferBinary(arrayBuffer) ? 0 : 1;
150156

151-
let result = await this.send(DAPLinkFlash.OPEN, new Uint32Array([streamType]));
157+
try {
158+
let result = await this.send(DAPLinkFlash.OPEN, new Uint32Array([streamType]));
152159

153-
// An error occurred
154-
if (result.getUint8(1) !== 0) {
155-
throw new Error('Flash error');
156-
}
160+
// An error occurred
161+
if (result.getUint8(1) !== 0) {
162+
throw new Error('Flash error');
163+
}
157164

158-
await this.writeBuffer(arrayBuffer, pageSize);
159-
this.emit(DAPLink.EVENT_PROGRESS, 1.0);
160-
result = await this.send(DAPLinkFlash.CLOSE);
165+
await this.writeBuffer(arrayBuffer, pageSize);
166+
this.emit(DAPLink.EVENT_PROGRESS, 1.0);
167+
result = await this.send(DAPLinkFlash.CLOSE);
161168

162-
// An error occurred
163-
if (result.getUint8(1) !== 0) {
164-
throw new Error('Flash error');
165-
}
169+
// An error occurred
170+
if (result.getUint8(1) !== 0) {
171+
throw new Error('Flash error');
172+
}
166173

167-
await this.send(DAPLinkFlash.RESET);
174+
await this.send(DAPLinkFlash.RESET);
175+
} catch (error) {
176+
await this.clearAbort();
177+
throw error;
178+
}
168179
}
169180

170181
/**
171182
* Get the serial baud rate setting
172183
* @returns Promise of baud rate
173184
*/
174185
public async getSerialBaudrate(): Promise<number> {
175-
const result = await this.send(DAPLinkSerial.READ_SETTINGS);
176-
return result.getUint32(1, true);
186+
try {
187+
const result = await this.send(DAPLinkSerial.READ_SETTINGS);
188+
return result.getUint32(1, true);
189+
} catch (error) {
190+
await this.clearAbort();
191+
throw error;
192+
}
177193
}
178194

179195
/**
@@ -182,7 +198,12 @@ export class DAPLink extends CmsisDAP {
182198
* @returns Promise
183199
*/
184200
public async setSerialBaudrate(baudrate: number = DEFAULT_BAUDRATE): Promise<void> {
185-
await this.send(DAPLinkSerial.WRITE_SETTINGS, new Uint32Array([baudrate]));
201+
try {
202+
await this.send(DAPLinkSerial.WRITE_SETTINGS, new Uint32Array([baudrate]));
203+
} catch (error) {
204+
await this.clearAbort();
205+
throw error;
206+
}
186207
}
187208

188209
/**
@@ -193,33 +214,43 @@ export class DAPLink extends CmsisDAP {
193214
public async serialWrite(data: string): Promise<void> {
194215
const arrayData = data.split('').map((e: string) => e.charCodeAt(0));
195216
arrayData.unshift(arrayData.length);
196-
await this.send(DAPLinkSerial.WRITE, new Uint8Array(arrayData).buffer);
217+
try {
218+
await this.send(DAPLinkSerial.WRITE, new Uint8Array(arrayData).buffer);
219+
} catch (error) {
220+
await this.clearAbort();
221+
throw error;
222+
}
197223
}
198224

199225
/**
200226
* Read serial data
201227
* @returns Promise of any arrayBuffer read
202228
*/
203229
public async serialRead(): Promise<ArrayBuffer | undefined> {
204-
const serialData = await this.send(DAPLinkSerial.READ);
205-
// Check if there is any data returned from the device
206-
if (serialData.byteLength === 0) {
207-
return undefined;
208-
}
230+
try {
231+
const serialData = await this.send(DAPLinkSerial.READ);
232+
// Check if there is any data returned from the device
233+
if (serialData.byteLength === 0) {
234+
return undefined;
235+
}
209236

210-
// First byte contains the vendor code
211-
if (serialData.getUint8(0) !== DAPLinkSerial.READ) {
212-
return undefined;
213-
}
237+
// First byte contains the vendor code
238+
if (serialData.getUint8(0) !== DAPLinkSerial.READ) {
239+
return undefined;
240+
}
214241

215-
// Second byte contains the actual length of data read from the device
216-
const dataLength = serialData.getUint8(1);
217-
if (dataLength === 0) {
218-
return undefined;
219-
}
242+
// Second byte contains the actual length of data read from the device
243+
const dataLength = serialData.getUint8(1);
244+
if (dataLength === 0) {
245+
return undefined;
246+
}
220247

221-
const offset = 2;
222-
return serialData.buffer.slice(offset, offset + dataLength);
248+
const offset = 2;
249+
return serialData.buffer.slice(offset, offset + dataLength);
250+
} catch (error) {
251+
await this.clearAbort();
252+
throw error;
253+
}
223254
}
224255

225256
/**

0 commit comments

Comments
 (0)