cancel
Showing results for 
Search instead for 
Did you mean: 

SPI clock frequency in STM32F769NI Disco board

Jeevan Rudramurthy
Associate II
Posted on December 12, 2017 at 06:35

how to configure for SPI clock frequency in SPI example provided.

Example give below i am using

In atmel board we are using 5mhz or 50khz SPI clock frequency.

int main(void)

{

  /* Enable the CPU Cache */

  CPU_CACHE_Enable();

  HAL_Init();

  /* Configure the system clock to 216 MHz */

  SystemClock_Config();

  /*##-1- Configure the SPI peripheral #######################################*/

  /* Set the SPI parameters */

  SpiHandle.Instance               = SPIx;

  SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;

  SpiHandle.Init.Direction         = SPI_DIRECTION_2LINES;

  SpiHandle.Init.CLKPhase          = SPI_PHASE_1EDGE;

  SpiHandle.Init.CLKPolarity       = SPI_POLARITY_LOW;

  SpiHandle.Init.DataSize          = SPI_DATASIZE_8BIT;

  SpiHandle.Init.FirstBit          = SPI_FIRSTBIT_MSB;

  SpiHandle.Init.TIMode            = SPI_TIMODE_DISABLE;

  SpiHandle.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;

  SpiHandle.Init.CRCPolynomial     = 7;

  SpiHandle.Init.NSS               = SPI_NSS_SOFT;

#ifdef MASTER_BOARD

  SpiHandle.Init.Mode = SPI_MODE_MASTER;

#else

  SpiHandle.Init.Mode = SPI_MODE_SLAVE;

  HAL_Delay(5); 

#endif /* MASTER_BOARD */

  if(HAL_SPI_Init(&SpiHandle) != HAL_OK)

  {

    /* Initialization Error */

    Error_Handler();

  }

#ifdef MASTER_BOARD

  /* Configure User push-button button */

  BSP_PB_Init(BUTTON_USER,BUTTON_MODE_GPIO);

  /* Wait for User push-button press before starting the Communication */

  while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET)

  {

    HAL_Delay(100);

  }

#endif /* MASTER_BOARD */

  /* Synchronization between Master and Slave */

  Slave_Synchro();

}

#ifdef MASTER_BOARD

static void Master_Synchro(void)

{

  uint8_t txackbytes = SPI_MASTER_SYNBYTE, rxackbytes = 0x00;

  do

  {

    /* Call SPI write function to send command to slave */

    if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t *)&txackbytes, (uint8_t *)&rxackbytes, 1, HAL_MAX_DELAY) != HAL_OK)

    {

      Error_Handler();

    }

  }while(rxackbytes != SPI_SLAVE_SYNBYTE);

}

#else

static void Slave_Synchro(void)

{

  uint8_t txackbyte = SPI_SLAVE_SYNBYTE, rxackbyte = 0x00;

//  do

  {

    if (HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t *)&txackbyte, (uint8_t *)&rxackbyte, 1, HAL_MAX_DELAY) != HAL_OK)

    {

      Error_Handler();

    }

  }

//  while (rxackbyte != SPI_MASTER_SYNBYTE);

}

#endif

/**

  * @brief  System Clock Configuration

  *         The system Clock is configured as follow :

  *            System Clock source            = PLL (HSE)

  *            SYSCLK(Hz)                     = 216000000

  *            HCLK(Hz)                       = 216000000

  *            AHB Prescaler                  = 1

  *            APB1 Prescaler                 = 4

  *            APB2 Prescaler                 = 2

  *            HSE Frequency(Hz)              = 25000000

  *            PLL_M                          = 25

  *            PLL_N                          = 432

  *            PLL_P                          = 2

  *            PLL_Q                          = 9

  *            PLL_R                          = 7 

  *            VDD(V)                         = 3.3

  *            Main regulator output voltage  = Scale1 mode

  *            Flash Latency(WS)              = 7

  * @param  None

  * @retval None

  */

void SystemClock_Config(void)

{

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_OscInitTypeDef RCC_OscInitStruct;

  HAL_StatusTypeDef ret = HAL_OK;

  /* Enable HSE Oscillator and activate PLL with HSE as source */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  RCC_OscInitStruct.PLL.PLLM = 25;

  RCC_OscInitStruct.PLL.PLLN = 432;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

  RCC_OscInitStruct.PLL.PLLQ = 9;

  RCC_OscInitStruct.PLL.PLLQ = 7;

 

  ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);

  if(ret != HAL_OK)

  {

    while(1) { ; }

  }

 

  /* Activate the OverDrive to reach the 216 MHz Frequency */ 

  ret = HAL_PWREx_EnableOverDrive();

  if(ret != HAL_OK)

  {

    while(1) { ; }

  }

 

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */

  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

 

  ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);

  if(ret != HAL_OK)

  {

    while(1) { ; }

  } 

}

