avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] arrays in avrgcc


From: Theodore A. Roth
Subject: Re: [avr-gcc-list] arrays in avrgcc
Date: Sat, 27 Sep 2003 21:50:37 -0700 (PDT)

On Fri, 26 Sep 2003, Reza Naima wrote:

:)I've actually tried putting PORTn or PINn in a struct, and the code
:)didn't work.  I never figured out how the macros expanded, but it just
:)seems like you can't do it.  

Yes, it can be done. Here's some code I wrote for a uart driver for the 
mega128:
                                                                                
struct uart_io
{
    volatile uint8_t *udr;
    volatile uint8_t *ucsr_a;
    volatile uint8_t *ucsr_b;
    volatile uint8_t *ucsr_c;
    volatile uint8_t *ubrr_l;
    volatile uint8_t *ubrr_h;
    ...
};                                                   

static struct uart_io uart0_io[1] = {{
    .udr    = &UDR0,
    .ucsr_a = &UCSR0A,
    .ucsr_b = &UCSR0B,
    .ucsr_c = &UCSR0C,
    .ubrr_l = &UBRR0L,
    .ubrr_h = &UBRR0H
}};

Likewise for the usart1 driver.


So, to put PORTn, PINn and DDRn in a struct, you could do somthing like 
this:

struct my_port {
    volatile uint8_t *port;
    volatile uint8_t *pin;
    volatile uint8_t *ddr;
    ...
};

struct my_port port_a = {
    .port = &PORTA;
    .pin  = &PINA;
    .ddr  = &DDRA;
};

I like to use the array[1] idiom so that all my struct accesses use pointers
(port_a->port versus port_a.port) as it keeps the code more consistent and
you don't need to remember to use the &foo when passing a pointer to a
function. This works since the name of an array is equivalent to a pointer
to the first element of the array.

Ted Roth



reply via email to

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