Tuesday, September 07, 2010
   
TEXT_SIZE

STM32 Controller: CAN Bus

One of the primary areas of functionality on the STM32small Controller is its CAN bus, this is quite an advanced peripheral (typically found in the automotive industry) and isn't typically the first choice of  the hobbyist - due largely to the cost of debugging tools, but there are other cost factors which have influenced it being restricted largely to professional targets.

However if you want to have lots of controllers talking on a data bus, it is one of the better choices to use.  Just a few of the reasons are:

  • Good over distances (1Mbps achievable up to 40 metres)
  • Robust in electrically noisy environments (due to it being a differential bus).
  • Added H/W features such as built in CRC checking of messages
  • Automatic bus arbitration
  • Speeds of up to 1Mbps

 

Here is a screenshot of the Qudon Foundation's Mk1 STM32 Controller transmitting its first CAN bus message - captured by a logic analyser on the TX pin of the transciever:

STM32 CAN Bus Signal Trace

 

The above was captured with a USBee SX Logic Analyzer - for the money (~£130 inc. / $140) an amazing too, not least because its one of the few (if not the only) low cost logic analyers that can process and decode CAN messages.   Saving you from the pain of having to decode them yourself!

You can see in the above screenshot that the controller trasmitted a message of ID: 0x5D with data payload of 0x11 (the CRC is calculated for us by the h/w CAN peripheral).

Now to verify CAN traffic on an actual CAN bus, most tools start getting expensive - the cheapest one we know of is from Peak Systems and costs ~£130 + S/W costs.   However, the industry standard is CANalyzer and you can see this tool in use below for use in verifying that messages seen on the logic analyzer (as above) really do get out on to the main CAN bus itself.

STM32 CAN Bus Setup

Now you can see the Logic Analyer tagged on to one side of the CAN bus transciever fitted to the STM32 controller board (See the black probe running across the blue PCB).   Then on the other side of the transciever we have the CANalyzer acting as the rest of the CAN bus and recieving/acknowledging our data (See the twisted cable coming off the blue board and into the CANalyzer black box).

One of the trickier things with CAN buses is the termination resistors - to much termination and you'll never see any data.....not enough.....same deal occurs.   The correct way to terminate the bus is to have 120ohm resistors on each end of the bus ONLY.   However, its all to easy to introduce unwanted termination (i.e. by fitting a node that brings its own termination on-board).

This is why on the STM32small controller it has been designed so that termination can be on-board or external termination can be used - this way you can have many STM32small controllers all connected together forming their own bus with only the ones at each end of the bus being set for applying termination.