Search code examples
stm32stm32f4discovery

Is this the right way to SPI?


I am currently trying to check with the function, if my SPI is working currectly. This function should return 1, if everything is fine...but it always return 0. Maybe values in pTxData are wrong ?

uint8_t Gyro_CanReadWHOAMI() {
    uint8_t pTxData[2] = {0x0F | 0x80, 0};
    uint8_t pRxData[2] = {0, 0};
    
    HAL_GPIO_WritePin(NCS_MEMS_SPI_GPIO_Port, NCS_MEMS_SPI_Pin, GPIO_PIN_RESET);
    HAL_SPI_TransmitReceive(&hspi5, pTxData, pRxData, 2, HAL_MAX_DELAY);
    
    HAL_GPIO_WritePin(NCS_MEMS_SPI_GPIO_Port, NCS_MEMS_SPI_Pin, GPIO_PIN_SET);
    uint8_t test = pRxData[1] == 0x32;
    return test;
}
int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  SystemClock_Config();
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI5_Init();

  uint32_t reads = 0;

  while (1)
  {
      Gyro_CanReadWHOAMI();
  }

}

Using STM32F429I-DISC1


Solution

  • Solved, wrong values

    uint8_t Gyro_CanReadWHOAMI() {
    
          uint8_t pTxData[2] = {0b10000000 | 0x0F, 0};
    
          uint8_t pRxData[2] = {0, 0};
    
          HAL_GPIO_WritePin(NCS_MEMS_SPI_GPIO_Port, NCS_MEMS_SPI_Pin, GPIO_PIN_RESET);
          HAL_SPI_TransmitReceive(&hspi5, pTxData, pRxData, 2, HAL_MAX_DELAY);
    
          HAL_GPIO_WritePin(NCS_MEMS_SPI_GPIO_Port, NCS_MEMS_SPI_Pin, GPIO_PIN_SET);
          uint8_t test = pRxData[1] == 0xD4;
          return test;
    

    }