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

Nathan Moore nategoose at gmail.com
Tue Apr 26 02:06:16 CEST 2011

The way I suggested doing it, by passing the temporary variable as an
input to an
empty inline assembly statement, gets around that.  It makes GCC think that you
have used the variable's value,  but without actually generating any
code for it.

    unsigned  tmp_var = inr(TC0_SR);
    asm volatile ("" : : "r" (tmp_var) : ); // gcc thinks we used the value here

You may have to have different sized versions of that assembly statement for
different sized values to keep GCC from thinking it needs to provide
more registers
or only read part of the value (thinking of AVR here).

Or you might be able to do something crazy like:

     #define FAKE_USE( x )  \
         do { \
              typeof(x) y = (x); \
              switch (y) { \
                    case 1: \
                         asm volatile ("" : : "r" (y) : ); \
                         break;  \
                    case 2: \
                         asm volatile ("" : : "w" (y) : ); \
                         break; \
                    default: \
                         assert(("Unexpected size in dummy usage", 0)); \
                  } \
          } while (0);

For AVR "r" is any single general register (8 bits) and "w" is a register pair.
I think that gcc warns you if it can determine that an assert will
definitely fail
 --- not sure about that, though.

Nathan Moore

More information about the En-Nut-Discussion mailing list