SPI on nucleo_f103rb does not work on any pin

Hello.

I am a newbie playing with the nucleo f103rb.

I am trying to use SPI.

It appears that is SPI only willing to work with dedicated pins.

But with any other other pins it won’t work, the boards gets stuck somewhere in the initialization, never reaching main(), and green LED blinking with some weird pattern (like 4 long blinks followed by 4 short blinks).

Here is the code that example.

#include “mbed.h”

SPI device(PB_5, NC, PB_3); // THIS WORKS PERFECTLY
// SPI device(PA_4, NC, PA_1); // THIS WILL NOT WORK - and I tried many GPIO combinations here - only dedicated SPI pins (hardware SPI pins) work

int main() {
int i = 0;

while(1) {
   device.write(0x55);
   device.write(i++);
   device.write(0xE0);
   wait_us(50);

}
}

I thought SPI should work with any bits. Am I wrong?

Please help !!!

Thanks!

Look on your board’s platform page:

https://os.mbed.com/platforms/ST-Nucleo-F103RB/

Scroll down a little and you will see the pin connections with the MCU functions that are available on each pin.
You will see that on the Arduino Connectors SPI is available on pins:

PA_5 and PB_3 are SCLK
PA_6 and PB_4 are MISO
PA_7 and PB_5 are MOSI

You can use any of these pin combinations on the Arduino connectors because it only support one of the SPI channels ‘SPI1’ here.

‘SPI2’ is available on the ‘MORPHO’ if you want to connect another SPI device.

This target will not allow pin multiplexing unlike some other targets that allow almost any function to be available on any pin.

Also remember that some of the MCU pins share board resources, for instance PA_5 is also connected to LED1, so if you use the SPI(sclk) function on this pin you can not use LED1 on you board and visa versa. As you mention it will flash in one way or another.

Paul
I’ve seen the picture with the pin assignment.
However my understanding was that one should follow it only for the hardware SPI pin assignment, while for software SPI (aka bit banging) i am free to choose any available pin combination (of course except the ones like PA_5 as it is occupied by the LED). Am I wrong?
Thanks
Dmitry

The SPI api will only work on the pins as detailed on the platform as I mentioned.

If you want to do your own SPI implementation you would need to set your pins to something like this:

DigitalOut CS(D8);      // chip (device) select pin
DigitalOut RS(D9);      // chip (device) reset pin
DigitalOut MOSI(D10);   // mosi(sometimes called SDA) data out to device pin
DigitalIn  MISO(D11);   // miso date from device pin
DigitalOut SCK(D12);    // clock

These can be set to any pin combination.

The problem with this is that almost all your MCU resorces will be tied up with your SPI function, you have no buffer so can not do anything else until you have finished your transmit or receive sequence.
I would not recommend it unless there’s a specific reason.

You can find all the F103RB MCU built in peripheral function pins here:

https://github.com/ARMmbed/mbed-os/tree/master/targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB

I have found this that will give you an example for software SPI: