Open
Description
These are now supported in modern Arduino chipsets. here's an examples of how to create multiple I2C/SPI peripherals.
https://github.com/adafruit/ArduinoCore-samd/blob/master/variants/grand_central_m4/variant.h#L177
they are auto-generated here:
https://github.com/adafruit/ArduinoCore-samd/blob/master/libraries/SPI/SPI.cpp#L469
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
matthijskooijman commentedon Nov 8, 2019
I just had a look over the SPI code, and it seems you can already do this by manually creating a new SPI object:
Note that you pass the pin numbers and then the code figures out what SPI unit is attached to those pins (within the options defined by the variant, which is currently limited to 1 SPI unit for each pin).
What I like very much about this, is the flexibility: The sketch does not need any hardcoded knowledge of the SPI unit to pin mapping, it just needs to know what pins have been used to connect the slave (of course those pins should be a valid combination, of course).
There are still some things I do not completely like about this approach, though. It currently works like this:
See
Arduino_Core_STM32/cores/arduino/stm32/spi_com.c
Lines 144 to 165 in 1e2a598
Arduino_Core_STM32/variants/NUCLEO_F401RE/PeripheralPins.c
Lines 180 to 188 in 1e2a598
What I do not like here is:
Arduino_Core_STM32/variants/NUCLEO_F401RE/PeripheralPins.c
Lines 182 to 183 in 1e2a598
This obviously reduces flexibility for the sketch, which is a pity. This limitation is there, because the pin mapping code looks for a given pin number in a given pinmap and then always returns the first one.
Some ideas on improving this:
SPI1
,SPI2
, etc. like it happens for AVR and SAMD. There would then not be any explicit pin configuration, but that could just assume default pins (either using defines in the variant as happens for theSPI
instance now, or just use the first pin that matches the unit in the pinmaps, which might be more robust). One complication here is the the HAL already containsSPI1
,SPI2
defines for the instance pointers, but these are always just typecasts of e.g.SPI1_BASE
, so they could perhaps be undef'd.I suspect that most of the above applies to other peripherals equally (at least timers, probably also I2c and others).
fpistm commentedon Nov 8, 2019
@matthijskooijman
You're right and I've already think about this but not so easy and will require some more study.
As an example Mbed define PY_n_ALTx pin in the array to differentiate alternative pin capabilities but with Arduino pin number PYn upper layer this become hard to handle.
matthijskooijman commentedon Nov 8, 2019
Do you have a link for that?
One related improvement I just realized: Currently, every transaction calls
spi_init()
to configure the settings (e.g. clock speed). However, this also completely sets up the SPI device, including scanning the pin map for which SPI unit to use. I think this should be split: Select and set up the SPI device once, onbegin()
, and then only change the needed settings on each transaction.fpistm commentedon Nov 9, 2019
Here the
PeripheralPins.c
for a Nucleo F411RE:https://github.com/ARMmbed/mbed-os/blob/816689d1bbd5e82b64aa8af3cb294f6dac7130ec/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F411xE/TARGET_NUCLEO_F411RE/PeripheralPins.c#L120
fpistm commentedon Nov 9, 2019
For that I will comment in #257
Hoek67 commentedon Apr 2, 2020
Check out https://stm32f4-discovery.net/api/group___t_m___d_e_l_a_y.html 3rd party library.
I use it for I2C and SPI as it supports multiple pins and in the case of SPI... DMA. I use a STM32F407VET6 which is supported. I altered a few functions so allow me to transfer SPI with DMA and do other stuff while waiting for the DMA to finish.
uzi18 commentedon Apr 2, 2020
@Hoek67 https://github.com/MaJerle/stm32f429/blob/master/00-STM32F429_LIBRARIES/tm_stm32f4_delay.c
fpistm commentedon Dec 15, 2020
Currently, I'm working (almost finished) on variant rework and make all pins from the pinmap array available. This will remove this restriction pointed by @matthijskooijman
I'm wondering if any official documentation on the
*_INTERFACE_COUNT
exists?About:
Unfortunately, I don't want undef it is really too risky as at sketch level user can use HAL/LL and could need to use SPIx peripheral define in the CMSIS so the new instance from SPIClass will probably be SPI_x.
xmenxwk commentedon Sep 29, 2021
MISO seems not needed, what if you only need MOSI and SCLK, like for display, where you just write and not need any data back from slave. With the above condition, we must define the MISO as well.
asukiaaa commentedon Oct 2, 2023
I could use second spi by referencing alt pin for stm32h753zi.
or
PB3,4,5 are assignable for SPI1,3,6.
Arduino_Core_STM32/variants/STM32H7xx/H742A(G-I)I_H743A(G-I)I_H753AII/PeripheralPins.c
Lines 335 to 341 in 96d8c93
If I use non alt pin, the spi bus is merged to default spi (SPI1).
Be careful to use alt pin if you want to other spi bus.