8 years, 10 months ago.

SPI pins for the F091

I am checking the Pins distribution for the F091 Nucleo and I have a couple of questions:

1) How many SPI does this nucleo have? I understand TWO(2).

2) I have worked with SPI2 with no problem, but with SPI1, why does it have two set of pins?? for example SCK is PA_5 and PB_3??

3) Even more important... which pin is SPI1 CS??? In the text I read it is PB_6 but in the figure I can see that SPI1 NSS is PA_4... so which is it??

Thank you very much for the help

Question relating to:

Affordable and flexible platform to ease prototyping using a STM32F091RCT6 microcontroller.

1 Answer

8 years, 10 months ago.

Hello,
You are right, there are 2 SPI on the STM32F091 mounted on this nucleo board.

For flexibility reasons, the STM32 hardware peripherals functions can usualy be directed to more than one pin.
For both SPI interfaces, each function can be directed to 2 different GPIO pins.
When initializing the SPI, just choose the pins which are the most convenient for you (or that are free from other functions).

SPI1
NSS: PA4 or PA15
SCK: PA5 or PB3
MOSI: PA6 or PB4
MISO: PA7 or PB5

SPI2
NSS: PB9 or PB12
SCK: PB10 or PB13
MOSI: PC2 or PB14
MISO: PC3 or PB15

CS pin is called NSS on STM32 for: active low(N) Slave Select.
PB6 does not have any SPI function, you are probably mistaking with I2C1_SCL

Thank you for your reply.

When initializing the SPI, just choose the pins which are the most convenient for you (or that are free from other functions).

How do I do that? (choose pins) Just by putting them in SPI()??

And also I am surprised at the double distribution for SPI2... it does not indicate that in the pin map

Also, I read about PB_6 in the homepage this question is posted in.(https://developer.mbed.org/platforms/ST-Nucleo-F091RC/) (read the blue box called "Information")

posted by Cristian Fuentes 26 Jan 2016

Like this for example.
Note that I did not try it on the board, I only compiled.
This is based on the original mbed SPI example

MBED SPI examples adapted to the NUCLEO-F091RC board

#include "mbed.h"
 
SPI spi(PB_4, PB_5, PB_3); // mosi, miso, sclk
DigitalOut cs(PA_15);
 
int main() {
    // Chip must be deselected
    cs = 1;

    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    spi.format(8,3);
    spi.frequency(1000000);
 
    // Select the device by seting chip select low
    cs = 0;
 
    // Send 0x8f, the command to read the WHOAMI register
    spi.write(0x8F);
 
    // Send a dummy byte to receive the contents of the WHOAMI register
    int whoami = spi.write(0x00);
    printf("WHOAMI register = 0x%X\n", whoami);
 
    // Deselect the device
    cs = 1;
}

Regarding the CS pin, it seems like mbed does not use the hardware NSS function. Any GPIO can be used to drive the CS pin. This allows to drive several SPI devices by using several GPIOs, one for each device.

posted by Maxime TEISSIER 26 Jan 2016

Any pin can be used as CS when you are SPI Master. The pin must be set and reset explicitly by your software like in the example above. However, when you use the mbed SPISlave class the CS, which is now an input pin, must be assigned to the correct NSS pin for the SPI hardware engine that you have selected.

posted by Wim Huiskamp 26 Jan 2016

Thank you!

posted by Cristian Fuentes 27 Jan 2016