This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

offline CRC calculation

Hi All,

I am using the CRC block in the TMS470M to provide a CRC of flash memory contents to prove code integrity. I am, as part of the programming sequence, calculating, and inserting the known CRC of the code into the hex file to be downloaded.

Has anyone any experience with a suitable algorithm to calculate the CRC? I have written the following C# routine to calculate the CRC (based on a C example from this forum) but I do not get the correct results when compared to the CRC block in the processor, with the same data. Can anyone offer any suggestions? have I made a really simple mistake?

thanks,

Phil

static UInt64 crc = 0;
static UInt64 calc_crc(UInt64 data)
{
  UInt64 nextCrc = 0;
  for (int i = 63; i >= 0; i--)
    { 
      nextCrc = (nextCrc & 0xfffffffffffffffe) | ((crc >> 63) ^ (data >> i));
      for (int j = 1; j < 64; j++)
      {
        if (j == 1 || j == 3 || j == 4)
        {
          nextCrc = (nextCrc & ~(1ul << j) ) | ( ( ( (crc >> (j - 1) ) ^ (crc >> 63) ^ (data >> i) ) & 1ul) << j);
        }

        else
        { // when others =>
          nextCrc = (nextCrc & ~(1ul << j)) | (((crc >> (j - 1)) & 1ul) << j);
        }
      } // end loop;
      crc = nextCrc;
    } // end loop
    Console.WriteLine(crc.ToString("x8"));
    return crc;
}

  • Here is the matlab code that I wrote for 470M CRC. Suppose you write the 1st 32 bit (from edit1.txt file) to PSA_SIGREGL_UL and 2nd 32 bit to PSA_SIGREGH_UL, 3rd to PSA_SIGREGL_UL, and so on.

    http://e2e.ti.com/cfs-file.ashx/__key/communityserver-discussions-components-files/407/5342.test3.m

    7103.Edit1.txt

  • Hi, thanks for the matlab example. I am not familiar with matlab, but I will try and engineer it into C.

    I have a related but slightly different question!

    when using the CRC module on the processor, I am calculating a crc based on 32bit numbers. I have tested this by:

    • zero the signature with the control register.
    • write each 32bit number to be crc'ed into the sigreg.
    • read out sigregl1 at the end as my 32bit crc.
    The data sheet suggests that any unused bits are treated as 0's. i.e writing 0xffffffff into sigreg is actually treated as 0x00000000ffffffff.
    does the same happen if I write into sigregl1 directly? I have trialed both methods, and the crc is different depending on whether I write into the 32bit location sigregl1 or the 64bit location sigreg.....
    any comments?
    Phil
  • I am not sure what you are asking for. Let me give you another example running on the processor side:

    8640.crc0_test_m3.c

    7608.crc.h

     

     

  • The system has some problem. I can no longer upload files.

  • Hi, thanks for the examples. my position is this...

    I can succesfully use the CRC module in the processor to calculate a CRC. depending on whether 32bit numbers are loaded into SIGREG (64bit wide) or SIGREGL(32bit wide) the results generated are different.

    I have written some code to calculate the CRC which runs on my PC. This will be used to precalculate the CRC of the flash memory in advance, which can then be used as a run time reference against that calculated by the CRC peripheral in the micro. My CRC calculator does not generate the same CRC as the processor.

    I am assuming that I cannot be the only person trying to do this? The only example given in the reference manual is in HDL, which I have never used, so your matlab example is currently the best I have!

    regards,

    Phil

  • At least two more customers asked the same question. And I built this matlab code and the CRC example on the processor for them. They matches well.

    I never tried 64bit write. Let me explain the 32 bit write. After that, 64 bit write is only a question which 32bit goes to which register.

    Suppose you write 0xC67E816B to PSA_SIGREGL (the case in my matlab and processor example), the processor treat it as C67E816B00000000. Then you can use this fomula in TRM to calculate the CRC.

    Suppose you write 4BFBE2FB to PSA_SIGREGH (the case in my matlab and processor example), the processor treat it as 000000004BFBE2FB. Then you can use this fomula in TRM to calculate the CRC.

    This is the reason you get different result by writing a 64bit or 32bit to PSA_SIGREG