Guest User

Untitled

a guest
Feb 19th, 2026
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | Source Code | 0 0
  1. import smbus2
  2. import time
  3. import os
  4.  
  5. EEPROM_ADDRESS = 0x50
  6. I2C_BUS = 1
  7. PAGE_SIZE = 64 # AT24C256 page size is 64 bytes
  8. EEPROM_SIZE = 32768 # Total size in bytes
  9.  
  10. def write_page(bus, eeprom_addr, memory_addr, data):
  11.     """Writes a page of data to the EEPROM."""
  12.     # The address is 16-bit for AT24C256, so it needs two bytes (MSB, LSB)
  13.     write_buffer = memory_addr.to_bytes(2, 'big') + data
  14.     msg = smbus2.i2c_msg.write(eeprom_addr, write_buffer)
  15.     bus.i2c_rdwr(msg)
  16.     time.sleep(0.005) # ~5ms delay for write cycle completion
  17.  
  18. def write_file_to_eeprom(filename):
  19.     """Reads a file and writes its content to the EEPROM."""
  20.     try:
  21.         with open(filename, 'rb') as f:
  22.             file_data = f.read()
  23.     except IOError as e:
  24.         print(f"Error reading file: {e}")
  25.         return
  26.  
  27.     if len(file_data) > EEPROM_SIZE:
  28.         print(f"File size ({len(file_data)} bytes) exceeds EEPROM size ({EEPROM_SIZE} bytes).")
  29.         return
  30.  
  31.     try:
  32.         with smbus2.SMBus(I2C_BUS) as bus:
  33.             current_addr = 0
  34.             for i in range(0, len(file_data), PAGE_SIZE):
  35.                 chunk = file_data[i:i + PAGE_SIZE]
  36.                 write_page(bus, EEPROM_ADDRESS, current_addr, chunk)
  37.                 current_addr += len(chunk)
  38.                 print(f"Written {current_addr} bytes...")
  39.             print(f"Successfully wrote {len(file_data)} bytes to EEPROM.")
  40.     except Exception as e:
  41.         print(f"I2C communication error: {e}")
  42.  
  43. if __name__ == "__main__":
  44.     file_to_write = "game.p8.rom" # Replace with your file path
  45.     write_file_to_eeprom(file_to_write)
  46.  
Advertisement
Add Comment
Please, Sign In to add comment