SPI Clock Divider Calculator

Calculate SPI prescaler (BR bits) and achievable SCLK for STM32, nRF52840, ESP32, and RP2040.

Parameters

Result

Actual SCLK

5.25 MHz

÷16

Error

-34.38%

exceeds 5% — review

Register / config snippet

SPI1->CR1 |= (3U << SPI_CR1_BR_Pos);  /* ÷16 → 5.25 MHz */

All available SCLKs — STM32F4 (APB2 84 MHz)

Prescaler / settingSCLKRegister / bits
÷242 MHzBR=0 (0b000)
÷421 MHzBR=1 (0b001)
÷810.5 MHzBR=2 (0b010)
÷16selected5.25 MHzBR=3 (0b011)
÷322.625 MHzBR=4 (0b100)
÷641.313 MHzBR=5 (0b101)
÷128656.25 kHzBR=6 (0b110)
÷256328.125 kHzBR=7 (0b111)

How STM32 SPI prescalers work

STM32 SPI peripherals derive SCLK from the APB peripheral clock via the BR[2:0] field in SPI_CR1. The three bits select a power-of-two prescaler:

BR[2:0]PrescalerSCLK (84 MHz PCLK2)
000÷242 MHz
001÷421 MHz
010÷810.5 MHz
011÷165.25 MHz
100÷322.625 MHz
101÷641.3125 MHz
110÷128656.25 kHz
111÷256328.125 kHz

Formula: SCLK = PCLK / 2^(BR+1)

In C, set it before enabling the peripheral:

/* Select ÷4 (BR=1) on SPI1 */
SPI1->CR1 &= ~SPI_CR1_BR_Msk;
SPI1->CR1 |= (1U << SPI_CR1_BR_Pos);  /* BR[2:0] = 001 */
SPI1->CR1 |= SPI_CR1_SPE;             /* enable */

With STM32 HAL, set hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4 before calling HAL_SPI_Init(). The HAL macro values are SPI_BAUDRATEPRESCALER_2 through SPI_BAUDRATEPRESCALER_256.

Note that the STM32 reference manual specifies a maximum SCLK — typically fPCLK/2 — so BR=000 is the maximum. You cannot run SPI faster than half the peripheral clock.

Which PCLK feeds your SPI peripheral?

This is the most common source of confusion on STM32:

STM32F4: SPI1, SPI4, SPI5, SPI6 run on APB2 (up to 84 MHz at 168 MHz system clock). SPI2 and SPI3 run on APB1 (up to 42 MHz). Check RCC_APB1ENR and RCC_APB2ENR to see which enable bits map to which peripherals.

STM32G0/G4: All SPI peripherals share a single APB clock domain (up to 64 MHz on G0, 170 MHz on G4). Consult your specific device’s clock tree in the datasheet.

STM32H7: The SPI peripherals can be clocked from different sources via the RCC_D2CCIP1R register — not just the APB clock. CubeMX handles this for you, but be aware the actual source may differ from the bus clock.

Use STM32CubeMX’s clock tree view to verify the actual peripheral clock frequency before running the calculator.

nRF52 SPIM: fixed frequencies, not a divider

The nRF52 SPIM peripheral does not have a programmable prescaler. Instead, the FREQUENCY register takes a fixed 32-bit constant from a small enumerated set:

NRF_SPIM0->FREQUENCY = 0x80000000UL;  /* 8 Mbps */
NRF_SPIM0->FREQUENCY = 0x40000000UL;  /* 4 Mbps */
NRF_SPIM0->FREQUENCY = 0x20000000UL;  /* 2 Mbps */
NRF_SPIM0->FREQUENCY = 0x10000000UL;  /* 1 Mbps */
/* etc. */

The constants are defined in nrf52840.h as SPIM_FREQUENCY_FREQUENCY_M8 through SPIM_FREQUENCY_FREQUENCY_K125. Any other value written to the register produces an undefined clock — do not treat these as continuous register values.

In Zephyr, set the frequency via the device tree: clock-frequency = <4000000>; in your SPI node, or pass it in the spi_config struct at runtime.

The maximum SPIM frequency on nRF52840 is 8 Mbps. The nRF5340 SPIM4 peripheral supports 32 Mbps, but that’s a different register map entirely.

Why you always pick SCLK ≤ fSCLK_max

SPI slaves specify a maximum clock frequency in their datasheet electrical characteristics, usually under conditions of VCC min, worst-case temperature, and maximum capacitive load. Exceeding this limit leads to setup/hold violations at the slave’s data input — you’ll see intermittent bit errors, often appearing only after the board warms up or at low supply voltage.

