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!
-----------------------------------------------------------------------------------------------------------------------