Skip to content

Commit

Permalink
Fixed the EEPROM header writing and reading
Browse files Browse the repository at this point in the history
* Fixes a bug where addresses 0x58-0x5F were being assumed to be
EEPROMs but were not contiguous.
* Fixes some bad assumptions on address length in the writing tools
* Adds templates for the two types of EEPROM officially supported
  • Loading branch information
hairymnstr committed Jun 13, 2024
1 parent a02ccf8 commit baad6b2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
27 changes: 19 additions & 8 deletions modules/scripts/prepare_eeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,31 @@
print(f"Detected eeprom at {hex(addr)}")

# Fill in your desired header info here:
header = HexpansionHeader(
# use this one for the M24C16:
header_m24c16 = HexpansionHeader(
manifest_version="2024",
fs_offset=32,
eeprom_page_size=16,
eeprom_total_size=2048,
vid=0xCAFE,
pid=0xD15C,
eeprom_total_size=1024 * (16 // 8),
vid=0xCA75,
pid=0x1337,
unique_id=0,
friendly_name="M24C16"
)
# use this template for the ZD24C64A
header_zd24c64 = HexpansionHeader(
manifest_version="2024",
fs_offset=32,
eeprom_page_size=32,
eeprom_total_size=1024 * (64 // 8),
vid=0xCA75,
pid=0x1337,
unique_id=0x0,
friendly_name="Flopagon",
friendly_name="ZD24C64A",
)

# Determine amount of bytes in internal address
#addr_len = 2 if header.eeprom_total_size > 256 else 1
#print(f"Using {addr_len} bytes for eeprom internal address")
# pick which one to use here
header = header_m24c16

# Write and read back header
write_header(
Expand Down
12 changes: 7 additions & 5 deletions modules/system/hexpansion/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ def from_bytes(cls, buf, validate_checksum=True):
def write_header(port, header, addr=0x50, addr_len=2, page_size=32):
i2c = I2C(port)

addr_bytes = [0] * addr_len
i2c.writeto(addr, bytes(addr_bytes))
if addr_len == 2:
addr_pack = ">H"
else:
addr_pack = ">B"

# We can't write more bytes than the page size in one transaction, so chunk the bytes if necessary:
header_bytes = header.to_bytes()
Expand All @@ -107,10 +109,10 @@ def write_header(port, header, addr=0x50, addr_len=2, page_size=32):
]

for idx, chunk in enumerate(header_chunks):
write_addr = bytes([idx * page_size])
print(f"Writing {len(chunk)} bytes at {write_addr}:", chunk)
write_addr = struct.pack(addr_pack, idx * page_size)
print(f"Writing {len(chunk)} bytes at {idx * page_size}:", chunk)

i2c.writeto(addr, bytes([idx * page_size]) + chunk)
i2c.writeto(addr, write_addr + chunk)

# Poll ACK
while True:
Expand Down
1 change: 0 additions & 1 deletion modules/system/hexpansion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def get_hexpansion_block_devices(i2c, header, addr=0x50, addr_len=2):
i2c=i2c,
chip_size=chip_size,
page_size=header.eeprom_page_size,
addr=addr,
block_size=block_size
)
partition = EEPROMPartition(
Expand Down

0 comments on commit baad6b2

Please sign in to comment.