cancel
Showing results for 
Search instead for 
Did you mean: 

Is there any example program for HAL_FLASHEx_ComputeCRC function

Rajkiran
Associate

Hi,

I wish to know is there any example program for calculating CRC for flash Bank using "HAL_FLASHEx_ComputeCRC() " HAL driver.

9 REPLIES 9
SF??r
Associate III

Hello,

would be interessing for me as well... on a broken Y revision I give up at the moment...

My source to check an erased (0xFF) second flash bank looks like this:

	  FLASH_CRCInitTypeDef crchandle = { 0 };
	  crchandle.Bank = FLASH_BANK_2;
	  crchandle.CRCStartAddr = FLASH_BANK2_BASE;
	  crchandle.CRCEndAddr = FLASH_BANK2_BASE + 0x100;
	  crchandle.TypeCRC = FLASH_CRC_ADDR;
	  crchandle.BurstSize = FLASH_CRC_BURST_SIZE_4;
 
	  uint32_t crc_out = 0;
	  HAL_FLASHEx_Unlock_Bank2();
	  FLASH->CRCCR2 = 0; //workaround
	  if(HAL_OK == HAL_FLASHEx_ComputeCRC(&crchandle, &crc_out))
		  logging_debug(LOGMOD_USBHOST, "CRC blocking: 0x%08X", crc_out);
	  HAL_FLASHEx_Lock_Bank2();

But fails...

Why? Errate says: Do not use Flash CRC but also that just fails at sector 7 ?!?!?!?!?!?!

Code fragment works on the V step

Core=400000000, 400 MHz

CPUID 411FC271 DEVID 450 REVID 2003

Cortex M7 r1p1

STM32H7xx

CRC blocking: 0x349A943E

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SF??r
Associate III

Hm, did you check the crc output?

I didn't get this result if I check it with this website:

http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

Here the 256 Bytes flash data:

0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xF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FF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF

>>Hm, did you check the crc output?

I didn't, but it is not blank, it has code for the CM4 core in it. I thought the issue was it seized up. Will cross-check the data later.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SF??r
Associate III

Have a V Revision now....

Same test: 0xB02CC19B

makes even no sense?! Can anyone help?

The minimum size seems to be 512-bytes (at burst 4).

I can make the sector (128KB), and bank (1MB) generate the same numbers as the equivalent address span.

So the numbers are consistent, but the math is not, currently got a query in about that.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Evangelist III
void flash_crc_test(void)
{
    FLASH_CRCInitTypeDef CRCInit = {};
    HAL_StatusTypeDef st;
    uint32_t CRC_Result;
 
    ttyprintf("Enter sector num. to CRC (0-15), or B1/B2 for whole bank:");
    char *s = tty_getline();
    if (!s) goto error;
    if (s[0] == 'b' || s[0] == 'B') {
        // CRC on bank
        CRCInit.TypeCRC = FLASH_CRC_BANK;
        if (s[1] == '1') {
            CRCInit.Bank = FLASH_BANK_1;
        } else if (s[1] == '2') {
            CRCInit.Bank = FLASH_BANK_2;
        }
        else {
            goto error;
        }
    } else {
        // CRC on sector
        CRCInit.TypeCRC = FLASH_CRC_SECTORS;
        CRCInit.Sector = atoi(s);
        CRCInit.Bank = FLASH_BANK_1;
        if (CRCInit.Sector >= FLASH_NUM_SECTORS_PER_BANK) {
            CRCInit.Bank = FLASH_BANK_2;
            CRCInit.Sector -= FLASH_NUM_SECTORS_PER_BANK;
        }
        CRCInit.NbSectors = 1;
    }
    CRCInit.BurstSize = FLASH_CRC_BURST_SIZE_256;
    CRCInit.CRCEndAddr = 0;
    CRCInit.CRCStartAddr = 0;
 
    // Unlock:
    if (CRCInit.Bank == FLASH_BANK_1) {
      st = HAL_FLASHEx_Unlock_Bank1();
    } else {
        st = HAL_FLASHEx_Unlock_Bank2();
    }
    if (st != HAL_OK) {
        ttyprintf("Flash unlock failed!\n");
        goto error;
    }
 
    uint32_t start_time = HAL_GetTick();
    st = HAL_FLASHEx_ComputeCRC(&CRCInit, &CRC_Result);
    if (st == HAL_OK) {
        ttyprintf("CRC=%8.8X time=%u ms\n", (unsigned)CRC_Result, (unsigned)(HAL_GetTick() - start_time));
    } else {
        ttyprintf("CRC calc error!\n");
    }
    return;
    error:
    ttyprintf("Error\n");
}

This example is for STM32H743/753 with two banks, 8 blocks each.

Don't forget to unlock.

-- pa

BTrav.1
Associate

Same issue for me - I'm running the FLASH CRC across an erased section of FLASH, aligned to my burst size, and I get 0xb02cc19b - same value you got. I went through the datasheet with a fine-tooth comb and stepped through the code, watching registers, and everything looks right, but it's giving the wrong results. (This is a STM32H753, silicon V) I'm starting to wonder if there's something missing like an erased value doesn't read 0xFF to the CRC module or there's another clock enable I need or something...

Did you ever get it figured out?

Ripley
Associate II

Hello,

Any news on the topic ? I observe same issue