-
Notifications
You must be signed in to change notification settings - Fork 981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for multiple I2C or SPI peripherals #705
Comments
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:
Some ideas on improving this:
I suspect that most of the above applies to other peripherals equally (at least timers, probably also I2c and others). |
@matthijskooijman |
Do you have a link for that? One related improvement I just realized: Currently, every transaction calls |
Here the |
For that I will comment in #257 |
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. |
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 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. |
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. |
I could use second spi by referencing alt pin for stm32h753zi. #define PIN_SPI_B_SCK PB_3_ALT1
#define PIN_SPI_B_MISO PB_4_ALT1
#define PIN_SPI_B_MOSI PB_5_ALT1
#include <SPI.h>
SPIClass SPI_B(pinNametoDigitalPin(PIN_SPI_B_MOSI),
pinNametoDigitalPin(PIN_SPI_B_MISO),
pinNametoDigitalPin(PIN_SPI_B_SCK));
// SPI3 bus was enabled or #define PIN_SPI_B_SCK PB_3_ALT1
#define PIN_SPI_B_MISO PB_4_ALT1
#define PIN_SPI_B_MOSI PB_5_ALT1
#include <SPI.h>
SPIClass SPI_B;
void begin() {
SPI_B.setMISO(PIN_SPI_B_MISO);
SPI_B.setMOSI(PIN_SPI_B_MOSI);
SPI_B.setSCLK(PIN_SPI_B_SCK);
// SPI3 bus was enabled
SPI_B.begin();
} 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). #define PIN_SPI_B_SCK PB3
#define PIN_SPI_B_MISO PB4
#define PIN_SPI_B_MOSI PB5
SPIClass SPI_B(PIN_SPI_B_MOSI, PIN_SPI_B_MISO, PIN_SPI_B_SCK);
// SPI1 bus was enabled
// Not SPI3 orSPI5 Be careful to use alt pin if you want to other spi bus. |
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
The text was updated successfully, but these errors were encountered: