I try to read out data from the AS5047U sensor (https://ams.com/documents/20143/36005/AS5047U_DS000637_1-00.pdf/8639418f-6c3a-1624-4e6f-18f52c962099) over SPI, but somehow my CRC is always different than that from the sensor. I can read out the correct position value and the crc value from the sensor and the crc stays the same for the same position value, so I think I got the right frame for crc.
I got for example the following values from my crc calculation and the crc given by the sensor (spi1.data is the received data over spi, spi.cmd is the sent data):
Any ideas what could be wrong here?
SPI crc processing:
rx_spi_16(&spi1.data[0],&spi1.cmd[0],2);
pos_sensor.crc = (spi1.data[1] >> 8);
uint8_t crc = crc8((uint8_t *)&spi1.data[0],2);
TX Command to send (16bit mode):
spi1.cmd[0] = 0x7FFF;
spi1.cmd[1] = 0;
CRC Calculation:
uint8_t crc8(uint8_t *message ,uint8_t length)
{
uint32_t crc;
int16_t i,bit;
crc = 0xff;
for ( i=0 ; i<length ; i++ )
{
crc ^= message[i];
for ( bit=0 ; bit<8 ; bit++)
{
if ( (crc & 0x80)!=0 )
{
crc <<= 1;
crc ^= 0x1D;
}
else
{
crc <<= 1;
}
}
}
return (~crc) & 0xFF;
}