Hi,
I would like to prevent structure members optimization. I used volatile keyword:
typedef volatile struct { volatile ... volatile uint32_t discard; volatile ... } regs_t;
I declared a member in another non-volatile struct:
typedef struct { ... volatile regs_t *regs; ... } peripheral_t;
I used an "empty" expression to just clear flags in register (the memory read access must be performed):
peripheral_t p; p.regs = 0x12345678; /* init to register block starting address */ p.regs->discard; /* gets optimized out when opt. is on */
The "empty" expression gets optimized out although I thought (hoped) I prevented it via the volatile keywords. - What is the right place to put the volatile? - Does the whole struct have to be volatile? - Does it make sense to define volatile type? (typedef volatile struct...)
Thanks for tips/explanations.
Regards Pavel
"to just clear flags in register"
That's a fundamaentally flawed idea in a High-Level Language (HLL).
You have no control over what instructions the compiler may generate for the source - it may do other stuff that affects flags...
If you need specific control of specific flags, you probably should be writing in assembler!
"the memory read access must be performed"
IS that true?
Andy,
I think the OP is talking about accessing peripheral registers. Using a structure with volatile members and the base address of the peripheral converted into a pointer to the appropriate structure to access peripheral registers is one of the common ways to communicate with a peripheral in C.
View all questions in Keil forum