@@ -653,7 +653,7 @@ def width(self) -> int:
653
653
"""
654
654
return 16 if self ._wide_port else 8
655
655
656
- def read (self , address : int , readlen : int = 1 ,
656
+ def read (self , address : Optional [ int ] , readlen : int = 1 ,
657
657
relax : bool = True ) -> bytes :
658
658
"""Read one or more bytes from a remote slave
659
659
@@ -695,7 +695,8 @@ def read(self, address: int, readlen: int = 1,
695
695
if do_epilog :
696
696
self ._do_epilog ()
697
697
698
- def write (self , address : int , out : Union [bytes , bytearray , Iterable [int ]],
698
+ def write (self , address : Optional [int ],
699
+ out : Union [bytes , bytearray , Iterable [int ]],
699
700
relax : bool = True ) -> None :
700
701
"""Write one or more bytes to a remote slave
701
702
@@ -735,9 +736,9 @@ def write(self, address: int, out: Union[bytes, bytearray, Iterable[int]],
735
736
if do_epilog :
736
737
self ._do_epilog ()
737
738
738
- def exchange (self , address : int ,
739
+ def exchange (self , address : Optional [ int ] ,
739
740
out : Union [bytes , bytearray , Iterable [int ]],
740
- readlen : int = 0 , relax : bool = True ) -> bytes :
741
+ readlen : int = 0 , relax : bool = True ) -> bytearray :
741
742
"""Send a byte sequence to a remote slave followed with
742
743
a read request of one or more bytes.
743
744
@@ -767,12 +768,14 @@ def exchange(self, address: int,
767
768
i2caddress = (address << 1 ) & self .HIGH
768
769
retries = self ._retry_count
769
770
do_epilog = True
771
+ data = bytearray ()
770
772
with self ._lock :
771
773
while True :
772
774
try :
773
775
self ._do_prolog (i2caddress )
774
776
self ._do_write (out )
775
- self ._do_prolog (i2caddress | self .BIT0 )
777
+ if i2caddress is not None :
778
+ self ._do_prolog (i2caddress | self .BIT0 )
776
779
if readlen :
777
780
data = self ._do_read (readlen )
778
781
do_epilog = relax
@@ -1017,7 +1020,7 @@ def _write_raw(self, data: int, write_high: bool):
1017
1020
cmd = bytes ([Ftdi .SET_BITS_LOW , low_data , low_dir ])
1018
1021
self ._ftdi .write_data (cmd )
1019
1022
1020
- def _do_prolog (self , i2caddress : int ) -> None :
1023
+ def _do_prolog (self , i2caddress : Optional [ int ] ) -> None :
1021
1024
if i2caddress is None :
1022
1025
return
1023
1026
self .log .debug (' prolog 0x%x' , i2caddress >> 1 )
@@ -1061,7 +1064,7 @@ def _send_check_ack(self, cmd: bytearray):
1061
1064
if ack [0 ] & self .BIT0 :
1062
1065
raise I2cNackError ('NACK from slave' )
1063
1066
1064
- def _do_read (self , readlen : int ) -> bytes :
1067
+ def _do_read (self , readlen : int ) -> bytearray :
1065
1068
self .log .debug ('- read %d byte(s)' , readlen )
1066
1069
if not readlen :
1067
1070
# force a real read request on device, but discard any result
@@ -1101,10 +1104,10 @@ def _do_read(self, readlen: int) -> bytes:
1101
1104
cmd_chunk .extend (read_not_last * chunk_size )
1102
1105
cmd_chunk .extend (self ._immediate )
1103
1106
1104
- def write_command_gen (length : int ):
1107
+ def write_command_gen (length : int ) -> bytearray :
1105
1108
if length <= 0 :
1106
1109
# no more data
1107
- return b''
1110
+ return bytearray ()
1108
1111
if length <= chunk_size :
1109
1112
cmd = bytearray ()
1110
1113
cmd .extend (read_not_last * (length - 1 ))
@@ -1120,6 +1123,7 @@ def write_command_gen(length: int):
1120
1123
rem -= len (buf )
1121
1124
else :
1122
1125
while rem :
1126
+ size = rem
1123
1127
if rem > chunk_size :
1124
1128
if not cmd :
1125
1129
# build the command sequence only once, as it may be
@@ -1132,7 +1136,6 @@ def write_command_gen(length: int):
1132
1136
cmd .extend (read_not_last * (rem - 1 ))
1133
1137
cmd .extend (read_last )
1134
1138
cmd .extend (self ._immediate )
1135
- size = rem
1136
1139
self ._ftdi .write_data (cmd )
1137
1140
buf = self ._ftdi .read_data_bytes (size , 4 )
1138
1141
self .log .debug ('- read %d byte(s): %s' ,
0 commit comments