kopia lustrzana https://github.com/peterhinch/micropython_eeprom
EEPROM: SPI and I2C full_test now more rigorous.
rodzic
3b2e2530ef
commit
5861e93f48
|
@ -5,20 +5,42 @@
|
||||||
|
|
||||||
import uos
|
import uos
|
||||||
import time
|
import time
|
||||||
from machine import I2C, Pin
|
from machine import I2C, Pin, SoftI2C
|
||||||
from eeprom_i2c import EEPROM, T24C512
|
from eeprom_i2c import EEPROM, T24C512
|
||||||
|
|
||||||
# Return an EEPROM array. Adapt for platforms other than Pyboard or chips
|
# Return an EEPROM array. Adapt for platforms other than Pyboard or chips
|
||||||
# smaller than 64KiB.
|
# smaller than 64KiB.
|
||||||
def get_eep():
|
def get_eep():
|
||||||
|
# Special code for Pyboard D: enable 3.3V output
|
||||||
if uos.uname().machine.split(" ")[0][:4] == "PYBD":
|
if uos.uname().machine.split(" ")[0][:4] == "PYBD":
|
||||||
Pin.board.EN_3V3.value(1)
|
Pin.board.EN_3V3.value(1)
|
||||||
time.sleep(0.1) # Allow decouplers to charge
|
time.sleep(0.1) # Allow decouplers to charge
|
||||||
eep = EEPROM(I2C(2), T24C512)
|
|
||||||
|
if uos.uname().sysname == "esp8266":
|
||||||
|
eep = EEPROM(SoftI2C(scl=Pin(13, Pin.OPEN_DRAIN), sda=Pin(12, Pin.OPEN_DRAIN)), T24C512)
|
||||||
|
else:
|
||||||
|
eep = EEPROM(I2C(2), T24C512) # Pyboard D or 1.x
|
||||||
print("Instantiated EEPROM")
|
print("Instantiated EEPROM")
|
||||||
return eep
|
return eep
|
||||||
|
|
||||||
|
|
||||||
|
# Yield pseudorandom bytes (random module not available on all ports)
|
||||||
|
def psrand8(x=0x3FBA2):
|
||||||
|
while True:
|
||||||
|
x ^= (x & 0x1FFFF) << 13
|
||||||
|
x ^= x >> 17
|
||||||
|
x ^= (x & 0x1FFFFFF) << 5
|
||||||
|
yield x & 0xFF
|
||||||
|
|
||||||
|
|
||||||
|
# Given a source of pseudorandom bytes yield pseudorandom 256 byte buffer.
|
||||||
|
def psrand256(rand, ba=bytearray(256)):
|
||||||
|
while True:
|
||||||
|
for z in range(256):
|
||||||
|
ba[z] = next(rand)
|
||||||
|
yield ba
|
||||||
|
|
||||||
|
|
||||||
# Dumb file copy utility to help with managing EEPROM contents at the REPL.
|
# Dumb file copy utility to help with managing EEPROM contents at the REPL.
|
||||||
def cp(source, dest):
|
def cp(source, dest):
|
||||||
if dest.endswith("/"): # minimal way to allow
|
if dest.endswith("/"): # minimal way to allow
|
||||||
|
@ -144,27 +166,38 @@ def cptest(eep=None): # Assumes pre-existing filesystem of either type
|
||||||
print("Fail mounting device. Have you formatted it?")
|
print("Fail mounting device. Have you formatted it?")
|
||||||
return
|
return
|
||||||
print("Mounted device.")
|
print("Mounted device.")
|
||||||
cp(__file__, "/eeprom/")
|
try:
|
||||||
# We may have the source file or a precompiled binary (*.mpy)
|
cp(__file__, "/eeprom/")
|
||||||
cp(__file__.replace("eep", "eeprom"), "/eeprom/")
|
# We may have the source file or a precompiled binary (*.mpy)
|
||||||
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
|
cp(__file__.replace("eep", "eeprom"), "/eeprom/")
|
||||||
print(uos.statvfs("/eeprom"))
|
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
|
||||||
|
print(uos.statvfs("/eeprom"))
|
||||||
|
except NameError:
|
||||||
|
print("Test cannot be performed by this MicroPython port. Consider using upysh.")
|
||||||
|
|
||||||
|
|
||||||
# ***** TEST OF HARDWARE *****
|
# ***** TEST OF HARDWARE *****
|
||||||
def full_test(eep=None, block_size=256):
|
# Write pseudorandom data to entire array, then read back. Fairly rigorous test.
|
||||||
|
def full_test(eep=None):
|
||||||
eep = eep if eep else get_eep()
|
eep = eep if eep else get_eep()
|
||||||
print(f"Testing with {block_size}byte blocks of random data...")
|
print("Testing with 256 byte blocks of random data...")
|
||||||
block = 0
|
r = psrand8() # Instantiate random byte generator
|
||||||
for sa in range(0, len(eep), block_size):
|
ps = psrand256(r) # Random 256 byte blocks
|
||||||
data = uos.urandom(block_size)
|
for sa in range(0, len(eep), 256):
|
||||||
eep[sa : sa + block_size] = data
|
ea = sa + 256
|
||||||
if eep[sa : sa + block_size] == data:
|
eep[sa:ea] = next(ps)
|
||||||
print(f"Block {block} passed\r", end="")
|
print(f"Address {sa}..{ea} written\r", end="")
|
||||||
else:
|
|
||||||
print(f"Block {block} readback failed.")
|
|
||||||
block += 1
|
|
||||||
print()
|
print()
|
||||||
|
r = psrand8() # Instantiate new random byte generator with same seed
|
||||||
|
ps = psrand256(r) # Random 256 byte blocks
|
||||||
|
for sa in range(0, len(eep), 256):
|
||||||
|
ea = sa + 256
|
||||||
|
if eep[sa:ea] == next(ps):
|
||||||
|
print(f"Address {sa}..{ea} readback passed\r", end="")
|
||||||
|
else:
|
||||||
|
print(f"Address {sa}..{ea} readback failed.")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
def help():
|
def help():
|
||||||
st = """Available commands:
|
st = """Available commands:
|
||||||
|
@ -180,4 +213,5 @@ def help():
|
||||||
"""
|
"""
|
||||||
print(st)
|
print(st)
|
||||||
|
|
||||||
|
|
||||||
help()
|
help()
|
||||||
|
|
|
@ -1,29 +1,59 @@
|
||||||
# eep_spi.py MicroPython test program for Microchip SPI EEPROM devices.
|
# eep_spi.py MicroPython test program for Microchip SPI EEPROM devices.
|
||||||
|
|
||||||
# Released under the MIT License (MIT). See LICENSE.
|
# Released under the MIT License (MIT). See LICENSE.
|
||||||
# Copyright (c) 2019-2022 Peter Hinch
|
# Copyright (c) 2019-2024 Peter Hinch
|
||||||
|
|
||||||
import uos
|
import uos
|
||||||
import time
|
import time
|
||||||
from machine import SPI, Pin
|
from machine import SPI, Pin, SoftSPI
|
||||||
from eeprom_spi import EEPROM
|
from eeprom_spi import EEPROM
|
||||||
|
|
||||||
|
ESP8266 = uos.uname().sysname == "esp8266"
|
||||||
# Add extra pins if using multiple chips
|
# Add extra pins if using multiple chips
|
||||||
cspins = (Pin(Pin.board.Y5, Pin.OUT, value=1), Pin(Pin.board.Y4, Pin.OUT, value=1))
|
if ESP8266:
|
||||||
|
cspins = (Pin(5, Pin.OUT, value=1), Pin(14, Pin.OUT, value=1))
|
||||||
|
else:
|
||||||
|
cspins = (Pin(Pin.board.Y5, Pin.OUT, value=1), Pin(Pin.board.Y4, Pin.OUT, value=1))
|
||||||
|
|
||||||
# Return an EEPROM array. Adapt for platforms other than Pyboard.
|
# Return an EEPROM array. Adapt for platforms other than Pyboard.
|
||||||
def get_eep(stm):
|
def get_eep(stm):
|
||||||
if uos.uname().machine.split(" ")[0][:4] == "PYBD":
|
if uos.uname().machine.split(" ")[0][:4] == "PYBD":
|
||||||
Pin.board.EN_3V3.value(1)
|
Pin.board.EN_3V3.value(1)
|
||||||
time.sleep(0.1) # Allow decouplers to charge
|
time.sleep(0.1) # Allow decouplers to charge
|
||||||
|
|
||||||
if stm:
|
if stm:
|
||||||
eep = EEPROM(SPI(2, baudrate=5_000_000), cspins, 256)
|
if ESP8266:
|
||||||
|
spi = SoftSPI(baudrate=5_000_000, sck=Pin(4), miso=Pin(0), mosi=Pin(2))
|
||||||
|
else: # Pyboard
|
||||||
|
spi = SPI(2, baudrate=5_000_000)
|
||||||
|
eep = EEPROM(spi, cspins, 256)
|
||||||
else:
|
else:
|
||||||
eep = EEPROM(SPI(2, baudrate=20_000_000), cspins, 128)
|
if ESP8266:
|
||||||
|
spi = SoftSPI(baudrate=20_000_000, sck=Pin(4), miso=Pin(0), mosi=Pin(2))
|
||||||
|
else:
|
||||||
|
spi = SPI(2, baudrate=20_000_000)
|
||||||
|
eep = EEPROM(spi, cspins, 128)
|
||||||
print("Instantiated EEPROM")
|
print("Instantiated EEPROM")
|
||||||
return eep
|
return eep
|
||||||
|
|
||||||
|
|
||||||
|
# Yield pseudorandom bytes (random module not available on all ports)
|
||||||
|
def psrand8(x=0x3FBA2):
|
||||||
|
while True:
|
||||||
|
x ^= (x & 0x1FFFF) << 13
|
||||||
|
x ^= x >> 17
|
||||||
|
x ^= (x & 0x1FFFFFF) << 5
|
||||||
|
yield x & 0xFF
|
||||||
|
|
||||||
|
|
||||||
|
# Given a source of pseudorandom bytes yield pseudorandom 256 byte buffer.
|
||||||
|
def psrand256(rand, ba=bytearray(256)):
|
||||||
|
while True:
|
||||||
|
for z in range(256):
|
||||||
|
ba[z] = next(rand)
|
||||||
|
yield ba
|
||||||
|
|
||||||
|
|
||||||
# Dumb file copy utility to help with managing EEPROM contents at the REPL.
|
# Dumb file copy utility to help with managing EEPROM contents at the REPL.
|
||||||
def cp(source, dest):
|
def cp(source, dest):
|
||||||
if dest.endswith("/"): # minimal way to allow
|
if dest.endswith("/"): # minimal way to allow
|
||||||
|
@ -149,27 +179,35 @@ def cptest(stm=False): # Assumes pre-existing filesystem of either type
|
||||||
print("Fail mounting device. Have you formatted it?")
|
print("Fail mounting device. Have you formatted it?")
|
||||||
return
|
return
|
||||||
print("Mounted device.")
|
print("Mounted device.")
|
||||||
cp(__file__, "/eeprom/")
|
try:
|
||||||
# We may have the source file or a precompiled binary (*.mpy)
|
cp(__file__, "/eeprom/")
|
||||||
cp(__file__.replace("eep", "eeprom"), "/eeprom/")
|
# We may have the source file or a precompiled binary (*.mpy)
|
||||||
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
|
cp(__file__.replace("eep", "eeprom"), "/eeprom/")
|
||||||
print(uos.statvfs("/eeprom"))
|
print('Contents of "/eeprom": {}'.format(uos.listdir("/eeprom")))
|
||||||
|
print(uos.statvfs("/eeprom"))
|
||||||
|
except NameError:
|
||||||
|
print("Test cannot be performed by this MicroPython port. Consider using upysh.")
|
||||||
|
|
||||||
|
|
||||||
# ***** TEST OF HARDWARE *****
|
# ***** TEST OF HARDWARE *****
|
||||||
def full_test(stm=False):
|
def full_test(stm=False):
|
||||||
eep = get_eep(stm)
|
eep = get_eep(stm)
|
||||||
block = 0
|
print("Testing with 256 byte blocks of random data...")
|
||||||
|
r = psrand8() # Instantiate random byte generator
|
||||||
|
ps = psrand256(r) # Random 256 byte blocks
|
||||||
for sa in range(0, len(eep), 256):
|
for sa in range(0, len(eep), 256):
|
||||||
data = uos.urandom(256)
|
ea = sa + 256
|
||||||
eep[sa : sa + 256] = data
|
eep[sa:ea] = next(ps)
|
||||||
got = eep[sa : sa + 256]
|
print(f"Address {sa}..{ea} written\r", end="")
|
||||||
if got == data:
|
print()
|
||||||
print(f"Block {block} passed\r", end="")
|
r = psrand8() # Instantiate new random byte generator with same seed
|
||||||
|
ps = psrand256(r) # Random 256 byte blocks
|
||||||
|
for sa in range(0, len(eep), 256):
|
||||||
|
ea = sa + 256
|
||||||
|
if eep[sa:ea] == next(ps):
|
||||||
|
print(f"Address {sa}..{ea} readback passed\r", end="")
|
||||||
else:
|
else:
|
||||||
print(f"Block {block} readback failed.")
|
print(f"Address {sa}..{ea} readback failed.")
|
||||||
break
|
|
||||||
block += 1
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue