Re: How CRC for CAN bus is calculated

Dinesh Guleria <[email protected]>
Newsgroups gmane.comp.hardware.bus.can
Message-ID <CAKpDV8udDbZ7AFwGaKrGcvb9qKAz69izcSPE1zKf45y6BmBiSA@mail.gmail.com>
@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.