# How to calculate CRC register values?

Question asked by robitzki.torsten on Apr 22, 2016
Latest reply on Apr 20, 2017 by waclawek.jan
Hi,
I need to implement an existing circa algorithm [1]. I have a table driven C implementation, but I would like to use the STM32L4 CRC peripheral. I’ve read the chapter in the reference manual, and I know which parameters the peripheral allows, but unfortunately, I fail to map them to the „usual“ definitions of CRC algorithms. Is there any application note, that describes the „CRC computation“ block in more details (I’ve tried to use the st.com-search, but that results just in sql errors)?

Any hint on how I would have to map a CRC algorithm with the following parameters to the parameters, the CRC peripheral is using?

bits:       8
poly:       0x9c
init:       0x5a
xor:        0x00
reverse:    false
non-direct: false

This should result in 0x18 for the test input „123456789“ [2]. That’s the code, where I’ve tried to implement that algorithm:

std::uint8_t calc_checksum8( const std::uint8_t* start, std::size_t size )
{
RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;

CRC->CR       = ( CRC->CR & ~CRC_CR_POLYSIZE ) | CRC_CR_POLYSIZE_1;
CRC->INIT     = 0x5a;
CRC->POL      = 0x9c;
CRC->CR      |= CRC_CR_RESET;

for ( const std::uint8_t* end = start + size ; start != end; ++start )
CRC->DR = *start;

const std::uint32_t result = CRC->DR;

RCC->AHB1ENR &= (~RCC_AHB1ENR_CRCEN);

return result;
}

But this results in: 0xfc.

I had the very same problem in the other direction. How can I describe the default CRC32 algorithm implemented by the STM32 hardware when it contains the reset defaults?

Thank you for any help, pointer or idea,

Torsten