Can you elaborate on this for a second?
I'm almost following but the padding and describe your data structures I'm not following.
I have data_byte0 struct with its own elements and 32 more unique struct as well. I need to apply each to every other byte in my data array.
I probably need to understand the layout a bit more
You have 64 bytes of memory, every other byte is a bitfield, and each of the 32 bitfields is different
Are the bytes in-between meaningful or just padding?
I assume the block has a structure as a message or control data for something. In this case you want your code to be written such that this high-level structure is evident in the code as someone reads it because that makes the meaning clearer and review and debugging easier.
You could define a structure to cover the whole block - it will be fairly long winded to do this, but will then allow straightforward access to the data. If your C compiler supports anonymous structures you can cut out some clutter
eg suppose you define
struct {
struct {
unsigned int byte_0_field_1:3;
unsigned int byte_0_field_2:1;
unsigned int byte_0_field_3:4;
} /* byte 0 */;
unsigned int pad1:8;
struct {
unsigned int byte_2_field_1:1;
unsigned int byte_2_field_2:2;
unsigned int byte_2_field_3:5;
} /* byte 1 */;
unsigned int pad3:8;
/* and so on for 30 more definitions */
} my_data_struct;
For safety I would add whatever instruction your compiler needs to peck the data tightly without additional padding.
You now have a struct my_data_struct which covers all 64 bytes and their meaning. Because you have defined the structure in its entirety the compiler will generate the necessary shifts etc.
You now can go a bit further and union this with a raw 64 byte buffer, and for convenience give the whole lot a type name
union {
struct my_data_struct;
unsigned char bytes[64]
} data_buffer;
If you want to use these in a function you just then declare a local variable -
data_buffer in;
data_buffer out;
You can now set fields pretty easily eg
out.byte_0_field_3 = 12;
out.byte_2_field_2 = 3;
assuming meaningful names in place of byte_X_field_X your code will now be very easy to read - note there's no casting being used here.
If you don't have anonymous structs then you will have to name the intermediaries but you can do this so the access ends up as
out.byte_0.field_3 = 12;
out.byte_2.field_2 = 3;
you can then call the SPI routine with
SPI_transfer(in.bytes, out.bytes, 64);
You might just need to cast in.bytes and out.bytes to void * for this call.
By describing the data *completely* to the compiler you have let it do the work of generating all of the bit shifts to access the data and saving you the leg work.
Generally I'm not sure all embedded programmers are comfortable with this so you can do it another way.
Lets assume you define a macro to extract a `w' bit wide bitfield at offset `o' bits
#define get_bitfield(byte, w, o) (((byte) >> (o)) & ((1<<(w))-1))
You can now define macros to read each field that you are interested in
#define BYTE_0_FIELD_1(data) get_bitfield(data[0], 3, 0)
#define BYTE_0_FIELD_2(data) get_bitfield(data[0], 1, 3)
#define BYTE_0_FIELD_3(data) get_bitfield(data[0], 4, 4)
#define BYTE_2_FIELD_1(data) get_bitfield(data[2], 1, 0)
#define BYTE_2_FIELD_2(data) get_bitfield(data[2], 2, 1)
#define BYTE_2_FIELD_3(data) get_bitfield(data[2], 5, 3)
You will need corresponding macros to set a bitfield within a word and to set each bitfield that you are interested in
Now you just need the in and out byte arrays in your function
unsigned char in[64], out[64]
and say
SET_BYTE_0_FIELD_3(out, 12);
SET_BYTE_2_FIELD_2(out, 3);
SPI_transfer(in.bytes, out.bytes, 64);
Hope you get the idea of the two styles, each produces something reasonable without a lot of casts getting in the way of reading the code.