2024-12-04 12:35 AM - last edited on 2024-12-04 03:03 AM by SofLit
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
}
}
2024-12-04 02:37 AM
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
2024-12-04 03:03 AM - edited 2024-12-04 03:41 AM
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:
Thank you for your understanding.
2024-12-04 03:22 AM
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?
2024-12-04 02:22 PM
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
2024-12-05 02:34 AM
You can use Linker symbols to determine the length of the binary image.
You want to be compute the CRC of the same binary data on the PC as on the STM32
Not sure why you're inverting the final output.
Also if you append the correct CRC and feed it through at the end the CRC computation will zero out as it divide cleanly thru the polynomial. ie no remainder
2024-12-06 12:34 AM
Hii sir,can you share code or related information for how to calculate CRC of flash memory of STM32L476 RG MCU.