Get variables struct

hello,
I am trying to create a function to get a servo position using c code in linux using usb-lib-1.0
i wanted to ask whether the variable struct that I have created is correct.

struct servoSetting
{
    unsigned short position;
    unsigned short target;
    unsigned short speed;
    unsigned char acceleration;
};
struct uscVariables
{
    // Fix bytecode_asm.asm if you change the order or size of
    // variables in this struct.

    // offset: 0
    unsigned char stackPointer;

    // offset: 1
    unsigned char callStackPointer;

    // offset: 2
     unsigned short errors;

    // offset: 4
     unsigned short programCounter;

    // offset: 6
    short buffer[3]; // protects other RAM from being corrupted by improper instructions

    // offset: 12
    short stack[32];

    // offset: 76
	unsigned short callStack[10];

    // offset: 96
    unsigned char scriptDone; // 1 = done; 2 = about to run a single step then be done - placed here to protect against accidental overwriting of Setting

    // offset: 97
    unsigned char buffer2; // protects other RAM from being corrupted by improper instructions

    // offset: 98
    struct servoSetting servoSetting[6];
};

Hello, atehleb.

Which model of Pololu servo controller do you have?

–David

Hello,
I have the 6 channel usb controller
pololu.com/catalog/product/1350

Ok, those structs look good to me, except that by default GCC will add padding to your structs to make the variables be aligned a certain way. You need to tell GCC to NOT add that padding by using the “packed” attribute:

struct __attribute__ ((packed)) servoSetting
{
 ...
};

struct __attribute__ ((packed)) uscVariables
{
  ...
};

You said you are in Linux so I assumed you are using GCC, but whatever compiler you are using should have an option for packing structs.

–David

Hi,
Thanks a lot for your reply.
However I am using g++ and it does not recognize __attribute__ ((packed))

I am assuming using #pragma pack(1)
and #pragma pack()
will work?

thanks
atehleb

Sorry, after some experimentation I have found that the attribute actually needs to go before the struct name. I have updated by previous post, so please try again.

–David

Yup, Thanks a lot for your help.
Also I checked and for g++ using

#pragma pack(1)
struct servoSetting
{
...
};
struct uscVariables
{
...
};
#pragma pack()

is also giving same results

atehleb