Code that works on 3.2 not working on LC

Status
Not open for further replies.

MrGlasspoole

Well-known member
Today i got a LC and 3.5 and my 3.2 code does not run on the LC:
Code:
/**************************************************************
* Connection                                                  *
***************************************************************
0  <> DIO0
10 <> NSS
11 <> MOSI
12 <> MISO
14 <> SCK
**************************************************************/

#include <RFM69.h>    //get it here: https://www.github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h>//get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h>      //comes with Arduino IDE (www.arduino.cc)

/**************************************************************
* RADIO SETUP                                                 *
**************************************************************/
#define NODEID        01    //unique for each node on same network
#define NETWORKID     204  //the same on all nodes that talk to each other
#define FREQUENCY     RF69_868MHZ
#define ENCRYPTKEY    "7*V9uQ)M<5epl7f#" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ENABLE_ATC    //comment out this line to disable AUTO TRANSMISSION CONTROL

#define ACK_TIME      20 // Number of milliseconds to wait for an ack
#define RETRY_PERIOD  5  // How soon to retry (in seconds) if ACK didn't come in
#define RETRY_LIMIT   5  // Maximum number of times to retry

/**************************************************************
* 32 bit Teensy                                               *
**************************************************************/
#define LED           13 // teensy
#define CLOCKPIN      14 // teensy alternative SCK since first SCK is the LED
#define T3_IRQ_PIN    0  // pin for irq on teensy
#define T3_IRQ_NUM    0  // number for IRQ on teensy
#define T3_SPI_CS     10 // pin for SPI CS on teensy

#define POINTS 6 // number of values to send at each transmission

/**************************************************************
* MISC SETUP                                                  *
**************************************************************/
#define SERIAL_BAUD   115200
#define DEBUG         1 // Set to 1 for serial port output

/**************************************************************
* DATA STRUCTURE                                              *
**************************************************************/
typedef struct __attribute__((__packed__)) {
  uint16_t nodeId;          // Store this nodeId
  float temp;              // Temperature reading
  uint16_t vcc;             // Battery voltage
  byte tx;                 // Transmit level
  float lx;                // Light level
  uint16_t values[POINTS];
} Payload;
Payload theData;

#ifdef IS_RFM69HW
  bool isRFM69HW = true;
#else
  bool isRFM69HW = false;
#endif

uint8_t irqPin = T3_IRQ_PIN;
uint8_t irqNum = T3_IRQ_NUM;
uint8_t csPin = T3_SPI_CS;

#ifdef ENABLE_ATC
  RFM69_ATC radio(csPin, irqPin, isRFM69HW, irqNum);
#else
  RFM69 radio(csPin, irqPin, isRFM69HW, irqNum);
#endif

bool promiscuousMode = false;

void setup() {
  Serial.begin(SERIAL_BAUD);
  while (!Serial) {;} // Wait for serial port to connect
  SPI.setSCK(CLOCKPIN); // alternative SCK pin
  pinMode(LED, OUTPUT);
  pinMode(irqPin, INPUT); // enable INPUT mode on IRQ pin

  radio.initialize(FREQUENCY, NODEID, NETWORKID);

  #ifdef IS_RFM69HW
    radio.setHighPower(); //only for RFM69HW!
  #endif
  radio.encrypt(ENCRYPTKEY);
  radio.promiscuous(promiscuousMode);
  char buff[50];
  sprintf(buff, "\nThe frequency is %d Mhz, Kenneth!", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);

  #ifdef ENABLE_ATC
    Serial.println("RFM69_ATC Enabled (Auto Transmission Control)");
  #endif
}

void loop() {
  if (radio.receiveDone()) {
    if (radio.DATALEN != sizeof(Payload)) {
      Serial.print("Invalid Payload received!");
    } else {
      theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else
      if (DEBUG) {
        Serial.print("Node:");
        Serial.print(theData.nodeId);
        Serial.print(", Temp:");
        Serial.print(theData.temp);
        Serial.print(", Lux:");
        Serial.print(theData.lx);
        Serial.print(", Vcc:");
        Serial.print(theData.vcc);
        Serial.print(", TX Level:");
        Serial.print(theData.tx);
        Serial.print(", RSSI:");
        Serial.print(radio.RSSI);
      }
    }

    if (radio.ACKRequested()) { // When a node requests an ACK, respond to the ACK
      radio.sendACK();
      Serial.print(" - ACK sent.");
    }
    
    Serial.println();
  }
}
I see this when i upload:
Code:
RFM69_Arduino_to_Teensy3.2_Receiver.ino: In function 'void loop()':

RFM69_Arduino_to_Teensy3.2_Receiver\RFM69_Arduino_to_Teensy3.2_Receiver.ino:105:34: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

       theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else

                                  ^

RFM69_Arduino_to_Teensy3.2_Receiver\RFM69_Arduino_to_Teensy3.2_Receiver.ino:105:15: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

       theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else

               ^
 
Last edited:
THANKS!
Such a little thing has such a big effect. I did not see the interrupt assignments (back of pinout). Moved to pin 2 and it works...

What do i need to do to use SPI1?
 
Last edited:
going to SPI1 is a little more work.

As part of the T3.5/T3.6 beta stuff, I made a version of Radiohead library, where I could use SPI1:
https://github.com/KurtE/RadioHead/tree/SPI-Transaction-Support

The basic work is to go into that library folder and copy the files RHHardwareSPI.h/.cpp to RHHardwareSPI1.h/.cpp and change all occurrences of: SPI. to SPI1.
And likewise RHHardwareSPI to RHHardwareSPI1

and created an instance of it like: RHHardwareSPI1 hardware_spi1;

Then in my app, in my case using a ...95, the sketch includes:
#include <RHHardwareSPI1.h>

And also the constructor needs to know about which SPI to use, like:
RH_RF95 rf95(RFM95_CS, RFM95_INT, hardware_spi1);

Again my source files for changes to library are up on github... Wonder if I should do a Pull request and make a version for SPI2...

Also if you are using alternate pins for SPI1, you would use SPI1.setMOSI or SPI1.setMISO or...
 
Maybe there is an easier way.
I need Serial 1 and 2 and one SPI.
But there is just one CS0 on TX2...
Not sure what you are saying with one CS0 on TX2?

I think you are saying that there is only one CS0 pin and it is on the same pin as TX2. FYI - Although it is not marked on the board, I think pin 2 is also a CS0 pin on the TLC (at least in the SPI library code).

Also depending on library usage, it may not matter. that is they may simply use the CS pin by doing digitalWrites to the pin, in which case you can use any IO pin... But I could be wrong, I have not looked through the Radiohead code to see, but my guess is this is the case.
 
Any free digital pin can be used as chip select. LowPowerLabs RFM69 library does not use the hardware cs function.
 
Not working:
Code:
#define T3_IRQ_PIN    3  // pin for irq on teensy
#define T3_IRQ_NUM   3  // number for IRQ on teensy
#define T3_SPI_CS     2  // pin for SPI CS on teensy
Working:
Code:
#define T3_IRQ_PIN    2  // pin for irq on teensy
#define T3_IRQ_NUM   2  // number for IRQ on teensy
#define T3_SPI_CS     3  // pin for SPI CS on teensy
??????
 
Status
Not open for further replies.
Back
Top