cancel
Showing results for 
Search instead for 
Did you mean: 

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.

imran.aftab
Associate III

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

  */

}

12 REPLIES 12

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.

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

And when I am only sending data from slave to master then master is receiving the previous data even at 48 MHz

For example Initially my slave rx buffer showing ff which I have not sent even and when I am sending data from slave take a example 2 then hen here at master its showing 0 and when I am sending next data 3 then its receiving 2 and so on master is receiving previous data which slave sent.