@@ -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