-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_I2cMaster.py
111 lines (95 loc) · 5.97 KB
/
test_I2cMaster.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
#!/usr/bin/env python
""" Testing I2c master write/read
"""
import time
from msg import tiny_frame
from msg import proto_i2c_msg as pm
from helper import (serial_port, generate_master_write_read_requests,
i2c_send_master_request, verify_master_write_read_requests)
class TestI2cMaster:
REQUEST_COUNT = 4 * 1000
DATA_SIZE_MIN = 1
DATA_SIZE_MAX = 128
I2C_CLOCK_FREQ = 400000
I2C0_SLAVE_ADDR = 0x01
I2C1_SLAVE_ADDR = 0x02
FRAM_SLAVE_ADDR = 0x50
FRAM_SIZE = 32768 # (== 2^15)
FRAM_0_MIN_ADDR = 0
FRAM_0_MAX_ADDR = FRAM_SIZE // 2 - 1
FRAM_1_MIN_ADDR = FRAM_SIZE // 2
FRAM_1_MAX_ADDR = FRAM_SIZE - 1
def test_i2c_master_write_read_fram(self, serial_port):
# Test master using external FRAM
tf = tiny_frame.tf_init(serial_port.write)
cfg0 = pm.I2cConfig(clock_freq=TestI2cMaster.I2C_CLOCK_FREQ, slave_addr=TestI2cMaster.I2C0_SLAVE_ADDR,
slave_addr_width=pm.AddressWidth.Bits7, mem_addr_width=pm.AddressWidth.Bits16,
pullups_enabled=True)
cfg1 = pm.I2cConfig(clock_freq=TestI2cMaster.I2C_CLOCK_FREQ, slave_addr=TestI2cMaster.I2C1_SLAVE_ADDR,
slave_addr_width=pm.AddressWidth.Bits7, mem_addr_width=pm.AddressWidth.Bits16,
pullups_enabled=True)
i2c_int0 = pm.I2cInterface(i2c_id=pm.I2cId.I2C0, config=cfg0)
i2c_int1 = pm.I2cInterface(i2c_id=pm.I2cId.I2C1, config=cfg1)
time.sleep(1)
requests_pipeline0 = generate_master_write_read_requests(slave_addr=TestI2cMaster.FRAM_SLAVE_ADDR,
min_addr=TestI2cMaster.FRAM_0_MIN_ADDR,
max_addr=TestI2cMaster.FRAM_0_MAX_ADDR,
min_size=TestI2cMaster.DATA_SIZE_MIN,
max_size=TestI2cMaster.DATA_SIZE_MAX,
count=TestI2cMaster.REQUEST_COUNT // 4)
requests_pipeline1 = generate_master_write_read_requests(slave_addr=TestI2cMaster.FRAM_SLAVE_ADDR,
min_addr=TestI2cMaster.FRAM_1_MIN_ADDR,
max_addr=TestI2cMaster.FRAM_1_MAX_ADDR,
min_size=TestI2cMaster.DATA_SIZE_MIN,
max_size=TestI2cMaster.DATA_SIZE_MAX,
count=TestI2cMaster.REQUEST_COUNT // 4)
requests_pipeline1 = []
while True:
i2c_send_master_request(i2c_int0, requests_pipeline0)
# TestI2cMaster.i2c_send_master_request(i2c_int1, requests_pipeline1)
if serial_port.in_waiting > 0:
# Read the incoming data
rx_data = serial_port.read(serial_port.in_waiting)
tf.accept(rx_data)
verify_master_write_read_requests(i2c_int0)
# TestI2cMaster.verify_master_write_read_requests(i2c_int1)
if ((len(requests_pipeline0 + requests_pipeline1) == 0) and
(len(i2c_int0.get_pending_master_request_ids() + i2c_int1.get_pending_master_request_ids()) == 0)):
break
"""
def test_i2c_master_write_read_self(self, serial_port):
# Test master using internal slave (no slave notification evaluation)
tf = tiny_frame.tf_init(serial_port.write)
cfg0 = pm.I2cConfig(clock_freq=TestI2cMaster.I2C_CLOCK_FREQ, slave_addr=TestI2cMaster.I2C0_SLAVE_ADDR,
slave_addr_width=pm.AddressWidth.Bits7, mem_addr_width=pm.AddressWidth.Bits16,
pullups_enabled=True)
cfg1 = pm.I2cConfig(clock_freq=TestI2cMaster.I2C_CLOCK_FREQ, slave_addr=TestI2cMaster.I2C1_SLAVE_ADDR,
slave_addr_width=pm.AddressWidth.Bits7, mem_addr_width=pm.AddressWidth.Bits16,
pullups_enabled=True)
i2c_int0 = pm.I2cInterface(i2c_id=pm.I2cId.I2C0, config=cfg0)
i2c_int1 = pm.I2cInterface(i2c_id=pm.I2cId.I2C1, config=cfg1)
time.sleep(1)
requests_pipeline0 = generate_master_write_read_requests(slave_addr=TestI2cMaster.I2C1_SLAVE_ADDR,
min_addr=0,
max_addr=pm.I2C_SLAVE_BUFFER_SPACE - 1,
max_size=TestI2cMaster.DATA_SIZE_MAX,
count=TestI2cMaster.REQUEST_COUNT // 4)
requests_pipeline1 = generate_master_write_read_requests(slave_addr=TestI2cMaster.I2C0_SLAVE_ADDR,
min_addr=0,
max_addr=pm.I2C_SLAVE_BUFFER_SPACE - 1,
max_size=TestI2cMaster.DATA_SIZE_MAX,
count=TestI2cMaster.REQUEST_COUNT // 4)
# requests_pipeline1 = []
while True:
i2c_send_master_request(i2c_int0, requests_pipeline0)
i2c_send_master_request(i2c_int1, requests_pipeline1)
if serial_port.in_waiting > 0:
# Read the incoming data
rx_data = serial_port.read(serial_port.in_waiting)
tf.accept(rx_data)
verify_master_write_read_requests(i2c_int0)
verify_master_write_read_requests(i2c_int1)
if ((len(requests_pipeline0 + requests_pipeline1) == 0) and
(len(i2c_int0.get_pending_master_request_ids() + i2c_int1.get_pending_master_request_ids()) == 0)):
break
"""