Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smbus2
- import time
- # Configuration
- I2C_BUS = 1
- EEPROM_ADDR = 0x50 # Default I2C address
- EEPROM_SIZE = 32768 # 32KB for AT24C256
- BLOCK_SIZE = 32 # Common block size for reading
- def read_eeprom_to_file(filename):
- bus = smbus2.SMBus(I2C_BUS)
- full_data = []
- print(f"Reading {EEPROM_SIZE} bytes from EEPROM at 0x{EEPROM_ADDR:02X}...")
- try:
- for addr in range(0, EEPROM_SIZE, BLOCK_SIZE):
- # Write 16-bit address to read from (MSB then LSB)
- msb = (addr >> 8) & 0xFF
- lsb = addr & 0xFF
- # Send the address pointer
- write = smbus2.i2c_msg.write(EEPROM_ADDR, [msb, lsb])
- # Read the block of data
- read = smbus2.i2c_msg.read(EEPROM_ADDR, BLOCK_SIZE)
- bus.i2c_rdwr(write, read)
- full_data.extend(list(read))
- # Optional: Progress indicator
- if addr % 4096 == 0:
- print(f"Progress: {addr}/{EEPROM_SIZE} bytes")
- # Save to file
- with open(filename, 'wb') as f:
- f.write(bytearray(full_data))
- print(f"Success! Data saved to {filename}")
- except Exception as e:
- print(f"Error reading EEPROM: {e}")
- finally:
- bus.close()
- if __name__ == "__main__":
- read_eeprom_to_file("output.p8")
Advertisement
Add Comment
Please, Sign In to add comment