I noticed that the baudRateRegister value of the GHI.IO.ControllerAreNetwork class does not reflect the requested SJW value. The corresponding bits are always 0 (SJW = 1).
Consider this code:
ControllerAreaNetwork.Timings timings = new ControllerAreaNetwork.Timings(0, 15, 4, 6, 3, false);
ControllerAreaNetwork canBus = new ControllerAreaNetwork(ControllerAreaNetwork.Channel.One, timings);
canBus.Enabled = true;
I want 500 kBit/s with a SJW of 3. The register value should be 0x003e8005 (equivalent to the more common BTR1=0x3E, BTR0=0x85), SJW bits = 10b. Instead the value is 0x003e0005, SJW bits = 00b. SJW is stored in bit 15:14.
I am using a FEZ Spider II with pure NetMF (therefore a G120E).
If this is value is indeed wrong, would a workaround be to directly set the MCU register with GHI.Processor.Register?
ControllerAreaNetwork.Timings timings = new ControllerAreaNetwork.Timings(0, 15, 4, 6, 3, false);
ControllerAreaNetwork canBus = new ControllerAreaNetwork(ControllerAreaNetwork.Channel.One, timings);
canBus.Enabled = true;
// put CAN controller into reset
GHI.Processor.Register CAN1MOD = new GHI.Processor.Register(0x40044000);
CAN1MOD.SetBits(0x00000001);
// set SJW bits
GHI.Processor.Register CAN1BTR = new GHI.Processor.Register(0x40044014);
CAN1BTR.SetBits(0x00008000);
// clear reset mode
CAN1MOD.ClearBits(0x00000001);
This seems to work. At least in the sense that the register has the new value and I can still communicate over the bus. I cannot really test whether SJW has changed or whether the firmware will at some later point rewrite the register.
@ jjs - SJW is currently not included in the register calculation on the G120. We will look into it for a future release.
If you want a safer way to ensure your set value, use reflection to set the private instance member baudRateRegister to your desired value before enabling CAN.