cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L052C8T3/ STM32CubeIDE: do-while loop leads to false disassembly and hanging in release-mode

FEber.1
Associate III

In search of the causes of excessive power consumption, I had the firmware compiled for release mode. This caused the program to hang in the first while loop.
The cause is an incorrect compilation. I cannot resolve this and would like to know what I am doing wrong.

Written in C

...
intcontent=0;
do
{
intcontent=adcdone;
}
while (intcontent==0);
...

Disassambly in debug mode:
...
intcontent=adcdone;
08000eb2: ldr r3, [pc, #204] ; (0x8000f80 <MainMeasureVoltage+304>)
08000eb4: ldrb r3, [r3, #0]
121 while (intcontent==0);
08000eb6: cmp r3, #0
08000eb8: beq.n 0x8000eb2 <MainMeasureVoltage+98>
...

Disassambly in release mode:
...
08000e3e: ldr r3, [pc, #168] ; (0x8000ee8 <MainMeasureVoltage+264>)
08000e40: ldrb r3, [r3, #0]
08000e42: cmp r3, #0
08000e44: bne.n 0x8000e48 <MainMeasureVoltage+104>
08000e46: b.n 0x8000e46 <MainMeasureVoltage+102>
...

Result:
Programming is hanging in: 08000e46: b.n 0x8000e46 <MainMeasureVoltage+102>

9 REPLIES 9
Ozone
Principal

> The cause is an incorrect compilation. I cannot resolve this and would like to know what I am doing wrong.

This is usually the wrong approach.
In 99,9% of cases, the problem is in front of the keyboard.

> In search of the causes of excessive power consumption, I had the firmware compiled for release mode. This caused the program to hang in the first while loop.

Your code fragment does not reveal much.
But first, "release mode" usually implies optimisation levels > 0.
I suppose your "adcdone" variable is set elsewhere, most probably in an interrupt routine. And interrupt routines are never detected as "called" by statical analysis, thus the compiler considers your loop pointless.
Unless you declare such variables as "volatile", the compiler will optimizing them away.

Make it volatile if it's changed in an interrupt or callback. That way the optimizer doesn't take the code too literally.

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

To "Ozone" & "Tesla DeLorean"

Thank you very much for your quick help.
Volatile was the magic word.

And another question for Ozone, where can I set the optimization level?

FEber.1
Associate III

@Tesla DeLorean: I have to postpone coffee due to missing cards.


@FEber.1 wrote:

Volatile was the magic word


Good news! Now please mark that as the solution.

Whenever something breaks on increasing optimisation level, it is usually due to issues with the code - and lack of 'volatile' is a favourite.

 

Please also see: How to insert source code.

 


@FEber.1 wrote:

where can I set the optimization level?


In the Project Properties (right-click Project name):

AndrewNeil_0-1751450170852.png

 

The options are documented here: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Where the IDE usually shows the build settings.
I don't use CubeIDE (which is basically Eclipse), but I suppose there is plenty of documentation and tutorials.

The debug and release configurations, which are usually created by default, have separate build settings.
You can have more configuration if you wish, each with different settings.

But I suggest to focus more on the background of the issue.
C was initially created about 50 years ago, and has no inherent concept of concurrency or multithreading. That has to come from the developer - you in this case.
The use of "volatile" is e.g. a lesson we all learned one or the other way.


@Ozone wrote:

The debug and release configurations, which are usually created by default, have separate build settings.
You can have more configuration if you wish, each with different settings.


@FEber.1 and you can modify the built-in ones, if you wish.

The screenshot I showed is my modified "Debug" Configuration.

 


@Ozone wrote:

The use of "volatile" is e.g. a lesson we all learned one or the other way.


Indeed.

And the optimiser can still catch us out; eg, No code generated for inline function call.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
FEber.1
Associate III

@Andrew Neil: Thank you very much. My surface looks a little different and I didn't find it straight away, although I was at this point at the beginning of the project.
This life is one of the hardest.

@Ozone:Many thanks to you too. Unfortunately, I no longer have a colleague at my location that I could ask. And I'm also not continuously involved in programming.