-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite-test.py
168 lines (141 loc) · 4.97 KB
/
write-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
import unittest
import serial
import serial.tools.list_ports
import numpy as np
def read_rom(filename):
f = open(filename, 'rb')
data = bytearray(f.read())
f.close()
return data
#
# Test board functionality
#
class TestBoard(unittest.TestCase):
def setUp(self):
# autofind any available boards
ports = serial.tools.list_ports.comports()
portfound = None
for port in ports:
if port.pid == 0x0A and port.vid == 0x2E8A:
portfound = port.device
break
# specify the COM port below
if portfound:
self.ser = serial.Serial(portfound,
19200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=None) # open serial port
while(not self.ser.isOpen()):
self.ser.open()
else:
raise Exception('Cannot find suitable port connection')
def teardDown(self):
while(not self.ser.isClosed()):
self.ser.close()
def test_board_id(self):
"""
Test reading board id
"""
self.ser.write(b'READINFO')
rsp = self.ser.read(8)
rsp = self.ser.read(16)
print(rsp)
self.assertEqual(rsp[0:4], b'PICO')
def test_device_id(self):
"""
Test reading board id
"""
self.ser.write(b'DEVIDSST')
rsp = self.ser.read(8)
rsp = self.ser.read(2)
#print(' '.join('0x%02X' % d for d in rsp))
self.assertEqual(rsp, bytearray([0xBF,0xB7]))
def test_execute_erase(self):
"""
Test erasing of sector on board
"""
self.ser.write(b'ESST0000')
rsp = self.ser.read(8)
rsp = self.ser.read(2) # read pollbyte count
print(' '.join('0x%02X' % d for d in rsp))
self.ser.write(b'RDBK0000')
rsp = self.ser.read(8)
rsp = self.ser.read(256)
self.assertEqual(rsp, bytearray([0xFF] * 256))
def test_execute_write(self):
"""
Test reading board id
"""
self.ser.write(b'ESST0030')
rsp = self.ser.read(8)
print(rsp)
rsp = self.ser.read(2)
print(' '.join('0x%02X' % d for d in rsp))
# generate random data
randomdata = bytearray(np.random.randint(0, 256, 256, dtype=np.uint8))
self.ser.write(b'WRBK003F')
rsp = self.ser.read(8)
print(rsp)
self.ser.write(randomdata)
checksum = np.uint8(self.ser.read(1)[0])
print(np.sum(randomdata) & 0xFF)
print(np.sum(checksum))
self.ser.write(b'RDBK003F')
rsp = self.ser.read(8)
print(rsp)
rsp = self.ser.read(256)
#print(' '.join('0x%02X' % d for d in randomdata))
#print(' '.join('0x%02X' % d for d in rsp))
self.assertEqual(rsp, randomdata)
def test_write_4k(self):
"""
Test reading board id
"""
self.ser.write(b'ESST0050')
rsp = self.ser.read(8)
print(rsp)
rsp = self.ser.read(2)
print(' '.join('0x%02X' % d for d in rsp))
# check erasing
self.ser.write(b'RDSECT05')
rsp = self.ser.read(8)
rsp = self.ser.read(0x1000)
self.assertEqual(rsp, bytearray(np.ones(0x1000, dtype=np.uint8) * 0xFF))
# generate random data
randomdata = bytearray(np.random.randint(0, 256, 0x1000, dtype=np.uint8))
self.ser.write(b'WRSECT05')
rsp = self.ser.read(8)
print(rsp)
self.ser.write(randomdata)
crc16checksum = self.ser.read(2)
# assert checksum
self.assertEqual(int.from_bytes(crc16checksum, byteorder='little', signed=False),
crc16(randomdata))
# check writing
self.ser.write(b'RDSECT05')
rsp = self.ser.read(8)
rsp = self.ser.read(0x1000)
# printhex(randomdata[0:32])
# printhex(rsp[0:32])
self.assertEqual(rsp, randomdata)
def crc16(data):
crc = int(0)
poly = 0x1021
for c in data: # fetch byte
crc ^= (c << 8) # xor into top byte
for i in range(8): # prepare to rotate 8 bits
crc = crc << 1 # rotate
if crc & 0x10000:
crc = (crc ^ poly) & 0xFFFF # xor with XMODEN polynomic
return crc
def printhex(data):
for i in range(0, len(data)):
print('%02X ' % data[i], end='')
if (i+1) % 16 == 0:
print()
print()
if __name__ == '__main__':
unittest.main()