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 support@ccsinfo.com

[Solved] What format is the CRC in CCS generated .hex?

 
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: 537
Location: Des Moines, Iowa, USA

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

[Solved] What format is the CRC in CCS generated .hex?
PostPosted: Tue Nov 05, 2019 4:24 pm     Reply with quote

I see a CRC is added as a comment in the .hex files the CCS compiler generates:

;SETTINGS MONITOR=38400
;PIC24EP256GP202
;CRC=5BA6 CREATED="05-Nov-19 11:42"

What type of CRC is this? I just updated my parser and through a few CRC-16 variants I have code for but none of them matches.
_________________
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 07, 2019 8:52 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Nov 06, 2019 12:27 am     Reply with quote

Have a look at this:
<www.ccsinfo.com/pdfs/CRC_Source_Code.pdf>

This is CCS code to generate the CRC.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 06, 2019 3:37 am     Reply with quote

Ttelmah, that's not the routine that is used. In the past, we tried to figure
out what CRC routine was used by CCS in these threads:
http://www.ccsinfo.com/forum/viewtopic.php?p=163228
http://www.ccsinfo.com/forum/viewtopic.php?t=17536
But we failed.

Today I wondered if somebody else had figured it out, so I did a
Google search for:
Quote:
CCS compiler checksum in hex file

I found this guy's website:
https://curioussystem.com/2012/12/11/editing-pic-hex-files/
Scroll down to this section:
Quote:
Embrace and Extend or, a custom CRC

He has a C# routine that I think he got from CCS tech support.
This routine actually generates the CRC at the end of the CCS Hex files.

I converted the routine to CCS and made the test program shown below.
It works. Finally, we know how to do it.

I compiled and ran the program below in MPLAB vs. 8.92 simulator.
I used CCS vs. 5.091. I got the following CRC in the UART1 output
window in MPLAB:
Quote:
DC54

That matches the CRC that CCS puts in the last line of the Hex file
that I generated for testing.
Quote:
CRC=DC54 CREATED="05-Nov-19 18:45"

This hex file is listed in the test program below.

Also, it's important to note that the CCS CRC is not a CRC of the PIC's
program memory. It's a CRC of the ASCII bytes in the Hex file, excluding
the CRC line itself.

Test program:
Code:

#include <18F46K22.h>
#fuses NOWDT
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)

// Here is the source file that I compiled with
// CCS vs. 5.091 to generate a short Hex file
// so I could test the CRC routine on it:
/*
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN

//======================================
void main()
{         


while(TRUE);
}
*/

// This is the hex file generated for the above file:
// :1000000002EF00F0F86AD09E0F01386B396B3A6B43
// :100010003B6B3C6B776A786A796A0DEF00F00300FE
// :020000040030CA
// :0E00000000F81E3C00BD81000FC00FE00F4055
// :00000001FF
// ;PIC18F46K22
// ;CRC=DC54  CREATED="05-Nov-19 18:45"

