-
Notifications
You must be signed in to change notification settings - Fork 7
Fixed size when polling length of FRAM to reflect actual size #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7b483a6
Fixed length to show sizze rather than highest index
6eaae80
Changed max_size to actual max size. Updated all messages and examples.
238bfe3
Changed max_size to actual max size. Updated all messages and examples.
9b9e2f0
Fixed Travis Error
2c06cbc
Added suggestions by @danh. Fixed off by one error on slicing.
3aa23f3
Fixed Travis Errors
cae01f6
Changed many uses of register to address
f54ae85
Fixed Travis Error
5365bea
Fixed Zero Address Check
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,8 +51,8 @@ | |
__version__ = "0.0.0-auto.0" | ||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FRAM.git" | ||
|
||
_MAX_SIZE_I2C = const(32767) | ||
_MAX_SIZE_SPI = const(8191) | ||
_MAX_SIZE_I2C = const(32768) | ||
_MAX_SIZE_SPI = const(8192) | ||
|
||
_I2C_MANF_ID = const(0x0A) | ||
_I2C_PROD_ID = const(0x510) | ||
|
@@ -80,7 +80,7 @@ def __init__(self, max_size, write_protect=False, wp_pin=None): | |
@property | ||
def write_wraparound(self): | ||
""" Determines if sequential writes will wrapaound highest memory address | ||
(``len(FRAM)``) address. If ``False``, and a requested write will | ||
(``len(FRAM) - 1``) address. If ``False``, and a requested write will | ||
extend beyond the maximum size, an exception is raised. | ||
""" | ||
return self._wraparound | ||
|
@@ -106,18 +106,18 @@ def write_protected(self): | |
return self._wp if self._wp_pin is None else self._wp_pin.value | ||
|
||
def __len__(self): | ||
""" The maximum size of the current FRAM chip. This is the highest | ||
""" The size of the current FRAM chip. This is one more than the highest | ||
register location that can be read or written to. | ||
|
||
.. code-block:: python | ||
|
||
fram = adafruit_fram.FRAM_xxx() # xxx = 'I2C' or 'SPI' | ||
|
||
# maximum size returned by len() | ||
# size returned by len() | ||
len(fram) | ||
|
||
# can be used with range | ||
for i in range(0, len(fram)) | ||
for i in range(0, len(fram) - 1) | ||
makermelissa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
return self._max_size | ||
|
||
|
@@ -134,9 +134,9 @@ def __getitem__(self, key): | |
fram[0:9] | ||
""" | ||
if isinstance(key, int): | ||
if key > self._max_size: | ||
raise ValueError("Register '{0}' greater than maximum FRAM size." | ||
" ({1})".format(key, self._max_size)) | ||
if key >= self._max_size: | ||
dhalbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise ValueError("Register '{0}' greater than maximum FRAM register." | ||
dhalbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" ({1})".format(key, self._max_size - 1)) | ||
dhalbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
buffer = bytearray(1) | ||
read_buffer = self._read_register(key, buffer) | ||
elif isinstance(key, slice): | ||
|
@@ -146,8 +146,8 @@ def __getitem__(self, key): | |
registers = list(range(key.start if key.start is not None else 0, | ||
key.stop if key.stop is not None else self._max_size)) | ||
if (registers[0] + len(registers)) > self._max_size: | ||
raise ValueError("Register + Length greater than maximum FRAM size." | ||
" ({0})".format(self._max_size)) | ||
raise ValueError("Register + Length greater than maximum FRAM register." | ||
dhalbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" ({0})".format(self._max_size - 1)) | ||
|
||
buffer = bytearray(len(registers)) | ||
read_buffer = self._read_register(registers[0], buffer) | ||
|
@@ -172,10 +172,9 @@ def __setitem__(self, key, value): | |
if not isinstance(value, (int, bytearray, list, tuple)): | ||
raise ValueError("Data must be a single integer, or a bytearray," | ||
" list, or tuple.") | ||
if key > self._max_size: | ||
if key >= self._max_size: | ||
raise ValueError("Requested register '{0}' greater than maximum" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
" FRAM size. ({1})".format(key, | ||
self._max_size)) | ||
" FRAM register. ({1})".format(key, self._max_size - 1)) | ||
|
||
self._write(key, value, self._wraparound) | ||
|
||
|
@@ -237,21 +236,21 @@ def _write(self, start_register, data, wraparound=False): | |
else: | ||
data_length = 1 | ||
data = [data] | ||
if (start_register + data_length) - 1 > self._max_size: | ||
if (start_register + data_length) > self._max_size: | ||
if wraparound: | ||
pass | ||
else: | ||
raise ValueError("Starting register + data length extends beyond" | ||
" FRAM maximum size. Use ``write_wraparound`` to" | ||
" FRAM maximum register. Use ``write_wraparound`` to" | ||
dhalbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" override this warning.") | ||
with self._i2c as i2c: | ||
for i in range(0, data_length): | ||
if not (start_register + i) > self._max_size: | ||
if not (start_register + i) > self._max_size - 1: | ||
buffer[0] = (start_register + i) >> 8 | ||
buffer[1] = (start_register + i) & 0xFF | ||
else: | ||
buffer[0] = ((start_register + i) - self._max_size) >> 8 | ||
buffer[1] = ((start_register + i) - self._max_size) & 0xFF | ||
buffer[0] = ((start_register + i) - self._max_size + 1) >> 8 | ||
buffer[1] = ((start_register + i) - self._max_size + 1) & 0xFF | ||
buffer[2] = data[i] | ||
i2c.write(buffer) | ||
|
||
|
@@ -324,12 +323,12 @@ def _write(self, start_register, data, wraparound=False): | |
else: | ||
data_length = 1 | ||
data = [data] | ||
if (start_register + data_length) - 1 > self._max_size: | ||
if (start_register + data_length) > self._max_size: | ||
if wraparound: | ||
pass | ||
else: | ||
raise ValueError("Starting register + data length extends beyond" | ||
" FRAM maximum size. Use 'wraparound=True' to" | ||
" FRAM maximum register. Use 'wraparound=True' to" | ||
makermelissa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" override this warning.") | ||
with self._spi as spi: | ||
spi.write(bytearray([_SPI_OPCODE_WREN])) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.