我正在尝试在 AVR MCU(AVR128DB48)上实现传感器(ST-VL53L5CX)的驱动程序。
驱动程序是预先编写好的,有一些I2C函数供用户填写(因此传感器可以在不同的平台上实现)。
驱动由多个c文件组成。API.c、API.h、Buffers.h、Platform.c 和 Platform.h。
传感器的固件以数组形式存储在 Buffers.h 文件中。例如:
const uint8_t Sensor_Firmware[] = {0x05,...,0x01};
这需要写入传感器。为此,我调用了一个已编写的函数,将多个字节写入 I2C 线路:
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;
}
这在发送位于我的 main.c 文件中的数组时有效。
例如:
//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
当从 .h 文件中传递一个没有 'const' 关键字的数组时,它也可以工作。
例如:
//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
当我将 'Sensor_Firmwware[]' 数组(使用 const 关键字)从 .h 文件传递给 'WrMulti' 时,它不会。它只是为来自“Sensor_Firmware”的每个字节发送“0x00”。
有谁知道这可能是为什么?
亲切的问候