Skip to content

Commit d69fb8e

Browse files
authored
Merge pull request #29 from probe-rs/fix-swd-sequence-and-swj-pins
Fix swd sequence and swj pins
2 parents cf3c508 + 79d59d4 commit d69fb8e

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
authors = ["Emil Fresk <[email protected]>"]
33
edition = "2021"
44
name = "rusty-probe"
5-
version = "0.2.0"
5+
version = "0.3.0"
66

77
[dependencies]
88
bitflags = "1.3.2"
@@ -19,7 +19,7 @@ embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = ["un
1919
panic-probe = { version = "0.3.0", features = ["print-defmt"] }
2020
replace_with = { version = "0.1.7", default-features = false, features = ["panic_abort"] }
2121
usbd-serial = "0.2.0"
22-
dap-rs = { git = "https://github.com/probe-rs/dap-rs.git", branch = "master", features = ["defmt"] }
22+
dap-rs = { version = "0.2.0", features = ["defmt"] }
2323
git-version = "0.3.5"
2424
pio-proc = "0.2.1"
2525
pio = "0.2.1"

src/dap.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,29 @@ impl swj::Dependencies<Swd, Jtag> for Context {
132132
}
133133
}
134134

135-
self.delay.delay_ticks(self.cycles_per_us * wait_us);
135+
// Delay until desired output state or timeout.
136+
let mut last = self.delay.get_current();
137+
for _ in 0..wait_us {
138+
last = self.delay.delay_ticks_from_last(self.cycles_per_us, last);
139+
140+
// If a pin is selected, make sure its output equals the desired output state, or else
141+
// continue waiting.
142+
let swclk_not_in_desired_state = mask.contains(swj::Pins::SWCLK)
143+
&& output.contains(swj::Pins::SWCLK) != self.swclk.is_high();
144+
let swdio_not_in_desired_state = mask.contains(swj::Pins::SWDIO)
145+
&& output.contains(swj::Pins::SWDIO) != self.swdio.is_high();
146+
let nreset_not_in_desired_state = mask.contains(swj::Pins::NRESET)
147+
&& output.contains(swj::Pins::NRESET) != self.nreset.is_high();
148+
149+
if swclk_not_in_desired_state
150+
|| swdio_not_in_desired_state
151+
|| nreset_not_in_desired_state
152+
{
153+
continue;
154+
}
155+
156+
break;
157+
}
136158

137159
let mut ret = swj::Pins::empty();
138160
ret.set(swj::Pins::SWCLK, self.swclk.is_high());
@@ -326,6 +348,37 @@ impl swd::Swd<Context> for Swd {
326348
Ok(())
327349
}
328350

351+
fn write_sequence(&mut self, mut num_bits: usize, data: &[u8]) -> swd::Result<()> {
352+
self.0.swdio_to_output();
353+
let mut last = self.0.delay.get_current();
354+
355+
for b in data {
356+
let bit_count = core::cmp::min(num_bits, 8);
357+
for i in 0..bit_count {
358+
self.write_bit((b >> i) & 0x1, &mut last);
359+
}
360+
num_bits -= bit_count;
361+
}
362+
363+
Ok(())
364+
}
365+
366+
fn read_sequence(&mut self, mut num_bits: usize, data: &mut [u8]) -> swd::Result<()> {
367+
self.0.swdio_to_input();
368+
let mut last = self.0.delay.get_current();
369+
370+
for b in data {
371+
let bit_count = core::cmp::min(num_bits, 8);
372+
for i in 0..bit_count {
373+
let bit = self.read_bit(&mut last);
374+
*b |= bit << i;
375+
}
376+
num_bits -= bit_count;
377+
}
378+
379+
Ok(())
380+
}
381+
329382
fn set_clock(&mut self, max_frequency: u32) -> bool {
330383
trace!("SWD set clock: freq = {}", max_frequency);
331384
self.0.process_swj_clock(max_frequency)

src/setup.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::systick_delay::Delay;
44
use crate::{dap, usb::ProbeUsb};
55
use core::mem::MaybeUninit;
66
use dap_rs::usb_device::class_prelude::UsbBusAllocator;
7-
use embedded_hal::digital::{InputPin, OutputPin};
7+
use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin};
88
use embedded_hal::pwm::SetDutyCycle;
99
use embedded_hal_02::adc::OneShot;
1010
use replace_with::replace_with_or_abort_unchecked;
@@ -303,13 +303,11 @@ where
303303
}
304304
}
305305

306-
/// Note: This function panics if the pin is in the wrong mode.
306+
/// Note: If the pin is an output this will only reflect the set state, NOT electrical state.
307307
pub fn is_high(&mut self) -> bool {
308308
match self {
309309
DynPin::Input(i) => i.is_high() == Ok(true),
310-
DynPin::Output(_) => {
311-
defmt::panic!("Input operation on output pin");
312-
}
310+
DynPin::Output(o) => o.is_set_high() == Ok(true),
313311
}
314312
}
315313
}

0 commit comments

Comments
 (0)