Guest User

Untitled

a guest
Feb 19th, 2026
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | Source Code | 0 0
  1. import smbus2
  2. import time
  3.  
  4. # Configuration
  5. I2C_BUS = 1
  6. EEPROM_ADDR = 0x50  # Default I2C address
  7. EEPROM_SIZE = 32768  # 32KB for AT24C256
  8. BLOCK_SIZE = 32      # Common block size for reading
  9.  
  10. def read_eeprom_to_file(filename):
  11.     bus = smbus2.SMBus(I2C_BUS)
  12.     full_data = []
  13.  
  14.     print(f"Reading {EEPROM_SIZE} bytes from EEPROM at 0x{EEPROM_ADDR:02X}...")
  15.  
  16.     try:
  17.         for addr in range(0, EEPROM_SIZE, BLOCK_SIZE):
  18.             # Write 16-bit address to read from (MSB then LSB)
  19.             msb = (addr >> 8) & 0xFF
  20.             lsb = addr & 0xFF
  21.            
  22.             # Send the address pointer
  23.             write = smbus2.i2c_msg.write(EEPROM_ADDR, [msb, lsb])
  24.             # Read the block of data
  25.             read = smbus2.i2c_msg.read(EEPROM_ADDR, BLOCK_SIZE)
  26.            
  27.             bus.i2c_rdwr(write, read)
  28.             full_data.extend(list(read))
  29.  
  30.             # Optional: Progress indicator
  31.             if addr % 4096 == 0:
  32.                 print(f"Progress: {addr}/{EEPROM_SIZE} bytes")
  33.  
  34.         # Save to file
  35.         with open(filename, 'wb') as f:
  36.             f.write(bytearray(full_data))
  37.        
  38.         print(f"Success! Data saved to {filename}")
  39.  
  40.     except Exception as e:
  41.         print(f"Error reading EEPROM: {e}")
  42.     finally:
  43.         bus.close()
  44.  
  45. if __name__ == "__main__":
  46.     read_eeprom_to_file("output.p8")
  47.  
Advertisement
Add Comment
Please, Sign In to add comment