@@ -520,6 +520,52 @@ def read(self, length):
520
520
# Read response bytes.
521
521
return bytearray (payload1 + payload2 )
522
522
523
+ def bulkread (self , data = [], lengthR = 'None' , readmode = 1 ):
524
+ """Half-duplex SPI write then read. Send command and payload to slave as bytearray
525
+ then consequently read out response from the slave for length in bytes.
526
+ Designed for use with NOR or NAND flash chips, and possibly SD cards...etc...
527
+ Read command is cut in half and performed twice in series to prevent single byte errors.
528
+ Hardware limits per command are enforced before doing anything.
529
+ Read length is an optional argument, so that it can function similar to transfer
530
+ but still half-duplex.
531
+ For reading without writing, one can send a blank array or skip that argument.
532
+ """
533
+ #check for hardware limit of FT232H and similar MPSSE chips
534
+ if (1 > lengthR > 65536 )| (len (data ) > 65536 ):
535
+ print 'the FTDI chip is limited to 65536 bytes (64 KB) of input/output per command!'
536
+ print 'use for loops for larger reads'
537
+ exit (1 )
538
+ #default mode is to act like `transfer` but half-duplex
539
+ if (lengthR == 'None' )& (readmode == 1 ):
540
+ lengthR = len (data )
541
+ #command parameters definition and math
542
+ #MPSSE engine sees length 0 as 1 byte, so - 1 lengths
543
+ commandW = 0x10 | (spi .lsbfirst << 3 ) | spi .write_clock_ve
544
+ lengthW = len (data ) - 1
545
+ len_lowW = (lengthW ) & 0xFF
546
+ len_highW = ((lengthW ) >> 8 ) & 0xFF
547
+ commandR = 0x20 | (spi .lsbfirst << 3 ) | (spi .read_clock_ve << 2 )
548
+ lengthR = lengthR / 2
549
+ len_lowR = (lengthR - 1 ) & 0xFF
550
+ len_highR = ((lengthR - 1 ) >> 8 ) & 0xFF
551
+ #logger debug info
552
+ #logger.debug('SPI bulkread with write command {0:2X}.'.format(commandW))
553
+ #logger.debug('and read command {0:2X}.'.format(commandR))
554
+ #begin command set
555
+ spi ._assert_cs ()
556
+ #write command, these have to be separated due to TypeError
557
+ spi ._ft232h ._write (str (bytearray ((commandW , len_lowW , len_highW ))))
558
+ spi ._ft232h ._write (str (bytearray (data )))
559
+ #read command, which is now divided into two commands
560
+ spi ._ft232h ._write (str (bytearray ((commandR , len_lowR , len_highR ))))
561
+ payload1 = spi ._ft232h ._poll_read (lengthR )
562
+ spi ._ft232h ._write (str (bytearray ((commandR , len_lowR , len_highR ))))
563
+ payload2 = spi ._ft232h ._poll_read (lengthR )
564
+ #end command set
565
+ spi ._deassert_cs ()
566
+ # Read response bytes.
567
+ return bytearray (payload1 + payload2 )
568
+
523
569
def transfer (self , data ):
524
570
"""Full-duplex SPI read and write. The specified array of bytes will be
525
571
clocked out the MOSI line, while simultaneously bytes will be read from
0 commit comments