cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 spi receive procedure without hal

JSovi.1
Associate

Good afternoon,

I am a newbie in the programing of stm32. Just working on a project, where is a serious problem with timing. Trying to implement FOC on the PMSM motor where I need to do a calculation in 50us loop, which is fast to communicate with angle sensor via SPI and HAL. Let me explain the situation.

I tried to work with HAL, but as I read everywhere and explored by myself: if you need speed put it away. So my plan is to use CubeMX to configure all necessary registers and read data directly from the register DR. One small thing, that sensor communicates with a 16-bit frame.

Code that I produced:

__HAL_SPI_ENABLE(&hspi3);
 
     HAL_GPIO_WritePin_Fast(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET); //switch off the pin 
     hspi3.Instance->DR = 0;
     while ((hspi3.Instance->SR & SPI_FLAG_RXNE) == 0){}         //Wait for Data Ready to Read
      RxByte[0] = hspi3.Instance->DR;                      //Read Data Register Directly
 
     HAL_GPIO_WritePin_Fast(GPIOD, GPIO_PIN_2, GPIO_PIN_SET); // switch on the pin
 
__HAL_SPI_DISABLE(&hspi3);
/**
  * @brief SPI3 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI3_Init(void)
{
 
  /* USER CODE BEGIN SPI3_Init 0 */
 
  /* USER CODE END SPI3_Init 0 */
 
  /* USER CODE BEGIN SPI3_Init 1 */
 
  /* USER CODE END SPI3_Init 1 */
  /* SPI3 parameter configuration*/
  hspi3.Instance = SPI3;
  hspi3.Init.Mode = SPI_MODE_MASTER;
  hspi3.Init.Direction = SPI_DIRECTION_2LINES;
  hspi3.Init.DataSize = SPI_DATASIZE_16BIT;
  hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi3.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi3.Init.NSS = SPI_NSS_SOFT;
  hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi3.Init.CRCPolynomial = 7;
  hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&hspi3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI3_Init 2 */
 
  /* USER CODE END SPI3_Init 2 */
 
}

In this state, it isn't working, has anyone idea how to solve this issue? Thank you

edit: already solved...

It needed to put something to DR register to init clock, which causes start the communication.

 hspi3.Instance->DR = 0;

Then you need to test if data are ready for reading and then read data from register DR.

while ((hspi3.Instance->SR & SPI_FLAG_RXNE) == 0){}        //Wait for Data Ready to Read

     RxByte[0] = hspi3.Instance->DR;                     //Read Data Register Directly

1 ACCEPTED SOLUTION

Accepted Solutions
Imen.D
ST Employee

Hello @JSovi.1​ and welcome to the STM32 Community =)

I'm glad to know that you have solved your issue.

You can add a post detailing how you solved your problem ("Need to put something to DR register to init clock"). then mark it as best by clicking on the "Select as Best" button. This will help other users find this solution more quickly.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

View solution in original post

1 REPLY 1
Imen.D
ST Employee

Hello @JSovi.1​ and welcome to the STM32 Community =)

I'm glad to know that you have solved your issue.

You can add a post detailing how you solved your problem ("Need to put something to DR register to init clock"). then mark it as best by clicking on the "Select as Best" button. This will help other users find this solution more quickly.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen