Hi,
I'm performing crc computation on K24 chip during run-time. As the address range to compute the crc is quite huge, it is broken up into several cycles. How to have the chip start a new calculation using an intermediate crc value from the previous calculation?
For example:
cycle 1: crc1 = Calculated crc from address 0x0001 to 0x0020
cycle 2: crc2 = calculated crc from crc1 and address 0x0021 to 0x0040
and so on...
How should crc1 be parsed in during cycle 2? As seed value or as a data?
Here's my code but it doesn't work:
<code>
static uint32_t Crc32(uint32_t const * const pData, uint32_t numLongs, uint32_t initCrc)
{
// Steps #1 and #2
CRC_CTRL = (CRC_CTRL_TOT(2U)) | // Input bits & bytes are transposed
(CRC_CTRL_TOTR(2U)) | // Output bits & bytes are transposed
(0UL << CRC_CTRL_WAS_SHIFT) | // Ready to write data
(1UL << CRC_CTRL_TCRC_SHIFT); // 32-bit CRC
// Step #3
CRC_GPOLY = 0x04C11DB7; // CRC32 polynomial
// Step #4 - Indicate seed will be written
CRC_CTRL = (CRC_CTRL_TOT(2U)) | // Input bits & bytes are transposed
(CRC_CTRL_TOTR(2U)) | // Output bits & bytes are transposed
(1UL << CRC_CTRL_WAS_SHIFT) | // Ready to write seed
(1UL << CRC_CTRL_TCRC_SHIFT); // 32-bit CRC
// Step #5 - Write seed
CRC_DATA = initCrc;
// Step #6 - Indicate that data follows
CRC_CTRL = (CRC_CTRL_TOT(2U)) | // Input bits & bytes are transposed
(CRC_CTRL_TOTR(2U)) | // Output bits & bytes are transposed
(0UL << CRC_CTRL_WAS_SHIFT) | // Ready to write data
(1UL << CRC_CTRL_TCRC_SHIFT); // 32-bit CRC
// Step #7 - Write the data values.
for (uint32_t i = 0; i < numLongs; i++)
{
CRC_DATA = pData[i];
}
// Step #8 - Return CRC result.
return CRC_DATA;
}
// Note: the following is a pseudo code just to illustrate the logic
// The following doesn't work. What's wrong?
main()
{
while (!crcDone)
{
readData(&data[0]);
if (cycle1)
{
crc = crc32(data[0], numBytes, 0xFFFFFFFFUL);
}
else
{
crc = crc32(data[0], numBytes, crc);
}
}
CompareCrcWithStoredCrc();
}
// I have also tried the following but it doesn't work either:
main()
{
while (!crcDone)
{
readData(&data[4]);
if (cycle1)
{
crc = crc32(data[4], numBytes, 0x0UL);
}
else
{
memcpy(&data[0], &crc, 4);
crc = crc32(data[0], numBytes+4, 0x0UL);
}
}
CompareCrcWithStoredCrc();
}
<code>
Appreciate some advice. Thanks!
Hi,
Comparing your code with the SDK example I see that your missing the clock enable for the CRC module.
I will suggest to check the example code that is in the MK24 SDK. You can download it from here:
Let me know if this helps you.
Best Regards,
Alexis Andalon
Hi Alexis,
The SCGC clock for CRC is already enabled in the startup code and i have checked that it is set for crc. So unfortunately it doesn't help. Thanks.
I'm curious to know if anyone has had any success using the crc this way?
Hi YiJun,
I found something strange in the previous code.
The function static uint32_t Crc32(uint32_t const * const pData, uint32_t numLongs, uint32_t initCrc) receive a pointer for the data but when you call the function you only use the value from the array data[0] or data[4]. I think if you store the data in the array you should pass the direction from the first pointer (data or &data[0]).
Also, I will suggest to test the SDK example, maybe you could base your application in this one.
Let me know if this helps you.
Best Regards,
Alexis Andalon