Skip to content
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

Custom SPI pins #121

Closed
aguglie opened this issue Feb 29, 2020 · 8 comments
Closed

Custom SPI pins #121

aguglie opened this issue Feb 29, 2020 · 8 comments
Labels
bug Something isn't working resolved Issue was resolved (e.g. bug fixed, or feature implemented)

Comments

@aguglie
Copy link
Contributor

aguglie commented Feb 29, 2020

Hi Jan,

I was testing out RadioLib on a Adafruit HUZZAH32 – ESP32 Feather and spot a strange issue: the labeled SPI pins are (MOSI: 18, MISO: 19, SCK: 5) which differ from the default ones (MOSI: 23, MISO: 19, SCK: 18).

Unfortunately, all the feather boards/radios agree on this pin mapping.

Meaning that i had to manually patch Module.cpp, specifying the pinout as: _spi->begin(5, 19, 18, SS);.

Have you already faced this sort of issue?
I was wondering about adding some parameters to Module's constructor, but I'm not sure about compatibility with other boards: seems that ESP822's SPI::begin is not accepting custom mapping.

Let me know 😄

@jgromes
Copy link
Owner

jgromes commented Feb 29, 2020

There's already a Module constructor that allows you to change the SPI instance:

RadioLib/src/Module.h

Lines 38 to 51 in b7c21ff

/*!
\brief SPI-based module constructor.
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as interrupt/GPIO.
\param rst Arduino pin to be used as hardware reset for the module.
\param spi SPI interface to be used. Defaults to Arduino hardware SPI interface, can also use software SPI implementations.
\param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0.
*/
Module(int16_t cs, int16_t irq, int16_t rst, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));

You should be able to create a new SPI class with the correct pins or change the pins of the default SPI prior to calling Module constructor.

@aguglie
Copy link
Contributor Author

aguglie commented Feb 29, 2020

Hi Jan, thanks for you fast reply.

There's no way to change the pins pior calling Module constructor, seems that the only way to change them is via begin(sck, miso, mosi, ss).

I'll extend SPI and wrap begin()to make it call begin(mySck, myMiso, ....).

Thanks.

P.S: Feel free to re-open if you spot a smarter way to do it!

@aguglie aguglie closed this as completed Feb 29, 2020
@jgromes
Copy link
Owner

jgromes commented Feb 29, 2020

There's no way to change the pins pior calling Module constructor

Not with the default examples, but there's no reason you can't do something like this (untested with HW but does compile):

#include <RadioLib.h>

SPIClass newSPI(HSPI);

Module* mod;
SX1268* radio;

void setup() {
  newSPI.begin(5, 19, 18, SS);
  mod = new Module(10, 2, 3, 9, newSPI);
  radio = new SX1268(mod);
}

See also https://github.com/espressif/arduino-esp32/blob/master/libraries/SPI/examples/SPI_Multiple_Buses/SPI_Multiple_Buses.ino

@jgromes
Copy link
Owner

jgromes commented Feb 29, 2020

Looking over Module.cpp however, I came across this:

_spi->begin();

It's not entirely clear whether that will re-initialize SPI on the default pins, this will have to be tested (and preferrably only initialize SPI if the user hasn't done so).

@jgromes jgromes reopened this Feb 29, 2020
@aguglie
Copy link
Contributor Author

aguglie commented Feb 29, 2020

That double call to begin was the reason why I was proposing the wrapper 😄

I’ll anyway test your solution and check what happens,
I think it’ll be disruptive (at least in ESP32) since second begin will overwrite the pinout matrix.

I’ll let you know soon.

Thank you

@jgromes
Copy link
Owner

jgromes commented Feb 29, 2020

I think it should be enough to discard the default SPI arguments in Module constructors and add new constructors that don't have SPIClass and SPISettings args. That way, when user wants to use the default SPI, they can just call the constructor with no SPI arguments.

@jgromes
Copy link
Owner

jgromes commented Mar 2, 2020

@Guglio95 Module::init() will now initialize the SPI interface only if it the default interface is used. Tested on Uno with default SPI and STM32 with non-default SPI and seems to be working, feel free to reopen if there are issues on ESP32.

@jgromes jgromes closed this as completed Mar 2, 2020
@jgromes jgromes added the bug Something isn't working label Mar 2, 2020
@aguglie
Copy link
Contributor Author

aguglie commented Mar 2, 2020

Hi Jan,

Thanks, I was about to text you,
This will for sure sort out the problem.

I found an easier way for my specific case, I discovered that when using the right platformio's board definition the default pins (miso/mosi/...) are changed according to the labels printed on the board.
But, in any case, with your change I'm able to customize the used pin as I wish.

Thanks 😄

@jgromes jgromes added the resolved Issue was resolved (e.g. bug fixed, or feature implemented) label Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working resolved Issue was resolved (e.g. bug fixed, or feature implemented)
Projects
None yet
Development

No branches or pull requests

2 participants