Title: Is it possible to generate CAN bus error using STM32?
Tags: c;embedded;stm32;can-bus
Question: For one of my project, I would like to generate some CAN bus errors (e.g. bit stuffing or CRC error).
This is just a small idea I had, but do you think it is possible to achieve ? I know all the protocol stuffs are handled by the CAN driver, but I am also not an expert in CAN communication, so maybe I have a little hope that this error generation can be achievable. What do you think ?
I am using a STM32 microcontroller and TJA CAN transceiver.

Here is the accepted answer: This is not handled by software at all, but by the on-chip CAN controller hardware. You will not be able to add things like stuffing or CRC errors with software alone, though it can be achieved by various hardware tricks.
One easy way to generate a whole lot of errors is to only have one node on the bus, or to short CAN High/Low. You'll rapidly go error active then error passive if you do this though.
More subtle ways is to pull Tx/Rx lines between the MCU and transceiver to wrong levels. If you connect one of these to an external GPIO pin and then sneak out a pulse directly after storing the message in the CAN buffer, you'll cause errors. Particularly if longer than 6 bits = stuff error. These will most likely be well in sync with the protocol, so this is probably what you want. You might have to add like 10k in series to the actual tx/rx pins so they get weaker drive strength than the GPIO.
Another method is to have 2 nodes sending identical arbitration fields (ID, RTR + misc bits) at once, but different data. This is achivable from software alone but it means you need to have 2 dumb nodes doing nothing but sending the same messages over and over at 100% bus load.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:04/02/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><embedded><stm32><can-bus>', 'Title': 'Is it possible to generate CAN bus error using STM32?', 'ViewCount': 1331, 'id': 55477177, 'user_AboutMe': None, 'user_AccountId': 14853383.0, 'user_DisplayName': 'Stabilo', 'user_Reputation': 45.0, 'user_Views': 27.0, 'user_id': 10726601.0}}