Skip to main content
Johi
Senior II
Sep 9, 2023 10:16 AM UTC
Solved

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

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.
TDK
TDKBest answer
Sep 12, 2023 4:10 PM UTC

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

3 replies

TDK
Sep 12, 2023 1:58 PM UTC

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
Sep 12, 2023 3:53 PM UTC

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
Sep 12, 2023 4:10 PM UTC

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