@@ -128,9 +128,9 @@ def configure(self, *, baudrate=100000, polarity=0, phase=0, bits=8):
128128 if phase not in (0 , 1 ):
129129 raise ValueError ("phase must be either 0 or 1" )
130130 self ._baudrate = baudrate
131- self ._bits = bits
132131 self ._polarity = polarity
133132 self ._phase = phase
133+ self ._bits = bits
134134 self ._half_period = (1 / self ._baudrate ) / 2 # 50% Duty Cyle delay
135135
136136 else :
@@ -156,18 +156,19 @@ def write(self, buffer, start=0, end=None):
156156 for byte in buffer [start :end ]:
157157 for bit_position in range (self ._bits ):
158158 bit_value = byte & 0x80 >> bit_position
159- # Flip clock off base
160- self ._sclk .value = 0
161- start_time = self ._wait (start_time )
162- # Write bit to MOSI.
163- if not self ._phase : # Mode 1, 3
159+ # Set clock to base
160+ if not self ._phase : # Mode 0, 2
164161 self ._mosi .value = bit_value
165- # Return clock to base
166- self ._sclk .value = 1
162+ self ._sclk .value = self ._polarity
167163 start_time = self ._wait (start_time )
168- if self ._phase : # Mode 0, 2
164+
165+ # Flip clock off base
166+ if self ._phase : # Mode 1, 3
169167 self ._mosi .value = bit_value
170- # Return pins to resting positions
168+ self ._sclk .value = not self ._polarity
169+ start_time = self ._wait (start_time )
170+
171+ # Return pins to base positions
171172 self ._mosi .value = 0
172173 self ._sclk .value = self ._polarity
173174
@@ -184,11 +185,11 @@ def readinto(self, buffer, start=0, end=None, write_value=0):
184185 for bit_position in range (self ._bits ):
185186 bit_mask = 0x80 >> bit_position
186187 bit_value = write_value & 0x80 >> bit_position
187- # Return clock to 0
188- self ._sclk .value = 0
188+ # Return clock to base
189+ self ._sclk .value = self . _polarity
189190 start_time = self ._wait (start_time )
190191 # Handle read on leading edge of clock.
191- if not self ._phase :
192+ if not self ._phase : # Mode 0, 2
192193 if self ._mosi is not None :
193194 self ._mosi .value = bit_value
194195 if self ._miso .value :
@@ -198,10 +199,10 @@ def readinto(self, buffer, start=0, end=None, write_value=0):
198199 # Set bit to 0 at appropriate location.
199200 buffer [byte_position ] &= ~ bit_mask
200201 # Flip clock off base
201- self ._sclk .value = 1
202+ self ._sclk .value = not self . _polarity
202203 start_time = self ._wait (start_time )
203204 # Handle read on trailing edge of clock.
204- if self ._phase :
205+ if self ._phase : # Mode 1, 3
205206 if self ._mosi is not None :
206207 self ._mosi .value = bit_value
207208 if self ._miso .value :
@@ -211,7 +212,7 @@ def readinto(self, buffer, start=0, end=None, write_value=0):
211212 # Set bit to 0 at appropriate location.
212213 buffer [byte_position ] &= ~ bit_mask
213214
214- # Return pins to resting positions
215+ # Return pins to base positions
215216 self ._mosi .value = 0
216217 self ._sclk .value = self ._polarity
217218
@@ -249,10 +250,10 @@ def write_readinto(
249250 bit_value = buffer_out [byte_position + out_start ] & 0x80 >> bit_position
250251 in_byte_position = byte_position + in_start
251252 # Return clock to 0
252- self ._sclk .value = 0
253+ self ._sclk .value = self . _polarity
253254 start_time = self ._wait (start_time )
254255 # Handle read on leading edge of clock.
255- if not self ._phase :
256+ if not self ._phase : # Mode 0, 2
256257 self ._mosi .value = bit_value
257258 if self ._miso .value :
258259 # Set bit to 1 at appropriate location.
@@ -261,10 +262,10 @@ def write_readinto(
261262 # Set bit to 0 at appropriate location.
262263 buffer_in [in_byte_position ] &= ~ bit_mask
263264 # Flip clock off base
264- self ._sclk .value = 1
265+ self ._sclk .value = not self . _polarity
265266 start_time = self ._wait (start_time )
266267 # Handle read on trailing edge of clock.
267- if self ._phase :
268+ if self ._phase : # Mode 1, 3
268269 self ._mosi .value = bit_value
269270 if self ._miso .value :
270271 # Set bit to 1 at appropriate location.
@@ -273,6 +274,10 @@ def write_readinto(
273274 # Set bit to 0 at appropriate location.
274275 buffer_in [in_byte_position ] &= ~ bit_mask
275276
277+ # Return pins to base positions
278+ self ._mosi .value = 0
279+ self ._sclk .value = self ._polarity
280+
276281 # pylint: enable=too-many-branches
277282
278283 @property
0 commit comments