Commit bd4f2624 authored by Matthieu Cattin's avatar Matthieu Cattin

Change API of the eeprom_24aa64 module.

    * read and write byte functions.
    * page write function with size and page boundary check.
    * sequencial read function.

Apply changes made by Richard to the eeprom_24aa64 module.

    Changes made by Richard Carrillo (7solutions) for the fmcdio5chttla.
        * Proper exception raising and handling.
        * Avoiding infinite loop by adding timeouts.

Some cleanup in the comments and commented debug messages.
parent 1fd065f7
#!/usr/bin/python
# Copyright CERN, 2011
# Author: Matthieu Cattin (CERN)
# Author (modifications): Richard Carrillo <rcarrillo(AT)sevensols.com>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Website: http://www.sevensols.com
# Last modifications: 26/4/2012
# Import standard modules
import sys
import rr
import time
# Import specific modules
import rr
import i2c
# Class to access the 24AA64 (64K EEPROM) chip.
# It uses the I2C class.
class C24AA64:
PAGE_SIZE = 32 # in bytes
PAGE_BOUNDARY_MASK = 0xFFFF - (PAGE_SIZE - 1)
def __init__(self, i2c, i2c_addr):
self.i2c = i2c
self.i2c_addr = i2c_addr
def wr_data(self, mem_addr, data):
if len(data) > 32:
print "Maximum sequence write size is 32 byte!"
return -1;
def wr_byte(self, mem_addr, byte):
self.i2c.start(self.i2c_addr, True)
self.i2c.write((mem_addr >> 8), False)
self.i2c.write((mem_addr & 0xFF), False)
#print('24AA64:write: data lenght=%d')%(len(data))
self.i2c.write(byte,True)
def rd_byte(self, mem_addr):
self.i2c.start(self.i2c_addr, True)
self.i2c.write((mem_addr >> 8), False)
self.i2c.write((mem_addr & 0xFF), False)
self.i2c.start(self.i2c_addr, False)
return self.i2c.read(True)
def wr_page(self, mem_addr, data):
#print '[24AA64] write data lenght=%d' % (len(data))
#print '[24AA64] write addr=%04X' % (mem_addr)
if(len(data) == 0):
raise i2c.I2CDeviceOperationError("Nothing to transmit, data size is 0!")
if(len(data) > self.PAGE_SIZE):
raise i2c.I2CDeviceOperationError("Maximum write size is " + str(self.PAGE_SIZE)+" byte!")
if((mem_addr | self.PAGE_BOUNDARY_MASK) ^ self.PAGE_BOUNDARY_MASK):
raise i2c.I2CDeviceOperationError("Start write address is not aligned to a page boundary!")
self.i2c.start(self.i2c_addr, True)
self.i2c.write((mem_addr >> 8), False)
self.i2c.write((mem_addr & 0xFF), False)
#print '[24AA64] write data lenght=%d' % (len(data))
i = 0
for i in range(len(data)-1):
#print('24AA64:write: i=%d')%(i)
#print '[24AA64] write i=%d' % (i)
self.i2c.write(data[i],False)
if len(data) > 1:
i += 1
else:
i = 0
#print('24AA64:write:last i=%d')%(i)
#print '[24AA64] write last i=%d' % (i)
self.i2c.write(data[i],True)
return 0;
def rd_data(self, mem_addr, size):
def rd_seq(self, mem_addr, size):
self.i2c.start(self.i2c_addr, True)
self.i2c.write((mem_addr >> 8), False)
self.i2c.write((mem_addr & 0xFF), False)
self.i2c.start(self.i2c_addr, False)
data = []
#print('24AA64:read: data lenght=%d')%(size)
#print '[24AA64] read data lenght=%d' % (size)
i=0
for i in range(size-1):
data.append(self.i2c.read(False))
#print('24AA64:read: i=%d')%(i)
#print('24AA64:read:last i=%d')%(i)
#print '[24AA64] read i=%d' % (i)
if len(data) > 1:
i += 1
#print '[24AA64] read last i=%d' % (i)
data.append(self.i2c.read(True))
return data;
return data
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment