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

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

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


From: Theodore A. Roth
Subject: [avr-libc-dev] [RFC] passing port info to a function
Date: Thu, 3 Oct 2002 17:03:18 -0700 (PDT)

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))

void
set_bits_func_correct (uint8_t *port, uint8_t mask)
{
    *port |= 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?

Ted Roth





reply via email to

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