2024-07-13 12:51 AM
Hello,
I'm currently working with the ICM-20602 IMU sensor and the LSM6DSV16X IMU sensor in an STM32-based project using STM32CubeIDE for stm32f446re. I've successfully interfaced with the ICM-20602 sensor and am able to read the WHO_AM_I register after ensuring that the sensor is out of sleep mode by configuring the POWER MANAGEMENT 1 register.
Here is the relevant code I used for the ICM-20602 sensor:
/* Function to read a register from the ICM-20602 via SPI */
uint8_t spi_read_register(uint8_t reg_addr) {
uint8_t tx_buf[2] = { reg_addr | 0x80, 0 }; // MSB set for read operation
uint8_t rx_buf[2] = { 0, 0 };
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); // Set CS low
HAL_SPI_TransmitReceive(&hspi3, tx_buf, rx_buf, 2, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); // Set CS high
return rx_buf[1];
}
/* Function to write to a register over SPI */
void spi_write_register(uint8_t reg_addr, uint8_t value) {
uint8_t tx_buf[2] = { reg_addr & 0x7F, value }; // MSB cleared for write operation
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); // Set CS low
HAL_SPI_Transmit(&hspi3, tx_buf, 2, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); // Set CS high
}
/* Function to enable necessary bits in POWER MANAGEMENT 1 register */
void enable_power_management(void) {
uint8_t pm1_data = spi_read_register(PWR_MGMT_1_REG_ADDR);
pm1_data &= ~SLEEP_BIT; // Clear SLEEP_BIT to exit sleep mode
spi_write_register(PWR_MGMT_1_REG_ADDR, pm1_data);
}
/* Function to read the WHO_AM_I register */
void read_who_am_i(void) {
uint8_t who_am_i = spi_read_register(WHO_AM_I_REG_ADDR);
char buffer[50];
sprintf(buffer, "WHO_AM_I register: 0x%02X\r\n", who_am_i);
UART_Transmit(buffer);
}
For the ICM-20602 sensor, I needed to clear the sleep bit (bit 6) in the POWER MANAGEMENT 1 register (address 0x6B) to ensure the sensor is active. After this configuration, reading the WHO_AM_I register returns the correct value of 0x12.
Now, I am working with the LSM6DSV16X sensor. When I try to read the WHO_AM_I register, I receive an incorrect value (0x80 instead of the expected 0x70).
My question is: Do I need to configure any specific registers or perform any initialization steps for the LSM6DSV16X sensor before reading the WHO_AM_I register, similar to the power management configuration required for the ICM-20602 sensor?
2024-07-22 04:58 AM
Hi @Apuroop ,
No, it is not necessary for LSM6DSV16X.
I suggest you to follow our official examples on Github to implement the WhoAmI reading.