2025-07-02 12:27 AM - last edited on 2025-07-02 3:36 AM by Andrew Neil
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>
2025-07-02 12:44 AM
> 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.
2025-07-02 12:47 AM
Make it volatile if it's changed in an interrupt or callback. That way the optimizer doesn't take the code too literally.
2025-07-02 2:48 AM
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?
2025-07-02 2:55 AM
@Tesla DeLorean: I have to postpone coffee due to missing cards.
2025-07-02 2:56 AM - edited 2025-07-02 3:00 AM
@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):
The options are documented here: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
2025-07-02 3:21 AM
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.
2025-07-02 3:30 AM - edited 2025-07-02 3:32 AM
@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.
2025-07-02 3:31 AM
@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.
2025-07-02 3:37 AM
@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.