cancel
Showing results for 
Search instead for 
Did you mean: 

Misbehaviour at different optimisation levels

FlorianPierson
Associate II

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
}

 

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
  // The controller only jumps to these two If queries at optimization level -O0 and -O1.
  if (struct_main.struUSB.bUSB_VerbindungGeaendert) { // USB connection has changed
    struct_main.struUSB.bUSB_VerbindungGeaendert = false; // Reset flag that USB connection has changed
    if (struct_main.struUSB.bUSB_Verbunden) { // USB has been connected
      // do something... This part is only reached at optimization level -O0 and -O1
    }
  }
}

Is this a bug in STM32CubeIDE or have I done something wrong in my compiler settings?

Thank you very much.

1 ACCEPTED SOLUTION

Accepted Solutions
FlorianPierson
Associate II

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.

View solution in original post

4 REPLIES 4

Variable that changes under interrupt needs to be volatile

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

Sorry, I forgot to mention that the variable was declared as volatile. Otherwise it would not work with the low optimization levels.

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. 

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

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.