Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Add SPI method: bulkread #95

Merged
merged 2 commits into from
Mar 29, 2019
Merged
Changes from all 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
45 changes: 45 additions & 0 deletions Adafruit_GPIO/FT232H.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,51 @@ def read(self, length):
self._deassert_cs()
# Read response bytes.
return bytearray(self._ft232h._poll_read(length))

def bulkread(self, data = [], lengthR = 'None', readmode = 1):
"""Half-duplex SPI write then read. Send command and payload to slave as bytearray
then consequently read out response from the slave for length in bytes.
Designed for use with NOR or NAND flash chips, and possibly SD cards...etc...
Read command is cut in half and performed twice in series to prevent single byte errors.
Hardware limits per command are enforced before doing anything.
Read length is an optional argument, so that it can function similar to transfer
but still half-duplex.
For reading without writing, one can send a blank array or skip that argument.
"""
#check for hardware limit of FT232H and similar MPSSE chips
if (1 > lengthR > 65536)|(len(data) > 65536):
print 'the FTDI chip is limited to 65536 bytes (64 KB) of input/output per command!'
print 'use for loops for larger reads'
exit(1)
#default mode is to act like `transfer` but half-duplex
if (lengthR == 'None')&(readmode == 1):
lengthR = len(data)
#command parameters definition and math
#MPSSE engine sees length 0 as 1 byte, so - 1 lengths
commandW = 0x10 | (spi.lsbfirst << 3) | spi.write_clock_ve
lengthW = len(data) - 1
len_lowW = (lengthW) & 0xFF
len_highW = ((lengthW) >> 8) & 0xFF
commandR = 0x20 | (spi.lsbfirst << 3) | (spi.read_clock_ve << 2)
lengthR = lengthR/2
len_lowR = (lengthR-1) & 0xFF
len_highR = ((lengthR-1) >> 8) & 0xFF
#logger debug info
#logger.debug('SPI bulkread with write command {0:2X}.'.format(commandW))
#logger.debug('and read command {0:2X}.'.format(commandR))
#begin command set
spi._assert_cs()
#write command, these have to be separated due to TypeError
spi._ft232h._write(str(bytearray((commandW, len_lowW, len_highW))))
spi._ft232h._write(str(bytearray(data)))
#read command, which is now divided into two commands
spi._ft232h._write(str(bytearray((commandR, len_lowR, len_highR))))
payload1 = spi._ft232h._poll_read(lengthR)
spi._ft232h._write(str(bytearray((commandR, len_lowR, len_highR))))
payload2 = spi._ft232h._poll_read(lengthR)
#end command set
spi._deassert_cs()
return bytearray(payload1 + payload2)

def transfer(self, data):
"""Full-duplex SPI read and write. The specified array of bytes will be
Expand Down