This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Reading to clear a register without warnings

Writing for a DM643x DSP.  I need to read a register in order to clear the hardware so I make a line "temp=register".  The function does nothing with the result that is read.  So I get a warning--

"func.c", line xx: warning: variable "temp" was set but never used

Two issues--

1. Clean code--how do I do this so I don't get a warning?

2. Reliable compiles--the register is listed as volatile and the code seems to be working.  However, given the warning, I want to make sure that the compiler does not optimize out this read operation.  Is there something I can or need to do to make sure this read operation takes place?

 

 

 

  • The way I would do it is to declare temp as a file level variable (not static). This will get rid of your warning and also ensure (as not static) that it will actually be read, so, for example:

    u32 myTemp;

    void myFunc(void)

    {

        myTemp = registerName;

    }

    Regards, Tony.

  • You can suppress the warning in a number of ways.  You can suppress that particular warning for the entire compilation using the -pds option (in particular -pds552).  Or, you can use "#pragma diag_suppress 552" to suppress that warning at particular points in the source code.  (See the Compiler User's Guide section on "Diagnostic Message Pragmas".)

    If your register variable is declared to be volatile all reads (and writes) of that variable will always be done.  In fact, you can read a volatile without storing it anywhere.

       volatile int reg;
       ...
       reg;  /* This always causes a read of the location allocated to 'reg' */
       ...