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

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

Re: [avr-gcc-list] Struct problem


From: Eric Fu
Subject: Re: [avr-gcc-list] Struct problem
Date: Sun, 1 Feb 2004 14:16:54 +1100

What I was trying to do was to specify different buffer size for different
UART with the same struct type. It doesn't need to be changed dynamically.
(Sorry if it was not clear). As you have guessed, I'm using a relatively
small micro ATmega16. I have to save SRAM and code size as much as I can.
Basically, I'm trying to port an old project with PIC micro that has 4Kbytes
in Assembly. Now with ATmega16 and C, the code size is already 1.5Kbytes,
and the application has barely started! Now I've changed back to my original
design, since it uses minimum code, although it may use a bit more SRAM.
I heard Atmel AVR is efficient in C. It now makes me wonder whether it is
anything better than a PIC.
Anyway, I did learned a lot from you gurus. Thanks for help.

Eric Fu


----- Original Message ----- 
From: "Brian Cuthie" <address@hidden>
To: "'Dave Hylands'" <address@hidden>; "'Eric Fu'"
<address@hidden>; <address@hidden>
Sent: Sunday, February 01, 2004 1:16 AM
Subject: RE: [avr-gcc-list] Struct problem


>
> Yeah, but the problem is that he can't dynamically change the buffer size
> this way, which is I think what he was trying to do.
>
> Eric, I think you're learning the hard way about the nature of embedded
> development, especially when using very small processors with limited
> resources. There's often a way you'd *like* to do something, and then
> there's the way you end up having to do it to avoid eating up too much
> memory.
>
> My advice is that you use fixed sized buffers (as Dave suggests). You can
> either make them part of your structs directly (like your original design,
> except you'll need to specify the size of the arrays) or you can do as
Dave
> has and have the memory blocks dynamically assigned to pointers in the
> structs. Which makes the most sense depends on what you're really trying
to
> accomplish, which right now is still somewhat of a mystery.
>
> Good luck.
>
> -brian
>
> > -----Original Message-----
> > From: address@hidden
> > [mailto:address@hidden On Behalf Of Dave Hylands
> > Sent: Saturday, January 31, 2004 1:17 AM
> > To: Eric Fu; address@hidden
> > Subject: RE: [avr-gcc-list] Struct problem
> >
> > You don't need to use malloc. You could just put the pointers
> > in you UART struct and still use global variables.
> >
> > byte rxBuf[ RX_BUFFER_SIZE0];
> > byte txBuf[ TX_BUFFER_SIZE0];
> >
> > uart0.pRxBuf = rxBuf;
> > uart0.pTxBuf = txBuf;
> >
> > If you don't need to use malloc, then this will save you the
> > 500 bytes.
> >
> > *(uart0.pRxBuf+TmpTail) is the same as uart0.pRxBuf[ TmpTail ];
> >
> > Personally, I find the 2nd form easier to read.
> >
> > --
> > Dave Hylands
> > Vancouver, BC, Canada
> > http://www.DaveHylands.com/
> >
> >
> > > -----Original Message-----
> > > From: Eric Fu [mailto:address@hidden
> > > Sent: Friday, January 30, 2004 9:44 PM
> > > To: address@hidden
> > > Subject: Re: [avr-gcc-list] Struct problem
> > >
> > >
> > > It works. That's great.
> > > But it added an unbelievable 500Bytes! It seems the code size under
> > > AVR GCC varies a lot for different implementations of the
> > same task.
> > > In my case, what I changed was to access  UART buffer from
> > arrays to
> > > pointers with malloc. I redefined:
> > >     ...
> > >   byte *pTxBuf,*pRxBuf;
> > >      ...
> > > Allocated memory as:
> > > uart0.pRxBuf = malloc(RX_BUFFER_SIZE0);  uart0.pTxBuf =
> > > malloc(TX_BUFFER_SIZE0);
> > >
> > > Used such as
> > > return *(uart0.pRxBuf+TmpTail);           // Return data
> > > There are no other changes apart from the change to access
> > the buffer.
> > > Is this normal?
> > >
> > > Eric Fu
> > >
> > > ----- Original Message -----
> > > From: "Brian Cuthie" <address@hidden>
> > > To: "'Eric Fu'" <address@hidden>; <address@hidden>
> > > Sent: Saturday, January 31, 2004 1:59 PM
> > > Subject: RE: [avr-gcc-list] Struct problem
> > >
> > >
> > > >
> > > > You can use malloc() to create your buffers and they'll still be
> > > > globally accessible so long as the pointers you store them in are
> > > > global.
> > > >
> > > > -brian
> > > >
> > > > > -----Original Message-----
> > > > > From: Eric Fu [mailto:address@hidden
> > > > > Sent: Friday, January 30, 2004 9:52 PM
> > > > > To: address@hidden; address@hidden
> > > > > Subject: Re: [avr-gcc-list] Struct problem
> > > > >
> > > > > Thanks for the tip.
> > > > > The buffers need to be Global, since it needs to be
> > accessible in
> > > > > different modules. My understanding is that malloc is for
> > > allocating
> > > > > dynamic memory. Anyway, I will read my C book for clue.
> > > Thanks for
> > > > > pointing the direction.
> > > > >
> > > > > Eric Fu
> > > > >
> > > > >
> > > > > ----- Original Message -----
> > > > > From: "Brian Cuthie" <address@hidden>
> > > > > To: "'Eric Fu'" <address@hidden>; <address@hidden>
> > > > > Sent: Saturday, January 31, 2004 12:22 PM
> > > > > Subject: RE: [avr-gcc-list] Struct problem
> > > > >
> > > > >
> > > > > >
> > > > > > The problem, quite simply, is that because the size
> > of TxBuf[]
> > > > > > isn't specified, the offset within the struct to
> > > RxBuf[] can't be
> > > > > computed at
> > > > > > compile time (which it needs to be).
> > > > > >
> > > > > > To do what you want you'll need to change the struct so
> > > > > that instead of
> > > > > > arrays, TxBuf and RxBuf are pointers. Then malloc the
> > > > > memory you need and
> > > > > > store the addresses in TxBuf and RxBuf respectively.
> > > > > >
> > > > > > You might want to re-read a few chapters in your C book.
> > > > > >
> > > > > > -brian
> > > > > >
> > > > > > > -----Original Message-----
> > > > > > > From: address@hidden
> > > > > > > [mailto:address@hidden On Behalf Of Eric Fu
> > > > > > > Sent: Friday, January 30, 2004 8:05 PM
> > > > > > > To: address@hidden
> > > > > > > Subject: [avr-gcc-list] Struct problem
> > > > > > >
> > > > > > > Hi All,
> > > > > > >
> > > > > > > I'm trying to declare a Structure for UART, so that
> > it can be
> > > > > > > used for more than one UARTs such as: typedef
> > struct UART {
> > > > > > > volatile byte RxHead;  volatile byte RxTail;  volatile byte
> > > > > > > TxHead;  volatile byte TxTail;  byte
> > TxBuf[],RxBuf[]; }UART;
> > > > > > > extern UART uart0; extern UART uart1;
> > > > > > >
> > > > > > > By doing this way, I can assign different buffer size to
> > > > > > > different UART such as: byte uart0.RxBuf[RX_BUFFER_SIZE0];
> > > > > > > byte uart0.TxBuf[RX_BUFFER_SIZE0]; byte
> > > > > > > uart1.RxBuf[RX_BUFFER_SIZE1]; byte
> > > > > > > uart1.TxBuf[RX_BUFFER_SIZE1];
> > > > > > >
> > > > > > > However, I get the following compile error:
> > > > > > > UART.h:47: error: flexible array member not at end of struct
> > > > > > >
> > > > > > > Could anyone give me a hint on How to tell the compiler
> > > > > to do this?
> > > > > > > Thanks
> > > > > > >
> > > > > > > Eric Fu
> > > > > > > _______________________________________________
> > > > > > > avr-gcc-list mailing list
> > > > > > > address@hidden
> > > > > > > http://www.avr1.org/mailman/listinfo/avr-gcc-list
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> > >
> > > _______________________________________________
> > > avr-gcc-list mailing list
> > > address@hidden
> > > http://www.avr1.org/mailman/listinfo/avr-gcc-> list
> > >
> > >
> >
> >
> > _______________________________________________
> > avr-gcc-list mailing list
> > address@hidden
> > http://www.avr1.org/mailman/listinfo/avr-gcc-list
> >
>




reply via email to

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