Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

CRC 16 Error

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
FanieTerblanche

Bascom Member



Joined: 20 Apr 2006
Posts: 26

blank.gif
PostPosted: Sat Apr 18, 2020 5:31 pm    Post subject: CRC 16 Error Reply with quote

Good Day,

I'm busy with a ZModem translation from C and I'm not getting the "correct" results for the CRC calculation. I'm adding an array of data as sent from software and the CRC is supposed to calculate to 0 which the CRC16UNI on the array from position 1 for 1031 bytes calculates to, but I need to calculate the CRC on the fly as I'm receiving the data and then it does not give me the correct result. What am I doing wrong ?

Kind Regards
Fanie.

My apologies if my initial post was hard to read.

Code:

$regfile = "M1281def.dat"
$crystal = 7372800
$framesize = 200
$swstack = 200
$hwstack = 200

Dim Databuffer(1500) As Byte
Dim Crc As Word
Dim Xxx As Word
Dim Bb As Byte

' Load the test data
Restore Testdata
For Xxx = 1 To 1031
  Read Databuffer(xxx)
Next

'Use the Crc16 function
Crc = 0
Crc = Crc16(databuffer(1) , 1031 )
Print "CRC16 Buffer         " ; Hex(crc )
'CRC16 Buffer         D290
' BAD Result

'Use the Crc16uni function and do the CRC 1 byte at a time
Crc = 0
For Xxx = 1 To 1031
  Crc = Crc16uni(databuffer(xxx) , 1 , Crc , &H1021 , 0 , 0)
Next
Print "CRC16uni Byte 4 Byte " ; Hex(crc )
'CRC16uni Byte 4 Byte D290
' BAD Result

'Use the Crc16uni function and do buffer on one go
Crc = 0
Crc = Crc16uni(Databuffer(1) , 1031 , Crc , &H1021 , 0 , 0)
Print "CRC16uni Buffer      " ; Hex(crc )
'CRC16uni Buffer      0000
' GOOD Result ?
Do

Loop

' Sample data
Testdata:
Data &H0A,&H00,&H00,&H00,&H00,&H46,&HAE,&H23,&H40,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,
Data &H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,
Data &H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,
Data &H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,
Data &H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,
Data &H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,
Data &H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,
Data &H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,
Data &H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,
Data &H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,
Data &H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,
Data &H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,
Data &H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,
Data &H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,
Data &H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,
Data &H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,
Data &H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,
Data &H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,
Data &H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,
Data &H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,
Data &H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,
Data &H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,
Data &H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,
Data &H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,
Data &H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,
Data &H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,
Data &H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,
Data &H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,
Data &H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,
Data &H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,
Data &H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,
Data &H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,&H36,&H37,&H38,&H39,&H30,&H31,&H32,&H33,&H34,&H35,
Data &H36,&H37,&H38,&H39,&H30,&H40,&H23
 


This Is The C And Assembler Code For Calculating The Crc
Code:

Void Getdata(void)
{
Word Crc = 0;
Word I = 0;
  While(allok &&(I <= 1024)
  {
    Getnextch(); // Get Ch from serial port
    If(allok)
    {
      Crc = crcupdate(crc , Ch);
      I ++;
      }
    }
  }
}

// CRC computing ##############################################################
/* CRC is computed using the self-made function presented below */
#pragma warn-
//-----------------------------------------------------------------------------
word crcUpdate(word crc, byte serialData)
//-----------------------------------------------------------------------------
{   // Please, DO NOT MODIFY ! Any change may cause severe malfunction !
#asm
  ldd  r30, y+2
  ldd  r31, y+1
  ld   r27, y
  eor  r30, r27
  mov  r26, r30
  swap r26
  andi r26, 0x0f
  eor  r30, r26
  mov  r26, r30
  swap r26
  andi r26, 0xf0
  eor  r31, r26
  mov  r26, r30
  swap r26
  andi r26, 0xf0
  lsl  r26
  mov  r27, r30
  lsr  r27
  lsr  r27
  lsr  r27
  eor  r31, r27
  eor  r30, r26
#endasm
}
#pragma warn+
 


(BASCOM-AVR version : 2.0.8.2 )


Last edited by FanieTerblanche on Mon Apr 20, 2020 9:14 am; edited 2 times in total
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1803

blank.gif
PostPosted: Sun Apr 19, 2020 9:26 pm    Post subject: Reply with quote

1) Your code is a mess and pain to the viewer's eye. Do you know, that 'Data'-statements exist, from where it's possible to fill up the SRam-array?
2) To make bad things worse, you don't use the code-tags while creating your post.
3) Your sample code is nonsense, as this does not do what the 'print' suggests:
Code:
Crc = 0
For Xxx = 1 To 1031
Crc = Crc16uni(databuffer(xxx) , 1 , Crc , &H1021 , 0 , 0)
Next
Print "CRC Byte 4 Byte " ; Hex(crc )

4) Your asm-code is either nonsense or incomplete.
A CRC needs to walk (loop) through some data, for that purpose the assembler code needs at least one loop and conditional branch.
This asm does not contain any loops and is nonfunctional on its own.
Code:
// CRC computing ##############################################################
/* CRC is computed using the self-made function presented below */
#pragma warn-
//-----------------------------------------------------------------------------
word rZModem_crcUpdate(word crc, byte serialData)
//-----------------------------------------------------------------------------
{ // Please, DO NOT MODIFY ! Any change may cause severe malfunction !
#asm
ldd r30, y+2
ldd r31, y+1
ld r27, y
eor r30, r27
mov r26, r30
swap r26
andi r26, 0x0f
eor r30, r26
mov r26, r30
swap r26
andi r26, 0xf0
eor r31, r26
mov r26, r30
swap r26
andi r26, 0xf0
lsl r26
mov r27, r30
lsr r27
lsr r27
lsr r27
eor r31, r27
eor r30, r26
#endasm
}
#pragma warn+
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1803

