avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] [RFC] passing port info to a function


From: Theodore A. Roth
Subject: Re: [avr-libc-dev] [RFC] passing port info to a function
Date: Fri, 4 Oct 2002 09:22:51 -0700 (PDT)

On Fri, 4 Oct 2002, E. Weddington wrote:

:) Caveat, add volatile:
:) #define PORTB_ADDR ((volatile uint8_t *)_SFR_ADDR(PORTB))
:)
:)
:) > void
:) > set_bits_func_correct (uint8_t *port, uint8_t mask)
:) > {
:) >     *port |= mask;
:) > }
:)
:) And here:
:) set_bits_func_correct (volatile uint8_t *port, uint8_t mask)
:)

I don't think "volatile" is needed in this instance. The reason is you are
passing the reference to the port, not the port. The compiler will not
optimize this away because it's smart enough to know that you are changing
what the pointer refers to which could affect the caller of the function.

The volatile is needed in set_bits_func_wrong() because port is passed by
value, the local _copy_ is changed, and the change can never make it out
of the function. Thus, the compiler thinks that the func does nothing
which can affect the caller, so just optimizes out the func completely.
Making port volatile, tells the compiler not to do that.

This is my interpretation of what happens when using various optimization
levels and looking at the disassembly of the object file. Feel free to
prove me wrong. ;-)

Ted Roth





reply via email to

[Prev in Thread] Current Thread [Next in Thread]