I have made this function which unpacks received I2C messages and puts the individual values in a uint32_t array. It works when I use a fixed macro function for a fixed number of 2 bytes into a uint16_t , but I attempted to use a for loop to append any number of bytes to my values, since different i2c packets might have different value types. My problem lays in the way I do the bit operations, my bitwise knowledge seems too limited. What is wrong with option 2 here?
#define BytesToU16LE(B) ( \
(uint16_t)((B)[1]) << 8 \
| (uint16_t)((B)[0]) \
)
uint8_t ezi2cBuffer[100];
void unpack_i2c_packet(uint8_t nb_values_in_packet, uint8_t bytes_per_value, uint32_t * values, uint8_t buffer_head_size)
{
uint8_t current_value_bytes[bytes_per_value];
uint16_t payload_size = bytes_per_value * nb_values_in_packet;
uint8_t current_value_index = 0;
if(!nb_values_in_packet | !bytes_per_value)
return;
for(int i = 0; i < payload_size; i++)
{
current_value_bytes[(i % bytes_per_value) + buffer_head_size] = ezi2cBuffer[i + buffer_head_size]; // message head does not contain values, separate from payload
if((i % bytes_per_value) == (bytes_per_value - 1))
{
/* OPTION 1 WITH MACRO WORKS */
values[current_value_index] = BytesToU16LE(current_value_bytes);
/* OPTION 2 FOR LOOP PUTS RANDOM DATA IN MY VAR ARRAY! */
for(int bi = 0; bi < bytes_per_value; bi ++)
{
values[current_value_index] |= ((uint32_t)(current_value_bytes[bi]) << (bi * 8));
}
current_value_index++;
}
}
}
values[current_value_index]
is not initialized in OPTION 2. You should initialize it to zero or the value after loop will depend on the value before the loop.
/* OPTION 2 FOR LOOP PUTS RANDOM DATA IN MY VAR ARRAY! */
values[current_value_index] = 0; /* add this */
for(int bi = 0; bi < bytes_per_value; bi ++)
{
values[current_value_index] |= ((uint32_t)(current_value_bytes[bi]) << (bi * 8));
}