cancel
Showing results for 
Search instead for 
Did you mean: 

Volatile qualifier does not matter in my interrupt routine

MJoe.1
Associate II

I am new to embedded C, and I recently watched some videos about volatile qualifier. They all mention about the same things. The scenarios for the use of a volatile qualifier :

1. when reading or writing a variable in ISR (interrupt service routine)

2. RTOS application or multi thread (which is not my case)

3. memory mapped IO (which is also not my case)

My question is that my code does not stuck in the `whiletest();`function below

 when my UART receives data and then triggers the `void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)` interrupt function

```

int test;

int main(void)

{

 test = 0;

 MX_GPIO_Init();

 MX_USART1_UART_Init();

 HAL_UART_Receive_IT(&huart1, (uint8_t *)&ch, 1);

 while (1)

  {

               Delay(500);             

               printf("the main is runing\r\n");

       whiletest();

  }

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

       if(huart->Instance == USART1)

       {

       Test = 1;

               HAL_UART_Receive_IT(&huart1, (uint8_t *)&ch, 1);

       }

}

void whiletest(void)

{

int count =0;

while(!test){

 count++;

 printf("%d\r\n",count);

 Delay(2000);

 }

}

```

I use keil IDE and stm32cubeIDE. I learned that the compiler would optimize some instructions away if you choose the o2 or o3 optimization level. Therefore, I chose the o2 level for build option, but it seems no effect on my code. The compiler does not optimize the load instruction away in the while loop and cache the test value `0` in the main function as the videos teach on youtube. It is confusing. In what situation I am supposed to use volatile qualifier while keep my code optimized (o2 or o3 level).zed (o2 or o3 level)

note: I am using stm32h743zi (M7)

1 ACCEPTED SOLUTION

Accepted Solutions

> I learned that the compiler would optimize some instructions away if you choose the o2 or o3 optimization level.

The compiler *may* optimize some instructions away, but it also may not, regardless of its settings.

volatile is there to tell the compiler that it *must not* optimize accesses to given variable.

(Modal verbs in English are confusing... As the english wikipedia - characteristically using an even more confusing pun - explains: "modal verbs are negated by the addition of the word not after them. The modification of meaning may not always correspond to simple negation, as in the case of must not.")

JW

View solution in original post

5 REPLIES 5
TDK
Guru

The code you posted doesn't have "volatile" in it anywhere. It's not clear what you're testing.

 test = 0;
...
       Test = 1;

Are you expecting these two to have the same value? Variables are case sensitive, thus they are different variables.

If you feel a post has answered your question, please click "Accept as Solution".

If the variable is changed outside of normal program flow, ie in an interrupt, a hardware register that's non-memory, or a DMA transfer into the variable.

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

> I learned that the compiler would optimize some instructions away if you choose the o2 or o3 optimization level.

The compiler *may* optimize some instructions away, but it also may not, regardless of its settings.

volatile is there to tell the compiler that it *must not* optimize accesses to given variable.

(Modal verbs in English are confusing... As the english wikipedia - characteristically using an even more confusing pun - explains: "modal verbs are negated by the addition of the word not after them. The modification of meaning may not always correspond to simple negation, as in the case of must not.")

JW

MJoe.1
Associate II

Sorry, that was a typo. It is test.

got it, so the volatile guarantees that the compiler will not optimise out the access. thx!