Discussion:
[Sdcc-user] problem with volatile struct member
Daniel Drotos
2017-07-18 14:59:15 UTC
Permalink
Hi,

I'm using sdcc-stm8 with following code:

struct FLASH_t { ... ; volatile uint8_t iapsr; ...};
#define FLASH ((struct FLASH_t *)BASE_ADDRESS_OF_FLASH_CONTROLLER)

Program waits end of flash operation in a busy loop:

while ((FLASH->iapsr & 5) == 0) ...;

it works well rereading value of iapsr in every check. But in
following structure:

uint8_t r= FLASH->iapsr;
while ((r & 5) == 0)
r= FLASH->iapsr;

iapsr is read just once and the cycle uses that value without
rereading the register. Even the following code uses the value which
has been read before the cycle:

uint8_t r= FLASH->iapsr & 5;
while ((FLASH->iapsr & 5) == 0) ...

It looks that "volatile"-ness of a struct member disappears in some
situations. Is it OK in C, or is it a bug?

Daniel

Continue reading on narkive:
Loading...