STM32 hardware CRC

Brief introduction

Based STM32F107VCT6 introduction STM32 and CRC hardware usage, and logging software testing to achieve.

STM32 introduction of CRC


Described in the manual using the STM32 CRC check hardware 32-bit CRC check polynomial 0x04C11DB7; 32bits-time calculation CRC calculation, the byte is not in accordance with the operation; CRC_DR register is reset 0xFFFFFFFF, i.e., the initial value of the CRC calculation .

STM32 use of CRC

  • The STM32 CRC_DR register as both an input register and an output register
  • As a direct write to the CRC calculation input data register
  • As an output register, return to the last CRC calculation result by the read operation
  • The results of the write data register each time a calculation result is a combination of new and previous results; CRC calculation before a new data block, the register needs to be reset CRC_DR

STM32F107 the standard library as an example, the CRC calculation step

uint32_t buff[4] = {0x01,0x02,0x03,0x04};
CRC_ResetDR();
CRC_CalcBlockCRC(buff,4);
delay(5);
ret = CRC_GetCRC();

STM32 software implementation of the CRC

STM32 hardware CRC calculated and the calculated result of the CRC32 conventional CRC values are different, if the communications hardware used in the CRC calculation value of the STM32, external with hardware implementations consistent CRC calculation result, the following detailed code, providing a reference link

uint32_t crc32_st(uint32_t *pbuf, uint32_t size)
{
	const uint32_t st_const_value = 0x04c11db7;
	uint32_t	crc_value = 0xffffffff;
	uint32_t	xbit;
	uint32_t 	bits;
	uint32_t	i;

	for (i = 0; i < size; i++)
	{
		xbit = 0x80000000;
		for (bits = 0; bits < 32; bits++)
		{
			if (crc_value & 0x80000000)
			{
				crc_value <<= 1;
				crc_value ^= st_const_value;
			}
			else
			{
				crc_value <<= 1;
			}
			if (pbuf[i] & xbit)
			{
				crc_value ^= st_const_value;
			}
			xbit >>= 1;
		}
	}
	return crc_value;
}

to sum up

STM32 when communicating with other platforms, if used CRC value, the CRC can be calculated consistent with STM32 this software.

Guess you like

Origin www.cnblogs.com/niu-li/p/12585813.html