So, after spending a few hours reading manuals, this is what I found about CAN bitrate calculation for G400.
First, a few assumptions, that are not obvious and were not written anywhere — until now:
[ol]
Peripheral clock for G400 is set to 133MHz.
Parameter [em]bitrate [/em]in CAN constructor
CAN(GHI.Premium.Hardware.CAN.Channel channel, uint bitrate, int receiveBufferSize)
is directly written to AT91SAM9 register CAN_BR (manual section 41.9.6)[/ol]
There are a few fields we have to set inside that register: BRP, SJW, PROPAG, PHASE1 and PHASE2. After spending some time with the manual, I’ve simplified things to the following formula:
T_bit*MCK=(BRP+1)*(4+PROPAG+PHASE1+PHASE2)
Now, if we express MCK in MHz, and T_bit (that is, bit time) in microseconds, we get:
[b]
T_bit*133=(BRP+1)*(4+PROPAG+PHASE1+PHASE2)
[/b]
Two important things here:
ul is called Time Quantum (TQ) and must be between 8 and 25, preferably 16;
both sides of the equation is a product.[/ul]
Formal part is done, lets move to examples!
=== Example 1 — 1 Mbps ===
For 1Mbps speed, bit time is 1 μs, so we have:
(BRP+1)*(4+PROP+PHASE1+PHASE2)=1*133=133
We need to split 133 into a product of two round numbers, and one part of the product shoulb be as close to 16 (TQ restriction!) as possible. There aren‘t many options: 133=7*19.
Since (4+PROP+PHASE1+PHASE2) has to be between 8 and 25 (preferably 16), we have only one option:
(BRP+1)=7
(4+PROP+PHASE1+PHASE2)=19
From there,
BRP=6
PROP+PHASE1+PHASE2=15
Weve got BRP already, now its time to deal with PROP. It depends on bus speed and length and has to be calculated (see the manual, section 41.7.4.1), but its a number between 0 and 8. For higher speeds and short lines it may be set to 1 or 2, and it may be 0 for lower speeds. Ive chosen 1 for 1Mbit, although it may not be entirely correct (2 would be closer to correct value), but since its working totally fine, I don’t care much. PROP is a relaxed parameter, it seems. So PROP=1. Then its easy:
PHASE1+PHASE2=14.
PHASE1 and PHASE2 should be kept equal, so we choose 7 and 7.
If one decides to be entirely correct, PROP may be set to 2. Then we choose PH1=6 and PH2=7 (PHASE2 has to be greater or equal to PHASE1).
SJW is something between 0 and 3. It cannot be greater that PH1, so in this case we choose 3.
Calling CAN constructor is all that’s left:
_can = new CAN(CAN.Channel.Channel_1, (uint)((brp << 16) + (sjw << 12) + (propag << 8) + (phase1 << 4) + (phase2 << 0)));
=== Example 2 — 300 kbps ===
In this case, we have
(BRP+1)*(4+PROP+PHASE1+PHASE2)=1/0.3 *133~443.
Unfortunately, 443 is a prime number, so we cannot split it. But we can make a little error and use 444. 444 is 22337=1237. In this case TQ=12 (37 is too much). A bit low. But what if we try 442? 442=21317=26*17. Better! We choose TQ=17. Then we have:
(BRP+1)=26
(4+PROP+PHASE1+PHASE2)=17
Bit rate is lower, so we can safely assume PROP=1. Then PHASE1=PHASE2=6.
SJW is, again, 3.
And, of course, we apply those settings:
_can = new CAN(CAN.Channel.Channel_1, (uint)((brp << 16) + (sjw << 12) + (propag << 8) + (phase1 << 4) + (phase2 << 0)));