Search code examples
cavr

How to pass a float array to the function that has unsigned char pointer as an argument?


Some back ground information; I have to interface an EEprom which has I2C interface. I want to save an array of floats in the memory and read it back. I want to make it as fast as possible. Currently I have the following solution for it which works perfectly fine.

float a[5];
unsigned char* p = (unsigned char *)a;
for ( i = 2; i < 22; i++)
{
    data [ i ] = p [ i -2 ];
}                                                           

 twi_master_trans(
if ( twi_master_trans ( EEprom_address  , data  , 22 ,NULL , 0)  == false )
{
    put_falsh_string("TWI major error1 ");
}

I am pointing at each element to an alternative location in memory. Why not I just give the starting point of my main array. I thought that should be done like this:

twi_master_trans ( EEprom_address  , (unsigned char *)a, 22 ,NULL , 0)

I don't understand why it does not work. Could you explain why? or what am I not understanding?

this is the definition of twi_master_trans:

   bool twi_master_trans(
        unsigned char slave_addr,
        unsigned char *tx_data,
        unsigned char tx_count,
        unsigned char *rx_data,
        unsigned char rx_count)

I know that my question is not very clear but I can't explain it any better. but I give it a try.

The argument for a function is pointer to unsigned char, I have an array of floats which has to be sent there. A pointer to starting byte in the float array is supposed to be what we pass to the function.

Thank You, I am a noob, sorry if it is a dumb question.


Solution

  • In the first snippet, the float data starts at byte 2 of the char array. In the second snippet, it starts at byte 0.

    It looks like the missing two bytes are the key to why one works and the other doesn't.