AnsweredAssumed Answered

STM32F4 DISCO(MB997) and LIS3DSH using HAL (Read WHO_AM_I return 254)

Question asked by peter.john on Jan 10, 2016
Latest reply on Jan 12, 2016 by 68516
Hello
I am trying configure STM32F4 DISCO to read LIS3DSH using HAL but I have problems on read WHO_AM_I (0x0F) with default value 0011 1111

I generate the configuration in MX cube:
void MX_SPI1_Init(void)
{


  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;//LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;//1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;//4;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;//7;
  HAL_SPI_Init(&hspi1);

main()
{
      ****
      MX_GPIO_Init();
      MX_SPI1_Init();
      HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
     __HAL_SPI_ENABLE(&hspi1);
     write_accelerator_register(0x20, 0x17, &hspi1);
     write_accelerator_register(0x24, 0x00, &hspi1);
     uint8_t id;
     id=read_accelerator_register(0x0F, &hspi1);
      ****
}

void write_accelerator_register(uint8_t piRegister, uint8_t piData, SPI_HandleTypeDef* phSPI)
{
     // Set SS to low to select the accelerator on the SPI interface.
     HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);


     // Send register index which we want to write.
     HAL_SPI_Transmit(phSPI, &piRegister, 1, 100);
     HAL_SPI_Transmit(phSPI, &piData,     1, 100);
     HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
}
uint8_t read_accelerator_register(uint8_t piRegister, SPI_HandleTypeDef* phSPI)
{
     uint8_t iResult = 0;


     // Set SS to low to select the accelerator on the SPI interface.
     HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);


     // Set MSB to indicate a read operation.
      piRegister |= 0x80;
      HAL_SPI_Transmit(phSPI, &piRegister, 1, 100);
      HAL_SPI_Receive(phSPI, &iResult, 1, 100);
     HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
     return iResult;
}
Can you help me?
Best regards
(Sorry for the english)

Outcomes