<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>CAN bus</title>
 <link rel="stylesheet" type="text/css" href="/style.css">
</head>
<body>
 <header>
   <h1 class="title">CAN bus</h1>
   <p class="date">2012-10-22</p>
 </header>
<p>I've started working with <a href="http://en.wikipedia.org/wiki/CAN_bus">CAN bus</a> for a new project I'm doing, using <a href="http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010406">Microchip's MCP2515 CAN controller</a> and <a href="http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010405">MCP2551 CAN transceiver</a>. In this blog post I'll explain how the CAN bus works at the bit level, to make it easier for us to debug when something isn't working.</p>
<div class="figure">
<img src="photos/thumbnails/breadboard.jpg" alt="CAN bus on a breadboard">
<p class="caption">
CAN bus on a breadboard
</p>
</div>
<p>I hooked up a quick circuit using an ATmega32 and the two CAN bus chips. I used SPI to send a single CAN data frame to the target ID 0x555, and then captured the frame using a logic analyzer.</p>
<div class="figure">
<img src="photos/thumbnails/frame.png" alt="CAN bus frame format">
<p class="caption">
CAN bus frame format
</p>
</div>
<h2 id="working-backwards">Working Backwards</h2>
<p>Let's pick through our message and understand where each of the bits came from. The bit stream that we captured with the logic analyzer was</p>
<p><span class="a">0</span><span class="b">10101010101</span><span class="c">000</span><span class="d">00100</span><span class="e">110011101001100</span><span class="f">1</span><span class="g">01</span><span class="h">1111111</span></p>
<p>which corresponds to the <span class="a">Start of Frame marker</span>, <span class="b">Arbitration Field</span>, <span class="c">Control Field</span> (including <span class="d">Data Length Code</span>), <span class="e">CRC sequence</span> (including <span class="f">CRC delimiter</span>), <span class="g">ACK field</span>, and <span class="h">End Of Frame (EOF) delimiter</span>. Note that the <span class="c">Control Field</span> contains a run of five <strong>0</strong> bits, which means a <strong>1</strong> bit has been stuffed into the value. So we know that we should remove that extra 1 bit from the <span class="d">Data Length Code</span> to get the correct value of <span class="d">0000</span>. So this message has a payload of 0 bytes. The "Arbitration Field" is just another name for the "Identifier", and our extracted identifier is <strong>10101010101</strong> or <strong>0x555</strong>.</p>
<p>The first three bits of the <span class="c">Control Field</span> are the RTR bit, IDE bit, and <strong>r0</strong> reserved bit. In our message these three bits are all 0. The RTR bit represents whether this CAN frame is a Remote Transmission Request or not. Apart from error handling, every CAN frame is either a Remote Frame (the sender is requesting some kind of reply from the receiving node), or is a data frame. The IDE (ID Extension) bit represents whether this data frame is addressed to an extended ID (an extended CAN message). Some manufacturers decided that 11 bits couldn't address all of their devices (211 = 2048). So they took an unused bit from the old CAN bus Control Field and called it the IDE bit. When the IDE bit is set, the message structure is slightly longer, and there is an additional 18-bit Identifier field meaning that an extended CAN message can address up to 229 = 536870912 different devices on the same bus (wow!). Alternatively, these ID bits can be split up among several fields (priority, category, sender type, etc). For now we'll ignore extended IDs, because we sent ourselves a standard CAN bus data frame (IDE bit = 0).</p>
<p>Since our <span class="d">Data Length</span> is <strong>0</strong>, we have 0 bytes of payload. So the next field is the <span class="e">CRC sequence</span>, and the <span class="f">CRC delimiter</span>. To understand where the CRC sequence came from, we need to understand a little bit about <a href="http://en.wikipedia.org/wiki/Cyclic_redundancy_check">CRC checksums</a>. I will assume that you've read up on how CRC works. The polynomial that CAN bus uses for the CRC calculation is <span class="math inline"><em>x</em><sup>15</sup> + <em>x</em><sup>14</sup> + <em>x</em><sup>10</sup> + <em>x</em><sup>8</sup> + <em>x</em><sup>7</sup> + <em>x</em><sup>4</sup> + <em>x</em><sup>3</sup> + <em>x</em><sup>0</sup></span> (according to the official documentation). We then checksum all the bits from the <span class="a">Start of Frame Marker</span> until just before the <span class="e">CRC sequence</span>, except for any stuffed bits that have been inserted to break up long runs of the same bit value. So after we remove the stuffed bit that was inserted into the Data Length field, we get the bit sequence <strong>0101010101010000000</strong>, or <strong>0x2AA80</strong>. We can use an online tool to help us perform the CRC calculation, and we will find that the generated value is <span class="e"><strong>0b110011101001100</strong></span>, which is the same value that was contained in the original CAN frame. The <span class="f">CRC delimiter</span> is always 1 (recessive state).</p>
<p>The first bit of the <span class="g">ACK field</span> (the ACK bit) is transmitted as 1 (recessive state) by the sender of a data frame, and anyone who has heard the message and verified its CRC checksum pulls the bus into a dominant state (0 bit) during the message's ACK bit, which tells the sender that at least one receiver has successfully read the message. Note that we can't tell how many other nodes successfully received the message -- we can only tell if <strong>nobody</strong> received it, or <strong>at least one node</strong> got it. Most CAN controllers are configured to repeat a message until it gets received by someone. The second bit of the ACK field is always 1 (recessive).</p>
<p>Finally, the <span class="h">End Of Frame (EOF) delimiter</span> marks the end of a CAN frame with 7 recessive bits.</p>
<p>That wasn't so bad!</p>
<p>UPDATE: I've added a bit of code to perform the CANbus CRC calculation below:</p>
<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdint.h&gt;