The “floor” mode in this calculator finds the fastest achievable SCLK that does not exceed your target. This is the safe default. Use “nearest” mode only if you’re characterising a device and want to know the closest achievable frequency in either direction.

Common scenario: a W25Q128 NOR flash is rated 104 MHz at VCC = 2.7–3.6 V. With STM32F4 APB2 at 84 MHz, the maximum achievable SPI clock is 42 MHz (÷2). That’s well within spec. On a ÷1 configuration — which the STM32 doesn’t even support — you’d need to verify the 84 MHz timing against the flash’s 104 MHz spec at your voltage.

Common mistakes

Wrong PCLK on STM32. Using the wrong APB clock is the most frequent error. A beginner configures PCLK2 = 84 MHz but routes their SPI2 through APB1 = 42 MHz, then wonders why CubeMX calculates a different baud rate than their manual calculation. Always verify via HAL_RCC_GetPCLK1Freq() / HAL_RCC_GetPCLK2Freq() at runtime if in doubt.

Ignoring capacitive load. At high SCLK frequencies, PCB trace length and connector capacitance matter. Above 10 MHz on anything but short traces, add series termination resistors (22–33 Ω) on SCLK, MOSI, and CS lines. A 10 MHz signal through 10 cm of FR4 trace and a 10 pF load at the slave pin can produce a rise time longer than the setup time requirement, especially with 3.3 V CMOS logic.

Forgetting CPOL/CPHA setup time. Changing SCLK frequency doesn’t change the CPOL/CPHA mode — but at higher speeds the setup time from CS assert to first clock edge (tCSS, typically 50–100 ns on flash devices) becomes harder to meet. Some devices require a delay between CS assert and the first clock edge. Software-controlled CS via GPIO is slower than hardware NSS, so at 20+ MHz, hardware NSS may be necessary.

nRF52: using the SPI (not SPIM) peripheral. The legacy SPI peripheral tops out at 8 Mbps and is deprecated. Use SPIM0–SPIM3. On nRF52840, SPIM3 supports high-speed mode up to 32 Mbps with a dedicated clock.

Common SPI device speed limits

DeviceMax SCLKNotes
W25Q128 NOR flash104 MHzFast Read mode; standard Read: 50 MHz
IS25LP064 NOR flash133 MHzFast Read; check Vcc derating at 1.8 V
BMI088 IMU10 MHzBoth accel and gyro interfaces
LSM6DSO IMU10 MHz4-wire SPI; 1 MHz in 3-wire mode
ADS131M08 ADC25 MHzFixed frame sync protocol
MAX31865 RTD5 MHzData valid on falling SCLK edges
nRF24L01+10 MHz4 MHz typical in practice for reliability
MCP2515 CAN ctrl10 MHz0.85 × fOSC; derate accordingly
SD card (SPI mode)25 MHzInitialise at ≤ 400 kHz, then ramp up

Frequently asked questions

Why can't I achieve exactly 25 MHz SPI on STM32F4? +

STM32F4 SPI1 uses PCLK2 (max 84 MHz) divided by powers of 2: 84/2=42, 84/4=21, 84/8=10.5 MHz. There's no prescaler that gives exactly 25 MHz. The closest without exceeding 25 MHz is 21 MHz (÷4). If you need exactly 25 MHz, you'd need to reconfigure PCLK2 to 50 or 100 MHz, which may not be possible without changing your PLL configuration.

How do I know which PCLK feeds my SPI peripheral on STM32? +

On STM32F4: SPI1, SPI4, SPI5, SPI6 are on APB2 (max 84 MHz). SPI2 and SPI3 are on APB1 (max 42 MHz). Check the clock tree in STM32CubeMX or the RCC section of your reference manual. The peripheral clock can be found in the RCC_APB1ENR / RCC_APB2ENR register descriptions.

What is the maximum SPI speed for common flash chips and ADCs? +

Common limits: W25Q128 NOR flash: 133 MHz (Fast Read, 0Bh), 50 MHz (standard read, 03h). ADS131M08 ADC: 25 MHz. BMI088 IMU: 10 MHz (gyro), 10 MHz (accel). nRF24L01+: 10 MHz. Most SPI devices specify fSCLK_max in their datasheet electrical characteristics table. Always check at your operating voltage — some devices derate at lower Vcc.

Newsletter

The embedded engineer's weekly cheat sheet

Register tricks, timing gotchas, and tool updates. One email per week. No fluff.

No spam. Unsubscribe anytime.