Skip to main content
imran.aftab
Associate II
March 23, 2020
Question

I am using a STM32f7 as a SPI master and STM32F0 as SPI slave. When I am Running Both MCU at 48 MHz then transmission and reception both are working well but when I am running STM32F7 at 216 MHz then its not working.

  • March 23, 2020
  • 5 replies
  • 3558 views

Please help me.

My code

SPI Master Code----------------------

Initialization

/* SPI4 init function */

void MX_SPI4_Init(void)

{

 hspi4.Instance = SPI4;

 hspi4.Init.Mode = SPI_MODE_MASTER;

 hspi4.Init.Direction = SPI_DIRECTION_2LINES;

 hspi4.Init.DataSize = SPI_DATASIZE_16BIT;

 hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi4.Init.CLKPhase = SPI_PHASE_2EDGE;

 hspi4.Init.NSS = SPI_NSS_SOFT;

 hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;

 hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi4.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi4.Init.CRCPolynomial = 7;

 hspi4.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

 hspi4.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

 if (HAL_SPI_Init(&hspi4) != HAL_OK)

 {

  Error_Handler();

 }

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(spiHandle->Instance==SPI4)

 {

 /* USER CODE BEGIN SPI4_MspInit 0 */

 /* USER CODE END SPI4_MspInit 0 */

  /* SPI4 clock enable */

  __HAL_RCC_SPI4_CLK_ENABLE();

  

  __HAL_RCC_GPIOE_CLK_ENABLE();

  /**SPI4 GPIO Configuration   

  PE2   ------> SPI4_SCK

  PE5   ------> SPI4_MISO

  PE6   ------> SPI4_MOSI 

  */

  GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_5|GPIO_PIN_6;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;

  HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

 /* USER CODE BEGIN SPI4_MspInit 1 */

 /* USER CODE END SPI4_MspInit 1 */

 }

}

Main code

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

HAL_Delay(4000);

spiDataTx[0]++;

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_4, GPIO_PIN_RESET);

HAL_SPI_Transmit(&hspi4, (uint8_t *)spiDataTx, 1, HAL_MAX_DELAY);

while(HAL_SPI_GetState(&hspi4)!=HAL_SPI_STATE_READY){}

HAL_SPI_Receive(&hspi4, (uint8_t *)spiDataRx, 1, HAL_MAX_DELAY);

HAL_GPIO_WritePin(GPIOF, GPIO_PIN_4, GPIO_PIN_SET);

//HAL_SPI_FlushRxFifo(&hspi4);

 }

 /* USER CODE END 3 */

Slave code-----------------

Initialization

/* SPI2 init function */

void MX_SPI2_Init(void)

{

 hspi2.Instance = SPI2;

 hspi2.Init.Mode = SPI_MODE_SLAVE;

 hspi2.Init.Direction = SPI_DIRECTION_2LINES;

 hspi2.Init.DataSize = SPI_DATASIZE_16BIT;

 hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;

 hspi2.Init.NSS = SPI_NSS_HARD_INPUT;

 hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi2.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi2.Init.CRCPolynomial = 7;

 hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

 hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

 if (HAL_SPI_Init(&hspi2) != HAL_OK)

 {

  Error_Handler();

 }

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 if(spiHandle->Instance==SPI2)

 {

 /* USER CODE BEGIN SPI2_MspInit 0 */

 /* USER CODE END SPI2_MspInit 0 */

  /* SPI2 clock enable */

  __HAL_RCC_SPI2_CLK_ENABLE();

  __HAL_RCC_GPIOB_CLK_ENABLE();

  /**SPI2 GPIO Configuration  

  PB12  ------> SPI2_NSS

  PB13  ------> SPI2_SCK

  PB14  ------> SPI2_MISO

  PB15  ------> SPI2_MOSI

  */

  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* SPI2 interrupt Init */

  HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(SPI2_IRQn);

 /* USER CODE BEGIN SPI2_MspInit 1 */

 /* USER CODE END SPI2_MspInit 1 */

 }

}

main code-------------------

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_SPI2_Init();

 /* USER CODE BEGIN 2 */

HAL_SPI_Receive_IT(&hspi2, (uint8_t *)spiDataRx, 1);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

if(spiRxStatus){

HAL_SPI_Transmit(&hspi2, (uint8_t *)spiDataRx, 1, HAL_MAX_DELAY);

HAL_SPI_Receive_IT(&hspi2, (uint8_t *)spiDataRx, 1);

spiRxStatus=0;

}

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 }

SPI Rx callback-------------------

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)

{

 /* Prevent unused argument(s) compilation warning */

 UNUSED(hspi);

spiRxStatus=1;

 /* NOTE : This function should not be modified, when the callback is needed,

      the HAL_SPI_RxCpltCallback should be implemented in the user file

  */

}

This topic has been closed for replies.

5 replies

TDK
March 23, 2020

Consider adding more details to make your post useful and/or answerable.

"If you feel a post has answered your question, please click ""Accept as Solution""."
imran.aftab
Associate II
March 24, 2020

I have added my code in Question Please verify

Jack Peacock_2
Associate II
March 23, 2020

The SPI clock rate is limited to the maximum for the SPI port on the STM32F0. Check the datasheet for the port you are using on the 'F0. If you increase the 'F7 clock rate you must prescale the SPI clock to match the maximum 'F0 rate.

Jack Peacock

imran.aftab
Associate II
March 24, 2020

Thanks For reply

I have tried one by one all prescaler in master device.

hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;

How can I configure SPI clock in slave device?

S.Ma
Principal
March 23, 2020

generally speaking the max spi clock speed is typically the slave max speed, which itself is typically SYSCLK/4 = 12 MHz. Master is typically SYSCLK/2 so change the prescaler to generate 12 MHz or less on SPI.SCK pin

imran.aftab
Associate II
March 24, 2020

Thanks For reply

I have tried one by one all prescaler in master device.

hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;

even at lowest speed its not working.

waclawek.jan
Super User
March 24, 2020

What are exactly symptoms of "not working"?

In master, you may need to add delays after NSS pin setting low/before setting high.

JW

imran.aftab
Associate II
March 24, 2020

I tried by giving delay too but its not working

Not working means

I am sending a character from master to slave and and the same character back to master.

When I am only sending character master to salve its working properly.

And when I am sending back the same data then slave even not receiving data but sometime it receives. But its never returning data to master.

But the same code when I run the master also at 48 MHz then it works perfectly.

waclawek.jan
Super User
March 24, 2020

If you insert delays between NSS edges and SPI transmission, and slow down the SPI clock at master, the waveform should be the same as with the master clocked at slower clock, i.e. indistinguishable by the slave. In other words, in that case the slave has no reason to behave differently.

You should observe the signals using logic analyzer, at both situations, and compare.

JW