2025-05-29 1:16 PM - edited 2025-05-29 1:21 PM
Here's my infinite loop in a cpp file
#include "main.h"
#include "lib.hpp"
void run() {
auto cnt = 0u;
while (1) {
if(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13))
cnt++;
}
}
I run the Debugger and execute the statements until I get to the "if" statement above then when pressing F6 another time the execution stops waiting to read the value of pin 13 (the blue button on the board). But while I have also added "cnt" onto the "live expressions" its value doesn't go beyond 0 even though I press the button couple of times.
My purpose is to see the value of "cnt" after pressing the button no matter how much it is.
Solved! Go to Solution.
2025-05-30 2:14 PM
Apparently the problem is with `cnt` being a local variable. When I make it global, i.e., declared before `void run() { }` it works properly and Live Expressions shows a big number for it after pressing the button even only once.
2025-05-29 1:42 PM - edited 2025-05-29 1:43 PM
Look at the disassembly. The compiler is probably optimizing it away since it has no effect.
Make cnt volatile if you want to avoid this.
volatile uint32_t cnt = 0;
2025-05-29 1:51 PM - edited 2025-05-29 1:55 PM
That's right, I forgot that.
Used `volatile auto cnt = ou;` and `cnt = cnt + 1;` then: debugger -> F6 until I reach the `if` statement then another F6 and pressing the button, yet nothing changes. The control also gets out of the debugger there.
EDIT: The value in the "live expressions" changes for `cnt` if I press and hold the button for a few moments!
2025-05-29 1:57 PM - edited 2025-05-29 1:57 PM
Perhaps don't F6 to single step, but let the program run normally and observe. Hit pause if you need it to stop.
2025-05-29 2:08 PM
Sorry I didn't get that (especially the part "run normally").
You mean to still run the debugger, yeah?
Then it stops at line `HAL_Init();` in `main.c` and I need to press F6 to let it go forward.
2025-05-29 2:34 PM
Ok, so you don't specify a board
Or how/where run() gets called, or where you initialize the pin, etc. So "run normally" would suggest doing whatever you're currently doing to make it execute, absent any context. Press the button several times, stop in the debugger, and see how many thousands of counts you got as in loops whilst you have the button pressed.
Perhaps inspect the machine code output by the compiler, in cnt in a memory variable or a register.
2025-05-29 2:55 PM
I used the label STM32F4 series in my first post above. My board is STM32F411.
`run()` is called once in `main.c`:
//while (1)
{
run();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
2025-05-29 5:07 PM
F6 is step over.
F8 is what I am calling "run normally".
2025-05-30 6:05 AM - edited 2025-05-30 6:15 AM
@TechyAry wrote:I used the label STM32F4 series
That's an entire series of chips - it doesn't identify a particular chip, or a particular board.
How to write your question to maximize your chances to find a solution
@TechyAry wrote:My board is STM32F411.
That's doesn't identify a board - that's just a partial chip number.
As you mentioned a "blue button", perhaps you mean a NUCLEO-F411RE board?
https://www.st.com/en/evaluation-tools/nucleo-f411re.html
2025-05-30 6:12 AM
@TDK wrote:Hit pause if you need it to stop.
@TechyAry that's this button:
What that does is to halt the execution of your code wherever it's currently at, and allows you to use the debugger to inspect variables, etc.
To set the code running again, you use the 'Resume' button (or F8).
There are many reports on here of unreliability with "live expressions" - this approach is reliable.