@@ -51,7 +51,7 @@ bitflags! {
5151/// DAP handler.
5252pub struct Dap < ' a , DEPS , LEDS , WAIT , JTAG , SWD , SWO > {
5353 state : State < DEPS , SWD , JTAG > ,
54- swo : Option < SWO > ,
54+ swo : SWO ,
5555 swo_streaming : bool ,
5656 version_string : & ' a str ,
5757 transfer_config : TransferConfig ,
7575 dependencies : DEPS ,
7676 leds : LEDS ,
7777 wait : WAIT ,
78- swo : Option < SWO > ,
78+ swo : SWO ,
7979 version_string : & ' a str ,
8080 ) -> Self {
8181 assert ! ( SWD :: available( & dependencies) || JTAG :: available( & dependencies) ) ;
@@ -93,19 +93,16 @@ where
9393 }
9494 // Bit 2: SWO UART optional
9595 // 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 ) ;
103101 }
104102 // Bit 4: Atomic commands not supported
105103 // 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 {
109106 caps. insert ( Capabilities :: SWO_STREAMING ) ;
110107 }
111108 caps
@@ -219,11 +216,7 @@ where
219216 }
220217 Ok ( DapInfoID :: SWOTraceBufferSize ) => {
221218 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 ( ) ) ;
227220 }
228221 Ok ( DapInfoID :: MaxPacketCount ) => {
229222 resp. write_u8 ( 1 ) ;
@@ -499,48 +492,34 @@ where
499492 }
500493
501494 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+ } ;
503499
504- let swo = if let Some ( swo ) = & mut self . swo {
505- swo
500+ if success {
501+ resp . write_ok ( ) ;
506502 } else {
507503 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 ( ) ,
517504 }
518505 }
519506
520507 fn process_swo_baudrate ( & mut self , mut req : Request , resp : & mut ResponseWriter ) {
521508 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) ;
527510 resp. write_u32 ( actual) ;
528511 }
529512
530513 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 ,
536517 } ;
537518
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 ( ) ;
544523 }
545524 }
546525
@@ -549,11 +528,7 @@ where
549528 // Bit 0: trace capture active
550529 // Bit 6: trace stream error (always written as 0)
551530 // 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 ( ) ) ;
557532
558533 resp. write_u8 ( active as u8 ) ;
559534 // Trace count: remaining bytes in buffer
@@ -565,11 +540,8 @@ where
565540 // Bit 0: trace capture active
566541 // Bit 6: trace stream error (always written as 0)
567542 // 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+
573545 resp. write_u8 ( active as u8 ) ;
574546 // Trace count: remaining bytes in buffer.
575547 resp. write_u32 ( len) ;
@@ -580,11 +552,7 @@ where
580552 }
581553
582554 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 ( ) ;
588556
589557 // Write status byte to response
590558 resp. write_u8 ( active as u8 ) ;
@@ -601,11 +569,7 @@ where
601569 }
602570
603571 // 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) ;
609573 resp. skip ( len as _ ) ;
610574
611575 // Go back and write length
@@ -1290,7 +1254,7 @@ fn transfer_with_retry<DEPS>(
12901254#[ cfg( test) ]
12911255mod test {
12921256 use super :: * ;
1293- use crate :: mock_device:: * ;
1257+ use crate :: { mock_device:: * , swo :: NoSwo } ;
12941258 use mockall:: predicate:: * ;
12951259
12961260 struct FakeLEDs { }
@@ -1312,7 +1276,7 @@ mod test {
13121276 StdDelayUs ,
13131277 MockSwdJtagDevice ,
13141278 MockSwdJtagDevice ,
1315- swo :: MockSwo ,
1279+ NoSwo ,
13161280 > ;
13171281
13181282 #[ test]
@@ -1321,7 +1285,7 @@ mod test {
13211285 MockSwdJtagDevice :: new ( ) ,
13221286 FakeLEDs { } ,
13231287 StdDelayUs { } ,
1324- None ,
1288+ NoSwo ,
13251289 "test_dap" ,
13261290 ) ;
13271291
@@ -1348,7 +1312,7 @@ mod test {
13481312 MockSwdJtagDevice :: new ( ) ,
13491313 FakeLEDs { } ,
13501314 StdDelayUs { } ,
1351- None ,
1315+ NoSwo ,
13521316 "test_dap" ,
13531317 ) ;
13541318
@@ -1378,7 +1342,7 @@ mod test {
13781342 MockSwdJtagDevice :: new ( ) ,
13791343 FakeLEDs { } ,
13801344 StdDelayUs { } ,
1381- None ,
1345+ NoSwo ,
13821346 "test_dap" ,
13831347 ) ;
13841348
@@ -1411,7 +1375,7 @@ mod test {
14111375 MockSwdJtagDevice :: new ( ) ,
14121376 FakeLEDs { } ,
14131377 StdDelayUs { } ,
1414- None ,
1378+ NoSwo ,
14151379 "test_dap" ,
14161380 ) ;
14171381
@@ -1445,7 +1409,7 @@ mod test {
14451409 MockSwdJtagDevice :: new ( ) ,
14461410 FakeLEDs { } ,
14471411 StdDelayUs { } ,
1448- None ,
1412+ NoSwo ,
14491413 "test_dap" ,
14501414 ) ;
14511415
0 commit comments