// Using Winhex, I converted the hex file above into
// the array of bytes shown below.  The last line in
// the hex file above (ie., the CRC line) is not
// included in the array below.
unsigned int8 hex_file[] = {
   0x3A, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x32, 0x45, 0x46, 0x30, 0x30, 0x46,
   0x30, 0x46, 0x38, 0x36, 0x41, 0x44, 0x30, 0x39, 0x45, 0x30, 0x46, 0x30, 0x31, 0x33, 0x38, 0x36,
   0x42, 0x33, 0x39, 0x36, 0x42, 0x33, 0x41, 0x36, 0x42, 0x34, 0x33, 0x0D, 0x0A, 0x3A, 0x31, 0x30,
   0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x33, 0x42, 0x36, 0x42, 0x33, 0x43, 0x36, 0x42, 0x37, 0x37,
   0x36, 0x41, 0x37, 0x38, 0x36, 0x41, 0x37, 0x39, 0x36, 0x41, 0x30, 0x44, 0x45, 0x46, 0x30, 0x30,
   0x46, 0x30, 0x30, 0x33, 0x30, 0x30, 0x46, 0x45, 0x0D, 0x0A, 0x3A, 0x30, 0x32, 0x30, 0x30, 0x30,
   0x30, 0x30, 0x34, 0x30, 0x30, 0x33, 0x30, 0x43, 0x41, 0x0D, 0x0A, 0x3A, 0x30, 0x45, 0x30, 0x30,
   0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x46, 0x38, 0x31, 0x45, 0x33, 0x43, 0x30, 0x30, 0x42, 0x44,
   0x38, 0x31, 0x30, 0x30, 0x30, 0x46, 0x43, 0x30, 0x30, 0x46, 0x45, 0x30, 0x30, 0x46, 0x34, 0x30,
   0x35, 0x35, 0x0D, 0x0A, 0x3A, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31, 0x46, 0x46, 0x0D,
   0x0A, 0x3B, 0x50, 0x49, 0x43, 0x31, 0x38, 0x46, 0x34, 0x36, 0x4B, 0x32, 0x32, 0x0D, 0x0A
};

// This is the routine used by CCS in their IDE to generate
// the CRC at the end of the Hex file.
unsigned int16 CRC16(unsigned int8 *inBytes, unsigned int16 length)
{
unsigned int16 crc_Dbyte = 0;
unsigned int16 byte_counter = 0;
unsigned int8 bit_counter;
unsigned int16 c;

for(byte_counter = 0; byte_counter < length; byte_counter++)
   {
    // Only sum up ASCII text bytes, not control chars.
    if((inBytes[byte_counter] > 31) && (inBytes[byte_counter] < 127))
      {
       c = ((unsigned int16)inBytes[byte_counter] << 8) & 0xFF00;
       
       for(bit_counter=0; bit_counter < 8; bit_counter++)
          {
           if(((crc_Dbyte ^ c) & 0x8000) != 0)
             {
              crc_Dbyte <<= 1;
              crc_Dbyte ^= 0x1021;
             }
           else
              crc_Dbyte <<= 1;
 
           c <<= 1;
          }
      }
   }

return crc_Dbyte;
}

