Skip to content

Commit b5e9bcb

Browse files
authored
Merge pull request peterhinch#22 from adeuring/master
Add support for I2C EEPROMs with page sizes other than 128 in eeprom_…
2 parents 26f2726 + 6399f9f commit b5e9bcb

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

eeprom/i2c/I2C.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Arguments:
132132
is `2**block_size` so is 512 bytes by default.
133133
5. `addr` override base address for first chip
134134
6. `max_chips_count` override max_chips_count
135+
7. `page_size=7` The binary logarithm of the page size of the EEPROMs, i.e., the page size in bytes is `2 ** page_size`. The page size may vary between chips from different manufacturers even for the same storage size. Note that specifying a too large value will most likely lead to data corruption in write operations. Look up the correct value for your setup in the chip's datasheet.
135136

136137
With `addr` and `max_chips_count` override, it's possible to make multiple
137138
configuration

eeprom/i2c/eep_i2c.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ def cptest(eep=None): # Assumes pre-existing filesystem of either type
132132
print("Fail mounting device. Have you formatted it?")
133133
return
134134
print("Mounted device.")
135-
cp("eep_i2c.py", "/eeprom/")
136-
cp("eeprom_i2c.py", "/eeprom/")
135+
cp(__file__, "/eeprom/")
136+
# We may have the source file or a precompiled binary (*.mpy)
137+
suffix = __file__[__file__.rfind('.'):]
138+
cp("eeprom_i2c" + suffix, "/eeprom/")
137139
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
138140
print(uos.statvfs("/eeprom"))
139141

eeprom/i2c/eeprom_i2c.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Logical EEPROM device consists of 1-8 physical chips. Chips must all be the
2020
# same size, and must have contiguous addresses.
2121
class EEPROM(BlockDevice):
22-
def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADDR, max_chips_count=_MAX_CHIPS_COUNT):
22+
def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADDR, max_chips_count=_MAX_CHIPS_COUNT, page_size=7):
2323
self._i2c = i2c
2424
if chip_size not in (T24C32, T24C64, T24C128, T24C256, T24C512):
2525
print("Warning: possible unsupported chip. Size:", chip_size)
@@ -29,6 +29,8 @@ def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADD
2929
self._i2c_addr = 0 # I2C address of current chip
3030
self._buf1 = bytearray(1)
3131
self._addrbuf = bytearray(2) # Memory offset into current chip
32+
self._page_size = 2 ** page_size
33+
self._page_mask = ~(self._page_size - 1)
3234

3335
# Check for a valid hardware configuration
3436
def scan(self, verbose, chip_size, addr, max_chips_count):
@@ -67,7 +69,7 @@ def _getaddr(self, addr, nbytes): # Set up _addrbuf and _i2c_addr
6769
self._addrbuf[0] = (la >> 8) & 0xFF
6870
self._addrbuf[1] = la & 0xFF
6971
self._i2c_addr = self._min_chip_address + ca
70-
pe = (addr & ~0x7F) + 0x80 # byte 0 of next page
72+
pe = (addr & self._page_mask) + self._page_size # byte 0 of next page
7173
return min(nbytes, pe - la)
7274

7375
# Read or write multiple bytes at an arbitrary address

0 commit comments

Comments
 (0)