Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions pyftdi/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ def set_mode(self, mode: int, cs_hold: Optional[int] = None) -> None:
self._cpha = bool(mode & 0x1)
cs_clock = 0xFF & ~((int(not self._cpol) and SpiController.SCK_BIT) |
SpiController.DO_BIT)
cs_select = 0xFF & ~((SpiController.CS_BIT << self._cs) |
cs_bit_sel = ~((self._controller._cs_idle
^ (SpiController.CS_BIT << self._cs))
& self._controller._cs_bits)
cs_select = 0xFF & ~(cs_bit_sel |
(int(not self._cpol) and SpiController.SCK_BIT) |
SpiController.DO_BIT)
self._cs_prolog = bytes([cs_clock, cs_select])
Expand Down Expand Up @@ -355,6 +358,8 @@ def __init__(self, cs_count: int = 1, turbo: bool = True):
self._frequency = 0.0
self._clock_phase = False
self._cs_bits = 0
self._cs_idle = 0
self._cs_act_hi = 0
self._spi_ports = []
self._spi_dir = 0
self._spi_mask = self.SPI_BITS
Expand Down Expand Up @@ -384,6 +389,9 @@ def configure(self, url: Union[str, UsbDevice],
frequency.
* ``cs_count`` count of chip select signals dedicated to select
SPI slave devices, starting from A*BUS3 pin
* ``cs_act_hi`` a bitfield specifying which SPI CS lines are active
high. Bit 4 is the first CS, bit 5 the second and so on. Bits
corresponding to pins not used/configured as CS are ignored.
* ``turbo`` whether to enable or disable turbo mode
* ``debug`` to increase log verbosity, using MPSSE tracer
"""
Expand All @@ -396,6 +404,9 @@ def configure(self, url: Union[str, UsbDevice],
if not 1 <= self._cs_count <= 5:
raise ValueError('Unsupported CS line count: %d' %
self._cs_count)
if 'cs_act_hi' in kwargs:
self._cs_act_hi = int(kwargs['cs_act_hi'])
del kwargs['cs_act_hi']
if 'turbo' in kwargs:
self._turbo = bool(kwargs['turbo'])
del kwargs['turbo']
Expand All @@ -421,6 +432,8 @@ def configure(self, url: Union[str, UsbDevice],
raise SpiIOError('Already configured')
self._cs_bits = (((SpiController.CS_BIT << self._cs_count) - 1) &
~(SpiController.CS_BIT - 1))
self._cs_act_hi &= self._cs_bits
self._cs_idle = (~self._cs_act_hi) & self._cs_bits
self._spi_ports = [None] * self._cs_count
self._spi_dir = (self._cs_bits |
SpiController.DO_BIT |
Expand All @@ -431,7 +444,7 @@ def configure(self, url: Union[str, UsbDevice],
# delay any truncation till the device is actually open
self._set_gpio_direction(16, (~self._spi_mask) & 0xFFFF, io_dir)
kwargs['direction'] = self._spi_dir | self._gpio_dir
kwargs['initial'] = self._cs_bits | (io_out & self._gpio_mask)
kwargs['initial'] = self._cs_idle | (io_out & self._gpio_mask)
if not isinstance(url, str):
self._frequency = self._ftdi.open_mpsse_from_device(
url, interface=interface, **kwargs)
Expand Down Expand Up @@ -800,7 +813,7 @@ def _exchange_half_duplex(self, frequency: float,
ctrl |= self._gpio_low
epilog.extend((Ftdi.SET_BITS_LOW, ctrl, direction))
# Restore idle state
cs_high = [Ftdi.SET_BITS_LOW, self._cs_bits | self._gpio_low,
cs_high = [Ftdi.SET_BITS_LOW, self._cs_idle | self._gpio_low,
direction]
if not self._turbo:
cs_high.append(Ftdi.SEND_IMMEDIATE)
Expand Down Expand Up @@ -907,7 +920,7 @@ def _exchange_full_duplex(self, frequency: float,
ctrl |= self._gpio_low
epilog.extend((Ftdi.SET_BITS_LOW, ctrl, direction))
# Restore idle state
cs_high = [Ftdi.SET_BITS_LOW, self._cs_bits | self._gpio_low,
cs_high = [Ftdi.SET_BITS_LOW, self._cs_idle | self._gpio_low,
direction]
if not self._turbo:
cs_high.append(Ftdi.SEND_IMMEDIATE)
Expand Down