@@ -51,7 +51,7 @@ bitflags! {
51
51
/// DAP handler.
52
52
pub struct Dap < ' a , DEPS , LEDS , WAIT , JTAG , SWD , SWO > {
53
53
state : State < DEPS , SWD , JTAG > ,
54
- swo : Option < SWO > ,
54
+ swo : SWO ,
55
55
swo_streaming : bool ,
56
56
version_string : & ' a str ,
57
57
transfer_config : TransferConfig ,
75
75
dependencies : DEPS ,
76
76
leds : LEDS ,
77
77
wait : WAIT ,
78
- swo : Option < SWO > ,
78
+ swo : SWO ,
79
79
version_string : & ' a str ,
80
80
) -> Self {
81
81
assert ! ( SWD :: available( & dependencies) || JTAG :: available( & dependencies) ) ;
@@ -93,19 +93,16 @@ where
93
93
}
94
94
// Bit 2: SWO UART optional
95
95
// Bit 3: SWO Manchester optional
96
- if let Some ( swo) = & swo {
97
- if swo. support ( ) . uart {
98
- caps. insert ( Capabilities :: SWO_UART ) ;
99
- }
100
- if swo. support ( ) . manchester {
101
- caps. insert ( Capabilities :: SWO_MANCHESTER ) ;
102
- }
96
+ if swo. support ( ) . uart {
97
+ caps. insert ( Capabilities :: SWO_UART ) ;
98
+ }
99
+ if swo. support ( ) . manchester {
100
+ caps. insert ( Capabilities :: SWO_MANCHESTER ) ;
103
101
}
104
102
// Bit 4: Atomic commands not supported
105
103
// Bit 5: Test Domain Timer not supported
106
- // Bit 6: SWO Streaming Trace supported
107
- if swo. is_some ( ) {
108
- // TODO this depends on the transport layer
104
+ // Bit 6: SWO Streaming Trace optional
105
+ if swo. support ( ) . streaming {
109
106
caps. insert ( Capabilities :: SWO_STREAMING ) ;
110
107
}
111
108
caps
@@ -219,11 +216,7 @@ where
219
216
}
220
217
Ok ( DapInfoID :: SWOTraceBufferSize ) => {
221
218
resp. write_u8 ( 4 ) ;
222
- let size = match & self . swo {
223
- Some ( swo) => swo. buffer_size ( ) ,
224
- None => 0 ,
225
- } ;
226
- resp. write_u32 ( size as u32 ) ;
219
+ resp. write_u32 ( self . swo . buffer_size ( ) ) ;
227
220
}
228
221
Ok ( DapInfoID :: MaxPacketCount ) => {
229
222
resp. write_u8 ( 1 ) ;
@@ -499,48 +492,34 @@ where
499
492
}
500
493
501
494
fn process_swo_mode ( & mut self , mut req : Request , resp : & mut ResponseWriter ) {
502
- let mode = req. next_u8 ( ) ;
495
+ let success = match swo:: SwoMode :: try_from ( req. next_u8 ( ) ) {
496
+ Ok ( mode) => self . swo . set_mode ( mode) ,
497
+ _ => false ,
498
+ } ;
503
499
504
- let swo = if let Some ( swo ) = & mut self . swo {
505
- swo
500
+ if success {
501
+ resp . write_ok ( ) ;
506
502
} else {
507
503
resp. write_err ( ) ;
508
- return ;
509
- } ;
510
-
511
- match swo:: SwoMode :: try_from ( mode) {
512
- Ok ( mode) => {
513
- swo. set_mode ( mode) ;
514
- resp. write_ok ( ) ;
515
- }
516
- _ => resp. write_err ( ) ,
517
504
}
518
505
}
519
506
520
507
fn process_swo_baudrate ( & mut self , mut req : Request , resp : & mut ResponseWriter ) {
521
508
let target = req. next_u32 ( ) ;
522
- let actual = if let Some ( swo) = & mut self . swo {
523
- swo. set_baudrate ( target)
524
- } else {
525
- 0
526
- } ;
509
+ let actual = self . swo . set_baudrate ( target) ;
527
510
resp. write_u32 ( actual) ;
528
511
}
529
512
530
513
fn process_swo_control ( & mut self , mut req : Request , resp : & mut ResponseWriter ) {
531
- let swo = if let Some ( swo) = & mut self . swo {
532
- swo
533
- } else {
534
- resp. write_err ( ) ;
535
- return ;
514
+ let success = match swo:: SwoControl :: try_from ( req. next_u8 ( ) ) {
515
+ Ok ( control) => self . swo . set_control ( control) ,
516
+ _ => false ,
536
517
} ;
537
518
538
- match swo:: SwoControl :: try_from ( req. next_u8 ( ) ) {
539
- Ok ( control) => {
540
- swo. set_control ( control) ;
541
- resp. write_ok ( ) ;
542
- }
543
- _ => resp. write_err ( ) ,
519
+ if success {
520
+ resp. write_ok ( ) ;
521
+ } else {
522
+ resp. write_err ( ) ;
544
523
}
545
524
}
546
525
@@ -549,11 +528,7 @@ where
549
528
// Bit 0: trace capture active
550
529
// Bit 6: trace stream error (always written as 0)
551
530
// Bit 7: trace buffer overflow (always written as 0)
552
- let ( active, len) = if let Some ( swo) = & self . swo {
553
- ( swo. is_active ( ) , swo. bytes_available ( ) )
554
- } else {
555
- ( false , 0 )
556
- } ;
531
+ let ( active, len) = ( self . swo . is_active ( ) , self . swo . bytes_available ( ) ) ;
557
532
558
533
resp. write_u8 ( active as u8 ) ;
559
534
// Trace count: remaining bytes in buffer
@@ -565,11 +540,8 @@ where
565
540
// Bit 0: trace capture active
566
541
// Bit 6: trace stream error (always written as 0)
567
542
// Bit 7: trace buffer overflow (always written as 0)
568
- let ( active, len) = if let Some ( swo) = & self . swo {
569
- ( swo. is_active ( ) , swo. bytes_available ( ) )
570
- } else {
571
- ( false , 0 )
572
- } ;
543
+ let ( active, len) = ( self . swo . is_active ( ) , self . swo . bytes_available ( ) ) ;
544
+
573
545
resp. write_u8 ( active as u8 ) ;
574
546
// Trace count: remaining bytes in buffer.
575
547
resp. write_u32 ( len) ;
@@ -580,11 +552,7 @@ where
580
552
}
581
553
582
554
fn process_swo_data ( & mut self , mut req : Request , resp : & mut ResponseWriter ) {
583
- let active = if let Some ( swo) = & mut self . swo {
584
- swo. is_active ( )
585
- } else {
586
- false
587
- } ;
555
+ let active = self . swo . is_active ( ) ;
588
556
589
557
// Write status byte to response
590
558
resp. write_u8 ( active as u8 ) ;
@@ -601,11 +569,7 @@ where
601
569
}
602
570
603
571
// Read data from UART
604
- let len = if let Some ( swo) = & mut self . swo {
605
- swo. polling_data ( & mut buf)
606
- } else {
607
- 0
608
- } ;
572
+ let len = self . swo . polling_data ( & mut buf) ;
609
573
resp. skip ( len as _ ) ;
610
574
611
575
// Go back and write length
@@ -1290,7 +1254,7 @@ fn transfer_with_retry<DEPS>(
1290
1254
#[ cfg( test) ]
1291
1255
mod test {
1292
1256
use super :: * ;
1293
- use crate :: mock_device:: * ;
1257
+ use crate :: { mock_device:: * , swo :: NoSwo } ;
1294
1258
use mockall:: predicate:: * ;
1295
1259
1296
1260
struct FakeLEDs { }
@@ -1312,7 +1276,7 @@ mod test {
1312
1276
StdDelayUs ,
1313
1277
MockSwdJtagDevice ,
1314
1278
MockSwdJtagDevice ,
1315
- swo :: MockSwo ,
1279
+ NoSwo ,
1316
1280
> ;
1317
1281
1318
1282
#[ test]
@@ -1321,7 +1285,7 @@ mod test {
1321
1285
MockSwdJtagDevice :: new ( ) ,
1322
1286
FakeLEDs { } ,
1323
1287
StdDelayUs { } ,
1324
- None ,
1288
+ NoSwo ,
1325
1289
"test_dap" ,
1326
1290
) ;
1327
1291
@@ -1348,7 +1312,7 @@ mod test {
1348
1312
MockSwdJtagDevice :: new ( ) ,
1349
1313
FakeLEDs { } ,
1350
1314
StdDelayUs { } ,
1351
- None ,
1315
+ NoSwo ,
1352
1316
"test_dap" ,
1353
1317
) ;
1354
1318
@@ -1378,7 +1342,7 @@ mod test {
1378
1342
MockSwdJtagDevice :: new ( ) ,
1379
1343
FakeLEDs { } ,
1380
1344
StdDelayUs { } ,
1381
- None ,
1345
+ NoSwo ,
1382
1346
"test_dap" ,
1383
1347
) ;
1384
1348
@@ -1411,7 +1375,7 @@ mod test {
1411
1375
MockSwdJtagDevice :: new ( ) ,
1412
1376
FakeLEDs { } ,
1413
1377
StdDelayUs { } ,
1414
- None ,
1378
+ NoSwo ,
1415
1379
"test_dap" ,
1416
1380
) ;
1417
1381
@@ -1445,7 +1409,7 @@ mod test {
1445
1409
MockSwdJtagDevice :: new ( ) ,
1446
1410
FakeLEDs { } ,
1447
1411
StdDelayUs { } ,
1448
- None ,
1412
+ NoSwo ,
1449
1413
"test_dap" ,
1450
1414
) ;
1451
1415
0 commit comments