Skip to content

Commit 2ad5925

Browse files
committed
tests/ports/alif_hardware: Add flash testing script.
This test is not intended to be run automatically and does not have a corresponding .exp file. Signed-off-by: Damien George <[email protected]>
1 parent 547207d commit 2ad5925

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# MIT license; Copyright (c) 2024 OpenMV LLC.
2+
#
3+
# Note: this test completely erases the filesystem!
4+
# It is intended to be run manually.
5+
6+
import alif
7+
import hashlib
8+
import os
9+
import sys
10+
import time
11+
import vfs
12+
13+
hash_algo = "sha256"
14+
15+
16+
def flash_make_filesystem():
17+
try:
18+
vfs.umount("/flash")
19+
except:
20+
pass
21+
bdev = alif.Flash()
22+
vfs.VfsFat.mkfs(bdev)
23+
vfs.mount(vfs.VfsFat(bdev), "/flash")
24+
sys.path.append("/flash")
25+
sys.path.append("/flash/lib")
26+
os.chdir("/flash")
27+
28+
29+
def flash_block_test():
30+
try:
31+
vfs.umount("/flash")
32+
except:
33+
pass
34+
dev = alif.Flash()
35+
36+
data512 = os.urandom(512)
37+
buf512 = bytearray(512)
38+
block_numbers = tuple(range(32)) + tuple(range(250, 266))
39+
40+
print("Block read/write integrity: ", end="")
41+
ok = True
42+
for block_n in block_numbers:
43+
dev.writeblocks(block_n, data512)
44+
dev.readblocks(block_n, buf512)
45+
if buf512 != data512:
46+
ok = False
47+
print(ok)
48+
49+
print("Block read back integrity: ", end="")
50+
ok = True
51+
for block_n in block_numbers:
52+
dev.readblocks(block_n, buf512)
53+
if buf512 != data512:
54+
ok = False
55+
print(ok)
56+
57+
N = 16 * 1024
58+
data_big = os.urandom(N)
59+
t0 = time.ticks_us()
60+
dev.writeblocks(0, data_big)
61+
dt = time.ticks_diff(time.ticks_us(), t0)
62+
print(f"Block write speed: {len(data_big) / 1024 / dt * 1_000_000} KiB/sec")
63+
64+
buf_big = bytearray(N)
65+
t0 = time.ticks_us()
66+
dev.readblocks(0, buf_big)
67+
dt = time.ticks_diff(time.ticks_us(), t0)
68+
print(f"Block read speed: {len(buf_big) / 1024 / dt * 1_000_000} KiB/sec")
69+
70+
if buf_big != data_big:
71+
raise RuntimeError("big block read-back failed")
72+
73+
try:
74+
import uctypes
75+
76+
xip = memoryview(dev)
77+
except:
78+
xip = None
79+
if xip is not None:
80+
t0 = time.ticks_us()
81+
buf_big[:] = xip[: len(buf_big)]
82+
dt = time.ticks_diff(time.ticks_us(), t0)
83+
print(f"XIP read speed: {len(buf_big) / 1024 / dt * 1_000_000} KiB/sec")
84+
85+
if buf_big != data_big:
86+
raise RuntimeError("XIP read-back failed")
87+
for i in range(len(buf_big)):
88+
if xip[i] != data_big[i]:
89+
raise RuntimeError("XIP byte-wise read-back failed")
90+
91+
92+
def flash_write_verify(path, size=1024 * 1024, block_size=1024):
93+
hash_sum = getattr(hashlib, hash_algo)()
94+
block_count = size // block_size
95+
96+
print("-" * 16)
97+
print(f"Writing file {size=} {block_size=}")
98+
t0 = time.ticks_ms()
99+
total_size = 0
100+
with open(path, "wb") as file:
101+
for i in range(block_count):
102+
buf = os.urandom(block_size)
103+
# Update digest
104+
hash_sum.update(buf)
105+
total_size += file.write(buf)
106+
if i % (block_count // 16) == 0:
107+
print(f"{i}/{block_count}", end="\r")
108+
dt = time.ticks_diff(time.ticks_ms(), t0)
109+
print(f"Flash write finished: {total_size / 1024 / dt * 1000} KiB/sec")
110+
digest = hash_sum.digest()
111+
112+
print("Reading file... ", end="")
113+
hash_sum = getattr(hashlib, hash_algo)()
114+
buf = bytearray(block_size)
115+
t0 = time.ticks_ms()
116+
total_size = 0
117+
with open(path, "rb") as file:
118+
for i in range(block_count):
119+
total_size += file.readinto(buf)
120+
dt = time.ticks_diff(time.ticks_ms(), t0)
121+
print(f"finished: {total_size / 1024 / dt * 1000} KiB/sec")
122+
123+
print("Verifying file... ", end="")
124+
hash_sum = getattr(hashlib, hash_algo)()
125+
t0 = time.ticks_ms()
126+
total_size = 0
127+
with open(path, "rb") as file:
128+
for i in range(block_count):
129+
buf = file.read(block_size)
130+
total_size += len(buf)
131+
# Update digest
132+
hash_sum.update(buf)
133+
dt = time.ticks_diff(time.ticks_ms(), t0)
134+
print(f"finished: {total_size / 1024 / dt * 1000} KiB/sec; ", end="")
135+
136+
if digest != hash_sum.digest():
137+
raise RuntimeError(f"{hash_algo} checksum verify failed")
138+
139+
print(f"{hash_algo} checksum verified")
140+
141+
142+
if __name__ == "__main__":
143+
flash_block_test()
144+
flash_make_filesystem()
145+
flash_write_verify("test0.bin", size=64 * 1024, block_size=1024)
146+
flash_write_verify("test1.bin", size=128 * 1024, block_size=1024)
147+
flash_write_verify("test2.bin", size=64 * 1024, block_size=2048)
148+
flash_write_verify("test4.bin", size=256 * 1024, block_size=4096)
149+
flash_write_verify("test4.bin", size=512 * 1024, block_size=16384)

0 commit comments

Comments
 (0)