-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_deprecated.py
95 lines (71 loc) · 2.93 KB
/
test_deprecated.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: i2cy([email protected])
# Project: CH347PythonLib
# Filename: test
# Created on: 2023/7/31
import struct
import random
import time
from hashlib import sha256
from ch347api import CH347HIDDev, VENDOR_ID, PRODUCT_ID
def generate_random_data(length=50):
# generate test bytes for transmission
res = []
for i in range(length):
res.append(int(random.random() * 255))
return bytes(res)
"""
Tests below requires MOSI-MISO short connected (outer-loop)
"""
if __name__ == '__main__':
# initialize a new CH347HIDDev device object
test_dev = CH347HIDDev(VENDOR_ID, PRODUCT_ID, 1)
# print HID device information
print("Manufacturer: %s" % test_dev.get_manufacturer_string())
print("Product: %s" % test_dev.get_product_string())
print("Serial No: %s" % test_dev.get_serial_number_string())
# -*- [ i2c test ] -*-
# initialize I2C with speed 400KHz
test_dev.init_I2C(2)
input("(press ENTER to perform I2C test)")
# write 0x75 to device with address 0x68
print("I2C test address 0x68")
status = test_dev.i2c_write(0x68, 0x75)
print("I2C write 0x75 test: {}".format(status))
# read 2 bytes from device with address 0x68
status, feedback = test_dev.i2c_read(0x68, 2)
print("I2C read 2 bytes test: {}, {}".format(status, feedback.hex()))
# read 1 byte of register 0x75 from device with address 0x68
status, feedback = test_dev.i2c_read(0x68, 1, 0x75)
print("I2C read 1 byte with register address 0x75 test: {}, {}".format(status, feedback.hex()))
# read 2 bytes of register 0x74 from device with address 0x68
status, feedback = test_dev.i2c_read(0x68, 2, b"\x74")
print("I2C read 2 bytes with register address 0x74 test: {}, {}".format(status, feedback.hex()))
# initialize SPI settings
test_dev.init_SPI(0, mode=0) # CLK speed: 60Mhz, SPI mode: 0b11
test_data_frame_length = 32768
time.sleep(0.2)
# generate test bytes
data = generate_random_data(test_data_frame_length)
input("(press ENTER to perform SPI test)")
# -*- [ spi test ] -*-
# write A5 5A 5A A5 through API
test_dev.set_CS1() # enable CS1 for transmission
test_dev.spi_write(b"\xa5\x5a\x5a\xa5")
test_dev.spi_read(2)
test_dev.set_CS1(False)
# specialized speed test (for project)
t0 = time.time()
test_dev.set_CS1() # enable CS1 for transmission
for ele in range(4 * 3 * 200_000 // test_data_frame_length + 1):
feed = test_dev.spi_read(test_data_frame_length)
test_dev.set_CS1(False)
print("1 sec of gtem data trans time spent {:.2f} ms".format((time.time() - t0) * 1000))
# read & write test
print("performing spi_read_write test...")
test_dev.set_CS1() # enable CS1 for transmission
feed = test_dev.spi_read_write(data)
test_dev.set_CS1(False)
print("R/W loop accusation test result: {}".format(bytes(feed) == data))
test_dev.close()