micropython_eeprom/eeprom/i2c/eep_i2c.py

153 wiersze
4.4 KiB
Python
Czysty Zwykły widok Historia

# eep_i2c.py MicroPython test program for Microchip I2C EEPROM devices.
2019-12-11 09:23:17 +00:00
# Released under the MIT License (MIT). See LICENSE.
# Copyright (c) 2019 Peter Hinch
import uos
import time
2019-12-11 09:23:17 +00:00
from machine import I2C, Pin
from eeprom_i2c import EEPROM, T24C512
2019-12-11 09:23:17 +00:00
# Return an EEPROM array. Adapt for platforms other than Pyboard or chips
# smaller than 64KiB.
def get_eep():
2022-09-22 09:04:29 +00:00
if uos.uname().machine.split(" ")[0][:4] == "PYBD":
2019-12-11 09:23:17 +00:00
Pin.board.EN_3V3.value(1)
time.sleep(0.1) # Allow decouplers to charge
2019-12-11 09:23:17 +00:00
eep = EEPROM(I2C(2), T24C512)
2022-09-22 09:04:29 +00:00
print("Instantiated EEPROM")
2019-12-11 09:23:17 +00:00
return eep
2022-09-22 09:04:29 +00:00
2019-12-11 09:23:17 +00:00
# Dumb file copy utility to help with managing EEPROM contents at the REPL.
def cp(source, dest):
2022-09-22 09:04:29 +00:00
if dest.endswith("/"): # minimal way to allow
dest = "".join((dest, source.split("/")[-1])) # cp /sd/file /eeprom/
with open(source, "rb") as infile: # Caller should handle any OSError
with open(dest, "wb") as outfile: # e.g file not found
2019-12-11 09:23:17 +00:00
while True:
buf = infile.read(100)
outfile.write(buf)
if len(buf) < 100:
break
2022-09-22 09:04:29 +00:00
2019-12-17 17:10:43 +00:00
# ***** TEST OF DRIVER *****
def _testblock(eep, bs):
2022-09-22 09:04:29 +00:00
d0 = b"this >"
d1 = b"<is the boundary"
2019-12-17 17:10:43 +00:00
d2 = d0 + d1
2022-09-22 09:04:29 +00:00
garbage = b"xxxxxxxxxxxxxxxxxxx"
2019-12-17 17:10:43 +00:00
start = bs - len(d0)
end = start + len(garbage)
2022-09-22 09:04:29 +00:00
eep[start:end] = garbage
res = eep[start:end]
2019-12-17 17:10:43 +00:00
if res != garbage:
2022-09-22 09:04:29 +00:00
return "Block test fail 1:" + str(list(res))
2019-12-17 17:10:43 +00:00
end = start + len(d0)
2022-09-22 09:04:29 +00:00
eep[start:end] = d0
2019-12-17 17:10:43 +00:00
end = start + len(garbage)
2022-09-22 09:04:29 +00:00
res = eep[start:end]
if res != b"this >xxxxxxxxxxxxx":
return "Block test fail 2:" + str(list(res))
2019-12-17 17:10:43 +00:00
start = bs
end = bs + len(d1)
2022-09-22 09:04:29 +00:00
eep[start:end] = d1
2019-12-17 17:10:43 +00:00
start = bs - len(d0)
end = start + len(d2)
2022-09-22 09:04:29 +00:00
res = eep[start:end]
2019-12-17 17:10:43 +00:00
if res != d2:
2022-09-22 09:04:29 +00:00
return "Block test fail 3:" + str(list(res))
2019-12-17 17:10:43 +00:00
2019-12-11 09:23:17 +00:00
def test():
eep = get_eep()
sa = 1000
for v in range(256):
eep[sa + v] = v
for v in range(256):
if eep[sa + v] != v:
2022-09-22 09:04:29 +00:00
print(
"Fail at address {} data {} should be {}".format(sa + v, eep[sa + v], v)
)
2019-12-11 09:23:17 +00:00
break
else:
2022-09-22 09:04:29 +00:00
print("Test of byte addressing passed")
2019-12-11 09:23:17 +00:00
data = uos.urandom(30)
sa = 2000
2022-09-22 09:04:29 +00:00
eep[sa : sa + 30] = data
if eep[sa : sa + 30] == data:
print("Test of slice readback passed")
2019-12-11 09:23:17 +00:00
2019-12-17 17:10:43 +00:00
block = 256
res = _testblock(eep, block)
if res is None:
2022-09-22 09:04:29 +00:00
print("Test block boundary {} passed".format(block))
2019-12-17 17:10:43 +00:00
else:
2022-09-22 09:04:29 +00:00
print("Test block boundary {} fail".format(block))
2019-12-17 17:10:43 +00:00
print(res)
block = eep._c_bytes
if eep._a_bytes > block:
res = _testblock(eep, block)
if res is None:
2022-09-22 09:04:29 +00:00
print("Test chip boundary {} passed".format(block))
2019-12-17 17:10:43 +00:00
else:
2022-09-22 09:04:29 +00:00
print("Test chip boundary {} fail".format(block))
2019-12-17 17:10:43 +00:00
print(res)
else:
2022-09-22 09:04:29 +00:00
print("Test chip boundary skipped: only one chip!")
2019-12-17 17:10:43 +00:00
# ***** TEST OF FILESYSTEM MOUNT *****
2019-12-11 09:23:17 +00:00
def fstest(format=False):
eep = get_eep()
try:
2022-09-22 09:04:29 +00:00
uos.umount("/eeprom")
except OSError:
pass
# ***** CODE FOR FATFS *****
2022-09-22 09:04:29 +00:00
# if format:
# os.VfsFat.mkfs(eep)
# ***** CODE FOR LITTLEFS *****
2019-12-11 09:23:17 +00:00
if format:
uos.VfsLfs2.mkfs(eep)
# General
2019-12-11 09:23:17 +00:00
try:
2022-09-22 09:04:29 +00:00
uos.mount(eep, "/eeprom")
except OSError:
raise OSError("Can't mount device: have you formatted it?")
2022-09-22 09:04:29 +00:00
print('Contents of "/": {}'.format(uos.listdir("/")))
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
print(uos.statvfs("/eeprom"))
2019-12-11 09:23:17 +00:00
def cptest(): # Assumes pre-existing filesystem of either type
2019-12-17 17:10:43 +00:00
eep = get_eep()
2022-09-22 09:04:29 +00:00
if "eeprom" in uos.listdir("/"):
print("Device already mounted.")
2019-12-17 17:10:43 +00:00
else:
try:
2022-09-22 09:04:29 +00:00
uos.mount(eep, "/eeprom")
2019-12-17 17:10:43 +00:00
except OSError:
2022-09-22 09:04:29 +00:00
print("Fail mounting device. Have you formatted it?")
2019-12-17 17:10:43 +00:00
return
2022-09-22 09:04:29 +00:00
print("Mounted device.")
cp("eep_i2c.py", "/eeprom/")
cp("eeprom_i2c.py", "/eeprom/")
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
print(uos.statvfs("/eeprom"))
2019-12-17 17:10:43 +00:00
# ***** TEST OF HARDWARE *****
2019-12-11 09:23:17 +00:00
def full_test():
eep = get_eep()
page = 0
for sa in range(0, len(eep), 128):
data = uos.urandom(128)
2022-09-22 09:04:29 +00:00
eep[sa : sa + 128] = data
if eep[sa : sa + 128] == data:
print("Page {} passed".format(page))
2019-12-11 09:23:17 +00:00
else:
2022-09-22 09:04:29 +00:00
print("Page {} readback failed.".format(page))
2019-12-11 09:23:17 +00:00
page += 1