Sumit,
You're right - the padding byte gets placed after the uint8_t.
The sizes can be deceptive though. The problem I ran into was actually
between the computer and the mote, passing an array of non-word aligned
structs. I was writing bytes into a buffer that was contained in an element
of an array of structures where the struct's weren't word aligned, and I was
passing that array in a message to a java app on the computer. So the
computer expected the values in the array to be placed in certain locations,
as they were defined in the .h file, and the msp430 actually placed the
values in different locations because of the padding bytes it automatically
adds.
sizeof(f1) = 1
sizeof(f2) = 4
sizeof(a) = 6
a.f1 = 0xAA;
a.f2 = 0xBBCCDDEE;
=> {0xAA, 0x0, 0xEE, 0xDD, 0xCC, 0xBB}
If you're doing everything mote-side and don't have to worry about
communicating with a pc, then you shouldn't see any problems.
-david
-----Original Message-----
From: Sumit Rangwala [mailto:[EMAIL PROTECTED]
Sent: Thursday, March 23, 2006 4:08 PM
To: David Moss
Cc: 'Jose L. Ponce'; [email protected]
Subject: RE: [Tinyos-help] Strange results when sending uint16_t
values.(SOLVED)
At 8:52am on Mar 22, electrons from David Moss conveyed:
> All structures must be created to enforce word alignment. You'll probably
> notice another problem if you construct an array of these
> DimmerControlMSG's, because word-alignment will be off due to the uint8_t.
Can you elaborate more on this (problem with array). Because
I had used structures like
struct a {
uint8_t f1;
uint32_t f2;
};
knowing that msp will add a padding byte between f1 and f2
and have found it to work.
Thanks,
Sumit
>
> A rule of thumb is always create structs with the biggest variables first
to
> the smallest variables last, always have an even number of bytes, and
never
> use the packed attribute:
>
> struct MyStruct {
> uint32_t bigvar1;
> uint32_t bigvar2;
> uint16_t mediumvar1;
> uint8_t smallvar1;
> uint8_t smallvar2;
> } MyStruct;
>
> Sorry I didn't point this issue out to you yesterday, I didn't realize the
> actual layout of the struct was what was getting in your way.
>
> David
>
>
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Jose L.
> Ponce
> Sent: Wednesday, March 22, 2006 8:42 AM
> To: [email protected]
> Subject: Re: [Tinyos-help] Strange results when sending uint16_t
> values.(SOLVED)
>
>
> Hi,
>
> I was able to solve the problem:
>
> I noticed that there was something wrong with the uint16 field: I
> programmed the mote to unpack the message and pack it in another one to
> be sent back to the computer. If I sent 0x0a05 the return message would
> be 0x0a00. I tried to divide the uint16 field in two uint8 fields (high
> and low), and with that everything worked.
>
> To cut a long story short, after a long hours of desperation, I did the
> simplest thing I could think of. If my message structure was:
>
> struct DimmerControlMSG
> {
> uint8_t todo;
> uint16_t val;
> };
>
> I changed it for:
>
> struct DimmerControlMSG
> {
> uint16_t val;
> uint8_t todo;
> };
>
> And now everything's working!!
>
> Any comments on this? Is this something which happens often?
>
> -Jose.
>
> On Tue, 2006-03-21 at 18:04 +0100, Jose L. Ponce wrote:
> > Hi all,
> >
> > I want to control the DAC of my telosb mote using commands from the
> > computer through the USB port. For that, I've programmed an application
> > which receives a message with two fields: uint8_t for the command and
> > uint16_t for the value we want to set the DAC to.
> >
> > There are two commands: set (1), with a value, to set the DAC, and show,
> > without a value, to see to which value the DAC is set to (when issuing a
> > set command, the mote returns the DAC value as well).
> >
> > On the computer side, the commands are issued using a small java
> > application:
> >
> > java Dimmer set X
> > java Dimmer show
> >
> > The problem is that the value field (uint16_t) is not recognised: I will
> > issue java Dimmer set 10 ten times and each time I will get a different
> > DAC output and a different return value from the mote. I have tried to
> > modify my message type and have the value field as a uint8_t. In this
> > way, issuing a set command from 0 to 256 will give me right values.
> > Above that, it doesn't seem to work anymore.
> >
> > Can anyone enlighten me a little on what I am doing wrong? It'd be much
> > appreciated.
> >
> > Below I include the relevant code.
> >
> > Thanks,
> >
> > - Jose.
> >
> > // DimmerControlM.nc (on the mote)
> > // DimmerControlMSG has two fields: todo (uint8_t) and value (uint16_t)
> >
> > task void sendMsg(){
> > struct DimmerControlMSG* sen;
> > sen = (struct DimmerControlMSG *)outw.data;
> > sen->value = value;
> >
> > call SendMsg.send( TOS_UART_ADDR, sizeof(struct
DimmerControlMSG),
> &outw);
> > }
> >
> > event TOS_MsgPtr ReceiveMsg.receive( TOS_MsgPtr inw ){
> > struct DimmerControlMSG* rec;
> > rec = (struct DimmerControlMSG *)inw->data;
> >
> > if( rec->todo & 0x1 ) {
> > value=rec->value;
> > if(call DAC.output(value) == SUCCESS)
> > call Leds.redToggle();
> > }
> >
> > post sendMsg();
> > return inw;
> > }
> >
> >
> > _______________________________________________
> > Tinyos-help mailing list
> > [email protected]
> >
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
> >
>
>
> _______________________________________________
> Tinyos-help mailing list
> [email protected]
> https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
>
>
> _______________________________________________
> Tinyos-help mailing list
> [email protected]
> https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
>
_______________________________________________
Tinyos-help mailing list
[email protected]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help