uint16_t can_crc_next(uint16_t crc, uint8_t data)
{
   uint8_t i, j;

   crc ^= (uint16_t)data &lt;&lt; 7;

   for (i = 0; i &lt; 8; i++) {
       crc &lt;&lt;= 1;
       if (crc &amp; 0x8000) {
           crc ^= 0xc599;
       }
   }

   return crc &amp; 0x7fff;
}

int main()
{
   int i;
   uint8_t data[] = {0x02, 0xAA, 0x80};
   uint16_t crc;

   crc = 0;

   for (i = 0; i &lt; sizeof(data); i++) {
       crc = can_crc_next(crc, data[i]);
   }

   printf(&quot;%x\n&quot;, crc);
}</code></pre>
<style>
a,.b,.c,.d,.e,.f,.g,.h { font-weight: bold; font-family:monospace; }
a {color: red}
b {color: mediumblue}
c {color: green}
e {color: deepskyblue}
g {color: orange}
d {color: purple}
f {color: magenta}
h {color: limegreen}
</style>
<h2 id="comments">Comments</h2>
<h4 id="pari">pari</h4>
<p>how can i write c code in code vision for calculation crc 15 bit?????</p>
<h4 id="qartis">qartis</h4>
<p>I updated the post and added a bit of example code to show how to calculate the CRC.</p>
<h4 id="pari-1">pari</h4>
<p>thanks for your help,but i dont understand how i can calculated crc for every data????and have out put by byneri format</p>
<h4 id="successful-engineer">Successful Engineer</h4>
<p>This is one of the best most concise explanations I've seen, thanks mate.</p>
<h4 id="sajith-p">Sajith P</h4>
<p>Better you should use #define for raw values, Eg #define polynomial c599</p>
<p>Readability would be more....!!</p>
<h4 id="dwayne">Dwayne</h4>
<p>Did you ever get anywhere with this project? I'm a computer science student with 20 years truck repair experience. I'm taking an SAE seminar on the CAN data link found in trucks. I'm hoping to write a project at may incorporate any of this material if it is available, Like wiring schematics ext. I'm not stealing ideas but rather looking for similar ideas. Thanks <a href="mailto:[email protected]" class="email">[email protected]</a></p>
</body>
</html>