1. How to choose parameters for a given baud rate for CAN.init()?
I would like to make a list of settings (prescaler, sjw, bs1, bs2, assume PCLK1=42MHz) to work with all of the supported baud rates of my PEAK usb-CAN adapter. The supported baud rates are 5000, 10000, 20000, 33333, 47619, 50000, 83333, 95238, 100000, 125000, 250000, 500000, 800000, 1000000.
I wrote a script to generate a list of all viable settings for each baud rate, but many of them don't work. Through trial and error with that list, I found settings that successfully send/receive at the highest four baud rates:
Code: Select all
# This worked for 125k baud rate
can1.init(CAN.NORMAL, extframe=False, prescaler=16, sjw=1, bs1=14, bs2=6)
# This worked for 250k baud rate
can1.init(CAN.NORMAL, extframe=False, prescaler=8, sjw=1, bs1=14, bs2=6)
# This worked for 500k baud rate
can1.init(CAN.NORMAL, extframe=False, prescaler=4, sjw=1, bs1=14, bs2=6)
# This worked for 1M baud rate
can1.init(CAN.NORMAL, extframe=False, prescaler=2, sjw=1, bs1=14, bs2=6)
2. How to receive data into a buffer using an IRC (e.g. CAN.rxcallback(), Timer, external interrupt)?
It seems that the `CAN.recv()` method allocates new memory when it's called, based on the memory error that I get when I try to call it from `CAN.rxcallback()`. I would like to preallocate a buffer for 1000 CAN messages, and have them stored in the buffer as they come in, without consideration with my main program loop, which is handling user inputs and drawing to a screen.
Is that possible with the micropython CAN library? If not, is there a particular reason to avoid what I'm suggesting?
Thanks in advance for any advice.