Skip to content

Commit 0d2f47e

Browse files
committed
Fix shift, tweak jtag config
1 parent 3515469 commit 0d2f47e

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

src/dap.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,19 +612,21 @@ where
612612
};
613613

614614
let count = req.next_u8();
615-
616-
config.device_count = count;
615+
if !config.update_device_count(count) {
616+
resp.write_err();
617+
return;
618+
}
617619

618620
let mut bits = 0;
619621
for n in 0..count as usize {
620622
let length = req.next_u8();
621-
config.ir_length[n] = length;
622-
config.ir_before[n] = bits;
623+
config.scan_chain[n].ir_length = length;
624+
config.scan_chain[n].ir_before = bits;
623625
bits += length as u16;
624626
}
625627
for n in 0..count as usize {
626-
bits -= config.ir_length[n as usize] as u16;
627-
config.ir_after[n] = bits;
628+
bits -= config.scan_chain[n].ir_length as u16;
629+
config.scan_chain[n].ir_after = bits;
628630
}
629631

630632
resp.write_ok();

src/jtag.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,56 @@ impl From<u8> for TransferInfo {
7777
}
7878
}
7979

80-
const MAX_CHAIN_LENGTH: usize = 16;
80+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
81+
pub struct TapConfig {
82+
/// The number of bits in the IR register.
83+
pub ir_length: u8,
84+
/// The number of bypass bits before the IR register.
85+
pub ir_before: u16,
86+
/// The number of bypass bits after the IR register.
87+
pub ir_after: u16,
88+
}
89+
90+
impl TapConfig {
91+
/// Empty value for array initialization
92+
pub const INIT: Self = Self {
93+
ir_length: 0,
94+
ir_before: 0,
95+
ir_after: 0,
96+
};
97+
}
8198

8299
/// JTAG interface configuraiton.
83100
pub struct Config {
84101
/// The number of devices on the JTAG chain.
85102
pub device_count: u8,
86103
/// The position of the selected device.
87104
pub index: u8,
88-
/// The length of the IR register for each device.
89-
pub ir_length: [u8; MAX_CHAIN_LENGTH],
90-
/// The number of bypass bits before the IR register for each device.
91-
pub ir_before: [u16; MAX_CHAIN_LENGTH],
92-
/// The number of bypass bits after the IR register for each device.
93-
pub ir_after: [u16; MAX_CHAIN_LENGTH],
105+
/// TAPs on the scan chain.
106+
pub scan_chain: &'static mut [TapConfig],
107+
}
108+
109+
impl Config {
110+
pub fn new(chain_buffer: &'static mut [TapConfig]) -> Self {
111+
Self {
112+
device_count: 0,
113+
index: 0,
114+
scan_chain: chain_buffer,
115+
}
116+
}
117+
118+
/// Returns information about the currently selected TAP.
119+
pub fn current_tap(&self) -> TapConfig {
120+
self.scan_chain[self.index as usize]
121+
}
122+
123+
pub(crate) fn update_device_count(&mut self, count: u8) -> bool {
124+
if count as usize >= self.scan_chain.len() {
125+
return false;
126+
}
127+
self.device_count = count;
128+
true
129+
}
94130
}
95131

96132
impl Config {
@@ -158,10 +194,10 @@ pub trait Jtag<DEPS>: From<DEPS> {
158194
const EXIT1_IR_TO_IDLE: &[bool] = &[true, false];
159195
self.tms_sequence(IDLE_TO_SHIFT_IR);
160196

161-
let device_index = self.config().index as usize;
162-
let ir_length = self.config().ir_length[device_index];
163-
let bypass_before = self.config().ir_before[device_index];
164-
let bypass_after = self.config().ir_after[device_index];
197+
let tap = self.config().current_tap();
198+
let ir_length = tap.ir_length;
199+
let bypass_before = tap.ir_before;
200+
let bypass_after = tap.ir_after;
165201

166202
// Send the bypass bits before the IR.
167203
bypass_bits(self, bypass_before, false);
@@ -344,7 +380,7 @@ fn shift_dr<DEPS>(jtag: &mut impl Jtag<DEPS>, data: u32, bypass_after: u16) -> u
344380
);
345381

346382
captured_dr >>= 1;
347-
captured_dr |= (captured_byte << 31) as u32;
383+
captured_dr |= (captured_byte as u32) << 31;
348384

349385
if bypass_after > 0 {
350386
if bypass_after > 1 {

0 commit comments

Comments
 (0)