|
|
View previous topic :: View next topic |
Author |
Message |
allenhuffman
Joined: 17 Jun 2019 Posts: 554 Location: Des Moines, Iowa, USA
|
[Solved] PIC24 hardware CRC-16-CCITT (poly 0x1021) calc |
Posted: Thu Nov 14, 2019 9:41 am |
|
|
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:
…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
|
|
Posted: Thu Nov 14, 2019 9:52 am |
|
|
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: 554 Location: Des Moines, Iowa, USA
|
|
Posted: Thu Nov 14, 2019 9:59 am |
|
|
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 ? |
|
|
|
|
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
|