cancel
Showing results for 
Search instead for 
Did you mean: 

Problems between STM32F413 and ICM42688 using SPI

SpeedyPLH
Associate II

HI All, 

I am trying to connect an STM32F413 and the TDK ICM-42688 IMU chip using SPI.  The connection works correctly and the ICM-42688 responds, but the data doesnt seem to be clocked into the STM32F413 chip.   

The ICM-42688 Timing data is:

ICM42688 SPI Timing.png

 So CPOL = 1, CPHA = 2 and SCLK ic high in idle.

(I have also tried CPOL = 1 and CPHA = 1  which also delivers an identical result)

The Device Config is as follows:  

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;

hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi1.Init.CRCPolynomial = 10;

 

SpeedyPLH_0-1713325909638.png

I have it slowed down at present to see if speed is the cause of my problems...

 

When I monitor the lines with my Saleae 16 I get a good response.

ICM42688 SPI Timing Saleae.png

The 0x76,00 is a write to the register 0x76 to select bank 0.

The 0xF5,00 is a read request for the Register 0x75 and it should return 0x47 which is is doing - YAY.

My problem is that the data doesnt seem to be clocked into the STM32F413 chip...

I always end up with 0x00.

I have tried a number of different permutations using HAL_SPI_Read and HAL_SPI_TransmitReceive and none seem to work.

Here is the code doing the work.

 

 
const uint16_t SPI_TIMEOUT = 100; // 100 ms
static constexpr uint8_t WHOAMI = 0x47;
 
uint8_t CSpiDevice::_readRegister(uint8_t *recv, uint16_t rLen)
{
uint8_t result = HAL_OK;
HAL_GPIO_WritePin(_pGPIO, _CS, GPIO_PIN_RESET);
// result = HAL_SPI_Receive((SPI_HandleTypeDef*)get_handle(), recv, (uint16_t)rLen, SPI_TIMEOUT);
result = HAL_SPI_TransmitReceive ((SPI_HandleTypeDef*)get_handle(), recv, recv, rLen, SPI_TIMEOUT);
HAL_GPIO_WritePin(_pGPIO, _CS, GPIO_PIN_SET);
return result == HAL_OK ? OK : ERR;
}
 
template <typename T>
uint8_t Cicm42688P::RegisterRead(T reg)
{
uint8_t cmd[2] {};
cmd[0] = static_cast<uint8_t>(reg) | DIR_READ;
SelectRegisterBank(reg);
if(_readRegister(cmd, sizeof(cmd))!= OK)
INFO_LOG(logger::CdataLog::errorCategory_e::data_error,sRReadErr,RReadErrLn,0);
return cmd[1];
}
 
static const char sProbeError[] = "probe() Returned (0x%hx)";
static constexpr uint8_t probeErrorLn = strlen(sProbeError);
 
uint8_t Cicm42688P::probe(void) {
SelectRegisterBank(REG_BANK_SEL_BIT::BANK_SEL_0, true);
uint8_t whoami = RegisterRead(Register::BANK_0::WHO_AM_I);
if (whoami == WHOAMI) {
INFO_LOG(logger::CdataLog::errorCategory_e::information_only,
sProbeError, probeErrorLn,whoami);
return OK;
 
} else {
INFO_LOG(logger::CdataLog::errorCategory_e::configuration_error,
sProbeError, probeErrorLn,whoami);
}
return ERR;
};

 

I am somewhat flumoxed at present.

Any and all thoughts are appeciated.  

Thanks in Advance.

 

cheers

Paul

5 REPLIES 5
SpeedyPLH
Associate II

Sorry Typo . 0x75 is a register in Bank 0. 

Amit29
Associate

Hey can u share the code for the driver , I am currently working on a project and i need the drivers code very urgently 

kindly share the libarary you used to interface the ICM-42688P 

Techn
Senior III

Spi_TransmitReceive , you have to fill with the register value and then dummy bytes of zero to clock out the data. For example 

rx[0] = 0x75; rx[1] = 0  till the len count. 

FYI. As per datasheet, the first bit should be 1 for read and 0 for write. So you can try eith 0xF5

If you feel a post has answered your question, please click "Accept as Solution".

sir do you have the code for ICM42688 for interfacing with stm32 ?? if yes ,kindly share it.

 

Qingcha
Associate

Hi,speedyPLH.

Using the same parameter(except the baudrate->656.25KBits/s ) in my project that it seems working,you can try my code

 

void SPI2_TestFunction(void)

{

SPI2_CS_Enable();

SPI2_Send_buffer[0]= 0x76|WriteOperation;

HAL_SPI_TransmitReceive(&hspi2, &SPI2_Send_buffer[0], &SPI2_Recv_buffer[0], 2, 10);

 

SPI2_CS_Disable();

HAL_Delay(10);

 

SPI2_CS_Enable();

SPI2_Send_buffer[0]= 0x75|ReadOperation;

HAL_SPI_TransmitReceive(&hspi2, &SPI2_Send_buffer[0], &SPI2_Recv_buffer[0], 2, 10);

 

SPI2_CS_Disable();

 

}

Qingcha_0-1729498713628.png