02.03.2014 Views

BSP Developer's Guide

BSP Developer's Guide

BSP Developer's Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8<br />

Writing Portable C Code<br />

attribute declaration. It is placed after the closing curly brace, but before any<br />

instance declarations or initializers. By including the vxWorks.h header file in your<br />

compilation, a toolchain specific header file is included to define this macro<br />

appropriately for the compiler tool being used.<br />

For example:<br />

struct aPackedStruct {<br />

UINT32 int1;<br />

INT16 aShort;<br />

INT8 aByte;<br />

} _WRS_PACK_ALIGN(1);<br />

struct aPackedStruct {<br />

UINT32 int1;<br />

INT16 aShort;<br />

INT8 aByte;<br />

} _WRS_PACK_ALIGN(1) anInstance = {0x1, 2,3};<br />

8<br />

typedef struct {<br />

UINT8 aByte;<br />

UINT16 aShort;<br />

UINT32 aLong;<br />

} _WRS_PACK_ALIGN(1) myPackedStruct;<br />

Specify Field Widths<br />

Always use specific field widths within a packed structure declaration. The basic<br />

data type int does not have a specific size and should be avoided. The same rule<br />

applies for long, unsigned, and char. The vxWorks.h header file defines basic data<br />

types with explicit sizes. Use these data types in any packed structure (INT8,<br />

INT16, INT32, UINT8, and so on). In general, think ahead to architectures with<br />

more than a 32-bit native integer.<br />

Avoid Bit Fields<br />

Do not include bit field definitions within a packed structure. Compilers are<br />

permitted to start labeling bits with either the least significant bit or the most<br />

significant bit. This issue can be dealt with easily by using macro constants to<br />

define the specific bit pattern within the data field.<br />

161

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!