0

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

D_A
  • 1
  • 1
  • What do you call `sending` into the sentence`sending an array that is located in my main.c file.` ? perhaps this https://stackoverflow.com/questions/2271902/static-vs-global can help you – Ôrel Nov 15 '21 at 14:11
  • 5
    It is a mistake to declare variables in a header file. Each C file that includes Buffers.h will be creating a duplicate. – stark Nov 15 '21 at 14:14
  • If you need that array only in one compilation unit, define it as `static` in the .c file. If you need it in multiple places, define it in exactly one .c file, and put an `extern` declaration (without initialisation) to the .h. (In the latter case either you need to make it a constant size or instead make it a pointer and have some other way to know the size.) – Arkku Nov 15 '21 at 14:21
  • Can you show _how_ you pass it into the function? – Stephen Leach Nov 15 '21 at 14:37
  • @StephenLeach I have edited the question to show how i pass the array – D_A Nov 15 '21 at 15:57
  • 1
    @stark I'm trying to edit the driver written by ST as little as possible, this is just how they have written it. But of course, if i can't get it to work reliably this way i will change – D_A Nov 15 '21 at 16:00
  • @D_A I was unable to duplicate the issue on my machine using similar code with a gcc compiler. My only recommendation would be that you cast &SensorFirmware to an (int *) – Stephen Leach Nov 15 '21 at 16:07
  • When you put your array into the header as `const`, the whole `temp_array` (in `WrMulti`) is known at compile time... so might end up in ROM, for example. I’d suggest inspecting the compiled driver. – numzero Nov 15 '21 at 23:00
  • @D_A - The first argument you pass to `WrMulti` is incompatible with its first parameter. – Armali Nov 16 '21 at 20:44

0 Answers0