RE: How CRC for CAN bus is calculated

"John Dammeyer" <[email protected]>
Newsgroups gmane.comp.hardware.bus.can
Message-ID <[email protected]>
CAN bus CRC is done on all destuffed bits. 
CRC_Rg is set to zero.
CalcCRC( &CRC_rg, BitValue );  Called for each bit.
 
void 
CalcCRC( WORD * CRC_rg, WORD CRC_NextBit ) {
WORD CRC_Next;
      CRC_Next = CRC_NextBit ^ ((*CRC_rg >> 14) & 1);
      (*CRC_rg) <<= 1;
      if (CRC_Next == 1) {
            (*CRC_rg) = (*CRC_rg ^ 0x4599) & 0x7fff;
      }
      CRC_Count++;
}


This worked on bits captured with a logic analyzer.
 
John Dammeyer
 
 
From: [email protected] [mailto:[email protected]] On Behalf Of Dinesh Guleria
Sent: January-08-14 10:40 PM
Cc: CANLIST
Subject: Re: [CANLIST] How CRC for CAN bus is calculated
 
@steve
 
Your code seems to be CRC-16 ?
 
>> Here's some example C code that calculates the CRC for a CAN message.
Does CAN controller does not do the CRC calculation of data message on its own ?
 
Regards,
Dinesh
 
On Wed, Jan 8, 2014 at 6:52 PM, Stephen Glow <[email protected]> wrote:
Here's some example C code that calculates the CRC for a CAN message.

Rgds,
Steve

static uint16_t CRC_AddBits( uint16_t crc, uint16_t value, int bct )
{
   uint16_t mask = 1<<(bct-1);
   while( mask )
   {
      int bit = (value&mask) ? 1 : 0;
      if( crc & 0x4000 )
     bit = !bit;
      crc <<= 1;
      if( bit ) crc ^= 0x4599;
      mask>>=1;
   }

   return crc & 0x7FFF;
}

static uint16_t CalcCRC( int id, int ct, uint8_t data[] )
{
   uint16_t crc = 0;
   int i;

   // Add start of frame and ID
   crc = CRC_AddBits( crc, id&0x7FF, 12 );

   // Add RTR, IDE, r1 and count
   crc = CRC_AddBits( crc, ct&0x0F, 7 );

   if( ct > 8 ) ct = 8;

   for( i=0; i<ct; i++ )
      crc = CRC_AddBits( crc, data[i], 8 );

   return crc;

}


On 01/08/2014 08:03 AM, yogesh zambare wrote:
Dear All,
Can anybody tell me how CRC for CAN bus is calculated?

Regards,
Yogesh Zambare
--
Archives and useful links: http://groups.yahoo.com/group/CANbus
Subscribe and unsubscribe at www.vector.com/canlist/
Report any problems to <[email protected]>

--
Archives and useful links: http://groups.yahoo.com/group/CANbus
Subscribe and unsubscribe at www.vector.com/canlist/
Report any problems to <[email protected]>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.