Skip to main content
Associate III
September 3, 2024
Question

The value in the WhoAmI register cannot be read properly.

  • September 3, 2024
  • 5 replies
  • 3379 views

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);
}

 

 

 

 

 

 

 

スクリーンショット 2024-08-30 183826.png

Microcontroller STM32F407vgt6,cubeide

Best regards.

 

5 replies

waclawek.jan
Super User
September 3, 2024

Show waveforms on the bus taken by oscilloscope or logic analyzer.

JW

Associate III
September 3, 2024

スクリーンショット 2024-09-03 193635.png

スクリーンショット 2024-09-03 194351.png

Orange is CS and blue is SCLK.

 

Best regards.

Andrew Neil
Super User
September 3, 2024

So what about MOSI and MISO?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Tesla DeLorean
Guru
September 3, 2024

Show the wiring diagram. 

Make sure you have a common ground.

Show CLK vs MOSI

Get the clock rate to 1 MHz rather than 10 MHz

 

 

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Associate III
September 3, 2024

スクリーンショット 2024-09-03 211921.png

スクリーンショット 2024-09-03 211942.png

スクリーンショット 2024-09-03 212001.png

Orange is SCLK and blue is MOSI.

Best regards.

Tesla DeLorean
Guru
September 3, 2024

Your scaling and what you're capturing is unhelpful. You need to capture the interaction of the data/clocks at the point where you're interacting with the WHOAMI register.

Do you work with someone who has more experience debugging digital circuits and signals?

Perhaps start by drawing a schematic of exactly what you've wired to what.

The make/model of boards the TDK IC is mounted on would also be good contextual information in your presentation of the problem.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
September 3, 2024

This doesn't seem to be correct - there are no pulses on SCK (nor MISO/MOSI).

Double-check the wiring.

JW

TDK
Super User
September 3, 2024

Not going to catch a 10 MHz signal with a 27 kHz sampling rate.

TDK_0-1725368624701.png

Code seems fine.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate III
September 3, 2024

スクリーンショット 2024-09-03 224102.png

How much is optimal in this case?

Best regards.

 

waclawek.jan
Super User
September 3, 2024

Show waveforms on the bus taken by oscilloscope, this time set with proper sampling rate (i.e. at least 4x the SPI baudrate, but more is preferrable).

JW