6 REPLIES 6
usama2
Associate II
Posted on December 12, 2017 at 07:00

You can set the spi baud rate prescaler 

SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;

You can select the prescaler value according to the APB clock source for the SPI peripheral.

SPI 1,4,5,6 are connected to APB2 and 2,3 to APB1

Posted on December 12, 2017 at 07:07

can you please tell me what is the baudrate need to keep for 50kHZ and 5Mhz according to you said.

Posted on December 12, 2017 at 07:29

Since the baudrate prescaler can only be set to values of 2,4,8,16,32,64,128,256 You'd need to change the APB frequency as well.

For example, to get a 5Mhz SPI CLK, (For example for SPI 1/4/5/6) you need to set the APB2 clock to some value like 80MHz, and then use a prescaler of 16 and you get 80M/16 = 5MHz.

To change the APB2, its prescalar can also only take a limited number of values. So you might have to decrease the HCLK.

Posted on December 12, 2017 at 07:58

where we need to change the

APB  frequency,

for given below example,

/**

  * @brief  System Clock Configuration

  *         The system Clock is configured as follow :

  *            System Clock source            = PLL (HSE)

  *            SYSCLK(Hz)                     = 216000000

  *            HCLK(Hz)                       = 216000000

  *            AHB Prescaler                  = 1

  *            APB1 Prescaler                 = 4

  *            APB2 Prescaler                 = 2

  *            HSE Frequency(Hz)              = 25000000

  *            PLL_M                          = 25

  *            PLL_N                          = 432

  *            PLL_P                          = 2

  *            PLL_Q                          = 9

  *            PLL_R                          = 7 

  *            VDD(V)                         = 3.3

  *            Main regulator output voltage  = Scale1 mode

  *            Flash Latency(WS)              = 7

  * @param  None

  * @retval None

  */

void SystemClock_Config(void)

{

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  RCC_OscInitTypeDef RCC_OscInitStruct;

  HAL_StatusTypeDef ret = HAL_OK;

  /* Enable HSE Oscillator and activate PLL with HSE as source */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

  RCC_OscInitStruct.PLL.PLLM = 25;

  RCC_OscInitStruct.PLL.PLLN = 432;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

  RCC_OscInitStruct.PLL.PLLQ = 9;

  RCC_OscInitStruct.PLL.PLLQ = 7;

 

  ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);

  if(ret != HAL_OK)

  {

    while(1) { ; }

  }

 

  /* Activate the OverDrive to reach the 216 MHz Frequency */ 

  ret = HAL_PWREx_EnableOverDrive();

  if(ret != HAL_OK)

  {

    while(1) { ; }

  }

 

  /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */

  RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; 

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

 

  ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7);

  if(ret != HAL_OK)

  {

    while(1) { ; }

  } 

}

Peter Chang
Associate III
Posted on December 12, 2017 at 10:38

Modify  

QSPIHandle.Init.ClockPrescaler     = 1;   /* QSPI SCLK freq = 216 MHz/(1+1) = 108 Mhz */

In function : uint8_t BSP_QSPI_Init(void)

It located at D:\STM32Cube_FW_F7_V1.6.0\Drivers\BSP\STM32F769I-Discovery\stm32f769i_discovery_qspi.c

MX25L512 can run SCLK up to 100MHz. Support DTR also.

Posted on December 12, 2017 at 11:10

 ,

 ,

I am using SPI full duplex example not QSPI example

/* ♯ ♯ -1- Configure the SPI peripheral ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ ♯ */

 , /* Set the SPI parameters */

 , SpiHandle.Instance , , , , , , , , , , , , , , = SPIx,

 , SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32,

 , SpiHandle.Init.Direction , , , , , , , , = SPI_DIRECTION_2LINES,

 , SpiHandle.Init.CLKPhase , , , , , , , , , = SPI_PHASE_1EDGE,

 , SpiHandle.Init.CLKPolarity , , , , , , = SPI_POLARITY_HIGH,

 , SpiHandle.Init.DataSize , , , , , , , , , = SPI_DATASIZE_8BIT,

 , SpiHandle.Init.FirstBit , , , , , , , , , = SPI_FIRSTBIT_MSB,

 , SpiHandle.Init.TIMode , , , , , , , , , , , = SPI_TIMODE_DISABLE,

 , SpiHandle.Init.CRCCalculation , , , = SPI_CRCCALCULATION_DISABLE,

 , SpiHandle.Init.CRCPolynomial , , , , = 7,

 , SpiHandle.Init.NSS , , , , , , , , , , , , , , = SPI_NSS_SOFT,