Can't pass array stored in .h file to C function

151 Views Asked by At

I am trying to implement a driver for a sensor(ST-VL53L5CX) on an AVR MCU (AVR128DB48).

The driver comes prewritten, with a few I2C functions for the user to fill in (So the sensor can be implemented on different platforms).

The driver is made up of multiple c files. API.c, API.h, Buffers.h, Platform.c and Platform.h.

The firmware for the sensor is stored in the Buffers.h file as an array. e.g:

const uint8_t Sensor_Firmware[] = {0x05,...,0x01};

This needs to be written to the sensor. To do this, I call a function I have written to write multiple bytes to the I2C line:

    uint8_t WrMulti(VL53L5CX_Platform *p_platform,uint16_t 
    RegisterAdress,uint8_t *p_values,uint32_t size)
   {  
    uint8_t count = 0;
    uint32_t new_size = size + 2; 
    
    //Split Register address into 2 bytes
    uint8_t temp0 = (uint8_t)((RegisterAdress & 0xFF00)>> 8); 
    uint8_t temp1 = (uint8_t)(RegisterAdress & 0x00FF); 
    
    //Create new array to hold Register Address and p_values 

    uint8_t temp_array[new_size] ;

    for (int i = 0; i <new_size; i++)
    {
        temp_array[i] = 0;
    }
 
    //Fill temp_array with register address and p_values

    temp_array[0] = temp0; 
    temp_array[1] = temp1; 
    
    for (int i = 2; i < (new_size); i++ )
    {
        temp_array[i] = p_values[count];
        count++;
    }
    
    
    //I2C
    
    uint8_t status = 255;
    while(!I2C0_Open(p_platform->address)); // sit here until we get the bus..
    I2C0_SetBuffer(temp_array,new_size);
    I2C0_SetAddressNackCallback(I2C0_SetRestartWriteCallback,NULL); //NACK polling?
    I2C0_MasterWrite();
    while(I2C0_BUSY == I2C0_Close()); // sit here until finished.
    
    
        /* This function returns 0 if OK */
    status = 0; 
    return status;
}

This works when sending an array that is located in my main.c file.

e.g:

   //buffer.h 

   const uint8_t Sensor_Firmware[] = {0x05,...,0x01};

//End buffer.h

.

 //main.c 
        void main (void) 
         {
    uint8_t I2C_address = 0x01; 
    uint8_t I2C_reg = 0x01; 
    uint32_t size = 16;
    
uint8_t temp_array[size]; 

for (int i = 0; i <size; i++)
{
   temp_array[i] = Sensor_Firmware[i];
}

WrMulti(I2C_address, I2C_reg, &temp_array[0], size); 
While(1)
{
;
}
}
//End main.c

It also work when passing an array from the .h file without the 'const' key word.

e.g:

   //buffer.h 

    uint8_t Sensor_Firmware[] = {0x05,...,0x01};

//End buffer.h

.

 //main.c 
        void main (void) 
         {
    uint8_t I2C_address = 0x01; 
    uint8_t I2C_reg = 0x01; 
    uint32_t size = 16;
    
uint8_t temp_array[size]; 

WrMulti(I2C_address, I2C_reg, &Sensor_Firmware[0], size); 
While(1)
{
;
}
}
//End main.c

It does not when I pass the 'Sensor_Firmwware[]' array (with the const key word) from the .h file to 'WrMulti'. It just ends up sending '0x00' for every byte that comes from 'Sensor_Firmware'.

Does anyone know why this is might be?

Kind regards

0

There are 0 best solutions below