2024-09-03 01:56 AM - edited 2024-09-03 03:52 AM
Hi guys,
I am currently trying to read the value of the WhoAmI register using SPI communication with the ICM42688P, but it returns 0xf3 when it should return 0x47.
Is there something wrong with my program or configuration?
Please tell me about it.
.hpp
#pragma once
#include "arm_math.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_gpio.h"
#include "spi.h"
class IMUICM42688P {
public:
IMUICM42688P(SPI_HandleTypeDef &hspi, GPIO_TypeDef *cs_x, uint16_t cs_pin);
void init();
uint8_t getWhoAmI();
float32_t getAccelX();
float32_t getAccelY();
float32_t getAccelZ();
private:
uint8_t read(const uint8_t address);
void write(const uint8_t address, const uint8_t data);
void setConfigs();
void setAccelConfig();
void setPowerManage();
SPI_HandleTypeDef &_hspi;
GPIO_TypeDef *cs_x_;
const uint16_t cs_pin_;
float32_t accel_range_; // default: 2.0f
float32_t imu_range_;
};
.cpp
#include "halplus/layer_driver/circuit/icm42688p.hpp"
IMUICM42688P::IMUICM42688P(SPI_HandleTypeDef &hspi, GPIO_TypeDef *cs_x, uint16_t cs_pin): _hspi(hspi), cs_x_(cs_x), cs_pin_(cs_pin){}
void IMUICM42688P::init(){
HAL_SPI_Init(&_hspi);
HAL_GPIO_WritePin(cs_x_, cs_pin_, GPIO_PIN_SET);
setConfigs();
}
uint8_t IMUICM42688P::getWhoAmI(){
const uint8_t address = 0x75; //< Input who_am_i: 0x75, return: 0x47
return read(address);
}
float32_t IMUICM42688P::getAccelX(){
const int16_t raw_accel_x = static_cast<int16_t>(((read(0x1F) << | read(0x20)));
return (accel_range_ * static_cast<float32_t>(raw_accel_x) / static_cast<float32_t>(INT16_MAX));
}
float32_t IMUICM42688P::getAccelY(){
const int16_t raw_accel_y = static_cast<int16_t>((read(0x21) << | read(0x22));
return (accel_range_ * static_cast<float32_t>(raw_accel_y) / static_cast<float32_t>(INT16_MAX));
}
float32_t IMUICM42688P::getAccelZ(){
const int16_t raw_accel_z = static_cast<int16_t>((read(0x23) << | read(0x24));
return (accel_range_ * static_cast<float32_t>(raw_accel_z) / static_cast<float32_t>(INT16_MAX));
}
uint8_t IMUICM42688P::read(const uint8_t address){
constexpr uint16_t size = 2;
const uint8_t read_mode = 0b10000000; //< (read:1, write:0)
const uint8_t tx_data[size] = {static_cast<uint8_t>(read_mode | address), 0x00};
uint8_t rx_data[size] = {0x00, 0x00};
HAL_GPIO_WritePin(cs_x_, cs_pin_, GPIO_PIN_RESET); // CS pin: LOW
HAL_SPI_TransmitReceive(&_hspi, (uint8_t*)tx_data, (uint8_t*)rx_data, size, 100);
HAL_GPIO_WritePin(cs_x_, cs_pin_, GPIO_PIN_SET);
return rx_data[1];
}
void IMUICM42688P::write(const uint8_t address, const uint8_t data){
constexpr uint16_t size = 2;
const uint8_t tx_data[size] = {address, data};
HAL_GPIO_WritePin(cs_x_, cs_pin_, GPIO_PIN_RESET); // CS pin: LOW
HAL_SPI_Transmit(&_hspi, (uint8_t*)tx_data, size, 100);
HAL_GPIO_WritePin(cs_x_, cs_pin_, GPIO_PIN_SET); // CS pin: HIGH
}
void IMUICM42688P::setConfigs(){
HAL_Delay(10);
setPowerManage();
HAL_Delay(10);
setAccelConfig();
HAL_Delay(10);
}
void IMUICM42688P::setAccelConfig(){
constexpr uint8_t accel_conf_addr = 0x50;
constexpr uint8_t accel_conf_data = 0b01100110; // range:2[g], rate:1k[Hz]
write(accel_conf_addr, accel_conf_data);
}
void IMUICM42688P::setPowerManage(){
constexpr uint8_t accel_conf_addr = 0x4E;
constexpr uint8_t accel_conf_data = 0b00011111; // range:2[g], rate:1k[Hz]
write(accel_conf_addr, accel_conf_data);
}
Microcontroller STM32F407vgt6,cubeide
Best regards.
2024-09-03 02:04 AM
Show waveforms on the bus taken by oscilloscope or logic analyzer.
JW
2024-09-03 03:51 AM
Orange is CS and blue is SCLK.
Best regards.
2024-09-03 03:57 AM
So what about MOSI and MISO?
2024-09-03 04:29 AM
Orange is MOSI and blue is MISO.
2024-09-03 04:36 AM
Can't really see much from those images - please increase the resolution.
Also, it seems to be all "blue" - no orange?
Can you not show all 4 channels together?
2024-09-03 04:41 AM
The blue and orange colors are overlapped so that only blue is visible.
Sorry, but I can only do 2 channels.
Best regards.
2024-09-03 04:48 AM
So shift the traces to not overlap!
2024-09-03 04:54 AM - edited 2024-09-03 05:01 AM
How about this?
2024-09-03 05:01 AM
Sorry, I made a mistake.
This is the original one.