cancel
Showing results for 
Search instead for 
Did you mean: 

memcmp crashes when accessing data stored in flash

KWest.1
Associate II

STM32H745zi

I am storing some configuration data in flash memory at address 0x080E0000.

If I call HAL_FLASHEx_Erase and then do a memcmp of 24 bytes at this address vs a buffer where I have prepared data I want to write there everything is fine although of course the data does not match.

However if i dont erase it and I just call memcmp of the new data with the data at this address I get this crash in the memcmp function:

CM7: r0 = 0x080e0000

CM7: r1 = 0x20000cf0

CM7: r2 = 0x00000018

CM7: r3 = 0x00000000

CM7: r12 = 0x2001fef3

CM7: lr = 0x08001c11

CM7: return_address = 0x08008778

CM7: xpsr = 0x21000200

CM7: SCB->HFSR = 0x40000000

CM7: Forced Hard Fault

CM7: SCB->CFSR = 0x00008200

CM7:  SCB->BFSR = 0x82

CM7:    BFARVALID - bfar address was the cause

CM7:       SCB->BFAR = 0x080e0000

CM7:    PRECISERR - instruction triggered the problem

R0-2 are the parameters I passed to memcmp

The assembly dump at 0x08008778 is

     memcmp:

0800876c:  push  {r4, r5, lr}

0800876e:  movs  r4, #0

08008770:  cmp   r2, r4

08008772:  bne.n  0x8008778 <memcmp+12>

08008774:  movs  r0, #0

08008776:  b.n   0x8008788 <memcmp+28>

08008778:  ldrb  r3, [r0, r4]

so basically it is crashing on the loading of the byte at 0x080E0000 ... is doing a memcmp illegal for the STM32H7xx not a legal operation?

3 REPLIES 3

Knowing what R4 contained would be helpful in this context. The handler I use dumps R4-R6 and the stack.

memcpy should work, something else is going on here.

Might want to check whats going on with the cache. Be very careful using the DCache Invalidate function.

The CM7 can fault on unaligned LDRD/STRD used in some optimizations.

Check the stack isn't misaligned, ie estack=0x2001FFFF !!

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
KWest.1
Associate II

eStack is 0x20020000 in my ld file so i think that is aligned.

I will need to try to work out code to capture the other registers.

I was using -Og (optimise for debug) in this test.

I am not sure what you mean about your DCache comment ... is there an article or something that outlines things to be wary of?

KWest.1
Associate II

I think I know what I have wrong.

It seems when you write to flash you write once to a page and once you close it you cannot write to it again until you erase the whole 128KB page. A failed write (of which i had many) likely left the page in bad shape.