The setup consists of two pyboards interconnected via CAN bus through CAN transceivers (TI SN65HVD231)
For easy reproduction of this problem, copy the following code into the main.py of each board and on the REPL run slave() on one board and master() on the other. After few seconds hit Ctrl-C and compare the count of sent vs received frames.
Results for different bitrates:
125k (prescaler=16, sjw=1, bs1=14, bs2=6) - works OK
250k (prescaler=8, sjw=1, bs1=14, bs2=6) - does not work at all
500k (prescaler=4, sjw=1, bs1=14, bs2=6) - 0.2% frames lost (not received)
1M (prescaler=2, sjw=1, bs1=14, bs2=6) - 0.6% frames lost (not received)
Code: Select all
import pyb
can = pyb.CAN(1, mode=pyb.CAN.NORMAL, prescaler=2, sjw=1, bs1=14, bs2=6)
can.setfilter(0, pyb.CAN.LIST16, 0, (123, 124, 125, 126))
def master():
count = 0
try:
while True:
can.send("message!", 123, timeout=1000)
count += 1
except KeyboardInterrupt:
print(count)
def slave():
count = 0
try:
while True:
if can.any(0):
can.recv(0)
count += 1
except KeyboardInterrupt:
print(count)