cancel
Showing results for 
Search instead for 
Did you mean: 

assignment discards volatile qualifier problem

WM_IR
Senior

I have a SPI Receive Interrupt function. where I have use *pRxBuffer as a volatile. But, I get the error of "assignment discards volatile qualifier". As attached in the figure below. How do I fix that line to make sure the warning will go away?

0693W00000BadmeQAB.png

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

It should be enough to just cast it to a non-volatile pointer directly.

pSPIHandle->pRxBuffer = (uint8_t *) pRxBuffer;

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

View solution in original post

6 REPLIES 6

An ugly but working solution is to cast to integer type and then back to pointer type. I use uintptr_t for that purpose.

pSPIHandle->pRxBuffer = (uint8_t *)(uintptr_t)pRxBuffer;

JW

TDK
Guru

It should be enough to just cast it to a non-volatile pointer directly.

pSPIHandle->pRxBuffer = (uint8_t *) pRxBuffer;

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

Yes this works. Could explain what is happening actually? I do not really understand. In the function SPIReceiveDataIT argument number 2, I put a volatile data type. Then, why I need cast back into non-volatile pointer again?

> Then, why I need cast back into non-volatile pointer again?

The target variable might not need the volatile qualifier. That depends on the context, you need to know.

That cast is required to satisfy the pedantic MISRA compatibility, I think.

> It should be enough to just cast it to a non-volatile pointer directly.

That depends on particular toolchain and its settings, in case of the combination of gcc and settings I'm using it does not. I'm not going to investigate, which setting enforces this, maybe -Wpedantic?

> Then, why I need cast back into non-volatile pointer again?

This is a warning (maybe escalated to error through the -Werror). Ozone above explained the reason for this particular case.

The C standard suggests some warnings (standard calls them diagnostics) and allows the toolchain to emit any extra warnings at its discretion. The selection of operations deemed dangerous enough to warrant a warning is at the toolchains' creators personal perception (sometimes supported by a similarly dubious selection, such as MISRA mentioned by Ozone above).

In gcc, you can switch warnings selectively off, either through command-line switches https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, through attribute selectively for a given function (see https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes "warning" attribute), or selectively for given source lines through a pragma (but this can be tricky if optimization is used, so I won't give a link here).

JW

Volatile means the compiler is prevented from optimizing out accesses to that variable. When the compiler generates code for a function, it does so with this in mine. Removing the qualifier would break this restriction as functions generated without this qualifier would not adhere to the restriction.

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