cancel
Showing results for 
Search instead for 
Did you mean: 

CRC32 Calculation of flash memory of STM32L476RG

saurabhkore
Associate II

I am working on STM32L476RG MCU for this one i want to calculate CRC of flash memory. for this i uses 3 logics but my calculated CRC of flash memory is not matched with hex file CRC of code.i share the link here using which i calculate CRC of flash memory.

https://simplycalc.com/crc32-file.php

Logic  For crc32 calculation

#include "stm32l476xx.h"

#define FLASH_BASE_ADDR 0x08000000 // Base address of flash memory
#define FLASH_SIZE 0x80FFFFFF // Size of the flash memory (1024 KB)

void CRC32_Init(void) {
// Enable CRC peripheral clock
RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;
CRC->CR=CRC_CR_RESET; // Reset crc to ensure it is known state
}

uint32_t CalculateCRC32(uint32_t* startAddr, uint32_t length) {
uint32_t crc = 0xFFFFFFFF; // Initial value for CRC32 calculation

// Enable CRC calculation
CRC32_Init();

// Start CRC calculation over the flash memory
for (uint32_t i = 0; i < length / 4; i++) {
// Write 32-bit data from flash memory to the CRC data register
CRC->DR = *(startAddr + i);
}

// Read final CRC value from the CRC data register
crc = CRC->DR;

// Return the CRC value (inverted to match CRC32 convention)
return ~crc;
}

int main(void) {
// Calculate CRC32 for the entire flash memory
uint32_t crc32_result = CalculateCRC32((uint32_t*)FLASH_BASE_ADDR, FLASH_SIZE);

// Now, crc32_result contains the CRC32 checksum of the flash memory
while (1) {
// Main loop
}
}
4 REPLIES 4

The STM32 CRC module processes words most-significant-byte-first. This is different from most online calculators.

If you need the result the online calculator gave you, you can work around it either by feeding the CRC module byte-by-byte; or by reversing the word before writing it to CRC_DR, e.g. using the __REV() CMSIS intrinsics.

 

 

JW

SofLit
ST Employee

Hello @saurabhkore ,

Please use </> button to paste your code. See tips on posting here. I'm editing your post.

Already suggested to you to follow that tips on code posting:

SofLit_0-1733312460100.png

 

Thank you for your understanding.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

hello sir,

i implement you're logic but still it is  mismatch in CRC values. what is you're opinion on my code it is ok? sir i want to ask one more question here, i am calculating CRC32 of flash memory(1Mb) but the size of hex file of code is 5.70 kb may be the size is mismatch. for crc32 calculation of flash which size matter ie-flash memory size or hex file of code size?

You probably want calculate CRC of your binary. Not that .hex is an ASCII interpretation of the binary, so it's size is bigger than the actual binary's size. Try to generate a .bin file, or have a look at the mapfile to learn the binary's size. 

JW