Re: How CRC for CAN bus is calculated
Stephen Glow <[email protected]>
| Newsgroups | gmane.comp.hardware.bus.can |
|---|---|
| Message-ID | <[email protected]> |
That's correct, the CAN controller normally does this calculation automatically, so it's generally not necessary to do it in software. I had written this in the past when I was analyzing the low level details of a CAN interface and wanted to validate the CRC. S On 01/09/2014 01:39 AM, Dinesh Guleria wrote: > @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] > <mailto:[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/ > <http://www.vector.com/canlist/> > Report any problems to <[email protected] > <mailto:[email protected]>> > > > -- > Archives and useful links: http://groups.yahoo.com/group/CANbus > Subscribe and unsubscribe at www.vector.com/canlist/ > <http://www.vector.com/canlist/> > Report any problems to <[email protected] > <mailto:[email protected]>> > >