cancel
Showing results for 
Search instead for 
Did you mean: 

Software CRC with a polynomial length of 8 and 16 bits

Alex Kovtun
Associate II

Hello, does anyone know how to programmatically calculate a CRC with a polynomial length of 8 and 16 bits? The algorithm described in AN4187 only works with a 32-bit polynomial. Here are the results:

IN_REVERSE_NONE, OUT_REVERSE_NONE, POLY_32B - 0xe8bccd9a

IN_REVERSE_NONE, OUT_REVERSE_NONE, POLY_16B - 0x3852

IN_REVERSE_NONE, OUT_REVERSE_NONE, POLY_8B - 0x26

IN_REVERSE_NONE, OUT_REVERSE_NONE, POLY_7B - 0x60

IN_REVERSE_NONE, OUT_REVERSE_BIT, POLY_32B - 0xaa45c417

IN_REVERSE_NONE, OUT_REVERSE_BIT, POLY_16B - 0x6e52

IN_REVERSE_NONE, OUT_REVERSE_BIT, POLY_8B - 0xfd

IN_REVERSE_NONE, OUT_REVERSE_BIT, POLY_7B - 0x5d

IN_REVERSE_BYTE, OUT_REVERSE_NONE, POLY_32B - 0x790ac5f0

IN_REVERSE_BYTE, OUT_REVERSE_NONE, POLY_16B - 0x1ae2

IN_REVERSE_BYTE, OUT_REVERSE_NONE, POLY_8B - 0x69

IN_REVERSE_BYTE, OUT_REVERSE_NONE, POLY_7B - 0x7

IN_REVERSE_BYTE, OUT_REVERSE_BIT, POLY_32B - 0x8794adc5

IN_REVERSE_BYTE, OUT_REVERSE_BIT, POLY_16B - 0x752a

IN_REVERSE_BYTE, OUT_REVERSE_BIT, POLY_8B - 0x70

IN_REVERSE_BYTE, OUT_REVERSE_BIT, POLY_7B - 0x77

IN_REVERSE_HALFWORD, OUT_REVERSE_NONE, POLY_32B - 0x4aaf6021

IN_REVERSE_HALFWORD, OUT_REVERSE_NONE, POLY_16B - 0x867c

IN_REVERSE_HALFWORD, OUT_REVERSE_NONE, POLY_8B - 0xdc

IN_REVERSE_HALFWORD, OUT_REVERSE_NONE, POLY_7B - 0x3b

IN_REVERSE_HALFWORD, OUT_REVERSE_BIT, POLY_32B - 0x71122de7

IN_REVERSE_HALFWORD, OUT_REVERSE_BIT, POLY_16B - 0x4c72

IN_REVERSE_HALFWORD, OUT_REVERSE_BIT, POLY_8B - 0x53

IN_REVERSE_HALFWORD, OUT_REVERSE_BIT, POLY_7B - 0x39

IN_REVERSE_WORD, OUT_REVERSE_NONE, POLY_32B - 0x3ec460e8

IN_REVERSE_WORD, OUT_REVERSE_NONE, POLY_16B - 0x655

IN_REVERSE_WORD, OUT_REVERSE_NONE, POLY_8B - 0xd8

IN_REVERSE_WORD, OUT_REVERSE_NONE, POLY_7B - 0x3

IN_REVERSE_WORD, OUT_REVERSE_BIT, POLY_32B - 0xd2eef894

IN_REVERSE_WORD, OUT_REVERSE_BIT, POLY_16B - 0xc88d

IN_REVERSE_WORD, OUT_REVERSE_BIT, POLY_8B - 0xbd

IN_REVERSE_WORD, OUT_REVERSE_BIT, POLY_7B - 0x1e

Software CRC32 - 0xe8bccd9a

Software CRC16 - 0xf6f4

Software CRC8 - 0xea

 CRC calculate function:

TYPE: CRC32 --> uint32_t, CRC16 --> uint16_t, CRC8 --> uint8_t

TYPE CRC_CalculateType(TYPE data)
{
	const TYPE polynom = LL_CRC_GetPolynomialCoef(CRC);
	const TYPE max_bits = sizeof(data) * 8;
	const TYPE mask = (TYPE ) 1 << (max_bits - 1);	
 
	TYPE crc = LL_CRC_GetInitialData(CRC) ^ data;
	for(int i = 0; i < max_bits; ++i)
        {
                crc = (crc & mask) \
                        ? (crc << 1 ^ polynom) \
                        : (crc << 1);
	}
	return crc;
}

3 REPLIES 3

>> The algorithm described in AN4187 only works with a 32-bit

Honestly seems more general than that. But does make some assumptions about bit shift direction, and data lsb/msb

Some of the STM32 products only support 32-bit in hardware, and have some novel shift, word length, and endian usage.

https://www.st.com/resource/en/application_note/dm00068118-using-the-crc-peripheral-in-the-stm32-family-stmicroelectronics.pdf

>>does anyone know how to programmatically calculate a CRC with a polynomial length of 8 and 16 bits?

Yes, I've built a number of HW and SW implementations. Posted several table driven methods to the forum.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Can you attach the links?

I'm stuck using the site search like everyone else...

Here's one https://community.st.com/s/question/0D50X0000CDmAkpSQF/calculate-crc8

I'd imagine if you dig you can find others.

The code you posted presupposes the data length is the same as the CRC, most cases people feed in bytes, there endian ordering becomes an issue. It also shifts left and assumes the data comes msb first.

What polynomial, size and shift direction are you looking for? Provide the specification, and example data.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..