How to use CPX with SPI modules like RadioHead+LoRa?

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
raldi
 
Posts: 25
Joined: Fri Mar 03, 2017 3:22 am

How to use CPX with SPI modules like RadioHead+LoRa?

Post by raldi »

I've been working with the Feather M0 LoRa product for some time, and am now looking to get the standalone (SPI-based) LoRa module working with the CPX. Specifically, I'm trying to get these two products working together:

https://www.adafruit.com/product/3333
https://www.adafruit.com/product/3073

The problem I'm having is that the LoRa module requires six connections besides power and ground: SCLK, MISO, MOSI, INT, RST, and CS, and I'm not sure how to hook them all up. Three of them appear to be at my discretion; the RadioHead constructor takes as parameters the pins for CS and INT, and it's up to me to handle RST, so I used A4, A5, and A6 for these, as seen below:

Code: Select all

#define RFM95_INT_PIN A4
#define RFM95_RST_PIN A5
#define RFM95_CS_PIN A6

RH_RF95 rf95(RFM95_CS_PIN, RFM95_INT_PIN);

void setup() {
  CircuitPlayground.begin(255);

  pinMode(RFM95_RST_PIN, OUTPUT);
  digitalWrite(RFM95_RST_PIN, LOW);
  delay(10);
  digitalWrite(RFM95_RST_PIN, HIGH);
  delay(10);

  if (!rf95.init()) panic();
}


The problem is, SCLK, MISO, and MOSI don't appear to be up to me. By adding some debugging, it appears that code outside my control sets SCLK=13, MISO=30, and MOSI=32, and these values don't seem to correspond to any of the available pads. (A1=15, A2=16, and A3=17). I've seen other CPX+SPI advice in this forums suggesting using A1 for SCLK, A2 for MISO, and A3 for MOSI, but when I try that, the call to rf95.init() fails, and appears to write "FF" to the serial port.

What's the right way to make these two devices work together? Should I be wiring them up differently, or making changes to the code so that it'll use A1-A3 for SPI?

User avatar
dastels
 
Posts: 15608
Joined: Tue Oct 20, 2015 3:22 pm

Re: How to use CPX with SPI modules like RadioHead+LoRa?

Post by dastels »

Related: viewtopic.php?f=58&t=162345. I connected an airlift breakout to a CPX. It might be a place to start.

Dave

User avatar
adafruit2
 
Posts: 22111
Joined: Fri Mar 11, 2005 7:36 pm

Re: How to use CPX with SPI modules like RadioHead+LoRa?

Post by adafruit2 »

the TFT/e-ink gizmos use the hardware SPI port, you could follow the schematic
https://learn.adafruit.com/adafruit-tft-gizmo/downloads
and then use SPI1 for the interface
https://github.com/adafruit/Adafruit-ST ... _gizmo.ino

User avatar
raldi
 
Posts: 25
Joined: Fri Mar 03, 2017 3:22 am

Re: How to use CPX with SPI modules like RadioHead+LoRa?

Post by raldi »

dastels wrote:Related: viewtopic.php?f=58&t=162345. I connected an airlift breakout to a CPX. It might be a place to start.
Dave
Thanks, but that's actually where I did start. :) The link seems to say I should connect:
A1 -> SCK
A2 -> MISO
A3 -> MOSI
So that's how I wired things up (see above) but it didn't work. Is there some code I need to add to switch SPI to use these pins instead of the defaults? I see there's a snippet of CircuitPython, but this is a C project.

Thanks again

User avatar
raldi
 
Posts: 25
Joined: Fri Mar 03, 2017 3:22 am

Re: How to use CPX with SPI modules like RadioHead+LoRa?

Post by raldi »

adafruit2 wrote:tuse SPI1 for the interface
https://github.com/adafruit/Adafruit-ST ... _gizmo.ino
Ah! I hadn't realized the constructor had a third optional parameter:

Code: Select all

