Skip to main content
Johi
Senior II
September 9, 2023
Solved

What is the added value of x=x; in driver code (FSMC)

  • September 9, 2023
  • 3 replies
  • 1239 views

I am porting ILI9341 code from Keil to Cube MX.

The original code contains:

 

 

void LCD_WR_REG(vu16 regval)
{ 
regval=regval; //ʹÓÃ-O2ÓÅ»¯µÄʱºò,±ØÐë²åÈëµÄÑÓʱ
LCD->LCD_REG=regval;//дÈëҪдµÄ¼Ä´æÆ÷ÐòºÅ 
}

 

 

vu16 seems to be __IO uint16_t i.e. volatile uint16_t

my question: regval=regval; seems obsolete code, but as the driver code seems to be written by somebody that knows what he or she is doing, I wonder what the reason behind this statement would be?  

This topic has been closed for replies.
Best answer by TDK

Accesses (reads and writes) to volatile variables cannot be optimized out. They must be performed on every read and write access--that is the effect of the "volatile" qualifier.

But yes, without the "volatile" qualifier they could be optimized out.

3 replies

TDK
Super User
September 12, 2023

It adds a small delay but is otherwise useless. The variable is stored on the stack. Reading/writing shouldn't have an effect outside of the delay due to the additional read/write.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Johi
JohiAuthor
Senior II
September 12, 2023

Thank you for the answer, it is in line with my expectations.

Can I assume that an optimizing compiler would remove this statement unless the variable is declared as volatile blocking the previously mentioned optimisation?

TDK
TDKBest answer
Super User
September 12, 2023

Accesses (reads and writes) to volatile variables cannot be optimized out. They must be performed on every read and write access--that is the effect of the "volatile" qualifier.

But yes, without the "volatile" qualifier they could be optimized out.

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