cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 VS MKI194V1(LSM6DSR)

Devon_Lai
Associate

Hi,

I'm currently using the STM32F103 SPI to read the MKI194V1, but I've noticed that the ID I'm reading is always 0x4A. Could you please help me check if there's an issue, or provide any sample code?

I also take a photo of the MKI194V1. The marking on the surface I think is SR 934

 

 
// SPI Initial
 
void MX_SPI2_Init(void)
{
 
  /* USER CODE BEGIN SPI2_Init 0 */
 
  /* USER CODE END SPI2_Init 0 */
 
  /* USER CODE BEGIN SPI2_Init 1 */
 
  /* USER CODE END SPI2_Init 1 */
  hspi2.Instance = SPI2;
  hspi2.Init.Mode = SPI_MODE_MASTER;
  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi2.Init.NSS = SPI_NSS_SOFT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi2.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI2_Init 2 */
 
  /* USER CODE END SPI2_Init 2 */
 
}
 
// Write data to Register
 
void LSM6DSR_Core_Write_Reg(TYPE_U08 U08_Reg, TYPE_U08 U08_Val)
{
  TYPE_U08 AxU08_TxBuf[2];
 
  SPI_MKI194_CS_LOW();
 
  AxU08_TxBuf[0] = U08_Reg;
  AxU08_TxBuf[1] = U08_Val;
 
  HAL_SPI_Transmit(&hspi2, AxU08_TxBuf, 2,1000);
 
  SPI_MKI194_CS_HIGH();
}
 
// Read One Register
 
TYPE_U08 LSM6DSR_Core_Read_Reg(TYPE_U08 U08_Reg)
{
TYPE_U08 AxU08_Ret;
 
U08_Reg |= 0x80;
 
SPI_MKI194_CS_LOW();
 
HAL_SPI_Transmit(&hspi2, &U08_Reg, 1,1000);
HAL_SPI_Receive(&hspi2, &AxU08_Ret, 1,1000);
 
SPI_MKI194_CS_HIGH();
 
return AxU08_Ret;
 
}
 
// Read All Registers
 
void LSM6DSR_Core_Read_All(void)
{
TYPE_U08 AxU08_i;
 
for(AxU08_i = 1 ; AxU08_i < 0x7E ; AxU08_i++)
{
U08_Reg[AxU08_i] = 0x00;
U08_Reg[AxU08_i] = LSM6DSR_Core_Read_Reg(AxU08_i);
}
}

 

S__119840846.jpg

2 REPLIES 2

The WHO_AM_I register should be 0x6B

I'd probably use the Transmit_Receive methods to ensure that the chip-select doesn't go high too early.

 

https://github.com/STMicroelectronics/STM32CubeL4/blob/master/Drivers/BSP/Components/lsm303c/lsm303c.c#L146

ACCELERO_IO_Read  https://github.com/STMicroelectronics/STM32CubeF4/blob/master/Drivers/BSP/STM32F4-Discovery/stm32f4_discovery.c#L575

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks for your help.

I found that sometimes I can receive the correct value (0x6B) and sometimes I can't. I suspect it might be a timing issue, but I haven't found a solution yet.