STM32 HAL Tutorial: Ensuring Data Integrity with CRC
Abstract
Learn how to use the CRC peripheral on STM32 using CubeMX and HAL drivers. Step-by-step guide to verify data integrity for communication, memory, or sensor data.
1. Introduction
In embedded systems, ensuring data integrity is crucial.
CRC (Cyclic Redundancy Check) is a widely used method to detect errors in:
- UART, SPI, I²C data transmission
- Flash memory data
- Sensor readings or logs
By the end of this episode, you’ll be able to:
- Configure CRC peripheral using CubeMX.
- Compute CRC for arrays of data.
- Verify data integrity in real-time using STM32 HAL functions.
As described in the RM0444 and AN4187, the standard (default) generator polynomial used by the STM32 CRC peripheral is the Ethernet CRC-32 polynomial 0x04C11DB7.
Initial CRC value gives more security to CRC. It is fixed to 0xFFFFFFFF or programmable by user. At start up, algorithm sets CRC to the initial CRC value XOR with the dividend.
2. Prerequisites
- STM32 board (any with CRC peripheral, most have it built-in)
- STM32CubeIDE installed
Basic knowledge of HAL and data communication (UART, SPI, I²C)
3. Configuring CRC in CubeMX
Step 1 – Open Project
- Create a new project in STM32CubeMX.
Step 2 – Enable CRC Peripheral
- Open CubeMX → Go to Peripherals → Enable CRC.
- No pins are required (CRC is internal).
Step 3 – Configure UART2
- Configure the UART2 as: 115200 / 8 / N / 1
- Use PA2 and PA3
Step 4 – Generate Code
Click Project → Generate Code
4. Using CRC with HAL
Step 1 – Compute CRC for an Array
uint32_t data[5] = {0x12345678, 0x87654321, 0xAABBCCDD, 0x11223344, 0xDEADBEEF};
uint32_t crcValue;
crcValue = HAL_CRC_Calculate(&hcrc, data, 5);
- &hcrc → Handle created by CubeMX for CRC peripheral
- data → Array to compute CRC for
- 5 → Number of 32-bit words in the array
Step 2 – Code implementation
Use the USER CODE to guide the portions of code you should copy and paste into your application.
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
/* USER CODE BEGIN PV */
uint32_t data[5] = {0x12345678, 0x87654321, 0xAABBCCDD, 0x11223344, 0xDEADBEEF};
__IO uint32_t u32CRCValueHw = 0;
__IO uint32_t u32CRCValueSoft= 0xFFFFFFFF;
/* USER CODE END PV */
/* USER CODE BEGIN PFP */
uint32_t Crc32(uint32_t Crc, uint32_t Data);
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart2,(uint8_t *) &ch, 1, 100);
return ch;
}
/* USER CODE END 0 */
/* USER CODE BEGIN 2 */
printf("\n\r================================ CRC TEST ================================\n\r");
u32CRCValueHw = HAL_CRC_Calculate(&hcrc, data, 5);
for(uint8_t i = 0; i <5; i++)
{
u32CRCValueSoft = Crc32(u32CRCValueSoft, data[i]);
}
printf("\tChecksum generated by STM32 hardware CRC Unit = 0x%08X\n\r", (unsigned int)u32CRCValueHw);
printf("\tChecksum generated by STM32 software CRC = 0x%08X\n\r", (unsigned int)u32CRCValueSoft);
/* USER CODE END 2 */
/* USER CODE BEGIN 4 */
uint32_t Crc32(uint32_t Crc, uint32_t Data)
{
uint8_t index;
Crc = Crc ^ Data;
for(index=0; index<32; index++)
{
if (Crc & 0x80000000)
Crc = (Crc << 1) ^ 0x04C11DB7; // Polynomial used in STM32
else
Crc = (Crc << 1);
}
return(Crc);
}
/* USER CODE END 4 */
5. Hands-On Lab Example
- Create a 5-word data array
- Compute CRC using HAL
- Compare it with the Software CRC Calculation
- Print both results on the terminal
Tip: Combine this with UART output for real-time debugging.
Full Source Code: hackerembedded/STM32_EP13
6. Compiling and Running
- Build Project → Click hammer icon.
- Flash Project → Connect STM32 and run (Ctrl + F11).
- Test: Monitor the results via serial terminal
7. Advantages of STM32 CRC Peripheral
- Hardware-accelerated → fast computation
- Reliable for data integrity checks
- Works for arrays of any size (multiple words)
- Ideal for communication protocols and memory safety
8. Common Issues & Fixes
| Issue | Cause | Solution |
|---|---|---|
| HAL_CRC_Calculate fails | CRC peripheral not initialized | Check CubeMX code generation |
| CRC mismatch | Wrong array length or data | Ensure correct number of 32-bit words and the size |
| Compiler error | Missing #include "stm32gxxx_hal_crc.h" | Include HAL CRC header |


