2023-05-23 07:31 AM - edited 2023-11-20 04:49 AM
Hi community, I am working on CRC generation with polynomial 0x8005 and initial value 0x0000, so I configured it as below
I am using the data as shown below and the CRC should be 0x43B8 but the value got from the API is different. So I am missing something.
uint32_t bufer[] = { 0x23, 0x41, 0x54, 0x4F, 0x4E, 0xFF, 0x53, 0x4C, 0x30, 0x43, 0x55, 0x52, 0xFF, 0x0A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 };
uint32_t crc = HAL_CRC_Calculate(&hcrc, bufer, sizeof(bufer));
I appreciate your input.
Solved! Go to Solution.
2023-05-25 01:37 PM
0xE2CB is a left shifting answer
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
int main(int argc, char **argv)
{
int i, j, k;
uint16_t crc;
uint8_t test[] = { 0x23,0x41,0x54,0x4F,0x4E,0xFF,0x53,0x4C,0x30,0x43,0x55,0x52,0xFF,0x0A,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
#define POLY 0x8005
crc = 0x0000;
for(j=0; j<sizeof(test); j++)
{
crc = crc ^ (test[j] << 8);
for(i=0; i<8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ POLY;
else
crc = (crc << 1);
}
}
printf("crc=%04X\n", crc);
return(1);
}
0x43BD is the right shifting answer with the 0xA001 poly (ie 0x8005 reversed)
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
int main(int argc, char **argv)
{
int i, j, k;
uint16_t crc;
uint8_t test[] = { 0x23,0x41,0x54,0x4F,0x4E,0xFF,0x53,0x4C,0x30,0x43,0x55,0x52,0xFF,0x0A,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
#define POLY 0xA001
crc = 0x0000;
for(j=0; j<sizeof(test); j++)
{
crc = crc ^ test[j];
for(i=0; i<8; i++)
{
if (crc & 0x0001)
crc = (crc >> 1) ^ POLY;
else
crc = (crc >> 1);
}
}
printf("crc=%04X\n", crc); // 0x43BD
return(1);
}
2023-05-23 02:55 PM
> uint32_t bufer[]
Are you sure you didn't mean uint8_t buffer[]?
JW
2023-05-23 03:28 PM
Not the MODBUS one, but the reverse 0xA001 vs 0x8005
https://community.st.com/s/question/0D50X00009XkhPASAZ/stm32foxx-crc-usage-of-hal-drivers
But needs to BYTE feed for sure.
You sure on the 0x43B8, I get that in the reversed / inverse sense. The STM32 is a left-shifting beast.
Do you have other exemplars ?
2023-05-23 03:36 PM
Apple gives me 0x43BD
https://opensource.apple.com/source/xnu/xnu-4570.41.2/bsd/libkern/crc16.c.auto.html
2023-05-24 12:04 AM
2023-05-24 02:01 AM
2023-05-24 02:36 AM
Hi @Community member, It's a typo it must be 0x43BD. I changed the array size to uint8_t and got a CRC of 0xe2cb which is CRC-16/BUYPASS.
2023-05-24 02:38 AM
Hi @Community member, yes you are right it's a typo. The CRC is 0x43BD. But I got 0xe2cb after changing it to uint8_t.
2023-05-24 03:07 AM
As Clive (Tesla) said above, you have to swap the bits order. You did not tell us the STM32 model you are using, but you may be able to experiment with bit ordering in CRC_CR.
JW
2023-05-24 11:04 PM
Hi @Community member, I am using NUCLEO-G491RE development board.