Unverified Commit b5a905b6 authored by Raphael Krauthann's avatar Raphael Krauthann Committed by GitHub

Reworked the utility/rp2 SPI class and removed static members (#892)

* Removed 'static' keywords in SPI.h such that multiple SPI busses can be used to connect multiple Radios. Also added an example for this use-case
Co-authored-by: default avatarBrendan <2bndy5@gmail.com>
parent e490a5a2
...@@ -174,3 +174,56 @@ To specify the default SPI pins used at build time, you can use either: ...@@ -174,3 +174,56 @@ To specify the default SPI pins used at build time, you can use either:
```shell ```shell
cmake --build . --config Release -DPICO_DEFAULT_SPI=0 -DPICO_DEFAULT_SPI_SCK_PIN=2 -DPICO_DEFAULT_SPI_TX_PIN=3 -DPICO_DEFAULT_SPI_RX_PIN=4 cmake --build . --config Release -DPICO_DEFAULT_SPI=0 -DPICO_DEFAULT_SPI_SCK_PIN=2 -DPICO_DEFAULT_SPI_TX_PIN=3 -DPICO_DEFAULT_SPI_RX_PIN=4
``` ```
## Using Multiple Radios
It is possible to drive multiple nRF24L01 transceivers on a single board. To do this each radio needs dedicated digital output pins for the CE and CSN pins.
If you want to drive each radio with a separate SPI bus, then the following example will demonstrate how to do that.
```cpp
#include <RF24.h>
// Declare the pin numbers connected to the radios' CE and CSN pins (respectively)
RF24 radio0(8, 5); // first radio object
RF24 radio1(14, 13); // second radio object
// By default, one SPI bus instance is created by the RF24 lib. We'll use this
// default instance of the `spi0` interface for our first radio, but we want a
// different SPI bus for the second radio.
//
// So, here we declare a second SPI bus instance:
SPI my_spi; // we specify the `spi1` bus interface below
bool setupRadios()
{
// Initialize the first radio using the default SPI instance
if (!radio0.begin()) {
printf("Radio0 hardware is not responding!\n");
return false;
}
// first radio object initialized successfully
// specify the the second SPI bus interface and corresponding GPIO pins
my_spi.begin(spi1, 10, 11, 12); // spi1 bus, SCK, TX, RX
if (!radio1.begin(&my_spi)) {
printf("Radio1 hardware is not responding!\n");
return false;
}
// second radio object initialized successfully
return true;
}
int main()
{
stdio_init_all(); // init necessary IO for the RP2040
while (!setupRadios()) { // if either radioX.begin() failed
sleep_ms(1000); // add 1 second delay for console readability
// hold program in infinite attempts to initialize the radios
}
// continue with program as normal ...
}
```
#include "spi.h" #include "spi.h"
spi_inst_t* SPI::_hw_id;
SPI::SPI() SPI::SPI()
{ {
} }
......
...@@ -33,7 +33,7 @@ public: ...@@ -33,7 +33,7 @@ public:
* @see begin(spi_inst_t, uint8_t, uint8_t, uint8_t) for using other pins as * @see begin(spi_inst_t, uint8_t, uint8_t, uint8_t) for using other pins as
* your SPI bus. * your SPI bus.
*/ */
static void begin(spi_inst_t* hw_id); void begin(spi_inst_t* hw_id);
/** /**
* Start SPI * Start SPI
...@@ -47,24 +47,24 @@ public: ...@@ -47,24 +47,24 @@ public:
* @see The [Pico SDK has a chart of applicable pins](https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-c-sdk.pdf#%5B%7B%22num%22%3A106%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C115%2C377.118%2Cnull%5D) * @see The [Pico SDK has a chart of applicable pins](https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-c-sdk.pdf#%5B%7B%22num%22%3A106%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C115%2C377.118%2Cnull%5D)
* that can be used for hardware driven SPI transactions. * that can be used for hardware driven SPI transactions.
*/ */
static void begin(spi_inst_t* hw_id, uint8_t _sck, uint8_t _tx, uint8_t _rx); void begin(spi_inst_t* hw_id, uint8_t _sck, uint8_t _tx, uint8_t _rx);
static uint8_t transfer(uint8_t tx_); uint8_t transfer(uint8_t tx_);
static void transfernb(const uint8_t* tbuf, uint8_t* rbuf, uint32_t len); void transfernb(const uint8_t* tbuf, uint8_t* rbuf, uint32_t len);
static void transfern(const uint8_t* buf, uint32_t len); void transfern(const uint8_t* buf, uint32_t len);
static void beginTransaction(uint32_t _spi_speed); void beginTransaction(uint32_t _spi_speed);
/** deinit the SPI bus (using hw_id passed to begin()) */ /** deinit the SPI bus (using hw_id passed to begin()) */
static void endTransaction(); void endTransaction();
virtual ~SPI(); virtual ~SPI();
private: private:
/** the ID of the hardware driven SPI bus */ /** the ID of the hardware driven SPI bus */
static spi_inst_t* _hw_id; spi_inst_t* _hw_id;
}; };
#endif // RF24_UTILITY_RP2_SPI_H_ #endif // RF24_UTILITY_RP2_SPI_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment