Skip to content

Commit ebe73ce

Browse files
committed
fix for larger devices
1 parent ec83577 commit ebe73ce

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

adafruit_fram.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
__version__ = "0.0.0-auto.0"
5252
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FRAM.git"
5353

54-
_MAX_SIZE_I2C = const(32768)
55-
_MAX_SIZE_SPI = const(8192)
54+
_MAX_SIZE_I2C = const(0x8000)
55+
_MAX_SIZE_SPI = const(0x2000)
5656

5757
_I2C_MANF_ID = const(0x0A)
5858
_I2C_PROD_ID = const(0x510)
@@ -310,7 +310,8 @@ class FRAM_SPI(FRAM):
310310

311311
# pylint: disable=too-many-arguments,too-many-locals
312312
def __init__(
313-
self, spi_bus, spi_cs, write_protect=False, wp_pin=None, baudrate=100000
313+
self, spi_bus, spi_cs, write_protect=False, wp_pin=None, baudrate=100000,
314+
max_size=_MAX_SIZE_SPI
314315
):
315316
from adafruit_bus_device.spi_device import ( # pylint: disable=import-outside-toplevel
316317
SPIDevice as spidev,
@@ -327,20 +328,26 @@ def __init__(
327328
raise OSError("FRAM SPI device not found.")
328329

329330
self._spi = _spi
330-
super().__init__(_MAX_SIZE_SPI, write_protect, wp_pin)
331+
super().__init__(max_size, write_protect, wp_pin)
331332

332333
def _read_address(self, address, read_buffer):
333-
write_buffer = bytearray(3)
334+
write_buffer = bytearray(4)
334335
write_buffer[0] = _SPI_OPCODE_READ
335-
write_buffer[1] = address >> 8
336-
write_buffer[2] = address & 0xFF
336+
if self._max_size > 0xFFFF:
337+
write_buffer[1] = (address >> 16) & 0xFF
338+
write_buffer[2] = (address >> 8) & 0xFF
339+
write_buffer[3] = address & 0xFF
340+
else:
341+
write_buffer[1] = (address >> 8) & 0xFF
342+
write_buffer[2] = address & 0xFF
343+
337344
with self._spi as spi:
338345
spi.write(write_buffer)
339346
spi.readinto(read_buffer)
340347
return read_buffer
341348

342349
def _write(self, start_address, data, wraparound=False):
343-
buffer = bytearray(3)
350+
buffer = bytearray(4)
344351
if not isinstance(data, int):
345352
data_length = len(data)
346353
else:
@@ -359,8 +366,13 @@ def _write(self, start_address, data, wraparound=False):
359366
spi.write(bytearray([_SPI_OPCODE_WREN]))
360367
with self._spi as spi:
361368
buffer[0] = _SPI_OPCODE_WRITE
362-
buffer[1] = start_address >> 8
363-
buffer[2] = start_address & 0xFF
369+
if self._max_size > 0xFFFF:
370+
buffer[1] = (start_address >> 16) & 0xFF
371+
buffer[2] = (start_address >> 8) & 0xFF
372+
buffer[3] = start_address & 0xFF
373+
else:
374+
buffer[1] = (start_address >> 8) & 0xFF
375+
buffer[2] = start_address & 0xFF
364376
spi.write(buffer)
365377
for i in range(0, data_length):
366378
spi.write(bytearray([data[i]]))

0 commit comments

Comments
 (0)