@@ -164,7 +164,7 @@ def __init__(self, vid=FT232H_VID, pid=FT232H_PID, serial=None):
164
164
self ._mpsse_enable ()
165
165
self ._mpsse_sync ()
166
166
# Initialize all GPIO as inputs.
167
- self ._write ('\x80 \x00 \x00 \x82 \x00 \x00 ' )
167
+ self ._write (b '\x80 \x00 \x00 \x82 \x00 \x00 ' )
168
168
self ._direction = 0x0000
169
169
self ._level = 0x0000
170
170
@@ -181,17 +181,17 @@ def _write(self, string):
181
181
# Get modem status. Useful to enable for debugging.
182
182
#ret, status = ftdi.poll_modem_status(self._ctx)
183
183
#if ret == 0:
184
- # logger.debug('Modem status {0:02X}'.format(status))
184
+ # logger.debug('Modem status {0:02X}'.format(status))
185
185
#else:
186
- # logger.debug('Modem status error {0}'.format(ret))
186
+ # logger.debug('Modem status error {0}'.format(ret))
187
187
length = len (string )
188
188
try :
189
189
ret = ftdi .write_data (self ._ctx , string , length )
190
190
except TypeError :
191
191
ret = ftdi .write_data (self ._ctx , string ); #compatible with libFtdi 1.3
192
192
# Log the string that was written in a python hex string format using a very
193
193
# ugly one-liner list comprehension for brevity.
194
- #logger.debug('Wrote {0}'.format(''.join(['\\x{0:02X}'.format(ord(x)) for x in string])))
194
+ #logger.debug('Wrote {0}'.format(''.join(['\\x{0:02X}'.format(x) for x in bytearray( string) ])))
195
195
if ret < 0 :
196
196
raise RuntimeError ('ftdi_write_data failed with error {0}: {1}' .format (ret , ftdi .get_error_string (self ._ctx )))
197
197
if ret != length :
@@ -227,7 +227,7 @@ def _poll_read(self, expected, timeout_s=5.0):
227
227
index += ret
228
228
# Buffer is full, return the result data.
229
229
if index >= expected :
230
- return str (response )
230
+ return bytes (response )
231
231
time .sleep (0.01 )
232
232
raise RuntimeError ('Timeout while polling ftdi_read_data for {0} bytes!' .format (expected ))
233
233
@@ -243,14 +243,14 @@ def _mpsse_sync(self, max_retries=10):
243
243
error response. Should be called once after enabling MPSSE."""
244
244
# Send a bad/unknown command (0xAB), then read buffer until bad command
245
245
# response is found.
246
- self ._write ('\xAB ' )
246
+ self ._write (b '\xAB ' )
247
247
# Keep reading until bad command response (0xFA 0xAB) is returned.
248
248
# Fail if too many read attempts are made to prevent sticking in a loop.
249
249
tries = 0
250
250
sync = False
251
251
while not sync :
252
252
data = self ._poll_read (2 )
253
- if data == '\xFA \xAB ' :
253
+ if data == b '\xFA \xAB ' :
254
254
sync = True
255
255
tries += 1
256
256
if tries >= max_retries :
@@ -261,20 +261,20 @@ def mpsse_set_clock(self, clock_hz, adaptive=False, three_phase=False):
261
261
to 30mhz and will pick that speed or the closest speed below it.
262
262
"""
263
263
# Disable clock divisor by 5 to enable faster speeds on FT232H.
264
- self ._write ('\x8A ' )
264
+ self ._write (b '\x8A ' )
265
265
# Turn on/off adaptive clocking.
266
266
if adaptive :
267
- self ._write ('\x96 ' )
267
+ self ._write (b '\x96 ' )
268
268
else :
269
- self ._write ('\x97 ' )
269
+ self ._write (b '\x97 ' )
270
270
# Turn on/off three phase clock (needed for I2C).
271
271
# Also adjust the frequency for three-phase clocking as specified in section 2.2.4
272
272
# of this document:
273
273
# http://www.ftdichip.com/Support/Documents/AppNotes/AN_255_USB%20to%20I2C%20Example%20using%20the%20FT232H%20and%20FT201X%20devices.pdf
274
274
if three_phase :
275
- self ._write ('\x8C ' )
275
+ self ._write (b '\x8C ' )
276
276
else :
277
- self ._write ('\x8D ' )
277
+ self ._write (b '\x8D ' )
278
278
# Compute divisor for requested clock.
279
279
# Use equation from section 3.8.1 of:
280
280
# http://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
@@ -284,14 +284,14 @@ def mpsse_set_clock(self, clock_hz, adaptive=False, three_phase=False):
284
284
divisor = int (divisor * (2.0 / 3.0 ))
285
285
logger .debug ('Setting clockspeed with divisor value {0}' .format (divisor ))
286
286
# Send command to set divisor from low and high byte values.
287
- self ._write (str (bytearray ((0x86 , divisor & 0xFF , (divisor >> 8 ) & 0xFF ))))
287
+ self ._write (bytes (bytearray ((0x86 , divisor & 0xFF , (divisor >> 8 ) & 0xFF ))))
288
288
289
289
def mpsse_read_gpio (self ):
290
290
"""Read both GPIO bus states and return a 16 bit value with their state.
291
291
D0-D7 are the lower 8 bits and C0-C7 are the upper 8 bits.
292
292
"""
293
293
# Send command to read low byte and high byte.
294
- self ._write ('\x81 \x83 ' )
294
+ self ._write (b '\x81 \x83 ' )
295
295
# Wait for 2 byte response.
296
296
data = self ._poll_read (2 )
297
297
# Assemble response into 16 bit value.
@@ -304,11 +304,11 @@ def mpsse_gpio(self):
304
304
"""Return command to update the MPSSE GPIO state to the current direction
305
305
and level.
306
306
"""
307
- level_low = chr (self ._level & 0xFF )
308
- level_high = chr ((self ._level >> 8 ) & 0xFF )
309
- dir_low = chr (self ._direction & 0xFF )
310
- dir_high = chr ((self ._direction >> 8 ) & 0xFF )
311
- return str (bytearray ((0x80 , level_low , dir_low , 0x82 , level_high , dir_high )))
307
+ level_low = (self ._level & 0xFF )
308
+ level_high = ((self ._level >> 8 ) & 0xFF )
309
+ dir_low = (self ._direction & 0xFF )
310
+ dir_high = ((self ._direction >> 8 ) & 0xFF )
311
+ return bytes (bytearray ((0x80 , level_low , dir_low , 0x82 , level_high , dir_high )))
312
312
313
313
def mpsse_write_gpio (self ):
314
314
"""Write the current MPSSE GPIO state to the FT232H chip."""
@@ -481,21 +481,21 @@ def write(self, data):
481
481
# Compute length low and high bytes.
482
482
# NOTE: Must actually send length minus one because the MPSSE engine
483
483
# considers 0 a length of 1 and FFFF a length of 65536
484
- # splitting into two lists for two commands to prevent buffer errors
485
- data1 = data [:len (data )/ 2 ]
486
- data2 = data [len (data )/ 2 :]
484
+ # splitting into two lists for two commands to prevent buffer errors
485
+ data1 = data [:len (data )/ /2 ]
486
+ data2 = data [len (data )/ /2 :]
487
487
len_low1 = (len (data1 ) - 1 ) & 0xFF
488
488
len_high1 = ((len (data1 ) - 1 ) >> 8 ) & 0xFF
489
- len_low2 = (len (data2 ) - 1 ) & 0xFF
489
+ len_low2 = (len (data2 ) - 1 ) & 0xFF
490
490
len_high2 = ((len (data2 ) - 1 ) >> 8 ) & 0xFF
491
491
self ._assert_cs ()
492
492
# Send command and length, then data, split into two commands, handle for length 1
493
- if len (data1 ) > 0 :
494
- self ._ft232h ._write (str (bytearray ((command , len_low1 , len_high1 ))))
495
- self ._ft232h ._write (str (bytearray (data1 )))
493
+ if len (data1 ) > 0 :
494
+ self ._ft232h ._write (bytes (bytearray ((command , len_low1 , len_high1 ))))
495
+ self ._ft232h ._write (bytes (bytearray (data1 )))
496
496
if len (data2 ) > 0 :
497
- self ._ft232h ._write (str (bytearray ((command , len_low2 , len_high2 ))))
498
- self ._ft232h ._write (str (bytearray (data2 )))
497
+ self ._ft232h ._write (bytes (bytearray ((command , len_low2 , len_high2 ))))
498
+ self ._ft232h ._write (bytes (bytearray (data2 )))
499
499
self ._deassert_cs ()
500
500
501
501
def read (self , length ):
@@ -513,21 +513,21 @@ def read(self, length):
513
513
# Compute length low and high bytes.
514
514
# NOTE: Must actually send length minus one because the MPSSE engine
515
515
# considers 0 a length of 1 and FFFF a length of 65536
516
- #force odd numbers to round up instead of down
517
- lengthR = length
518
- if length % 2 == 1 :
519
- lengthR += 1
520
- lengthR = lengthR / 2
521
- #when odd length requested, get the remainder instead of the same number
522
- lenremain = length - lengthR
516
+ #force odd numbers to round up instead of down
517
+ lengthR = length
518
+ if length % 2 == 1 :
519
+ lengthR += 1
520
+ lengthR = lengthR / /2
521
+ #when odd length requested, get the remainder instead of the same number
522
+ lenremain = length - lengthR
523
523
len_low = (lengthR - 1 ) & 0xFF
524
524
len_high = ((lengthR - 1 ) >> 8 ) & 0xFF
525
525
self ._assert_cs ()
526
526
# Send command and length.
527
527
# Perform twice to prevent error from hardware defect/limits
528
- self ._ft232h ._write (str (bytearray ((command , len_low , len_high ))))
528
+ self ._ft232h ._write (bytes (bytearray ((command , len_low , len_high ))))
529
529
payload1 = self ._ft232h ._poll_read (lengthR )
530
- self ._ft232h ._write (str (bytearray ((command , len_low , len_high ))))
530
+ self ._ft232h ._write (bytes (bytearray ((command , len_low , len_high ))))
531
531
payload2 = self ._ft232h ._poll_read (lenremain )
532
532
self ._deassert_cs ()
533
533
# Read response bytes
@@ -559,12 +559,12 @@ def bulkread(self, data = [], lengthR = 'None', readmode = 1):
559
559
len_highW = ((lengthW ) >> 8 ) & 0xFF
560
560
commandR = 0x20 | (self .lsbfirst << 3 ) | (self .read_clock_ve << 2 )
561
561
#force odd numbers to round up instead of down
562
- length = lengthR
563
- if lengthR % 2 == 1 :
564
- length += 1
565
- length = length / 2
562
+ length = lengthR
563
+ if lengthR % 2 == 1 :
564
+ length += 1
565
+ length = length / /2
566
566
#when odd length requested, get the remainder instead of the same number
567
- lenremain = lengthR - length
567
+ lenremain = lengthR - length
568
568
len_lowR = (length - 1 ) & 0xFF
569
569
len_highR = ((length - 1 ) >> 8 ) & 0xFF
570
570
#logger debug info
@@ -573,12 +573,12 @@ def bulkread(self, data = [], lengthR = 'None', readmode = 1):
573
573
#begin command set
574
574
self ._assert_cs ()
575
575
#write command, these have to be separated due to TypeError
576
- self ._ft232h ._write (str (bytearray ((commandW , len_lowW , len_highW ))))
577
- self ._ft232h ._write (str (bytearray (data )))
576
+ self ._ft232h ._write (bytes (bytearray ((commandW , len_lowW , len_highW ))))
577
+ self ._ft232h ._write (bytes (bytearray (data )))
578
578
#read command, which is divided into two commands
579
- self ._ft232h ._write (str (bytearray ((commandR , len_lowR , len_highR ))))
579
+ self ._ft232h ._write (bytes (bytearray ((commandR , len_lowR , len_highR ))))
580
580
payload1 = self ._ft232h ._poll_read (length )
581
- self ._ft232h ._write (str (bytearray ((commandR , len_lowR , len_highR ))))
581
+ self ._ft232h ._write (bytes (bytearray ((commandR , len_lowR , len_highR ))))
582
582
payload2 = self ._ft232h ._poll_read (lenremain )
583
583
self ._deassert_cs ()
584
584
#end command set
@@ -601,26 +601,26 @@ def transfer(self, data):
601
601
# Compute length low and high bytes.
602
602
# NOTE: Must actually send length minus one because the MPSSE engine
603
603
# considers 0 a length of 1 and FFFF a length of 65536
604
- data1 = data [:len (data )/ 2 ]
605
- data2 = data [len (data )/ 2 :]
606
- len_low1 = (len (data1 ) - 1 ) & 0xFF
604
+ data1 = data [:len (data )// 2 ]
605
+ data2 = data [len (data )/ /2 :]
606
+ len_low1 = (len (data1 ) - 1 ) & 0xFF
607
607
len_high1 = ((len (data1 ) - 1 ) >> 8 ) & 0xFF
608
- len_low2 = (len (data2 ) - 1 ) & 0xFF
608
+ len_low2 = (len (data2 ) - 1 ) & 0xFF
609
609
len_high2 = ((len (data2 ) - 1 ) >> 8 ) & 0xFF
610
- payload1 = ''
611
- payload2 = ''
612
- #start command set
610
+ payload1 = ''
611
+ payload2 = ''
612
+ #start command set
613
613
self ._assert_cs ()
614
614
# Perform twice to prevent error from hardware defect/limits
615
- # Send command and length, then data, split into two commands, handle for length 1
616
- if len (data1 ) > 0 :
617
- self ._ft232h ._write (str (bytearray ((command , len_low1 , len_high1 ))))
618
- self ._ft232h ._write (str (bytearray (data1 )))
619
- payload1 = self ._ft232h ._poll_read (len (data1 ))
620
- if len (data2 ) > 0 :
621
- self ._ft232h ._write (str (bytearray ((command , len_low2 , len_high2 ))))
622
- self ._ft232h ._write (str (bytearray (data2 )))
623
- payload2 = self ._ft232h ._poll_read (len (data2 ))
615
+ # Send command and length, then data, split into two commands, handle for length 1
616
+ if len (data1 ) > 0 :
617
+ self ._ft232h ._write (bytes (bytearray ((command , len_low1 , len_high1 ))))
618
+ self ._ft232h ._write (bytes (bytearray (data1 )))
619
+ payload1 = self ._ft232h ._poll_read (len (data1 ))
620
+ if len (data2 ) > 0 :
621
+ self ._ft232h ._write (bytes (bytearray ((command , len_low2 , len_high2 ))))
622
+ self ._ft232h ._write (bytes (bytearray (data2 )))
623
+ payload2 = self ._ft232h ._poll_read (len (data2 ))
624
624
#self._ft232h._write('\x87')
625
625
self ._deassert_cs ()
626
626
# Read response bytes.
@@ -642,7 +642,7 @@ def __init__(self, ft232h, address, clock_hz=100000):
642
642
# Enable drive-zero mode to drive outputs low on 0 and tri-state on 1.
643
643
# This matches the protocol for I2C communication so multiple devices can
644
644
# share the I2C bus.
645
- self ._ft232h ._write ('\x9E \x07 \x00 ' )
645
+ self ._ft232h ._write (b '\x9E \x07 \x00 ' )
646
646
self ._idle ()
647
647
648
648
def _idle (self ):
@@ -660,9 +660,9 @@ def _transaction_start(self):
660
660
def _transaction_end (self ):
661
661
"""End I2C transaction and get response bytes, including ACKs."""
662
662
# Ask to return response bytes immediately.
663
- self ._command .append ('\x87 ' )
663
+ self ._command .append (b '\x87 ' )
664
664
# Send the entire command to the MPSSE.
665
- self ._ft232h ._write ('' .join (self ._command ))
665
+ self ._ft232h ._write (b '' .join (self ._command ))
666
666
# Read response bytes and return them.
667
667
return bytearray (self ._ft232h ._poll_read (self ._expected ))
668
668
@@ -703,12 +703,12 @@ def _i2c_read_bytes(self, length=1):
703
703
"""
704
704
for i in range (length - 1 ):
705
705
# Read a byte and send ACK.
706
- self ._command .append ('\x20 \x00 \x00 \x13 \x00 \x00 ' )
706
+ self ._command .append (b '\x20 \x00 \x00 \x13 \x00 \x00 ' )
707
707
# Make sure pins are back in idle state with clock low and data high.
708
708
self ._ft232h .output_pins ({0 : GPIO .LOW , 1 : GPIO .HIGH }, write = False )
709
709
self ._command .append (self ._ft232h .mpsse_gpio ())
710
710
# Read last byte and send NAK.
711
- self ._command .append ('\x20 \x00 \x00 \x13 \x00 \xFF ' )
711
+ self ._command .append (b '\x20 \x00 \x00 \x13 \x00 \xFF ' )
712
712
# Make sure pins are back in idle state with clock low and data high.
713
713
self ._ft232h .output_pins ({0 : GPIO .LOW , 1 : GPIO .HIGH }, write = False )
714
714
self ._command .append (self ._ft232h .mpsse_gpio ())
@@ -719,12 +719,12 @@ def _i2c_write_bytes(self, data):
719
719
"""Write the specified number of bytes to the chip."""
720
720
for byte in data :
721
721
# Write byte.
722
- self ._command .append (str (bytearray ((0x11 , 0x00 , 0x00 , byte ))))
722
+ self ._command .append (bytes (bytearray ((0x11 , 0x00 , 0x00 , byte ))))
723
723
# Make sure pins are back in idle state with clock low and data high.
724
724
self ._ft232h .output_pins ({0 : GPIO .LOW , 1 : GPIO .HIGH }, write = False )
725
725
self ._command .append (self ._ft232h .mpsse_gpio () * _REPEAT_DELAY )
726
726
# Read bit for ACK/NAK.
727
- self ._command .append ('\x22 \x00 ' )
727
+ self ._command .append (b '\x22 \x00 ' )
728
728
# Increase expected response bytes.
729
729
self ._expected += len (data )
730
730
0 commit comments