AnsweredAssumed Answered

Porting STM32F4 SPI -> STM32F7 SPI

Question asked by kovaliov.nikolaj on Sep 17, 2015
Latest reply on Sep 18, 2015 by kovaliov.nikolaj
Good day,

I tried to port STM32F4discovery SPI to STM32F7discovery SPI, without any luck.

Can you please help me with that.

Here is my code.

I want to port this function:


uint8_t SPI_SEND_DATA(uint8_t data){
 
  /* Wait if TXE cleared, Tx FIFO is full. */
  while ((SPI2->SR & TXE) == 0);
  SPI2->DR = data;
 
  /* Wait if RNE cleared, Rx FIFO is empty. */
  while ((SPI2->SR & RXNE) == 0);
  return SPI2->DR;
}


to f7 without any luck:


SPI_HandleTypeDef spi2_;

uint8_t SPI_SEND_DATA(uint8_t data){
       
  while((spi2_.Instance->SR & SPI_FLAG_TXE) == 0);
  spi2_.Instance->DR = data;
         
  while((spi2_.Instance->SR & SPI_FLAG_RXNE) == 0);
  return spi2_.Instance->DR;
}


or:

uint8_t SPI_SEND_DATA(uint8_t data){

  uint8_t data_return;  
  HAL_SPI_TransmitReceive(&spi2_, &data, &data_return, 1, 5000);
  return data_return;
}


Here is my init function:

void spi2_init(void){
        
  GPIO_InitTypeDef  GPIO_InitStruct;
    
  __HAL_RCC_GPIOI_CLK_ENABLE(); //CS CLOCK
  __HAL_RCC_GPIOI_CLK_ENABLE(); //SCK CLOCK
  __HAL_RCC_GPIOB_CLK_ENABLE(); //MISO CLOCK
  __HAL_RCC_GPIOB_CLK_ENABLE(); //MOSI CLOCK
  
  __HAL_RCC_SPI2_CLK_ENABLE(); //SPI CLOCK
          
  GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull  = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;


  GPIO_InitStruct.Pin = GPIO_PIN_0;       
  HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); //PI0 CS
      
  GPIO_InitStruct.Pin       = GPIO_PIN_1;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); //PI1 SCK

  GPIO_InitStruct.Pin = GPIO_PIN_14;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); //PB14 MISO

  GPIO_InitStruct.Pin = GPIO_PIN_15;
  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); //PB15 MOSI

  spi2_.Instance               = SPI2;
  spi2_.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  spi2_.Init.Direction         = SPI_DIRECTION_2LINES;
  spi2_.Init.CLKPhase          = SPI_PHASE_1EDGE;
  spi2_.Init.CLKPolarity       = SPI_POLARITY_LOW;
  spi2_.Init.DataSize          = SPI_DATASIZE_8BIT;
  spi2_.Init.FirstBit          = SPI_FIRSTBIT_MSB;
  spi2_.Init.TIMode            = SPI_TIMODE_DISABLE;
  spi2_.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
  spi2_.Init.CRCPolynomial     = 7;
  spi2_.Init.NSS               = SPI_NSS_SOFT;

  spi2_.Init.Mode = SPI_MODE_MASTER;

  if(HAL_SPI_Init(&spi2_) != HAL_OK)
  {
  printf("\nSPI INIT ERROR!");
  }
     
  printf("\nSPI INIT OK!");
}


Outcomes