EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

CRC calculation

Started by janka vietzen February 1, 2007
for the moment I struggle with the modbus protocol checksum what seems to
use a quite odd 16 bit CRC polynom 0xA001. I have no idea if my ATmega
assembly code works correct becouse its responsible for both directions
generation and test same time and for the moment I do not have further
modbus certificated devices.

Is there any CRC calculator known what allows to comapare reference values?

On Thu, 01 Feb 2007 21:06:53 +0100, janka vietzen <janvi@t-online.de>
wrote:

>for the moment I struggle with the modbus protocol checksum what seems to >use a quite odd 16 bit CRC polynom 0xA001. I have no idea if my ATmega >assembly code works correct becouse its responsible for both directions >generation and test same time and for the moment I do not have further >modbus certificated devices. > >Is there any CRC calculator known what allows to comapare reference values?
Isn't there sample code for calculating modbus CRCs included in the documentation on modbus? I seem to recall seeing it near the end of one particular manual. Jon
> Isn't there sample code for calculating modbus CRCs included in the
I dont have many modbus manuals but there are 2 diffrent code samples for crc written in C. One uses 2 bested byte - bit loops and the other uses 255 byte lookup tables for low and highbyte each. Unfortunately I dont tried any Compilers for Atmels ATmega128 (probably there is a gcc available) and even the cpu family is new for me why i decided to start with assembler
janka vietzen wrote:
>>Isn't there sample code for calculating modbus CRCs included in the > > > I dont have many modbus manuals but there are 2 diffrent code samples for > crc written in C. One uses 2 bested byte - bit loops and the other uses 255 > byte lookup tables for low and highbyte each. Unfortunately I dont tried > any Compilers for Atmels ATmega128 (probably there is a gcc available) and > even the cpu family is new for me why i decided to start with assembler
There is GCC available for AVRs (including your chip). I suppose that you mean the binary Modbus encoding, also called Modbus/RTU. (There's a hexadecimal encoding resembling Intel's hex object code, but it does not use CRC at all, but a straight checksum). The Modbus CRC is the old IBM's CRC-16, used e.g. in the early disks and in the Bisync data communication protocol. For eight-bit processors, the simplest way of calculating the CRC is the use of two 256 (not 255) byte tables, if you do not want to do it bit-by-bit. The table-driven method calculates the CRC for each incoming byte, not each bit separately. -- Tauno Voipio tauno voipio (at) iki fi
"Jonathan Kirwan" <jkirwan@easystreet.com> wrote in message 
news:5mi4s25krt4qes5j25j5mr2k06sih0utqc@4ax.com...
> On Thu, 01 Feb 2007 21:06:53 +0100, janka vietzen <janvi@t-online.de> > wrote: > >>for the moment I struggle with the modbus protocol checksum what seems to >>use a quite odd 16 bit CRC polynom 0xA001. I have no idea if my ATmega >>assembly code works correct becouse its responsible for both directions >>generation and test same time and for the moment I do not have further >>modbus certificated devices. >> >>Is there any CRC calculator known what allows to comapare reference >>values? > > Isn't there sample code for calculating modbus CRCs included in the > documentation on modbus? I seem to recall seeing it near the end of > one particular manual. > > Jon
It is probably 10 years since I used Modbus, and hopefully the manuals have been updated, but I recall the code sample in my Modicon manual being incorrect :-( I still have some hair left though. Regards, Richard. + http://www.FreeRTOS.org + http://www.SafeRTOS.com for Cortex-M3, ARM7, ARM9, HCS12, H8S, MSP430 Microblaze, Coldfire, AVR, x86, 8051, PIC24 & dsPIC .... and soon AVR32
Jonathan Kirwan <jkirwan@easystreet.com> writes:

> On Thu, 01 Feb 2007 21:06:53 +0100, janka vietzen <janvi@t-online.de> > wrote: > >>for the moment I struggle with the modbus protocol checksum what seems to >>use a quite odd 16 bit CRC polynom 0xA001. I have no idea if my ATmega >>assembly code works correct becouse its responsible for both directions >>generation and test same time and for the moment I do not have further >>modbus certificated devices. >> >>Is there any CRC calculator known what allows to comapare reference values? > > Isn't there sample code for calculating modbus CRCs included in the > documentation on modbus? I seem to recall seeing it near the end of > one particular manual.
I'm sure I saw that too. (checks...) Yes, it's in the "Modbus over serial line specification and implementation guide V1.0". I downloaded it from the modbus web site. -- John Devereux
On Feb 1, 9:06 pm, janka vietzen <j...@t-online.de> wrote:
> for the moment I struggle with the modbus protocol checksum what seems to > use a quite odd 16 bit CRC polynom 0xA001. I have no idea if my ATmega > assembly code works correct becouse its responsible for both directions > generation and test same time and for the moment I do not have further > modbus certificated devices. > > Is there any CRC calculator known what allows to comapare reference values?
Here's my modbus CRC code for AVR. It calculates CRC over data in 'rx_buf' with length in r24. The result has first CRC byte in r25, second CRC byte in r24 crc_update: ldi r31, hi8(rx_buf)// ldi r30, lo8(rx_buf)// ldi r26, 0xff // crc_high ldi r27, 0xff // crc_low ldi r18, 0xa0 // xor_high ldi r19, 0x01 // xor_low crc_byte: ld r20, Z+ // get next data byte eor r27, r20 // crc ^= data ldi r20, 8 // 8 bits crc_bit: lsr r26 // ror r27 // brcc crc_no_xor // eor r26, r18 // crc ^= 0xa001 eor r27, r19 // crc_no_xor: dec r20 // brne crc_bit // dec r24 // brne crc_byte // movw r24, r26 // r25:r24 = CRC16 A table based solution will be faster, but this worked fine for me, and I didn't have room for a table.
Arlet wrote:
many thanx, i am going to test your code. 

here is my first try with atMega

;** R0  tempory  use
;** R16 counts Bits of a Byte
;** R17 counts Bytes of txbuf
;** R18:R19 contains polynom
;** R20:R21 CRC data 
;** R30:R31 Z Register points to txbuf

crcgen: ldi r17,low(txend-txbuf+1)      ;r16 rxbuf length
        ldi zl,low(txbuf)       ;       Z points to txbuf start
        ldi zh,high(txbuf)
        ldi     r18,0x01
        ldi r19,0xa0            ;r18:r19 = CRC Polynom = 0xa001
        ldi r20,0xff
        ldi r21,0xff            ;r20:r21 is CRC register 1)
        ld r0,z+                ;get first Byte from txbuf

