[Tinyos-devel] TinyOS-2.x platform independent types?
David Gay
dgay42 at gmail.com
Thu Sep 7 08:10:06 PDT 2006
On 9/7/06, Matti Juhani Öhman <mohman at cc.hut.fi> wrote:
> what is the current status of TinyOS platform independent types (aka
> network types)? I am trying to compose a message structure with 24-bit
> little endian integers with no success.
>
> The most useful source of information I could find is the TinyOS
> Programming Manual. It explains platform independent types it does not
> mention bit fields at all.
>
> I did some test with the nesC compiler. It seems that big endian types
> support bit fields but little endian types do not:
>
> typedef nx_struct test_t {
> nx_uint32_t a:24;
> nx_uint32_t b:24;
> nx_uint32_t c:24;
> nx_uint32_t d:24;
> } test_t;
>
> -> compiles just fine, but
>
>
> typedef nx_struct test_t {
> nxle_uint32_t a:24;
> nxle_uint32_t b:24;
> nxle_uint32_t c:24;
> nxle_uint32_t d:24;
> } test_t;
>
> -> gives a compiler error: "type of `a' cannot be used as a bit-field"
>
>
> Shouldn't it be possible to declare little endian bitfields?
Mixing bit-fields of different endianness doesn't quite work, because
the endianness of bit-fields actually implies two things:
1) if a bitfield spans multiple bytes, which bytes contain the most
significant bits (for big-endian that's the lowered numbered bytes,
for little-endian the higher numbered ones).
2) at what end of the byte do you start using bits (for little-endian,
you start at the least significant bits, for big-endian you start at
the most significant bits)
Also remember that fields are allocated in order of declaration
Rule 1 isn't a problem, but rule 2 is: what does
nx_struct {
nx_uint8_t a : 2;
nxle_uint8_t b : 2;
}
mean? Rule 2 would seem to imply there's a 4-bit hole in the middle of
the byte this structure takes. It gets more fun if you then do:
nx_struct {
nx_uint8_t a : 2;
nxle_uint8_t b : 2;
nx_uint8_6 c : 3;
}
Where does c live? In the next byte? In the hole? What if c is 5 bits
(so bigger than the hole)?
It gets worse from here. So, to avoid any very mysterious behaviour,
bitfields in external types are only big-endian.
Btw, there is actually one consistent way of mixing big and little
endian bit fields. But it means that you have to reverse the bits for
one of those within bytes. No thanks :-)
David Gay
More information about the Tinyos-devel
mailing list