Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-str committed Nov 21, 2023
2 parents 8e7e1e4 + cebb50f commit 95baca6
Showing 1 changed file with 10 additions and 40 deletions.
50 changes: 10 additions & 40 deletions modules/spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@author: Nicolas Striebig
"""
import logging
from bitstring import BitArray

from modules.setup_logger import logger

Expand Down Expand Up @@ -251,7 +250,7 @@ def read_spi_fifo(self, max_reads: int = 1) -> bytearray:
readcount = 0

while not (self.get_spi_config() & SPI_READ_FIFO_EMPTY) and readcount < max_reads:
readbuffer = self.read_spi(1024)
readbuffer = self.read_spi(4096)
read_stream.extend(readbuffer)

readcount += 1
Expand Down Expand Up @@ -280,8 +279,7 @@ def send_routing_cmd(self) -> None:
logger.info("SPI: Send routing cmd")
self.write_spi(bytearray([SPI_HEADER_EMPTY, 0, 0, 0, 0, 0, 0, 0]), False)

def write_spi(self, data: bytearray, MSBfirst: bool = True,
buffersize: int = 1023) -> None:
def write_spi(self, data: bytearray, MSBfirst: bool = True) -> None:
"""
Write to Nexys SPI Write FIFO
Expand All @@ -291,47 +289,19 @@ def write_spi(self, data: bytearray, MSBfirst: bool = True,
"""

if not MSBfirst:

for index, item in enumerate(data):

item_rev = BitArray(uint=item, length=8)
item_rev.reverse()

data[index] = item_rev.uint
for index in range(len(data)):
data[index] = int(format(data[index], '08b')[::-1], 2)

logger.debug('SPIdata: %s', data)

waiting = True
i = 0
counter = buffersize / 3

# WrFIFO bit positions in spi_config register 0x21
compare_empty = 2
compare_full = 4

# Check if WrFIFO is Empty
while waiting:

result = self.get_spi_config()

# Wait until WrFIFO empty
if result & compare_empty:
waiting = False
# Wait until WrFIFO is Empty
while not self.get_spi_config() & SPI_WRITE_FIFO_EMPTY:
continue

while i < len(data):

if counter > 0:
writebuffer = bytearray(data[i:(i + 16)])

if not self.get_spi_config() & SPI_WRITE_FIFO_FULL:
self.direct_write_spi(bytes(bytearray(data[i:(i + 16)])))
logger.debug('Write SPI bytes %d:%d', i, i + 16)
i += 16
counter -= 1

self.direct_write_spi(bytes(writebuffer))

else:
result = self.get_spi_config()

if result & compare_empty:
counter = buffersize / 3
elif not result & compare_full:
counter = 1

0 comments on commit 95baca6

Please sign in to comment.