2020-12-14 12:31 PM
ADXL355_Result ADXL355_Init(I2C_HandleTypeDef* I2Cx,ADXL355 * DataStruct, ADXL355_Device DeviceNumber, ADXL355_Accelerometer AccelerometerSensitivity)
{
uint8_t WHO_AM_I = (uint8_t)ADXL355_DEVID_AD ;
uint8_t temp;
I2C_HandleTypeDef* Handle = I2Cx;
/* Format I2C address */
DataStruct->Address = (uint8_t)DeviceNumber;
uint8_t address = DataStruct->Address;
/* Check if device is connected */
if(HAL_I2C_IsDeviceReady(Handle,address,2,5)!=HAL_OK)
{
return ADXL355_Result_Error;
}
if(HAL_I2C_Mem_Read(Handle, address, WHO_AM_I, 1, &temp, 1, 1000) != HAL_OK)
{
return ADXL355_Result_Error;
}
/* Checking */
while(temp != ADXL355_ID)
{
/* Return error */
return ADXL355_Result_DeviceInvalid;
}
//------------------
/* POWER_CTL */
//------------------
/* Set POWER_CTL register */
if(ADXL355_POWERCTL(I2Cx , DataStruct) != ADXL355_Result_Ok)
{
return ADXL355_Result_Error;
}
//------------------
/* Config accelerometer */
ADXL355_SetAccelerometer(I2Cx,DataStruct, AccelerometerSensitivity);
/* Return OK */
return ADXL355_Result_Ok;
}
ADXL355_Result ADXL355_SetAccelerometer(I2C_HandleTypeDef* I2Cx,ADXL355* DataStruct, ADXL355_Accelerometer AccelerometerSensitivity)
{
uint8_t temp;
I2C_HandleTypeDef* Handle = I2Cx;
uint8_t address = DataStruct->Address;
uint8_t regAdd =(uint8_t )ADXL355_Range;
/* Config accelerometer */
while(HAL_I2C_Master_Transmit(Handle, (uint16_t)address,®Add, 1, 1000) != HAL_OK);
/*{
return SD_MPU6050_Result_Error;
}*/
while(HAL_I2C_Master_Receive(Handle, (uint16_t)address, &temp, 1, 1000) != HAL_OK);
/*{
return SD_MPU6050_Result_Error;
}*/
temp = (temp & 0xFC) | (uint8_t)AccelerometerSensitivity ;
while(HAL_I2C_Master_Transmit(Handle, (uint16_t)address,&temp, 1, 1000) != HAL_OK);
/*{
return SD_MPU6050_Result_Error;
}*/
/* Set sensitivities for multiplying gyro and accelerometer data */
switch (AccelerometerSensitivity) {
case ADXL355_Accelerometer_2G:
DataStruct->Acce_Mult = (float)1 / ADXL355_ACCE_SENS_2;
break;
case ADXL355_Accelerometer_4G:
DataStruct->Acce_Mult = (float)1 / ADXL355_ACCE_SENS_4;
break;
case ADXL355_Accelerometer_8G:
DataStruct->Acce_Mult = (float)1 / ADXL355_ACCE_SENS_8;
break;
default:
break;
}
/* Return OK */
return ADXL355_Result_Ok;
}
ADXL355_Result ADXL355_POWERCTL(I2C_HandleTypeDef* I2Cx, ADXL355* DataStruct)
{
uint8_t temp;
I2C_HandleTypeDef* Handle = I2Cx;
uint8_t address = DataStruct->Address;
uint8_t regAdd =(uint8_t )ADXL355_POWER_CTL;
/* Config accelerometer */
while(HAL_I2C_Master_Transmit(Handle, (uint16_t)address,®Add, 1, 1000) != HAL_OK)
{
return ADXL355_Result_Error;
}
while(HAL_I2C_Master_Receive(Handle, (uint16_t)address, &temp, 1, 1000) != HAL_OK)
{
return ADXL355_Result_Error;
}
temp = temp & 0xFE ; // Set measurement bit in POWER_CTL register
while(HAL_I2C_Master_Transmit(Handle, (uint16_t)address,&temp, 1, 1000) != HAL_OK)
{
return ADXL355_Result_Error;
}
return ADXL355_Result_Ok;
}
ADXL355_Result ADXL355_ReadAccelerometer(I2C_HandleTypeDef* I2Cx,ADXL355* DataStruct)
{
uint32_t X = 0, Y = 0 , Z = 0;
uint8_t data[9];
uint8_t reg = ADXL355_XDATA3 ;
I2C_HandleTypeDef* Handle = I2Cx ;
uint8_t address = DataStruct->Address ;
/* Read accelerometer data */
//while(HAL_I2C_Master_Transmit(Handle, (uint16_t)address, ®, 1, 1000) != HAL_OK) ;
while(HAL_I2C_Mem_Read(Handle, address, reg, 1, data , 6, 1000) != HAL_OK) ;
for(int t=0;t<3;t++)
{
X<<=8;
X+=data[t];
}
for(int t=3;t<6;t++)
{
Y<<=8;
Y+=data[t];
}
for(int t=6;t<9;t++)
{
Z<<=8;
Z+=data[t];
}
DataStruct->Accelerometer_X = ADXL355_Acceleration_Data_Conversion(X);
DataStruct->Accelerometer_Y = ADXL355_Acceleration_Data_Conversion(Y);
DataStruct->Accelerometer_Z = ADXL355_Acceleration_Data_Conversion(Z);
ADXL_AX = (float)DataStruct->Accelerometer_X*DataStruct->Acce_Mult;
ADXL_AY = (float)DataStruct->Accelerometer_Y*DataStruct->Acce_Mult;
ADXL_AZ = (float)DataStruct->Accelerometer_Z*DataStruct->Acce_Mult;
/* Return OK */
return ADXL355_Result_Ok;
}
2020-12-14 01:01 PM
Make sure you have pull-ups on the SCL/SDA pins (external 2K7 suggested), and they are configured in open-drain.
Not going to dig into the code. The device needs to respond to its Slave Address with a NACK, or none of the rest will matter.
The Slave Address in ST terms with be (0x1D << 1) or (0x53 << 1) for the secondary.
2020-12-14 01:15 PM
in datasheet of ADXL355 it says that
ASEL (pin) = 0, device address = 0x1D
ASEL (pin) = 1, device address = 0x53
why we shift it left?
2020-12-14 02:21 PM
Because the address bits are the high order 7-bits, and this is how ST manages them.
If you've not shifted them, and it's not working, should tell you something about the implementation.
2020-12-14 02:53 PM
thanks
the device could read and identify the slave (sensor) but, the output of it is zero for all axis
2020-12-14 04:04 PM
I guess. I don't use the ADXL355, but several similar devices need to be configured/started when first brought up.
If the device doesn't NACK you'll get an error from the HAL I2C commands.
So you should be able to access the device, and get the WHO_AM_I register successfully. If that works you are talking to the device at least.