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

Re: [bluetooth-dev] Re: Porting to ARM



Bill Pringlemeir wrote:

>  Gordon> I don't see how one can use swapping to solve the
>  Gordon> problem. Perhaps I'm slow, but could you provide an example
>  Gordon> of what you mean?
>
> Hi Gordon,
>
> Sorry to be so trite.  I was doing other stuff, so I didn't actually
> give an example that compiles.  I looked at the HCI code (hmmm...) and
> I think that this will give you an idea of what I am talking about.
>
> [begin]
> /* gcc foo.c -o foo */
> typedef unsigned int   u32;
> typedef unsigned short u16;
> typedef unsigned char  u8;
>
> #define swap16(x) (((x)<<8)|((x)>>8))
>
> typedef struct cmd_pkt {
>     u8 type;
>     union bar {
>         struct foo {
>             u16 ocf:10;
>             u16 ogf:6;
>         } foo;
>         u16 raw;
>     } bar;
>
>     u8 len;
>
>     u8 data[256];
> } __attribute__ ((packed)) cmd_pkt;
>
> /* little endian transit of ogf = 0x3f, ocf = 0x0 */
> unsigned char abuffer[12] = { 0, 0, 0xfc, 3, 4, 5, 6, 7, 8, 9, 10, 11};
>
> int main(int argc, char* argv[])
> {
>     int i;
>     cmd_pkt *cmd;
>
> /*      for(i=0;i<sizeof(abuffer);i++) */
> /*          printf("%d ",abuffer[i]); */
> /*      printf("\n"); */
>
>     cmd = (cmd_pkt*) abuffer;
>
>     printf("ogf = %x, ocf = %x\n", cmd->bar.foo.ogf, cmd->bar.foo.ocf);
>
>     cmd->bar.raw = swap16(cmd->bar.raw);
>
>     printf("ogf = %x, ocf = %x\n", cmd->bar.foo.ogf, cmd->bar.foo.ocf);
>
> /*      for(i=0;i<sizeof(abuffer);i++) */
> /*          printf("%d ",abuffer[i]); */
> /*      printf("\n"); */
>
>     return 0;
> }
> [end]
>
> Try this on little endian and big endian targets.  Btw, is your ARM
> big endian (must be samsung?).  The following defines would leave the
> code almost as is...
>
> #define OGF bar.foo.ogf
> #define OCF bar.foo.ocf
>
> The first printf will be correct on LE and the second on BE processors.
> I hope no one is porting this to a VAX ;-)
>
> hth,
> Bill

You're right. Cool. Thank you.

--Gordon