CAN bus operation question
I’m new to CAN bus and I have inherited some code that has about 8 devices on a single CAN bus line using a Renesas RX231 chip. Currently the CAN bus is kind of what I would consider a “Free for all” whereas any device can send a command to another device. Each device also sends out pings to every other device. The master and slave relationship is still there with the master acting as sort of the “collector” of data and then the issuer of certain commands. From this I am seeing a lot of weird things like random messages being ignored or interrupts being skipped. It’s very random and not too often but when it does happen it’s a huge problem. Is this a good architecture for the CAN bus? I was thinking of trying to move it from the “free for all” state it’s currently in to a strict master/slave relationship where the master interrogates one device, waits for data, and then requests data from another. I’m not too worried this will slow things down a bit. I think what is currently happening is that two devices are trying to talk to each other at the same time and messing up messaging. Kind of like a data collision. Is this possible with CAN? My guess is yes. I’m hoping to get some solid CAN bus advice from a few individuals who have worked with CAN bus before and who can give me a good idea of how to architect the system to avoid this. Thank you!!
Posted Dec 11, 2019, 3:19 PM UTC
I haven't worked with that particular chip before, but the "free for all" method is how CAN busses typically work. CAN is multimaster specifically to enable that sort of operation. The transfer layer of the protocol (handled by the CAN controller on your micro) has a feature called arbitration, to ensure that even if 2 devices talk over each other, the higher priority message still gets transmitted and the lower can be retried later. I'd encourage you to deeply read the Wikipedia page on CAN Bus, which is surprisingly detailed!
In your case, I could see a few things happening:
The last thing I'll say is that if you do move to a request-response system, that's built into the protocol. You set the RTR (remote transmission request) bit for a frame, then the other node responds with the data on the same message ID.
edit to fix formatting and add the bit about RTR
To expand on your third point, the mailboxes can be set up with a filter and mask so they only receive messages with specific IDs or specific groupings of bits. The chips I work with call this the Acceptance Mask and Acceptance Code, not sure if that’s the standard naming. Setting these up will reduce the amount of interrupts and message processing, assuming each device doesn’t need to listen to everything on the bus.
Yup, thanks for the clarification. Number of interrupts can make or break your software design.