60
60
_SPI_MANF_ID = const (0x04 )
61
61
_SPI_PROD_ID = const (0x302 )
62
62
63
+
63
64
class FRAM :
64
65
"""
65
66
Driver base for the FRAM Breakout.
@@ -121,7 +122,6 @@ def __len__(self):
121
122
"""
122
123
return self ._max_size
123
124
124
-
125
125
def __getitem__ (self , address ):
126
126
""" Read the value at the given index, or values in a slice.
127
127
@@ -135,20 +135,28 @@ def __getitem__(self, address):
135
135
"""
136
136
if isinstance (address , int ):
137
137
if not 0 <= address < self ._max_size :
138
- raise ValueError ("Address '{0}' out of range. It must be 0 <= address < {1}."
139
- .format (address , self ._max_size ))
138
+ raise ValueError (
139
+ "Address '{0}' out of range. It must be 0 <= address < {1}." .format (
140
+ address , self ._max_size
141
+ )
142
+ )
140
143
buffer = bytearray (1 )
141
144
read_buffer = self ._read_address (address , buffer )
142
145
elif isinstance (address , slice ):
143
146
if address .step is not None :
144
147
raise ValueError ("Slice stepping is not currently available." )
145
148
146
- regs = list (range (address .start if address .start is not None else 0 ,
147
- address .stop + 1 if address .stop is not None else self ._max_size ))
149
+ regs = list (
150
+ range (
151
+ address .start if address .start is not None else 0 ,
152
+ address .stop + 1 if address .stop is not None else self ._max_size ,
153
+ )
154
+ )
148
155
if regs [0 ] < 0 or (regs [0 ] + len (regs )) > self ._max_size :
149
- raise ValueError ("Address slice out of range. It must be 0 <= [starting address"
150
- ":stopping address] < {0}."
151
- .format (self ._max_size ))
156
+ raise ValueError (
157
+ "Address slice out of range. It must be 0 <= [starting address"
158
+ ":stopping address] < {0}." .format (self ._max_size )
159
+ )
152
160
153
161
buffer = bytearray (len (regs ))
154
162
read_buffer = self ._read_address (regs [0 ], buffer )
@@ -171,11 +179,15 @@ def __setitem__(self, address, value):
171
179
172
180
if isinstance (address , int ):
173
181
if not isinstance (value , (int , bytearray , list , tuple )):
174
- raise ValueError ("Data must be a single integer, or a bytearray,"
175
- " list, or tuple." )
182
+ raise ValueError (
183
+ "Data must be a single integer, or a bytearray," " list, or tuple."
184
+ )
176
185
if not 0 <= address < self ._max_size :
177
- raise ValueError ("Address '{0}' out of range. It must be 0 <= address < {1}."
178
- .format (address , self ._max_size ))
186
+ raise ValueError (
187
+ "Address '{0}' out of range. It must be 0 <= address < {1}." .format (
188
+ address , self ._max_size
189
+ )
190
+ )
179
191
180
192
self ._write (address , value , self ._wraparound )
181
193
@@ -190,6 +202,7 @@ def _write(self, start_address, data, wraparound):
190
202
# Implemened by subclass
191
203
raise NotImplementedError
192
204
205
+
193
206
class FRAM_I2C (FRAM ):
194
207
""" I2C class for FRAM.
195
208
@@ -200,17 +213,19 @@ class FRAM_I2C(FRAM):
200
213
:param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
201
214
Must be a ``digitalio.DigitalInOut`` object.
202
215
"""
203
- #pylint: disable=too-many-arguments
204
- def __init__ (self , i2c_bus , address = 0x50 , write_protect = False ,
205
- wp_pin = None ):
206
- from adafruit_bus_device .i2c_device import I2CDevice as i2cdev
216
+
217
+ # pylint: disable=too-many-arguments
218
+ def __init__ (self , i2c_bus , address = 0x50 , write_protect = False , wp_pin = None ):
219
+ from adafruit_bus_device .i2c_device import ( # pylint: disable=import-outside-toplevel
220
+ I2CDevice as i2cdev ,
221
+ )
222
+
207
223
dev_id_addr = 0xF8 >> 1
208
224
read_buf = bytearray (3 )
209
225
with i2cdev (i2c_bus , dev_id_addr ) as dev_id :
210
- dev_id .write_then_readinto (bytearray ([(address << 1 )]),
211
- read_buf )
212
- manf_id = (((read_buf [0 ] << 4 ) + (read_buf [1 ] >> 4 )))
213
- prod_id = (((read_buf [1 ] & 0x0F ) << 8 ) + read_buf [2 ])
226
+ dev_id .write_then_readinto (bytearray ([(address << 1 )]), read_buf )
227
+ manf_id = (read_buf [0 ] << 4 ) + (read_buf [1 ] >> 4 )
228
+ prod_id = ((read_buf [1 ] & 0x0F ) << 8 ) + read_buf [2 ]
214
229
if (manf_id != _I2C_MANF_ID ) and (prod_id != _I2C_PROD_ID ):
215
230
raise OSError ("FRAM I2C device not found." )
216
231
@@ -241,9 +256,11 @@ def _write(self, start_address, data, wraparound=False):
241
256
if wraparound :
242
257
pass
243
258
else :
244
- raise ValueError ("Starting address + data length extends beyond"
245
- " FRAM maximum address. Use ``write_wraparound`` to"
246
- " override this warning." )
259
+ raise ValueError (
260
+ "Starting address + data length extends beyond"
261
+ " FRAM maximum address. Use ``write_wraparound`` to"
262
+ " override this warning."
263
+ )
247
264
with self ._i2c as i2c :
248
265
for i in range (0 , data_length ):
249
266
if not (start_address + i ) > self ._max_size - 1 :
@@ -264,6 +281,7 @@ def write_protected(self, value):
264
281
if not self ._wp_pin is None :
265
282
self ._wp_pin .value = value
266
283
284
+
267
285
# the following pylint disables are related to the '_SPI_OPCODE' consts, the super
268
286
# class setter '@FRAM.write_protected.setter', and pylint not being able to see
269
287
# 'spi.write()' in SPIDevice. Travis run for reference:
@@ -282,18 +300,22 @@ class FRAM_SPI(FRAM):
282
300
:param int baudrate: SPI baudrate to use. Default is ``1000000``.
283
301
"""
284
302
285
- _SPI_OPCODE_WREN = const (0x6 ) # Set write enable latch
286
- _SPI_OPCODE_WRDI = const (0x4 ) # Reset write enable latch
287
- _SPI_OPCODE_RDSR = const (0x5 ) # Read status register
288
- _SPI_OPCODE_WRSR = const (0x1 ) # Write status register
289
- _SPI_OPCODE_READ = const (0x3 ) # Read memory code
290
- _SPI_OPCODE_WRITE = const (0x2 ) # Write memory code
291
- _SPI_OPCODE_RDID = const (0x9F ) # Read device ID
292
-
293
- #pylint: disable=too-many-arguments,too-many-locals
294
- def __init__ (self , spi_bus , spi_cs , write_protect = False ,
295
- wp_pin = None , baudrate = 100000 ):
296
- from adafruit_bus_device .spi_device import SPIDevice as spidev
303
+ _SPI_OPCODE_WREN = const (0x6 ) # Set write enable latch
304
+ _SPI_OPCODE_WRDI = const (0x4 ) # Reset write enable latch
305
+ _SPI_OPCODE_RDSR = const (0x5 ) # Read status register
306
+ _SPI_OPCODE_WRSR = const (0x1 ) # Write status register
307
+ _SPI_OPCODE_READ = const (0x3 ) # Read memory code
308
+ _SPI_OPCODE_WRITE = const (0x2 ) # Write memory code
309
+ _SPI_OPCODE_RDID = const (0x9F ) # Read device ID
310
+
311
+ # pylint: disable=too-many-arguments,too-many-locals
312
+ def __init__ (
313
+ self , spi_bus , spi_cs , write_protect = False , wp_pin = None , baudrate = 100000
314
+ ):
315
+ from adafruit_bus_device .spi_device import ( # pylint: disable=import-outside-toplevel
316
+ SPIDevice as spidev ,
317
+ )
318
+
297
319
_spi = spidev (spi_bus , spi_cs , baudrate = baudrate )
298
320
299
321
read_buffer = bytearray (4 )
@@ -328,9 +350,11 @@ def _write(self, start_address, data, wraparound=False):
328
350
if wraparound :
329
351
pass
330
352
else :
331
- raise ValueError ("Starting address + data length extends beyond"
332
- " FRAM maximum address. Use 'wraparound=True' to"
333
- " override this warning." )
353
+ raise ValueError (
354
+ "Starting address + data length extends beyond"
355
+ " FRAM maximum address. Use 'wraparound=True' to"
356
+ " override this warning."
357
+ )
334
358
with self ._spi as spi :
335
359
spi .write (bytearray ([_SPI_OPCODE_WREN ]))
336
360
with self ._spi as spi :
@@ -354,9 +378,9 @@ def write_protected(self, value):
354
378
write_buffer = bytearray (2 )
355
379
write_buffer [0 ] = _SPI_OPCODE_WRSR
356
380
if value :
357
- write_buffer [1 ] = 0x8C # set WPEN, BP0, and BP1
381
+ write_buffer [1 ] = 0x8C # set WPEN, BP0, and BP1
358
382
else :
359
- write_buffer [1 ] = 0x00 # clear WPEN, BP0, and BP1
383
+ write_buffer [1 ] = 0x00 # clear WPEN, BP0, and BP1
360
384
with self ._spi as spi :
361
385
spi .write (write_buffer )
362
386
if self ._wp_pin is not None :
0 commit comments