diff --git a/Python/components/eepromat24c_comp.py b/Python/components/eepromat24c_comp.py new file mode 100644 index 0000000000000000000000000000000000000000..fba3fc3b45cfdab75b828ac6a7e9fbd5c7520ba6 --- /dev/null +++ b/Python/components/eepromat24c_comp.py @@ -0,0 +1,143 @@ +""" +Management of AT24C0XC EEPROMs + +17/01/2018:EEPROM AT24C0XC +This code is running on board V2 +""" +# -*- coding: iso-8859-1 -*- + +import time +from lli import i2c + +class EepromAT24C(object): + """ Class to manage the AT24C0XC EEPROM. + """ + + MAX_CHARS = 1000/8 # 1K + + def __init__(self, dev, bus, add=0): + self.dev = dev + self.bus = bus + self.add = add + + def init_mem(self, car='\0'): + """Init the full memory bytes with the NULL value + """ + for pos in range(self.MAX_CHARS): + self.write_single(pos, ord(car)) + for pos in range(self.MAX_CHARS): + if self.read_single(pos) != ord(car): + print ("ERROR init : pos ", pos, " cur ", self.read_single(pos), " is not ", ord(car)) + print ("init mem done") + + def read_single(self, reg): + """ Read at the position reg [] in byte in the eeprom + """ + res = None + res = i2c.read(self.dev, self.bus, self.add, reg) + time.sleep(0.01) + return res[1] + + def write_single(self, reg, val): + """ Write value val at the position reg in byte in the eeprom + """ + res = None + res = i2c.write(self.dev, self.bus, self.add, reg, val) + time.sleep(0.01) + return res[1] + + def read_string(self, reg, car='\0'): + """ Read a string terminated by the car "car" starting at position reg + returns a the string with the ending car + """ + pos = reg + strg = "" + while True: + cur = self.read_single(pos) + strg = chr(cur) if strg is None else strg + chr(cur) + if (cur == ord(car)) or (len(strg) >= (self.MAX_CHARS - reg)): + break + pos = pos + 1 + # necessary when several read in sequence are done to prevent loss! + time.sleep(0.01) + return strg + + def write_string(self, reg, txt): + """Write the string txt in the eeprom starting at position reg + """ + pos = reg + for cur in txt: + self.write_single(pos, ord(cur)) + if self.read_single(pos) != ord(cur): + print ("Error write at pos ", pos) + if pos >= (self.MAX_CHARS): + break + pos = pos + 1 + time.sleep(0.01) + + def store_identification(self, id_string): + """Write the passed string as the identification code into the eeprom + """ + self.write_string(0, id_string+'\0') + + def read_identification(self): + """Read the identification string from the eeprom + """ + val = self.read_string(0, car='}') + + # protection against \xff\xff\.... + if len(val) > 0 and val[0] == "\xff": + val = "?"*len(val) + + return val + + +def do_test(dev, bus, add): + """Test accesses to the mezzanine and board eeproms + """ + dum_code = "OPQRSTU9876543210" + + mezz = EepromAT24C(dev, bus, add) + + #mezz.init_mem() + print (mezz.read_string(0)) # check meme is full of NULL chars + + #mezz.write_string(0, dum_code) + print (mezz.read_string(0, car="U")) + print (mezz.read_string(0)) + print (mezz.read_string(7, car="2")) + + board_id = "p40_fv21prXXX" + + mezz.store_identification(board_id) + print (mezz.read_identification()) + + print ("test done") + +def do_debug(dev): + import addresses_comp as const + mezz = EepromAT24C(dev, const.EEPROM_FPGA_BUS, const.EEPROM_U64_ADD) + print (mezz.read_identification()) + +def main(): + """Main test script + """ + import addresses_comp as const + + # do_test(0, const.EEPROM_FPGA_BUS, const.EEPROM_U64_ADD) # main board + # do_test(0, const.LTC2990_PWR_BUS, const.EEPROM_U19_ADD) # main board + + for dev in (0, 2, 4, 6, 8, 10, 12, 14): + print "Board in logical slot ", dev + board = EepromAT24C(dev, const.EEPROM_FPGA_BUS, const.EEPROM_U64_ADD) + print board.read_identification() + mezz = EepromAT24C(dev, const.LTC2990_PWR_BUS, const.EEPROM_U19_ADD) + print mezz.read_identification() + print + +if __name__ == "__main__": + main() + print("board 0") + do_debug(0) + print("board 2") + do_debug(2)