AnsweredAssumed Answered

STM32F103 CRC32 of uint8_t buffer

Question asked by Fede Rico on Jan 10, 2018
Latest reply on Jan 10, 2018 by Clive One

Hi there,


I wrote a serial protocol to exchange data between MCU and a PC. 

This protocol provides a checksum to verify data integrity.

At the moment I use a simple sum of each byte but I think that the use of CRC peripheral could be an improvements.


Unfortunately the CRC peripheral requires a uint32_t buffer but my payload is an uint8_t.


Casting the uint8_t to uint32_t is a problem because the CRC will be wrong because on the PC side.

I computed the CRC as byte (from serial interface)  but I computed the CRC on MCU as word (due to cast uint8_t to uint32_t ).


/* This is my packet structure */
typedef struct PROTOCOL_Packet
     uint8_t             header;
     uint16_t            length;
     uint8_t             payload[128];
     uint32_t          checksum;
} PROTOCOL_Packet_t;

/* This is my routine to compute the CRC: */

  if( CRC_Calculate( (uint32_t*)pkt->payload.bytes, (uint32_t)(*pkt).length, &pkt->checksum ) == APP_TRUE )
    SERIAL_Write( (uint8_t*)pkt, pkt->length + 7 );

/* Where CRC_Calculate is: */
uint8_t CRC_Calculate( uint32_t* buffer, uint32_t bufferLen, OUT uint32_t* crc32)

*crc32   = HAL_CRC_Calculate( &hcrc, buffer, (bufferLen/4) );

  return TRUE;



I can't change the type of my payload  to uint32_t because on the PC side I want to avoid byte manipulation to retrieve the uint32_t back from bytes.


Any suggestion?


Thanks in advance