Skip to content

Commit d50d260

Browse files
committed
jtag: fix implementation using the updated jtagtools.bits API
Signed-off-by: Emmanuel Blot <emmanuel.blot@free.fr>
1 parent 9206c54 commit d50d260

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

pyftdi/jtag.py

+23-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""JTAG tools.
88
"""
99

10+
from binascii import hexlify
1011
from logging import getLogger
1112
from time import sleep
1213
from typing import Optional, Union
@@ -132,8 +133,7 @@ def system_reset(self) -> None:
132133
def quit(self) -> None:
133134
self.close()
134135

135-
def write_tms(self, modesel: BitSequence, immediate: bool = True) \
136-
-> BitSequence:
136+
def write_tms(self, modesel: BitSequence) -> BitSequence:
137137
if not isinstance(modesel, BitSequence):
138138
raise ValueError('Not a BitSequence')
139139
length = len(modesel)
@@ -153,38 +153,32 @@ def write_tms(self, modesel: BitSequence, immediate: bool = True) \
153153
fcmd = Ftdi.RW_BITS_TMS_PVE_NVE
154154
else:
155155
self._log.debug('WRITE TMS [%d] %s', length, modesel)
156-
fcmd = Ftdi.WRITE_BITS_TMS_NVE
156+
fcmd = Ftdi.RW_BITS_TMS_PVE_NVE
157157
self._log.debug('TMS %02x', byte)
158158
cmd = bytes((fcmd, length-1, byte))
159-
self._ftdi.purge_rx_buffer()
159+
# self._ftdi.purge_rx_buffer()
160160
self._push_bytes(cmd)
161-
if immediate or lbit:
162-
self._flush()
163-
if lbit:
164-
bs = self._read_bits(length)
165-
return bs.pop_right()
166-
return BitSequence()
161+
self._flush()
162+
bs = self._read_bits(length)
163+
return bs
167164

168-
def write(self, out: BitSequence, immediate: bool = True) -> None:
165+
def write(self, out: BitSequence) -> None:
169166
if not isinstance(out, BitSequence):
170167
raise ValueError('Not a BitSequence')
171168
self._last_tdi = out.pop_left_bit()
172-
self._log.debug("WRITE TDI %s [+%u]", out, self._last_tdi)
173169
byte_count = len(out)//8
174170
pos = 8 * byte_count
175171
bit_count = len(out)-pos
176-
self._log.debug('%dB, %db len %d', byte_count, bit_count, len(out))
172+
self._log.debug('%d B, %d b len %d', byte_count, bit_count, len(out))
177173
if byte_count:
178174
self._prepare_write_bytes(byte_count)
179-
self._write_bytes(out[:pos])
175+
self._write_bytes(out[bit_count:])
180176
if bit_count:
181177
self._prepare_write_bits(bit_count)
182-
self._write_bits(out[pos:])
183-
if immediate:
184-
self._flush()
178+
self._write_bits(out[:bit_count])
179+
self._flush()
185180

186-
def read(self, length: int, tdi: Optional[bool] = None) -> BitSequence:
187-
# tdi argument is not used with PyFtdi for now
181+
def read(self, length: int) -> BitSequence:
188182
self._log.debug("READ TDO %d", length)
189183
byte_count = length // 8
190184
bit_count = length - 8 * byte_count
@@ -197,24 +191,24 @@ def read(self, length: int, tdi: Optional[bool] = None) -> BitSequence:
197191
if byte_count:
198192
bs.push_right(self._read_bytes(byte_count))
199193
if bit_count:
200-
bs.push_right(self._read_bits(bit_count))
194+
bs.push_left(self._read_bits(bit_count))
201195
return bs
202196

203197
def exchange(self, out: BitSequence) -> BitSequence:
204198
self._last_tdi = out.pop_left_bit()
205199
length = len(out)
206-
self._log.debug("WRITE TDI %s [+%u] + READ TDO %u", out, self._last_tdi,
207-
length)
200+
self._log.debug("WRITE TDI %s [+%u] + READ TDO %u",
201+
out, self._last_tdi, length)
208202
byte_count = length // 8
209203
bit_count = length - 8 * byte_count
210204
pos = 8 * byte_count
211205
bs = BitSequence()
212206
if byte_count:
213207
self._prepare_exchange_bytes(byte_count)
214-
self._write_bytes(out[:pos])
208+
self._write_bytes(out[-pos:])
215209
if bit_count:
216210
self._prepare_exchange_bits(bit_count)
217-
self._write_bits(out[pos:])
211+
self._write_bits(out[:-pos])
218212
self._flush()
219213
if byte_count:
220214
bs = self._read_bytes(byte_count)
@@ -234,6 +228,7 @@ def _push_bytes(self, cmd: Union[bytes, bytearray]):
234228

235229
def _flush(self):
236230
self._log.debug('flushing %d bytes', len(self._write_buff))
231+
self._log.debug(' [%s]', hexlify(self._write_buff).decode())
237232
if self._write_buff:
238233
self._ftdi.write_data(self._write_buff)
239234
self._write_buff = bytearray()
@@ -253,7 +248,7 @@ def _prepare_write_bits(self, length: int) -> None:
253248
def _write_bits(self, out: BitSequence) -> None:
254249
"""Output bits on TDI"""
255250
byte = out.to_byte()
256-
self._log.debug('WRITE BITS %s / 0x%02x', out, byte)
251+
self._log.debug('%s (0x%02x)', out, byte)
257252
self._push_bytes(bytes((byte,)))
258253

259254
def _prepare_exchange_bits(self, length: int) -> None:
@@ -299,11 +294,10 @@ def _read_bytes(self, length: int) -> BitSequence:
299294
data = self._ftdi.read_data_bytes(length, 4)
300295
if len(data) != length:
301296
raise JtagError('Failed to read from FTDI')
302-
bs = BitSequence.from_bytestream(data, lsbyte=True)
303-
self._log.debug('READ BYTES %s', bs)
297+
bs = BitSequence.from_bytestream(data, True)
304298
return bs
305299

306300
def _write_bytes(self, out: BitSequence) -> None:
307301
"""Output bytes on TDI"""
308-
self._log.debug('WRITE BYTES %s', out)
309-
self._push_bytes(out.to_bytestream())
302+
self._log.debug('%s', out)
303+
self._push_bytes(out.to_bytestream(True))

0 commit comments

Comments
 (0)