[En-Nut-Discussion] Hardware Register Access Using Volatile Pointer

Harald Kipp harald.kipp at egnite.de
Mon Apr 25 19:51:25 CEST 2011


Hi all,

today (with GCC) we are using

 #define inr(_reg)   (*((volatile unsigned int *)(_reg)))

and friend to directly access I/O registers. It seems to work, but that's deceptive.

The problem is, that the compiler will not guarantee, that the sequence of statements is kept. To force this, a memory barrier can be used

 asm volatile("": : :"memory");

If used, the volatile attribute in inr() is dispensable, because the memory barrier will enforce the write.

Now, where I stumbled is the case, where I simply want to read an I/O status register to clear a flag. In this case I'm not interested in the value, I simply want to read from the given address and throw away the contents. The question is: Will

 #define inr(_reg)   (*((unsigned int *)(_reg)))
 #define mem_barrier() asm volatile("": : :"memory")

 inr(TC0_SR);
 mem_barrier();

ensure, that a value is read to clear the AT91 timer status register TC0_SR? Or might it be possible that the compiler optimizes out the register access?

Regards,

Harald



More information about the En-Nut-Discussion mailing list