Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smbus2
- import time
- import os
- EEPROM_ADDRESS = 0x50
- I2C_BUS = 1
- PAGE_SIZE = 64 # AT24C256 page size is 64 bytes
- EEPROM_SIZE = 32768 # Total size in bytes
- def write_page(bus, eeprom_addr, memory_addr, data):
- """Writes a page of data to the EEPROM."""
- # The address is 16-bit for AT24C256, so it needs two bytes (MSB, LSB)
- write_buffer = memory_addr.to_bytes(2, 'big') + data
- msg = smbus2.i2c_msg.write(eeprom_addr, write_buffer)
- bus.i2c_rdwr(msg)
- time.sleep(0.005) # ~5ms delay for write cycle completion
- def write_file_to_eeprom(filename):
- """Reads a file and writes its content to the EEPROM."""
- try:
- with open(filename, 'rb') as f:
- file_data = f.read()
- except IOError as e:
- print(f"Error reading file: {e}")
- return
- if len(file_data) > EEPROM_SIZE:
- print(f"File size ({len(file_data)} bytes) exceeds EEPROM size ({EEPROM_SIZE} bytes).")
- return
- try:
- with smbus2.SMBus(I2C_BUS) as bus:
- current_addr = 0
- for i in range(0, len(file_data), PAGE_SIZE):
- chunk = file_data[i:i + PAGE_SIZE]
- write_page(bus, EEPROM_ADDRESS, current_addr, chunk)
- current_addr += len(chunk)
- print(f"Written {current_addr} bytes...")
- print(f"Successfully wrote {len(file_data)} bytes to EEPROM.")
- except Exception as e:
- print(f"I2C communication error: {e}")
- if __name__ == "__main__":
- file_to_write = "game.p8.rom" # Replace with your file path
- write_file_to_eeprom(file_to_write)
Advertisement
Add Comment
Please, Sign In to add comment