Skip to content

Commit ddac32d

Browse files
authored
Merge pull request #20 from jerryneedell/jerryn_bigfram
fix addressing for larger SPI parts
2 parents ec83577 + f80c71c commit ddac32d

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

adafruit_fram.py

Lines changed: 27 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,13 @@ 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,
314+
spi_bus,
315+
spi_cs,
316+
write_protect=False,
317+
wp_pin=None,
318+
baudrate=100000,
319+
max_size=_MAX_SIZE_SPI,
314320
):
315321
from adafruit_bus_device.spi_device import ( # pylint: disable=import-outside-toplevel
316322
SPIDevice as spidev,
@@ -327,20 +333,26 @@ def __init__(
327333
raise OSError("FRAM SPI device not found.")
328334

329335
self._spi = _spi
330-
super().__init__(_MAX_SIZE_SPI, write_protect, wp_pin)
336+
super().__init__(max_size, write_protect, wp_pin)
331337

332338
def _read_address(self, address, read_buffer):
333-
write_buffer = bytearray(3)
339+
write_buffer = bytearray(4)
334340
write_buffer[0] = _SPI_OPCODE_READ
335-
write_buffer[1] = address >> 8
336-
write_buffer[2] = address & 0xFF
341+
if self._max_size > 0xFFFF:
342+
write_buffer[1] = (address >> 16) & 0xFF
343+
write_buffer[2] = (address >> 8) & 0xFF
344+
write_buffer[3] = address & 0xFF
345+
else:
346+
write_buffer[1] = (address >> 8) & 0xFF
347+
write_buffer[2] = address & 0xFF
348+
337349
with self._spi as spi:
338350
spi.write(write_buffer)
339351
spi.readinto(read_buffer)
340352
return read_buffer
341353

342354
def _write(self, start_address, data, wraparound=False):
343-
buffer = bytearray(3)
355+
buffer = bytearray(4)
344356
if not isinstance(data, int):
345357
data_length = len(data)
346358
else:
@@ -359,8 +371,13 @@ def _write(self, start_address, data, wraparound=False):
359371
spi.write(bytearray([_SPI_OPCODE_WREN]))
360372
with self._spi as spi:
361373
buffer[0] = _SPI_OPCODE_WRITE
362-
buffer[1] = start_address >> 8
363-
buffer[2] = start_address & 0xFF
374+
if self._max_size > 0xFFFF:
375+
buffer[1] = (start_address >> 16) & 0xFF
376+
buffer[2] = (start_address >> 8) & 0xFF
377+
buffer[3] = start_address & 0xFF
378+
else:
379+
buffer[1] = (start_address >> 8) & 0xFF
380+
buffer[2] = start_address & 0xFF
364381
spi.write(buffer)
365382
for i in range(0, data_length):
366383
spi.write(bytearray([data[i]]))

0 commit comments

Comments
 (0)