2024-07-19 07:08 AM
Hello,
I am using STM32H745 to setup and communicate with LIS2DW12 accelerometer via SPI 4-wires. I am trying to setup the accelerometer and I started with reading the WHO_AM_I register. It's default value is (43h) and I changed the value of the first bit to '1' so that my STM32 can read the value.
The SPI communication is setup as follows:
With the read and write fucntions set like this:
HAL_StatusTypeDef spi_write_LIS2DS12(uint8_t regAddr, uint8_t *pData)
{
HAL_StatusTypeDef ret;
uint8_t address = regAddr & 0x7F;
uint8_t sendData[2] = {address, *pData};
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 0);
ret = HAL_SPI_Transmit(&hspi2, sendData, 2, 20);
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, 1);
return ret;
}
HAL_StatusTypeDef spi_read_LIS2DS12(uint8_t regAddr, uint8_t *pData)
{
HAL_StatusTypeDef ret = 0x01;
uint8_t address = regAddr | 0x80; // Set the Read/Write bit to 1 for read operation
uint8_t sendData[2]= {address,0};
uint8_t receiveData[2] = {0};
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(&hspi2, sendData, receiveData, 2, 50);
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
*pData = receiveData[1];
return ret;
}
When I observe the MISO and MOSI with a logic analyzer, I can see that good register value is sent, but I have random 0s and 1s as a return of MISO. Here is a picture of how it looks:
I tried to add a reboot and/or soft reset before reading the WHO_AM_I registor, but it did not change much. Does anyone know what is the cause of this behaviour and how can it be fixed?
Solved! Go to Solution.
2024-07-22 02:36 AM
Hi @NuEta ,
As written also in datasheet the whoami is 44h:
Moreover, I suggest you to follow our PID example to implement the readings of LIS2DW12. Let me know if this helps.
2024-07-22 02:36 AM
Hi @NuEta ,
As written also in datasheet the whoami is 44h:
Moreover, I suggest you to follow our PID example to implement the readings of LIS2DW12. Let me know if this helps.
2024-07-23 01:51 AM
I am communicating through SPI thanks to HAL_transmit functions
I don't understand how to implement what you gave me with my actual code
2024-07-23 01:54 AM
Is this function I don't understand what should be *ctx
int32_t __weak lis2ds12_read_reg(const stmdev_ctx_t *ctx, uint8_t reg,
uint8_t *data,
uint16_t len)
{
int32_t ret;
if (ctx == NULL)
{
return -1;
}
ret = ctx->read_reg(ctx->handle, reg, data, len);
return ret;
}