Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Re: type cast in GCC

2006-02-28 by bdmlpc

--- In lpc2000@yahoogroups.com, "weartronics" <nic@...> wrote:
>
> Dear lpc2000,
> 
> In the Keil LPC2148 USB examples, EP0 buffer is declared as a byte array
> BYTE  EP0Buf[USB_MAX_PACKET0];
> 
> but later it is type cast to the packed word array
> *((__packed WORD *)EP0Buf) = 0;
> 
> I don't understand how the pointer EP0Buf would be changed by casting
> it to the __packed word pointer. Is anyone able to explain the
> function of the type cast to me so I can write a GCC equivalent (where
> the type cast to a packed type is not supported)?
> 
> Thanks,
> 
> Nic
>

Hi Nic,

the __packed keyword or __attribute__((packed)) statement should
prevent compiler from optimizing memory storage of variables and
memory accesses. Appling these statements to a structure for example
means that the compiler should put all struct-members together.
Otherwise the compiler could insert "padding bytes" to align
u16-variables to 2-byte-boundaries and u32-variables to
4-byte-boundaries to prevent mis-aligned memory accesses (which need
more than one memory cycle) on some architectures. __packed forces the
compiler not to optimize.
In your particular case I'm not sure if __packed is really needed,
because due to the cast the compiler is still forced to work on a
(maybe) mis-aligned adress pointer.

  Sten

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.