//======================================
void main(void)
{
unsigned int16 CheckSum;

Checksum = CRC16(hex_file, sizeof(hex_file));

printf("%LX \r", Checksum);

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Nov 06, 2019 4:27 am     Reply with quote

That's funny, that was posted as a link on a previous question about the
hex file CRC saying that it gave the same result. However it was then said
that the CRC did change with compiler versions... :(
allenhuffman



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

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

PostPosted: Wed Nov 06, 2019 8:10 am     Reply with quote

PCM programmer wrote:
Also, it's important to note that the CCS CRC is not a CRC of the PIC's
program memory. It's a CRC of the ASCII bytes in the Hex file, excluding
the CRC line itself.


Thank you for this. I would not have expected that. My routine was pulling out the
data bytes (and another version included the header/checksum, with no success).
_________________
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 ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Nov 06, 2019 8:45 am     Reply with quote

Though (of course), if you actually 'think' about it this makes sense, since it is
then verifying the file being sent, including the header, and length information
which would be 'missed' if you only checked the actual data.... Very Happy
allenhuffman



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

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

PostPosted: Wed Nov 06, 2019 8:48 am     Reply with quote

Tested and verified:
Code:
>;CRC=5BA6  CREATED="05-Nov-19 11:42"
> CRC=5ba6

I noticed the sample data given included the CR and LFs and I was surprised they'd include OS-specific text format in their CRC calc. Then I saw the CRC code skips those. Good deal.

Thanks!
_________________
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 ?
allenhuffman



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

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

PostPosted: Wed Nov 06, 2019 10:39 am     Reply with quote

Ttelmah wrote:
Have a look at this:
<www.ccsinfo.com/pdfs/CRC_Source_Code.pdf>

This is CCS code to generate the CRC.

What was the CRC in that PDF example for? Is that a software implementation of the hardware PIC CRC? I note the PDF is also incomplete, cutting off in the middle of a function -- but it seems the CRC function is there, at least.
_________________
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 ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 06, 2019 1:19 pm     Reply with quote

allenhuffman wrote:

<www.ccsinfo.com/pdfs/CRC_Source_Code.pdf>

What was the CRC in that PDF example for?

It's an implementation of the CRC-16-CCITT. It's listed in the CRC table
in this Wikipedia article:
https://en.wikipedia.org/wiki/Cyclic_redundancy_check
allenhuffman



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

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

PostPosted: Wed Nov 06, 2019 1:21 pm     Reply with quote

PCM programmer wrote:
It's an implementation of the CRC-16-CCITT. That one is listed in the
CRC table in this Wikipedia article:
https://en.wikipedia.org/wiki/Cyclic_redundancy_check


Is that what the PIC hardware CRC uses? I've been looking at Microchip documents trying to see if they name it.
_________________
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 ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 06, 2019 1:32 pm     Reply with quote

The PIC you listed in the 1st post of this thread is PIC24EP256GP202.
On page 374 of the current data sheet, in TABLE 26-1, it lists a 16-bit
polynomial of 0x1021 for bits <x15:0>. (Translating binary into hex).
That's the same as in the table in the pdf file. It's also the same as
in the Wikipedia table for CRC-16-CCITT.

However, I would carefully test both the pdf code and the PIC24 CCS
code before declaring they produce the same result.
allenhuffman



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

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

PostPosted: Wed Nov 06, 2019 1:40 pm     Reply with quote

It looks like one of the C samples I found was the same code used by the C# blog.

https://www.embeddedrelated.com/showcode/295.php

So if I had known about parsing the ASCII and stopping at
the ;CRC= line (including the two before it) it might have worked.
_________________
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 ?
allenhuffman



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

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

PostPosted: Thu Nov 07, 2019 8:42 am     Reply with quote

The CRC used can be verified on this site:

https://crccalc.com/

Choose "Calc CRC-16" and then "CRC-16/XMODEM" is the one that should match.

You can select ASCII and paste in a small .hex file (their input buffer
is small). Since this online calc also includes line endings, you have
to remove them, so if you have something like this:

Code:
:080000000002040000000000F2
:100400000F782200F07F220020A0B700000000003B
:1004100081E0A8004427EF002CA3EF000E020400A7
:08042000000000000040FE0096
:020000040002F8
:08AFF8000282FF007F3FFF0011
:00000001FF
;PIC24FJ128GA006
;CRC=5491  CREATED="07-Nov-19 08:36"


...you would paste it in, then delete the last line (;CRC=…) and then
go to the end of each line and DELETE the carriage return so you
end up with one long line:

Code:
:080000000002040000000000F2:100400000F782200F07F220020A0B700000000003B:1004100081E0A8004427EF002CA3EF000E020400A7:08042000000000000040FE0096:020000040002F8:08AFF8000282FF007F3FFF0011:00000001FF;PIC24FJ128GA006


When I do that, the web tool has:

Code:
Algorithm     Result Check  Poly   Init   RefIn fOut  XorOut     
CRC-16/XMODEM 0x5491 0x31C3 0x1021 0x0000 false false 0x0000


You will see the resulting 0x5491 which matches the .hex file:

Code:
;CRC=5491  CREATED="07-Nov-19 08:36"


And the polynomial of 0x1021 which appears in the source for the CRC routine:

Code:
              crc_Dbyte ^= 0x1021;


I was amused to find out that the CRC used here is apparently the
one we used in the 1980s for XMODEM/CRC (original Xmodem was
a checksum, and I didn't see the CRC until Ymodem or Zmodem, but
I found it was also added to Xmodem as well).
_________________
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