blank.gif
PostPosted: Mon Apr 20, 2020 4:26 pm    Post subject: Re: CRC 16 Error Reply with quote

FanieTerblanche wrote:
'CRC16uni Buffer 0000
' GOOD Result ?

Are you sure or are you guessing?
I get &h7002 assumed the C-code works ok.
Code:
Dim Ch As Byte
Crc = 0
For Xxx = 1 To 1031
    Ch = databuffer(xxx)
   !LDS  r27, {Ch}
   !LDS  r30, {Crc + 0}
   !LDS  r31, {Crc + 1}
   !EOR  r30, r27
   !MOV  r26, r30
   !SWAP r26
   !ANDI r26, &h0f
   !EOR  r30, r26
   !MOV  r26, r30
   !SWAP r26
   !ANDI r26, &hf0
   !EOR  r31, r26
   !MOV  r26, r30
   !SWAP r26
   !ANDI r26, 0xf0
   !LSL  r26
   !MOV  r27, r30
   !LSR  r27
   !LSR  r27
   !LSR  r27
   !EOR  r31, r27
   !EOR  r30, r26
   !STS {Crc + 0}, r30
   !STS {Crc + 1}, r31
Next
Print "ASM-CRC      " ; Hex(crc )


Edit:
Code:
Var = CRC16UNI( source ,length , initial, polynomial,refin,refout)

'length' in CRC16UNI is an one byte long variable, thus with length of 1031 actually only the low byte is used and CRC is built over the range of 7 bytes.
So this answers also your questionmark'd statement
Quote:
' GOOD Result ?
with: No.

Edit II:
And in case it works, the following is more cleaner, but with more overhead = a tad slower.
Code:
Crc = 0
For Xxx = 1 To 1031
  Crc =  rZModem_crcUpdate(Crc, databuffer(xxx))
Next
  Print "ASM-Crc      " ; Hex(crc )

function rZModem_crcUpdate(CRC As Word, serialData As Byte) As Word
   !LDD  XL,  Y+0
   !LDD  XH,  Y+1
   !LD   r25, X
   !LDD  XL,  Y+2
   !LDD  XH,  Y+3
   !LD   r30, X+
   !LD   r31, X
   !MOV  r27, r25
   !EOR  r30, r27
   !MOV  r26, r30
   !SWAP r26
   !ANDI r26, &h0f
   !EOR  r30, r26
   !MOV  r26, r30
   !SWAP r26
   !ANDI r26, &hf0
   !EOR  r31, r26
   !MOV  r26, r30
   !SWAP r26
   !ANDI r26, 0xf0
   !LSL  r26
   !MOV  r27, r30
   !LSR  r27
   !LSR  r27
   !LSR  r27
   !EOR  r31, r27
   !EOR  r30, r26
   !LDD XL, Y+4
   !LDD XH, Y+5
   !ST X+,  r30
   !ST X,   r31
end function
Back to top
View user's profile
FanieTerblanche

Bascom Member



Joined: 20 Apr 2006
Posts: 26

blank.gif
PostPosted: Fri Apr 24, 2020 12:24 pm    Post subject: Re: CRC 16 Error Reply with quote

Hi MWS,

Thank you for your help. It turns out that the checksum code in the C sample was "rubbish" the CRC16Uni function works, but I had to exclude the first 7 bytes and add the 2 CRC bytes that followed the sample data. The documentation on this protocol sucks Mad

Regards
Fanie.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1803

blank.gif
PostPosted: Fri Apr 24, 2020 1:19 pm    Post subject: Re: CRC 16 Error Reply with quote

FanieTerblanche wrote:
Thank you for your help.

No problem.
Quote:
It turns out that the checksum code in the C sample was "rubbish"

My example still shows how C-rubbish can be successfully transformed into equivalent Bascom-rubbish.
Quote:
the CRC16Uni function works

Keep in mind that because of this, from mcs.lib:
Code:
[_CRC16UNI]
...
;Y+0 stack holds the number of bytes
...
 ld r23,y+
...
 dec r23

this one does work:
Code:
Crc = Crc16uni(databuffer(xxx) , 1 , Crc , &H1021 , 0 , 0)

while this one does not:
Code:
Crc = Crc16uni(Databuffer(1) , 1031 , Crc , &H1021 , 0 , 0)

Reason is the counter in processor register r23, which is only one byte wide, allowing at maximum to do a CRC over 255 bytes:
Code:
Crc = Crc16uni(Databuffer(1) , 255 , Crc , &H1021 , 0 , 0)

As the index is calculated with two bytes, for a CRC over the whole range a:
Code:
Crc = Crc16uni(Databuffer(1) , 255 , Crc , &H1021 , 0 , 0)
Crc = Crc16uni(Databuffer(256) , 255 , Crc , &H1021 , 0 , 0)
Crc = Crc16uni(Databuffer(511) , 255 , Crc , &H1021 , 0 , 0)
Crc = Crc16uni(Databuffer(766) , 255 , Crc , &H1021 , 0 , 0)
Crc = Crc16uni(Databuffer(1021) , 10 , Crc , &H1021 , 0 , 0)
 

should do (not tested).
Quote:
but I had to exclude the first 7 bytes and add the 2 CRC bytes that followed the sample data.

I already thought about such too, but as you was so confident with your C-code, it was reasonable to believe a translation will do.
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum