31
31
# imports
32
32
from micropython import const
33
33
34
+ try :
35
+ from typing import Optional , Union , Sequence
36
+ from digitalio import DigitalInOut
37
+ from busio import I2C , SPI
38
+ except ImportError :
39
+ pass
40
+
34
41
__version__ = "0.0.0-auto.0"
35
42
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FRAM.git"
36
43
55
62
class FRAM :
56
63
"""
57
64
Driver base for the FRAM Breakout.
65
+
66
+ :param int max_size: The maximum size of the EEPROM
67
+ :param bool write_protect: Turns on/off initial write protection
68
+ :param DigitalInOut wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
69
+ Must be a ``DigitalInOut`` object.
58
70
"""
59
71
60
- def __init__ (self , max_size , write_protect = False , wp_pin = None ):
72
+ def __init__ (
73
+ self ,
74
+ max_size : int ,
75
+ write_protect : bool = False ,
76
+ wp_pin : Optional [DigitalInOut ] = None ,
77
+ ) -> None :
61
78
self ._max_size = max_size
62
79
self ._wp = write_protect
63
80
self ._wraparound = False
@@ -70,21 +87,21 @@ def __init__(self, max_size, write_protect=False, wp_pin=None):
70
87
self ._wp_pin = wp_pin
71
88
72
89
@property
73
- def write_wraparound (self ):
90
+ def write_wraparound (self ) -> bool :
74
91
"""Determines if sequential writes will wrapaound highest memory address
75
92
(``len(FRAM) - 1``) address. If ``False``, and a requested write will
76
93
extend beyond the maximum size, an exception is raised.
77
94
"""
78
95
return self ._wraparound
79
96
80
97
@write_wraparound .setter
81
- def write_wraparound (self , value ) :
82
- if not value in ( True , False ):
98
+ def write_wraparound (self , value : bool ) -> None :
99
+ if not isinstance ( value , bool ):
83
100
raise ValueError ("Write wraparound must be 'True' or 'False'." )
84
101
self ._wraparound = value
85
102
86
103
@property
87
- def write_protected (self ):
104
+ def write_protected (self ) -> bool :
88
105
"""The status of write protection. Default value on initialization is
89
106
``False``.
90
107
@@ -97,7 +114,7 @@ def write_protected(self):
97
114
"""
98
115
return self ._wp if self ._wp_pin is None else self ._wp_pin .value
99
116
100
- def __len__ (self ):
117
+ def __len__ (self ) -> int :
101
118
"""The size of the current FRAM chip. This is one more than the highest
102
119
address location that can be read or written to.
103
120
@@ -113,7 +130,7 @@ def __len__(self):
113
130
"""
114
131
return self ._max_size
115
132
116
- def __getitem__ (self , address ) :
133
+ def __getitem__ (self , address : Union [ int , slice ]) -> bytearray :
117
134
"""Read the value at the given index, or values in a slice.
118
135
119
136
.. code-block:: python
@@ -154,7 +171,7 @@ def __getitem__(self, address):
154
171
155
172
return read_buffer
156
173
157
- def __setitem__ (self , address , value ):
174
+ def __setitem__ (self , address : Union [ int , slice ], value : Union [ int , Sequence [ int ]] ):
158
175
"""Write the value at the given starting index.
159
176
160
177
.. code-block:: python
@@ -203,28 +220,36 @@ def __setitem__(self, address, value):
203
220
204
221
self ._write (address .start , value , self ._wraparound )
205
222
206
- def _read_address (self , address , read_buffer ) :
223
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
207
224
# Implemented by subclass
208
225
raise NotImplementedError
209
226
210
- def _write (self , start_address , data , wraparound ):
227
+ def _write (
228
+ self , start_address : int , data : Union [int , Sequence [int ]], wraparound : bool
229
+ ) -> None :
211
230
# Implemened by subclass
212
231
raise NotImplementedError
213
232
214
233
215
234
class FRAM_I2C (FRAM ):
216
235
"""I2C class for FRAM.
217
236
218
- :param: ~busio.I2C i2c_bus: The I2C bus the FRAM is connected to.
219
- :param: int address: I2C address of FRAM. Default address is ``0x50``.
220
- :param: bool write_protect: Turns on/off initial write protection.
237
+ :param ~busio.I2C i2c_bus: The I2C bus the FRAM is connected to.
238
+ :param int address: I2C address of FRAM. Default address is ``0x50``.
239
+ :param bool write_protect: Turns on/off initial write protection.
221
240
Default is ``False``.
222
- :param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
241
+ :param wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
223
242
Must be a ``digitalio.DigitalInOut`` object.
224
243
"""
225
244
226
245
# pylint: disable=too-many-arguments
227
- def __init__ (self , i2c_bus , address = 0x50 , write_protect = False , wp_pin = None ):
246
+ def __init__ (
247
+ self ,
248
+ i2c_bus : I2C ,
249
+ address : int = 0x50 ,
250
+ write_protect : bool = False ,
251
+ wp_pin : Optional [DigitalInOut ] = None ,
252
+ ) -> None :
228
253
from adafruit_bus_device .i2c_device import ( # pylint: disable=import-outside-toplevel
229
254
I2CDevice as i2cdev ,
230
255
)
@@ -241,15 +266,20 @@ def __init__(self, i2c_bus, address=0x50, write_protect=False, wp_pin=None):
241
266
self ._i2c = i2cdev (i2c_bus , address )
242
267
super ().__init__ (_MAX_SIZE_I2C , write_protect , wp_pin )
243
268
244
- def _read_address (self , address , read_buffer ) :
269
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
245
270
write_buffer = bytearray (2 )
246
271
write_buffer [0 ] = address >> 8
247
272
write_buffer [1 ] = address & 0xFF
248
273
with self ._i2c as i2c :
249
274
i2c .write_then_readinto (write_buffer , read_buffer )
250
275
return read_buffer
251
276
252
- def _write (self , start_address , data , wraparound = False ):
277
+ def _write (
278
+ self ,
279
+ start_address : int ,
280
+ data : Union [int , Sequence [int ]],
281
+ wraparound : bool = False ,
282
+ ) -> None :
253
283
# Decided against using the chip's "Page Write", since that would require
254
284
# doubling the memory usage by creating a buffer that includes the passed
255
285
# in data so that it can be sent all in one `i2c.write`. The single-write
@@ -283,8 +313,8 @@ def _write(self, start_address, data, wraparound=False):
283
313
284
314
# pylint: disable=no-member
285
315
@FRAM .write_protected .setter
286
- def write_protected (self , value ) :
287
- if value not in ( True , False ):
316
+ def write_protected (self , value : bool ) -> None :
317
+ if not isinstance ( value , bool ):
288
318
raise ValueError ("Write protected value must be 'True' or 'False'." )
289
319
self ._wp = value
290
320
if not self ._wp_pin is None :
@@ -294,25 +324,25 @@ def write_protected(self, value):
294
324
class FRAM_SPI (FRAM ):
295
325
"""SPI class for FRAM.
296
326
297
- :param: ~busio.SPI spi_bus: The SPI bus the FRAM is connected to.
298
- :param: ~digitalio.DigitalInOut spi_cs: The SPI CS pin.
299
- :param: bool write_protect: Turns on/off initial write protection.
300
- Default is ``False``.
301
- :param: wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
302
- Must be a ``digitalio.DigitalInOut`` object.
327
+ :param ~busio.SPI spi_bus: The SPI bus the FRAM is connected to.
328
+ :param ~digitalio.DigitalInOut spi_cs: The SPI CS pin.
329
+ :param bool write_protect: Turns on/off initial write protection.
330
+ Default is ``False``.
331
+ :param wp_pin: (Optional) Physical pin connected to the ``WP`` breakout pin.
332
+ Must be a ``digitalio.DigitalInOut`` object.
303
333
:param int baudrate: SPI baudrate to use. Default is ``1000000``.
304
334
:param int max_size: Size of FRAM in Bytes. Default is ``8192``.
305
335
"""
306
336
307
337
# pylint: disable=too-many-arguments,too-many-locals
308
338
def __init__ (
309
339
self ,
310
- spi_bus ,
311
- spi_cs ,
312
- write_protect = False ,
313
- wp_pin = None ,
314
- baudrate = 100000 ,
315
- max_size = _MAX_SIZE_SPI ,
340
+ spi_bus : SPI ,
341
+ spi_cs : DigitalInOut ,
342
+ write_protect : bool = False ,
343
+ wp_pin : Optional [ DigitalInOut ] = None ,
344
+ baudrate : int = 100000 ,
345
+ max_size : int = _MAX_SIZE_SPI ,
316
346
):
317
347
from adafruit_bus_device .spi_device import ( # pylint: disable=import-outside-toplevel
318
348
SPIDevice as spidev ,
@@ -331,7 +361,7 @@ def __init__(
331
361
self ._spi = _spi
332
362
super ().__init__ (max_size , write_protect , wp_pin )
333
363
334
- def _read_address (self , address , read_buffer ) :
364
+ def _read_address (self , address : int , read_buffer : bytearray ) -> bytearray :
335
365
write_buffer = bytearray (4 )
336
366
write_buffer [0 ] = _SPI_OPCODE_READ
337
367
if self ._max_size > 0xFFFF :
@@ -347,7 +377,12 @@ def _read_address(self, address, read_buffer):
347
377
spi .readinto (read_buffer )
348
378
return read_buffer
349
379
350
- def _write (self , start_address , data , wraparound = False ):
380
+ def _write (
381
+ self ,
382
+ start_address : int ,
383
+ data : Union [int , Sequence [int ]],
384
+ wraparound : bool = False ,
385
+ ) -> None :
351
386
buffer = bytearray (4 )
352
387
if not isinstance (data , int ):
353
388
data_length = len (data )
@@ -381,11 +416,11 @@ def _write(self, start_address, data, wraparound=False):
381
416
spi .write (bytearray ([_SPI_OPCODE_WRDI ]))
382
417
383
418
@FRAM .write_protected .setter
384
- def write_protected (self , value ) :
419
+ def write_protected (self , value : bool ) -> None :
385
420
# While it is possible to protect block ranges on the SPI chip,
386
421
# it seems superfluous to do so. So, block protection always protects
387
422
# the entire memory (BP0 and BP1).
388
- if value not in ( True , False ):
423
+ if not isinstance ( value , bool ):
389
424
raise ValueError ("Write protected value must be 'True' or 'False'." )
390
425
self ._wp = value
391
426
write_buffer = bytearray (2 )
0 commit comments