RH_RF95(uint8_t slaveSelectPin = SS, uint8_t interruptPin = 2, RHGenericSPI& spi = hardware_spi);
Okay, so it sounds like I need to pass SPI1 as this third parameter? Something like this?

Code: Select all

#include <spi.h>
//...
RH_RF95 rf95(RFM95_CS_PIN, RFM95_INT_PIN, SPI1);
}
And then, based on your other link, it looks like I can then wire CS=A1, MOSI=A2, and SCK=A3, but for the LoRa module, I also need to hook up MISO, and I'm not sure what pin that needs to go to. Is SPI1 tied to specific pins? If so, how can I look up what the pins are?

Thanks

User avatar
raldi
 
Posts: 25
Joined: Fri Mar 03, 2017 3:22 am

Re: How to use CPX with SPI modules like RadioHead+LoRa?

Post by raldi »

I found my way to https://learn.adafruit.com/using-atsamd ... s?view=all .. do I need to include wiring_private.h and make some calls to pinPeripheral() to make this work?

User avatar
raldi
 
Posts: 25
Joined: Fri Mar 03, 2017 3:22 am

Re: How to use CPX with SPI modules like RadioHead+LoRa?

Post by raldi »

Well, here's where I'm up to now:

Code: Select all

#define RFM95_SCLK A1
#define RFM95_MISO A2
#define RFM95_MOSI A3
#define RFM95_INT_PIN A4
#define RFM95_RST_PIN A5
#define RFM95_CS_PIN A6

RHSoftwareSPI my_spi;
RH_RF95 rf95(RFM95_CS_PIN, RFM95_INT_PIN, my_spi);

void setup() {
  my_spi.setPins(RFM95_MISO, RFM95_MOSI, RFM95_SCLK);
  Serial.begin(9600);
  while(!Serial);
  CircuitPlayground.begin(255);

  pinMode(RFM95_RST_PIN, OUTPUT);
  // Reset radio
  digitalWrite(RFM95_RST_PIN, LOW);
  delay(10);
  digitalWrite(RFM95_RST_PIN, HIGH);
  delay(10);

  if (!rf95.init()) panic();
}
And, indeed, panic() gets called. I also tried changing A1 to 6, A2 to 9, and A3 to 10 based on:
PA05 SERCOM0.1 D6 A1
PA06 SERCOM0.2 D9 A2
PA07 SERCOM0.3 D10 A3
from https://github.com/adafruit/ArduinoCore ... pp#L38-L44
but same result.

Do I need to make something like this? If so, did I get the parameters right, and how do I stick this square peg into the RadioHead library's round hole?

Code: Select all

SPIClass my_spi_class (&sercom0, RFM95_MISO, RFM95_SCLK, RFM95_MOSI, SPI_PAD_2_SCK_3, SERCOM_RX_PAD_1);

User avatar
raldi
 
Posts: 25
Joined: Fri Mar 03, 2017 3:22 am

Re: How to use CPX with SPI modules like RadioHead+LoRa?

Post by raldi »

Got this working! The two big pitfalls are: (1) The pads all have analog and digital pins, but this code only works if you refer to them by their digital names, and (2) calling CircuitPlayground.begin() tramples any pin configuration you've already done, so you need to do that *afterwards*.

Here's my working code:

Code: Select all

#define RFM95_SCLK 6
#define RFM95_MISO 9
#define RFM95_MOSI 10
#define RFM95_INT_PIN 3
#define RFM95_RST_PIN 2
#define RFM95_CS_PIN 0

RHSoftwareSPI my_spi;
RH_RF95 rf95(RFM95_CS_PIN, RFM95_INT_PIN, my_spi);

void setup() 
  CircuitPlayground.begin(255);
  Serial.begin(9600);
  while (!Serial);
  my_spi.setPins(RFM95_MISO, RFM95_MOSI, RFM95_SCLK);
  if (!rf95.init())
    Serial.println("init failed");

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”