Skip to content

Commit de6ad34

Browse files
authored
Add log feature, fmt.rs (#7)
* Add log feature, fmt.rs * Add link to original fmt.rs
1 parent 7d36867 commit de6ad34

File tree

5 files changed

+331
-46
lines changed

5 files changed

+331
-46
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ mockall = "0.13.0"
2727
optional = true
2828
version = "0.3"
2929

30+
[dependencies.log]
31+
optional = true
32+
version = "0.4"
33+
3034
[features]
3135
defmt = ["dep:defmt"]
3236
usb-hs = []
37+
log = ["dep:log"]

src/dap.rs

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ where
8181

8282
let resp = &mut ResponseWriter::new(req.command, rbuf);
8383

84-
// defmt::trace!("Dap command: {}", req.command);
84+
trace!("Dap command: {}", req.command);
8585

8686
match req.command {
8787
Command::DAP_Info => self.process_info(req, resp, version),
@@ -227,12 +227,12 @@ where
227227
}
228228
};
229229

230-
// defmt::info!(
231-
// "DAP connect: {}, SWD: {}, JTAG: {}",
232-
// port,
233-
// SWD::AVAILABLE,
234-
// JTAG::AVAILABLE
235-
// );
230+
info!(
231+
"DAP connect: {}, SWD: {}, JTAG: {}",
232+
port,
233+
SWD::AVAILABLE,
234+
JTAG::AVAILABLE
235+
);
236236

237237
match (SWD::AVAILABLE, JTAG::AVAILABLE, port) {
238238
// SWD
@@ -386,31 +386,33 @@ where
386386

387387
resp.write_ok(); // assume ok until we finish
388388
let sequence_count = req.next_u8();
389-
let success = (0..sequence_count).map(|_| {
390-
// parse the seqnence info
391-
let sequence_info = req.next_u8();
392-
let nbits: usize = match sequence_info & 0x3F {
393-
// CMSIS-DAP says 0 means 64 bits
394-
0 => 64,
395-
// Other integers are normal.
396-
n => n as usize,
397-
};
398-
let nbytes = (nbits + 7) / 8;
399-
let output = (sequence_info & 0x80) == 0;
400-
401-
if output {
402-
let output_data = req.data.get(..nbytes).ok_or(())?;
403-
swd.write_sequence(nbits, output_data).or(Err(()))?;
404-
req.consume(nbytes);
405-
Ok(0)
406-
} else {
407-
let input_data = resp.remaining().get_mut(..nbytes).ok_or(())?;
408-
swd.read_sequence(nbits, input_data).or(Err(()))?;
409-
resp.skip(nbytes);
410-
Ok(nbytes)
411-
}
412-
}).all(|r: Result<usize, ()>| r.is_ok());
413-
389+
let success = (0..sequence_count)
390+
.map(|_| {
391+
// parse the seqnence info
392+
let sequence_info = req.next_u8();
393+
let nbits: usize = match sequence_info & 0x3F {
394+
// CMSIS-DAP says 0 means 64 bits
395+
0 => 64,
396+
// Other integers are normal.
397+
n => n as usize,
398+
};
399+
let nbytes = (nbits + 7) / 8;
400+
let output = (sequence_info & 0x80) == 0;
401+
402+
if output {
403+
let output_data = req.data.get(..nbytes).ok_or(())?;
404+
swd.write_sequence(nbits, output_data).or(Err(()))?;
405+
req.consume(nbytes);
406+
Ok(0)
407+
} else {
408+
let input_data = resp.remaining().get_mut(..nbytes).ok_or(())?;
409+
swd.read_sequence(nbits, input_data).or(Err(()))?;
410+
resp.skip(nbytes);
411+
Ok(nbytes)
412+
}
413+
})
414+
.all(|r: Result<usize, ()>| r.is_ok());
415+
414416
if !success {
415417
resp.write_u8_at(0, 0xFF);
416418
}
@@ -994,17 +996,26 @@ mod test {
994996
);
995997

996998
// write 8 bits, read 5 bits, write 33 bits
997-
let report = [0x1Du8, 3, 8, 0b10111101, 0x80 | 5, 33, 0x56, 0x83, 0xAB, 0x32, 0x01];
999+
let report = [
1000+
0x1Du8,
1001+
3,
1002+
8,
1003+
0b10111101,
1004+
0x80 | 5,
1005+
33,
1006+
0x56,
1007+
0x83,
1008+
0xAB,
1009+
0x32,
1010+
0x01,
1011+
];
9981012
let mut rbuf = [0u8; 64];
9991013
dap.state.to_swd();
10001014
match &mut dap.state {
10011015
State::Swd(swd) => {
10021016
swd.expect_write_sequence()
10031017
.once()
1004-
.with(
1005-
eq(8),
1006-
eq([0b10111101u8]),
1007-
)
1018+
.with(eq(8), eq([0b10111101u8]))
10081019
.return_const(Ok(()));
10091020
swd.expect_read_sequence()
10101021
.once()
@@ -1015,19 +1026,13 @@ mod test {
10151026
});
10161027
swd.expect_write_sequence()
10171028
.once()
1018-
.with(
1019-
eq(33),
1020-
eq([0x56, 0x83, 0xAB, 0x32, 0x01]),
1021-
)
1029+
.with(eq(33), eq([0x56, 0x83, 0xAB, 0x32, 0x01]))
10221030
.return_const(Ok(()));
10231031
}
10241032
_ => assert!(false, "can't switch to swd"),
10251033
}
10261034
let rsize = dap.process_command(&report, &mut rbuf, DapVersion::V2);
10271035
assert_eq!(rsize, 3);
1028-
assert_eq!(
1029-
&rbuf[..3],
1030-
&[0x1Du8, 0x00, 0x1F]
1031-
)
1036+
assert_eq!(&rbuf[..3], &[0x1Du8, 0x00, 0x1F])
10321037
}
10331038
}

0 commit comments

Comments
 (0)