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 :
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;
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)
My way of trying to figure out the problem could be the culprit here ! Thank you for providing such an insightful answer. I will look up the assembly code with delay() and printf() deleted.
I know a company https://sirinsoftware.com/services/embedded-software-development-services/ that specializes well in this kind of development. they will be able to help in any question related to this type of development