cancel
Showing results for 
Search instead for 
Did you mean: 

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

Johi
Senior III

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?  

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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".

View solution in original post

3 REPLIES 3
TDK
Guru

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
Senior III

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
Guru

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".