1616T24C64 = const (8192 ) # 8KiB 64Kbits
1717T24C32 = 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.
2129class 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