SPI, I²C and UART inputs

Hello guys,
Some boards have multiple SPI, I²C, and UART ports, and sometimes, we need to chose one to work with, but how in the Arduino IDE !? More precisely I'm talking about the STM32 boards, wich happen to have 3 I²C ports, 3 SPI ports, and 6 UART ports. How to chose wich of these ports to use in the Arduino IDE !?

The device that to which you are interfacing will often dictate which you must use. A display, for instance, may have only an SPI or an I2C interface.

Then there are the pros and cons of each that may make it more suitable. I2C and SPI are not for long distance. They are good for less than a meter unless special care is taken. UART is good for several meters. SPI requires a chip select pin for each device on its bus where I2C does not. SPI can be much faster than I2C. UART is a one to one interface, I2C and SPI are buses that can have multiple devices on each bus.

What are the devices that you wish to interface? How far apart are the devices? How much data to exchange? How fast?

On a Mega there exist Serial, Serial1 ... Serial4 UART ports. Perhaps your ports are similarly numbered.

The connection to the IDE is fixed by hardware and bootloader of your board (USB, BT...).

I know all this. But my question is for example, I want to use an NRF24L01 wich has an SPI connection with an STM32, I want to chose wich of the 3 SPI ports on the STM32 I want to use

it usually depends on the development board or PCB one is using e.g. how many SPI, I2C, Uart, Canbus, etc interfaces are brought out which one can connect too ???
on a large PCB it may be all interfaces, on a small PCB only one or two due to problems with tracking
some microcontrollers have "programable pin" facilities where one can map the required devices to particular pins
one generally starts with a requirement specification for the project, e.g. what IO devices are required? then looking at the specification of development boards, pinouts, etc - see what will meet the requirements allowing for some expansion - end users once they see a prototype add extra requirements

Choose a port that is not operating anything else. For example, the ESP32 had 4 Hardware SPI ports, but 2 of them communicate with the internal flash memory, so those 2 should not be used for anything else.

UART ports are similar. The first port, RX0 and TX0, is usually the one that is used for programming and debugging, so often people recommend to use a different hardware UART, if you have it available.

Even knowing this however, I recently had a lot of headache trying to get the second available SPI port on the ESP32 (HSPI) to function properly, but the other one (VSPI) worked fine, so it's hard to fully answer your question.

I think you @GigaNerdTheReckoning @groundFungus @DrDiettrich @horace did not understand my question, so I'll ask it differently
For example, I have a board that has 2 SPI ports, on the pins 1,2 and 3, and it's labeled in the pinout as SPI1, and it's the default SPI port, and the other on the pins 7,8 and 9 labeled in the pinout as SPI2. Now, we pretend that for some reason I need to use the pins 1,2 and 3 for other things, and that I still need for example an NRF24L01 connected to the pins 7,8 and 9. If I write a code normally in the ARDUINO IDE, the SPI port used will be automatically the SPI1 (the default SPI) that I connected it to other components, then how do I choose the SPI2 to be used !?

Also depends upon the library being used as to which port will be used.

Some libraries allow the hardware/software port objects to be setup and then the object is passed to the library for the libraries use. Other libraries use predefined ports and it is up to the user to connect the hardware to those ports.

And, of course using the ports API to access the connected device is an option, bypassing the library. In this instance the choice to which goes where is up you.

Then use this port for SPI and use the SPI2 pins for other purposes.

Ok but for example the SPI1 pins are the only ADC or PWM pins left, then I must use the SPI2, but how !?

What STM32 board are you using?

I have an STM32F103C8T6 and two STM32F407VET6.

Have you consulted the documentation for the core that is installed in the IDE for that processor?

Is this the core? >> API · stm32duino/wiki Wiki · GitHub

SPI documentation for that core. >> API · stm32duino/wiki Wiki · GitHub

Oh yeah, thank you so much, in the documentation I found how to choose the port

If your question was answered to your satisfaction, please mark the thread as solved so that other members do not waste their time opening the thread wanting to help only to find that the problem has been solved.

I did so, thank you