cancel
Showing results for 
Search instead for 
Did you mean: 

I always getting 0xff in my SPI RX Buffer

Sdrid.1
Associate II

I used the STM32F401 discovery board which has an L3GD20H gyroscope sensor communicated with the MCU via SPI1, I made the necessary configuration by Cube MX

I tried to read the device ID of the gyroscope but each time I received 0xff,

can someone help me to resolve this problem to continue my work

  */
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
  SpiTxBuff[0]=0x0f;
  HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET);
  HAL_SPI_Transmit(&hspi1,&SpiTxBuff[0],1,50);
  HAL_SPI_Receive(&hspi1,&SpiRxBuff[0],1,50);
  HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_SET);
  
  
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

4 REPLIES 4

Add delays after setting CS low or high

Check the configuration of PE3, and scope it to see it go LOW/HIGH

Use HAL_SPI_TransmitReceive, send 0x0F,0xFF

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

why i should send 0x0f , 0xff

the adress of the register who I AM is 0x0f

To generate clocks for the pad​ byte to get a response on the symmetrical bus to clock out the ID

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sdrid.1
Associate II
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  
  
 
  HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET);
  spiTxBuf[0]=0x0f|0x80;
  spiTxBuf[1]=0x00;
  spiRxBuf[1]=0xff;
  HAL_SPI_Transmit(&hspi1,spiTxBuf,2,50);
  HAL_SPI_Receive(&hspi1,&spiRxBuf[1],1,50);
  HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_SET);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}