SPI speed on NUCLEO-F103RB

Hello, I tried to test SPI interface on NUCLEO-F103RB with mbed OS.
And I faced strange thing that something like the CLK frequency of the SCLK pin was completely different from what I specify in the code, 100kHz.
I confirmed that the SCLK frequency from the pin was 281kHz when the code was run on NUCLEO-F103RB.

I just specify the SCLK frequency other than 100kHz, like 1kHz, 1MHz and also default. but the frequency on the SCLK pin was not changed. that was stuck on 281kHz.

Does any one have ideas to solve this problem?

Thanks,

— code —

SPI device_1(D4, D5, D3);
DigitalOut device_1_ss(PB_6);
int main() {    
    device_1_ss = 0;
    device_1.frequency(100000);
    device_1.format(16,1);

    device_1_ss = 1;
    device_1.write(0x1111);
    device_1_ss = 0;
}

Dear Masa,

Did you solve this issue already?
But to verify; Please note that this device and board has 2 SPI bus connections.
For the SPI driver you specify one bus and for the digital out (chip select) a pin next the the second bus.
I wonder if you are looking at the wrong bus by accident.

Also, D3/PB_3 (SCLK) is a shared pin with PWM2. Your code sample does not show it, but please make sure you’re not enabling PWM2.

Please refer to:

and

Thanks,
sirjyge

Hi,

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, 256. And it depends also on the frequency that clocks the SPI peripheral.

In practice, on the NUCLEO_F103RB, the SPI1 clock is equal to 72 MHz. So you can only have these output frequencies: 36 MHz (72 MHz / 2), 18 MHz (72 MHz / 4), 9 MHz, 4.5 MHz, 2.25 MHz, 1.125 MHz, 562 kHz and 281 kHz (72 MHz / 256).

If you want to have 1 MHz, you need to write a value above 1.125 MHz. For example:
device_1.frequency(1200000); // real value = 1.125 MHz

I have checked it and it works correctly for me.

Hope it helps

Hello Jyri,
Thank you for your kind help.
Yes, I understand the situation of this problem, and the lower frequency of the SPI SCLK was limited by the number of digits of prescaler.

Best,
Masa

Hello bcostm
Thank you for the detailed description on the SPI prescaler.
I will change the design of my gadget to use SPI with more higher frequency than previously thought, 100kHz.

Thank you so much!

Masa

No problem.

I have created a WIKI page for future reference:
https://os.mbed.com/teams/ST/wiki/SPI-output-clock-frequency