When programming micro-controllers, there sometimes are registers that need to be read in order to reset some flags. These registers are memory mapped and are declared as pointers to volatile in the code.
Assume the next snippet as example:
typedef volatile struct _Ifx_SCU
{
...
uint32_t reg;
...
}Ifx_SCU;
#define MODULE_SCU ((*(Ifx_SCU*)0xF0036000u))
void foo(void)
{
...
MODULE_SCU.reg; /* Read required to reset bits in reg */
...
}
Here reg has to be read in order to reset it's bits, there is no other way to reset some bits other thatn reading them, So, MISRA rule checker is complaining that there is dead code and it's right.
My question is What would be an ALTERNATE way of reading and discarding **reg value in order to avoid having "dead code"?** Because using the method in the post Casting the results of a volatile expression to void which has a very similar situation, I still getting a MISRA c 2012 rule violation. I'm not able to change anything from the #define MODULE_SCU or the struct so a correct alternative method is the way to go.
I don't want to just silence the compiler casting to void as I read from this thread: What does void casting do? because if I cast to void then the optimizer may optimize away that read and that is not desirable.
Don't pay too much attention to the correctness of the snippet, I only included it to illustrate the question