Hello experts,
I have a question of the crc_demo sample in KINETIS_120MHZ_SC provided by the freescale.
According to Kinetis K70 Reference Manual "35.3.2.1 16-bit CRC", the usage of CRC_CRC is described as below.
6. Clear the CTRL[WAS] bit to start writing data values.
7. Write data values into CRC[HU:HL:LU:LL]. A CRC is computed on every data
value write, and the intermediate CRC result is stored back into CRC[LU:LL].
8. When all values have been written, read the final CRC result from CRC[LU:LL].
I think it would be inconsistent with the sample code.
The original code is as follows.
sizeWords = sizeBytes>>1; j = 0; for(i=0;i<sizeWords;i++){ data_in = (msg[j] << 8) | (msg[j+1]); j += 2; CRC_CRCH=data_in; //DES was CRC_CRC_LOW=data_in; } if(j<sizeBytes) { pCRCBytes = (uint8_t*)&CRC_CRC; *pCRCBytes++ = msg[j]; } data_out=CRC_CRCH; //DES was CRC_CRC;
I guess it might be as follows.
sizeWords = sizeBytes>>1; j = 0; for(i=0;i<sizeWords;i++){ data_in = (msg[j+1] << 8) | (msg[j]); // CHANGE j += 2; CRC_CRCL=data_in; // CHANGE } if(j<sizeBytes) { pCRCBytes = (uint8_t*)&CRC_CRC; *pCRCBytes++ = msg[j]; } data_out=CRC_CRCL; //CHANGE
Am I not correct?
Can anyone teach me the truth?
Best regards,
Yasuhiko Koumoto.
Solved! Go to Solution.
Hello Yasuhiko Koumoto:
I think the sample code is correct and your confusion is about endianness. In the sample project, the message buffer is filled from a UART channel in sequence, so you can consider that the bytes are stored in Big Endian mode:
printf("\r\nPlease enter an ASCII Message:");
bByteCount = Input_Message(&strMsg[0]);
...
uint32_t Input_Message(uint8_t *MsgBuf)
{
uint8_t ch;
uint32_t count;
count = 0;
do
{
ch = in_char();
out_char(ch);
if(ch == '\r')
{
break;
}
MsgBuf[count++] = ch;
if(count >= MAX_CHARS)
{
break;
}
}while(1);
return (count);
}
But the CRC registers in K70 are Little Endian, so the correct way to calculate CRC16 for this buffer is with the original code:
sizeWords = sizeBytes>>1;
j = 0;
for(i=0;i<sizeWords;i++){
data_in = (msg[j] << 8) | (msg[j+1]);
j += 2;
CRC_CRCL=data_in;
}
if(j<sizeBytes)
{
pCRCBytes = (uint8_t*)&CRC_CRC;
*pCRCBytes++ = msg[j];
}
data_out=CRC_CRC; // CRC_CRCL is also correct
return(data_out);
Regards!,
Jorge Gonzalez
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi, Koumoto-san
Please pay more patience. I will check your issue. Thanks.
Best Regards
Paul
Hi Paul,
thank you. I am interested in the result.
Best regards,
Yasuhiko Koumoto.
Hi, Yasuhiko
Are you still having problems?
Regards
Patricia
Hello Patricia,
I have still problem because I have not gotten any answer from Paul.
Best regards,
Yasuhiko Koumoto.
Hello Yasuhiko Koumoto:
I think the sample code is correct and your confusion is about endianness. In the sample project, the message buffer is filled from a UART channel in sequence, so you can consider that the bytes are stored in Big Endian mode:
printf("\r\nPlease enter an ASCII Message:");
bByteCount = Input_Message(&strMsg[0]);
...
uint32_t Input_Message(uint8_t *MsgBuf)
{
uint8_t ch;
uint32_t count;
count = 0;
do
{
ch = in_char();
out_char(ch);
if(ch == '\r')
{
break;
}
MsgBuf[count++] = ch;
if(count >= MAX_CHARS)
{
break;
}
}while(1);
return (count);
}
But the CRC registers in K70 are Little Endian, so the correct way to calculate CRC16 for this buffer is with the original code:
sizeWords = sizeBytes>>1;
j = 0;
for(i=0;i<sizeWords;i++){
data_in = (msg[j] << 8) | (msg[j+1]);
j += 2;
CRC_CRCL=data_in;
}
if(j<sizeBytes)
{
pCRCBytes = (uint8_t*)&CRC_CRC;
*pCRCBytes++ = msg[j];
}
data_out=CRC_CRC; // CRC_CRCL is also correct
return(data_out);
Regards!,
Jorge Gonzalez
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------