CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

[Solved] PIC24 hardware CRC-16-CCITT (poly 0x1021) calc

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

[Solved] PIC24 hardware CRC-16-CCITT (poly 0x1021) calc
PostPosted: Thu Nov 14, 2019 9:41 am     Reply with quote

I see others trying to figure this out in this forum and at Microchip's.

I am looking into the CCS hardware CRC routines. Since the CRC used by the .hex files is CRC-16-CCITT, I wanted to use the same one in the PIC24.

The polynomial is 0x1021, listed here:

https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations_of_cyclic_redundancy_checks

Here is the online CRC calculator I used to verify that my C# implementation for the .hex file worked:

https://crccalc.com/

If you go to that site, and select "Input type: hex" then give it this hex string:

Code:
AABBCCDDEEFF


…then click "Calc CRC-16", it shows a value down in the "CRC-16/XMODEM" line. Click on that and it will sort to the top.

That was the value (initialize to 0x0000) that matched the .hex file CRC for me.

When I try to do this using the CCS library, I am not getting a match:

Code:
    // CRC test
    unsigned int16 crc;
   
    // https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Polynomial_representations_of_cyclic_redundancy_checks
    // CRC-16-CCITT
    setup_crc (16, 12, 5); // 0x1021 polynomial values.
   
    crc_init (0x0000);

    unsigned int8 data[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
   
    crc = crc_calc16 (data, 6, 8); // 8-bit data.   

    printf ("CRC is %04x\r\n", crc);


From the Wiki page, it shows the polynomial values of:

Quote:
x^16 + x^12 + x^5 + 1


So I am using setup_Crc(16, 12, 5). I initialize to 0 and then calculate over the even number of bytes (6), specifying that they are 8-bit values.

The website shows 0x53F5 for the result, by my code shows 0x05f8.

I am obviously missing a step, or using an incorrect polynomial or init value.

Anyone used this?
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?


Last edited by allenhuffman on Thu Nov 14, 2019 10:14 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 9:52 am     Reply with quote

In their example file, Ex_crc_hw.c, CCS has this line in main():
Code:
 setup_crc(16, 12, 5, 0);   //0x1021 CRC CCITT

The example file is dated 8/9/2018.
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Nov 14, 2019 9:59 am     Reply with quote

PCM programmer wrote:
In their example file, Ex_crc_hw.c, CCS has this line in main():
Code:
 setup_crc(16, 12, 5, 0);   //0x1021 CRC CCITT

The example file is dated 8/9/2018.


Thank you. That solved it. I really need to stop reading the CCS manual. The examples show:

Code:
Examples:

setup_crc (12, 5);                  // CRC Polynomial is X12 + X5 + 1
setup_crc(16, 15, 3, 1);           // CRC Polynomial is X16 + X15 + X3 + X1+ 1


…so they include the x^# digit but not the +1 at the end, and don't show a zero.

Thanks! I looked at that example code, and since it used different values I figured the 0 was part of that different polynomial.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
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


Powered by phpBB © 2001, 2005 phpBB Group