2023-11-06 03:25 AM
Hello.
I have a problem with the different optimisation levels.
I am programming the STM32F103ZET controller and using SMTCubeIDE V1.13.2
A controller pin triggers an external interrupt. This interrupt calls a function that sets a variable. This variable is queried in the main while loop.
If I compile this with the optimisation -O0 or -O1, then my program works. With all other optimisation levels, the recall function is still called, but the variable no longer seems to be changed, as no change takes place when querying in the main loop.
Here are my functions:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
// Interrupt for USB connection
if (GPIO_Pin == USB_ERKENNUNG_Pin)
{
// Check if callback is defined
if (p_eehl_interrupt__callback_usb_connect != NULL) {
(*p_eehl_interrupt__callback_usb_connect)(HAL_GPIO_ReadPin(USB_ERKENNUNG_GPIO_Port, USB_ERKENNUNG_Pin) == GPIO_PIN_SET); // Calling the callback function
}
}
}
static void main_func_recall_usb_connect(void)
{
struct_main.struUSB.bUSB_Verbunden = true;
struct_main.struUSB.bUSB_VerbindungGeaendert = true;
// If I control an LED via a pin here, this works at every optimization level
}
Is this a bug in STM32CubeIDE or have I done something wrong in my compiler settings?
Thank you very much.
Solved! Go to Solution.
2023-11-06 05:28 AM
Thank you for the quick answers. The problem was actually in a completely different place. Due to a bug in my software the main loop was blocked and therefore the if query was never processed.
I apologise for the false alarm.
2023-11-06 03:31 AM
Variable that changes under interrupt needs to be volatile
2023-11-06 04:02 AM
Sorry, I forgot to mention that the variable was declared as volatile. Otherwise it would not work with the low optimization levels.
2023-11-06 04:24 AM
At low optimization levels code is probably not moved out of the loop.
Review the generated code in assembler, see what the compiler is doing with it, and why it might be failing.
2023-11-06 05:28 AM
Thank you for the quick answers. The problem was actually in a completely different place. Due to a bug in my software the main loop was blocked and therefore the if query was never processed.
I apologise for the false alarm.