forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmachine_can_06_remote_req.py
More file actions
70 lines (52 loc) · 1.74 KB
/
Copy pathmachine_can_06_remote_req.py
File metadata and controls
70 lines (52 loc) · 1.74 KB
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
from machine import CAN
import time
# Test CAN remote transmission requests
ID = 0x750
FILTER_ID = 0x101
FILTER_MASK = 0x7FF
can = CAN(1, 500_000)
def receiver_irq_recv(can):
assert can.irq().flags() & can.IRQ_RX # the only enabled IRQ
can_id, data, flags, _errors = can.recv()
is_rtr = flags == CAN.FLAG_RTR
print(
"recv",
hex(can_id),
is_rtr,
len(data) if is_rtr else bytes(data),
)
if is_rtr:
# The 'data' response of a remote request should be all zeroes
assert bytes(data) == b"\x00" * len(data)
# Receiver
def instance0():
can.irq(receiver_irq_recv, trigger=can.IRQ_RX, hard=False)
can.set_filters(None) # receive all
multitest.next()
multitest.wait("enable filter")
can.set_filters([(FILTER_ID, FILTER_MASK, 0)])
multitest.broadcast("filter set")
multitest.wait("done")
# Sender
def instance1():
multitest.next()
can.send(ID, b"abc", CAN.FLAG_RTR) # length==3 remote request
time.sleep_ms(5)
can.send(ID, b"abc", 0) # regular message using the same ID
time.sleep_ms(5)
can.send(ID, b"abcde", CAN.FLAG_RTR) # length==5 remote request
time.sleep_ms(5)
multitest.broadcast("enable filter")
multitest.wait("filter set")
# these two messages should be filtered out
can.send(ID, b"abc", CAN.FLAG_RTR) # length==3 remote request
time.sleep_ms(5)
can.send(ID, b"abc", 0) # regular message using the same ID
time.sleep_ms(5)
# these messages should be filtered in
can.send(FILTER_ID, b"def", CAN.FLAG_RTR) # length==3 remote request
time.sleep_ms(5)
can.send(FILTER_ID, b"hij", 0) # regular message using the same ID
time.sleep_ms(5)
multitest.broadcast("done")
print("done")