Skip to content

Commit 208b308

Browse files
committed
Add support for I2C EEPROMs with page sizes other than 128 in eeprom_i2c.py.
1 parent 98ebb3c commit 208b308

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@
1616
T24C64 = const(8192) # 8KiB 64Kbits
1717
T24C32 = const(4096) # 4KiB 32Kbits
1818

19+
_PAGE_SIZES = {
20+
T24C32: const(0x20),
21+
T24C64: const(0x20),
22+
T24C128: const(0x40),
23+
T24C256: const(0x40),
24+
T24C512: const(0x80),
25+
}
26+
1927
# Logical EEPROM device consists of 1-8 physical chips. Chips must all be the
2028
# same size, and must have contiguous addresses.
2129
class EEPROM(BlockDevice):
22-
def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADDR, max_chips_count=_MAX_CHIPS_COUNT):
30+
def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADDR, max_chips_count=_MAX_CHIPS_COUNT, page_size=None):
2331
self._i2c = i2c
2432
if chip_size not in (T24C32, T24C64, T24C128, T24C256, T24C512):
2533
print("Warning: possible unsupported chip. Size:", chip_size)
@@ -29,6 +37,16 @@ def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADD
2937
self._i2c_addr = 0 # I2C address of current chip
3038
self._buf1 = bytearray(1)
3139
self._addrbuf = bytearray(2) # Memory offset into current chip
40+
if page_size is None:
41+
if chip_size not in _PAGE_SIZES:
42+
raise ValueError(
43+
f'The page size for chip size {chip_size} is not known.'
44+
'Please specify it in the parameter page_size')
45+
self._page_mask = ~(_PAGE_SIZES[chip_size] - 1)
46+
self._page_size = _PAGE_SIZES[chip_size]
47+
else:
48+
self._page_mask = ~(page_size - 1)
49+
self._page_size = page_size
3250

3351
# Check for a valid hardware configuration
3452
def scan(self, verbose, chip_size, addr, max_chips_count):
@@ -67,7 +85,7 @@ def _getaddr(self, addr, nbytes): # Set up _addrbuf and _i2c_addr
6785
self._addrbuf[0] = (la >> 8) & 0xFF
6886
self._addrbuf[1] = la & 0xFF
6987
self._i2c_addr = self._min_chip_address + ca
70-
pe = (addr & ~0x7F) + 0x80 # byte 0 of next page
88+
pe = (addr & self._page_mask) + self._page_size # byte 0 of next page
7189
return min(nbytes, pe - la)
7290

7391
# Read or write multiple bytes at an arbitrary address

0 commit comments

Comments
 (0)