kopia lustrzana https://github.com/peterhinch/micropython_eeprom
Add support for I2C EEPROMs with page sizes other than 128 in eeprom_i2c.py.
rodzic
98ebb3c7db
commit
208b3089bb
|
@ -132,8 +132,10 @@ def cptest(eep=None): # Assumes pre-existing filesystem of either type
|
||||||
print("Fail mounting device. Have you formatted it?")
|
print("Fail mounting device. Have you formatted it?")
|
||||||
return
|
return
|
||||||
print("Mounted device.")
|
print("Mounted device.")
|
||||||
cp("eep_i2c.py", "/eeprom/")
|
cp(__file__, "/eeprom/")
|
||||||
cp("eeprom_i2c.py", "/eeprom/")
|
# We may have the source file or a precompiled binary (*.mpy)
|
||||||
|
suffix = __file__[__file__.rfind('.'):]
|
||||||
|
cp("eeprom_i2c" + suffix, "/eeprom/")
|
||||||
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
|
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
|
||||||
print(uos.statvfs("/eeprom"))
|
print(uos.statvfs("/eeprom"))
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,18 @@ T24C128 = const(16384) # 16KiB 128Kbits
|
||||||
T24C64 = const(8192) # 8KiB 64Kbits
|
T24C64 = const(8192) # 8KiB 64Kbits
|
||||||
T24C32 = const(4096) # 4KiB 32Kbits
|
T24C32 = const(4096) # 4KiB 32Kbits
|
||||||
|
|
||||||
|
_PAGE_SIZES = {
|
||||||
|
T24C32: const(0x20),
|
||||||
|
T24C64: const(0x20),
|
||||||
|
T24C128: const(0x40),
|
||||||
|
T24C256: const(0x40),
|
||||||
|
T24C512: const(0x80),
|
||||||
|
}
|
||||||
|
|
||||||
# Logical EEPROM device consists of 1-8 physical chips. Chips must all be the
|
# Logical EEPROM device consists of 1-8 physical chips. Chips must all be the
|
||||||
# same size, and must have contiguous addresses.
|
# same size, and must have contiguous addresses.
|
||||||
class EEPROM(BlockDevice):
|
class EEPROM(BlockDevice):
|
||||||
def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADDR, max_chips_count=_MAX_CHIPS_COUNT):
|
def __init__(self, i2c, chip_size=T24C512, verbose=True, block_size=9, addr=_ADDR, max_chips_count=_MAX_CHIPS_COUNT, page_size=None):
|
||||||
self._i2c = i2c
|
self._i2c = i2c
|
||||||
if chip_size not in (T24C32, T24C64, T24C128, T24C256, T24C512):
|
if chip_size not in (T24C32, T24C64, T24C128, T24C256, T24C512):
|
||||||
print("Warning: possible unsupported chip. Size:", chip_size)
|
print("Warning: possible unsupported chip. Size:", chip_size)
|
||||||
|
@ -29,6 +37,16 @@ class EEPROM(BlockDevice):
|
||||||
self._i2c_addr = 0 # I2C address of current chip
|
self._i2c_addr = 0 # I2C address of current chip
|
||||||
self._buf1 = bytearray(1)
|
self._buf1 = bytearray(1)
|
||||||
self._addrbuf = bytearray(2) # Memory offset into current chip
|
self._addrbuf = bytearray(2) # Memory offset into current chip
|
||||||
|
if page_size is None:
|
||||||
|
if chip_size not in _PAGE_SIZES:
|
||||||
|
raise ValueError(
|
||||||
|
f'The page size for chip size {chip_size} is not known.'
|
||||||
|
'Please specify it in the parameter page_size')
|
||||||
|
self._page_mask = ~(_PAGE_SIZES[chip_size] - 1)
|
||||||
|
self._page_size = _PAGE_SIZES[chip_size]
|
||||||
|
else:
|
||||||
|
self._page_mask = ~(page_size - 1)
|
||||||
|
self._page_size = page_size
|
||||||
|
|
||||||
# Check for a valid hardware configuration
|
# Check for a valid hardware configuration
|
||||||
def scan(self, verbose, chip_size, addr, max_chips_count):
|
def scan(self, verbose, chip_size, addr, max_chips_count):
|
||||||
|
@ -67,7 +85,7 @@ class EEPROM(BlockDevice):
|
||||||
self._addrbuf[0] = (la >> 8) & 0xFF
|
self._addrbuf[0] = (la >> 8) & 0xFF
|
||||||
self._addrbuf[1] = la & 0xFF
|
self._addrbuf[1] = la & 0xFF
|
||||||
self._i2c_addr = self._min_chip_address + ca
|
self._i2c_addr = self._min_chip_address + ca
|
||||||
pe = (addr & ~0x7F) + 0x80 # byte 0 of next page
|
pe = (addr & self._page_mask) + self._page_size # byte 0 of next page
|
||||||
return min(nbytes, pe - la)
|
return min(nbytes, pe - la)
|
||||||
|
|
||||||
# Read or write multiple bytes at an arbitrary address
|
# Read or write multiple bytes at an arbitrary address
|
||||||
|
|
Ładowanie…
Reference in New Issue