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: E. Weddington
Subject: Re: [avr-libc-dev] [RFC] passing port info to a function
Date: Fri, 04 Oct 2002 09:05:14 -0600

On 3 Oct 2002 at 17:03, Theodore A. Roth wrote:

> Hi,
> 
> This is regarding this thread:
> 
> http://mail.freesoftware.fsf.org/pipermail/avr-libc-dev/2002-September
> /000312.html
> 
> I just got bit by this myself and wasted time figuring it out only to
> see that it had already been solved. D'oh!
> 
> So, to hopefully save others from having to figure it out yet again,
> I'm thinking about making a new FAQ entry: "How do I access an IO
> register that is passed in to a function?"
> 
> I'm thinking about including this code and dissecting it:
> 
> cat <<EOF > example.c
> #include <inttypes.h>
> #include <avr/io.h>
> 
> void
> set_bits_func_wrong (volatile uint8_t port, uint8_t mask)
> {
>     port |= mask;
> }
> 
> #define PORTB_ADDR ((uint8_t *)_SFR_ADDR(PORTB))
>

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)


> #define set_bits_macro(port,mask) ((port) |= (mask))
> 
> int main (void)
> {
>     set_bits_func_wrong (PORTB, 0xaa);
>     set_bits_func_correct (PORTB_ADDR, 0x55);
>     set_bits_macro (PORTB, 0x00);
> 
>     return (0);
> }
> EOF
> 
> Looking at the disassmbly of this code is very instructional. You can
> see how the macro is best in this simple (contrived) example. The
> correct function would become a code size win if you are doing enough
> in the function to overcome the function call overhead (saving space,
> but being a tad slower accessing the io port).
> 
> Does this seem like a reasonable thing to document?
> 
Absolutely!

Eric





reply via email to

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