EmbeddedRelated.com
Forums

type cast in GCC

Started by weartronics February 27, 2006
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
	

An Engineer's Guide to the LPC2100 Series

Hi Nic,
I had the same problem...
It seems that Keil's __packed keyword in the front of a pointer effectively

means 
memcpy(packed_p, source_p, sizeof(source));

In your case your can write
	EP0Buf[0] = 0;
	EP0Buf[1] = 0;
or just 
*(short *)EP0Buf = 0;
if you add __atribute__((aligned(2))) to the EP0Buf declaration:
	char __atribute__((aligned(2))) EP0Buf[USB_MAX_PACKET0];
This will in result write to assembly:
	.comm EP0Buf, USB_MAX_PACKET0, 2
where last digit '2' is an optional align passed to linker.

Have fun,
Dmitry.

P.S. in my case I couldn't run Keil's example on my board with gcc
anyway.
	On Tuesday 28 February 2006 05:54, weartronics 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
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>

--- In lpc2000@lpc2..., "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
	
Hi Dmitry,

Thanks, this makes some sense now. I wish Keil had documented the __packed
better (I looked at the manual on their web site but it doesn't discuss a
__packed typecast and I would never have guessed that the typecast
actually rearranges the array memory!) I'll give this a go next. Thanks
also for your info bdmlpc.

Kind regards,

Nic

> Hi Nic,
> I had the same problem...
> It seems that Keil's __packed keyword in the front of a pointer
> effectively
> means
> memcpy(packed_p, source_p, sizeof(source));
>
> In your case your can write
> 	EP0Buf[0] = 0;
> 	EP0Buf[1] = 0;
> or just
> *(short *)EP0Buf = 0;
> if you add __atribute__((aligned(2))) to the EP0Buf declaration:
> 	char __atribute__((aligned(2))) EP0Buf[USB_MAX_PACKET0];
> This will in result write to assembly:
> 	.comm EP0Buf, USB_MAX_PACKET0, 2
> where last digit '2' is an optional align passed to linker.
>
> Have fun,
> Dmitry.
>
> P.S. in my case I couldn't run Keil's example on my board with
gcc anyway.
>
>
>
> On Tuesday 28 February 2006 05:54, weartronics 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
>>
>>
>>
>>
>>
>>
>> Yahoo! Groups Links
>>
>>
>>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>