Skip to content

Commit 79d59d4

Browse files
committed
Fix SWJ_Pins
Change the SWJ_Pins command to wait until a desired state is reached instead of unconditionally wait for the maximum duration. This will make so that SWJ_Pins does not panic anymore.
1 parent 056574c commit 79d59d4

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/dap.rs

Lines changed: 23 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());

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)