Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _set_frequency(self, frequency: float) -> float:
"""Convert a frequency value into a TCK divisor setting"""
if not self.is_mpsse:
raise FtdiFeatureError('Cannot change frequency w/ current mode')
if frequency > self.frequency_max:
raise FtdiFeatureError('Unsupported frequency: %f' % frequency)
# Calculate base speed clock divider
divcode = Ftdi.ENABLE_CLK_DIV5
divisor = int((Ftdi.BUS_CLOCK_BASE+frequency/2)/frequency)-1
divisor = max(0, min(0xFFFF, divisor))
actual_freq = Ftdi.BUS_CLOCK_BASE/(divisor+1)
error = (actual_freq/frequency)-1
# Should we use high speed clock available in H series?
if self.is_H_series:
# Calculate high speed clock divider
divisor_hs = int((Ftdi.BUS_CLOCK_HIGH+frequency/2)/frequency)-1
divisor_hs = max(0, min(0xFFFF, divisor_hs))
actual_freq_hs = Ftdi.BUS_CLOCK_HIGH/(divisor_hs+1)
error_hs = (actual_freq_hs/frequency)-1
# Enable if closer to desired frequency (percentually)
if abs(error_hs) < abs(error):
divcode = Ftdi.DISABLE_CLK_DIV5
divisor = divisor_hs
actual_freq = actual_freq_hs
def _write_raw(self, data: int, write_high: bool) -> None:
if not self._ftdi.is_connected:
raise SpiIOError("FTDI controller not initialized")
direction = self.direction
low_data = data & 0xFF
low_dir = direction & 0xFF
if write_high:
high_data = (data >> 8) & 0xFF
high_dir = (direction >> 8) & 0xFF
cmd = bytes([Ftdi.SET_BITS_LOW, low_data, low_dir,
Ftdi.SET_BITS_HIGH, high_data, high_dir])
else:
cmd = bytes([Ftdi.SET_BITS_LOW, low_data, low_dir])
self._ftdi.write_data(cmd)
def __read(self, cl, al, count):
cmds = []
cmd_type = 0
if cl == 1:
cmd_type |= flashdevice_defs.ADR_CL
if al == 1:
cmd_type |= flashdevice_defs.ADR_AL
cmds += [ftdi.Ftdi.READ_EXTENDED, cmd_type, 0]
for _ in range(1, count, 1):
cmds += [ftdi.Ftdi.READ_SHORT, 0]
cmds.append(ftdi.Ftdi.SEND_IMMEDIATE)
if self.ftdi is None or not self.ftdi.is_connected:
return
self.ftdi.write_data(Array('B', cmds))
if self.is_slow_mode():
data = self.ftdi.read_data_bytes(count*2)
data = data[0:-1:2]
else:
data = self.ftdi.read_data_bytes(count)
return bytes(data)
def _read_raw(self, read_high: bool) -> int:
if read_high:
cmd = bytes([Ftdi.GET_BITS_LOW,
Ftdi.GET_BITS_HIGH,
Ftdi.SEND_IMMEDIATE])
fmt = '
# Calculate base speed clock divider
divcode = Ftdi.ENABLE_CLK_DIV5
divisor = int((Ftdi.BUS_CLOCK_BASE+frequency/2)/frequency)-1
divisor = max(0, min(0xFFFF, divisor))
actual_freq = Ftdi.BUS_CLOCK_BASE/(divisor+1)
error = (actual_freq/frequency)-1
# Should we use high speed clock available in H series?
if self.is_H_series:
# Calculate high speed clock divider
divisor_hs = int((Ftdi.BUS_CLOCK_HIGH+frequency/2)/frequency)-1
divisor_hs = max(0, min(0xFFFF, divisor_hs))
actual_freq_hs = Ftdi.BUS_CLOCK_HIGH/(divisor_hs+1)
error_hs = (actual_freq_hs/frequency)-1
# Enable if closer to desired frequency (percentually)
if abs(error_hs) < abs(error):
divcode = Ftdi.DISABLE_CLK_DIV5
divisor = divisor_hs
actual_freq = actual_freq_hs
error = error_hs
# FTDI expects little endian
if self.is_H_series:
cmd = bytearray((divcode,))
else:
cmd = bytearray()
cmd.extend((Ftdi.SET_TCK_DIVISOR, divisor & 0xff,
(divisor >> 8) & 0xff))
self.write_data(cmd)
self.validate_mpsse()
# Drain input buffer
self.purge_rx_buffer()
# Note that bus frequency may differ from clock frequency, when
# 3-phase clock is enable, in which case bus frequency = 2/3 clock
def set_dtr(self, state: bool) -> None:
"""Set dtr line
:param state: new DTR logical level
"""
value = Ftdi.SIO_SET_DTR_HIGH if state else Ftdi.SIO_SET_DTR_LOW
if self._ctrl_transfer_out(Ftdi.SIO_REQ_SET_MODEM_CTRL, value):
raise FtdiError('Unable to set DTR line')
def _read_mpsse(self, count: int) -> Tuple[int]:
if self._width > 8:
cmd = bytearray([Ftdi.GET_BITS_LOW, Ftdi.GET_BITS_HIGH] * count)
fmt = '<%dH' % count
else:
cmd = bytearray([Ftdi.GET_BITS_LOW] * count)
fmt = None
cmd.append(Ftdi.SEND_IMMEDIATE)
if len(cmd) > self.MPSSE_PAYLOAD_MAX_LENGTH:
raise ValueError('Too many samples')
self._ftdi.write_data(cmd)
size = scalc(fmt) if fmt else count
data = self._ftdi.read_data_bytes(size, 4)
if len(data) != size:
raise FtdiError('Cannot read GPIO')
if fmt:
return sunpack(fmt, data)
return data
def _write_bits(self, out: BitSequence) -> None:
"""Output bits on TDI"""
length = len(out)
byte = out.tobyte()
# print("WRITE BITS %s" % out)
cmd = bytearray((Ftdi.WRITE_BITS_NVE_LSB, length-1, byte))
self._stack_cmd(cmd)
def _set_frequency(self, frequency: float) -> float:
"""Convert a frequency value into a TCK divisor setting"""
if not self.is_mpsse:
raise FtdiFeatureError('Cannot change frequency w/ current mode')
if frequency > self.frequency_max:
raise FtdiFeatureError('Unsupported frequency: %f' % frequency)
# Calculate base speed clock divider
divcode = Ftdi.ENABLE_CLK_DIV5
divisor = int((Ftdi.BUS_CLOCK_BASE+frequency/2)/frequency)-1
divisor = max(0, min(0xFFFF, divisor))
actual_freq = Ftdi.BUS_CLOCK_BASE/(divisor+1)
error = (actual_freq/frequency)-1
# Should we use high speed clock available in H series?
if self.is_H_series:
# Calculate high speed clock divider
divisor_hs = int((Ftdi.BUS_CLOCK_HIGH+frequency/2)/frequency)-1
divisor_hs = max(0, min(0xFFFF, divisor_hs))
actual_freq_hs = Ftdi.BUS_CLOCK_HIGH/(divisor_hs+1)
error_hs = (actual_freq_hs/frequency)-1
# Enable if closer to desired frequency (percentually)
if abs(error_hs) < abs(error):
divcode = Ftdi.DISABLE_CLK_DIV5
divisor = divisor_hs
actual_freq = actual_freq_hs
error = error_hs
# FTDI expects little endian
if self.is_H_series:
def __init__(self, trst: bool = False, frequency: float = 3.0E6):
"""
trst uses the nTRST optional JTAG line to hard-reset the TAP
controller
"""
self._ftdi = Ftdi()
self._trst = trst
self._frequency = frequency
self.direction = (JtagController.TCK_BIT |
JtagController.TDI_BIT |
JtagController.TMS_BIT |
(self._trst and JtagController.TRST_BIT or 0))
self._last = None # Last deferred TDO bit
self._write_buff = bytearray()