ST


A world leader in providing the semiconductor solutions that make a positive contribution to people’s lives, both today and in the future.

SPI output clock frequency

The SPI output frequency can only be equal to some values. This is due because the SPI output frequency is divided by a prescaler which is equal to 2, 4, 8, 16, 32, 64, 128 or 256. It depends also on SPI input clock frequency.

To illustrate this, we take the NUCLEO_F103RB as example. Follow these steps to know the available SPI1 output frequencies:

1) Find in the device Reference Manual which input clock is used for SPI1.

You can also look in the spi_api.c file (function spi_get_clock_freq). You can see that PCLK2 clock is used for SPI1.

2) Check in system_clock.c file (function SetSysClock_PLL_HSE used per default for this board) which value has PCLK2 clock.

This is given by the line:

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;           // 72 MHz

Now you know that SPI1 input clock is equal to 72 MHz.

3) Knowing the SPI input clock, you can only have these SPI output frequencies:

  • 36 MHz (72 MHz / 2)
  • 18 MHz (72 MHz / 4)
  • 9 MHz (72 MHz / 8)
  • 4.5 MHz (72 MHz / 16)
  • 2.25 MHz (72 MHz / 32)
  • 1.125 MHz (72 MHz / 64)
  • 562 kHz (72 MHz / 128)
  • 281 kHz (72 MHz / 256)

4) If you want to have a SPI output frequency around 1 MHz, you will need to pass a value above 1.125 MHz and below 2.25 MHz when you call the SPI frequency method.

SPI mydevice(SPI_MOSI, SPI_MISO, SPI_SCK);
mydevice.frequency(1200000); // real value = 1.125 MHz

5) If you want to have other output frequencies, you will have to modify the different clock dividers inside the system_clock.c file. But note that will also have an impact on other peripherals. Read the device reference manual for more information about clock tree.