cancel
Showing results for 
Search instead for 
Did you mean: 

Variable address in memory is changing when reading I2C1 CR2 register. STM32H503CBU6.

evan-rasmussen
Associate

Setup Info:

Compiler: IAR EW for Arm 9.50.1, Debug build configuration, High optimization

Micro: STM32H503CBU6

 

I am running into an issue where when I am trying to copy the CR2 register from I2C1, it copies the wrong value and the addresses of my other variables change.

Before attempt to copy CR2, here's the register values and variable values and addresses:

evanrasmussen_0-1714581645903.png

After stepping over this line, this is what I am seeing for the variable values and addresses:

evanrasmussen_1-1714581718152.png

As you can see, not only is the copied value of CR2 wrong (should be 0 after clearing the specified bits), but the values and locations of my other variables have changed as well. Please note that I2c[m_HalIndex] is the same as I2C1; I have it defined in a header file elsewhere. Putting I2C1 explicitly was part of debugging what could be going on.

I haven't seen this sort of issue before. The code I am using was working on a STM32G071, and I've switched to the H503 because of the faster clock speed. Any guidance on this would be much appreciated. Please let me know if you need any more information, but be advised, this is part of a company project so I can only share so much.

Thank you!

3 REPLIES 3

Debuggers are often confused by optimizations.

Christian N
ST Employee

This post has been escalated to the ST Online Support Team for additional assistance. We'll contact you directly.

 

Dazai
Associate III

As @David Littell mentioned, it is possible you are seeing weird values because you have high optimization set in the compiler, which can confuse the debugger when trying to step through code. I would suggest setting it to low, debug, or no optimization, and see if the values are correct. You might also consider splitting that line to first assign TempCr2 to I2C1->CR2, then applying the bitmask, to ensure you at least are getting a good read of CR2 by itself.

Is that code in an ISR? If so, are you using the 'volatile' keyword for variables modified in the ISR?