In my application, the user can configure the bit rate of the CAN bus.
On startup, CAN peripheral is configured to a default bit rate.
During operation, a command is given to change the bit rate to a handful of available options, in which I call a function that handles this change. Place the peripheral into initialization mode (INITRQ), assign the appropriate values to CANBTR1 & CANBTR0, then takes it out of initialization mode.
Using the same come to set bitrate on startup works as it should. However, I get inconsistant results when used during normal operation to change bitrate on the fly. Sometimes it works perfectly, sometimes it would stay at the previously assigned bitrate. The interface is not the issue, it definitely has something to do with the registers. Am I missing something?
Here's is my code, I've shortened it for simplicity:
CAN0CTL0 |= INITRQ; //request init mode
while (!(CAN0CTL1 & INITAK)); //wait for acknowledge
//set bit timing registers
//set to 500Kbps
CAN0BTR0 = 0;
CAN0BTR1 = TSEG12 | TSEG20;
CAN0CTL0 &= ~INITRQ; //request exit init mode
while (CAN0CTL1 & INITAK); //wait for acknowledge