bytlop: eor r20,r0              ;exor crc lowbyte with next data byte
        ldi r16,8+1             ;count 8 bits per byte
bitlop: dec r16                 ;one bit has been processed
        breq nbyte              ;-> byte is complete
        lsl     r20             ;shift 16 bit CRC Register 
        rol     r21                                     
        brcc bitlop             ;-> shift continius if C=0 
        eor r20,r18             ;if Carry = 1 
        eor r21,r19             ;exor with 16 bit Polynom 
        rjmp bitlop             ;repeat for all 8 bits

nbyte:  ld r0,z+                ;fetch next byte from rxbuf
        dec r17                 ;one byte has been processed
        brne bytlop             ;-> until all Bytes processed
        ret                     ;CRC should be in R20:R21 now


Assume txbuf contains the 6 byte modbus rtu message: 1,3,0,0,0,2
the crc should be 0xc40b (what I can see in a old trace record)
but my code calculates 0x98e8 for crc result. Any bugs obvious?

janka vietzen wrote:
> for the moment I struggle with the modbus protocol checksum what seems to > use a quite odd 16 bit CRC polynom 0xA001. I have no idea if my ATmega > assembly code works correct becouse its responsible for both directions > generation and test same time and for the moment I do not have further > modbus certificated devices. > > Is there any CRC calculator known what allows to comapare reference values?
A year or so ago I used a Windows Modbus application downloadable from England as a reference for verifying correct Modbus operation with my AVR-gcc based product. The Modbus app I am thinking of would work for free for 5 or 10 minutes each time it was launched. Otherwise $60 or so. Couldn't write anything like it for only $60, money well spent. You need something for your Modbus design to talk to, else you don't know that it works.
"David Kelly" <n4hhe@Yahoo.com> wrote in message 
news:12s74p1c5ea08fd@corp.supernews.com...
> janka vietzen wrote: >> for the moment I struggle with the modbus protocol checksum what seems to >> use a quite odd 16 bit CRC polynom 0xA001. I have no idea if my ATmega >> assembly code works correct becouse its responsible for both directions >> generation and test same time and for the moment I do not have further >> modbus certificated devices. >> >> Is there any CRC calculator known what allows to comapare reference >> values? > > A year or so ago I used a Windows Modbus application downloadable from > England as a reference for verifying correct Modbus operation with my > AVR-gcc based product. The Modbus app I am thinking of would work for free > for 5 or 10 minutes each time it was launched. Otherwise $60 or so. > Couldn't write anything like it for only $60, money well spent. > > You need something for your Modbus design to talk to, else you don't know > that it works. >
This one worked with Siemens power meter: int crc16_siemens9200(unsigned char *ptr, int count) { unsigned int crc; char i; crc = 0xffff; while (--count >= 0) { crc = crc ^ (unsigned int) *ptr++; i = 8; do { if (crc & 0x0001) crc = (crc >> 1) ^ 0xA001; else crc = (crc >> 1); } while(--i); } return (crc); } Alex.

The 2024 Embedded Online Conference