need some coding suggestions

Hi,

So I have this library for MCP2515 in which the following struct is defined:

typedef struct
{
	uint16_t id;
	struct {
		uint8_t rtr : 1;
		uint8_t length : 4;
	} header;
	uint8_t data[8];
} tCAN;

Now I have this project where I am reading some data and storing them as uint32_t.
To be able to read the individual bytes I have use 'union' as follows:

union { 
    struct {
		uint32_t tstamp; 
		uint32_t sData;
	};
    uint8_t bytes[8];
} in; //global variable

I then update the in 'loop' the CAN message to be transmitted as follows:

tCAN message; //global variable

void loop {

    //update in.tstamp;
    //update in.sData;
	
	message.data[0] = in.bytes[3];
	message.data[1] = in.bytes[2];
	message.data[2] = in.bytes[1];
	message.data[3] = in.bytes[0];
	message.data[4] = in.bytes[5];
	message.data[5] = in.bytes[4];
	message.data[6] = in.bytes[6];
	message.data[7] = in.bytes[7];
    
    send_msg(&message);
       
}

What I would like to know is whether or not its possible to use pointers or otherwise make the 'message.data' address same as 'in.bytes' so that I don't have to update 'data' everytime.

what i mean is something like that (tried it... does not work):-

in setup:

        in.bytes[3] = &message.data[0];
	in.bytes[2] = &message.data[1];
        in.bytes[1] = &message.data[2];
        in.bytes[0] = &message.data[3];
        in.bytes[5] = &message.data[4];
        in.bytes[4] = &message.data[5];
        in.bytes[6] = &message.data[6];
        in.bytes[7] = &message.data[7];

any suggestions?

Is the byte order swapping really necessary? If not, you could just do:

send_msg(&in);

gfvalvo:
Is the byte order swapping really necessary? If not, you could just do:

send_msg(&in);

unfortunately yes... hence the challenge! :slight_smile:

What about a sequence array:

#define ELEMENTS(x) (sizeof(x) / sizeof(x[0]))

uint8_t sequence[] = {3, 2, 1, 0, 5, 4, 6, 7};

// ...more code...

   int i;

   for (i = 0; i < ELEMENTS(sequence); i++) {
      message.data[i] = in.bytes[sequence[i]];
   }

Not much of an improvement, but if the order of sequence[] or its size changes, it would make it easier to change.

Thank for the suggestion econjack.

however what I want to improve on (it possible) is not the sequencing of data but rather how to retieve data from memory location

currently the computed data is stored in 'in.tstamp, in.sData' memory location which is the same location as 'in.bytes'

I then have to put 'in.bytes' into 'message.data' (CAN message struct) before outputing it using 'send_msg'

I would like to get rid on the last step if possible by somehow making 'message.data[y]' share the same memory location as 'in.bytes[z]'.
'in' and 'message' are both global variables so it should be somehow possible, right? I just don't know how to make it work...

the stuct as mentioned in my inital post is part of a library and would like to about modifying it.

sherzaad:
Thank for the suggestion econjack.

however what I want to improve on (it possible) is not the sequencing of data but rather how to retieve data from memory location

currently the computed data is stored in 'in.tstamp, in.sData' memory location which is the same location as 'in.bytes'

I then have to put 'in.bytes' into 'message.data' (CAN message struct) before outputing it using 'send_msg'

I would like to get rid on the last step if possible by somehow making 'message.data[y]' share the same memory location as 'in.bytes[z]'.
'in' and 'message' are both global variables so it should be somehow possible, right? I just don't know how to make it work...

the stuct as mentioned in my inital post is part of a library and would like to about modifying it.

I don't know how they can share the same memory if each data structure requires the bytes to be in a different order.