Project

General

Profile

RFM69 native on ATtiny

Added by woelfs almost 4 years ago

Hi all,

I am trying to run the new native RFM69 driver on a ATtiny. I am currently using Nathan Chantrell’s TinyTX PCB
with a ATtiny 84 and a RFM69.

So far I adapted the spi.h from last weeks example by porting jeelib’s RF12 ATtiny code.

#include 

template< int N>
class SpiDev {
  static uint8_t spiTransferByte (uint8_t out) {
#ifdef SPCR
    SPDR = out;
    while ((SPSR & (1< SpiDev10;
#else
#
  typedef SpiDev<1> SpiDev1;
#endif

I read the SPI and USI AVR specs and I think SPI / USI emulation is fine. The pins should be fine as well, because the TinyTx with RF12 library and a RFM12 radio is working.

To test my implementation I modified the rf69demo sketch as a sender / receiver (ugly I know) and compiled once for the ATtiny 84 internal 8MHz and once on the Arduino Uno.

  // Report received data on the serial port.
#include 
#include 

#include "spi.h"
#include "rf69.h"

#define sender 

#ifndef sender
  RF69 rf;
  uint8_t rxBuf[64];
  #define nodeID 28
#else
  RF69 rf;
  #define nodeID 29
  uint16_t cnt = 0;
  uint8_t txBuf[62];
#endif

void setup () {

#ifndef sender
  Serial.begin(57600);
  Serial.println("\n[rf69demo]");
#else
  for (int i = 0; i < (int) sizeof txBuf; ++i)
    txBuf[i] = i;
#endif

  rf.init(nodeID, 42, 8686);
  //rf.encrypt("mysecret");
  rf.txPower(15); // 0 = min .. 31 = max

}

void loop () {

#ifdef sender
  if (++cnt == 0) {
    int txLen = ++txBuf[0] % (sizeof txBuf + 1);
    rf.send(0, txBuf, txLen);
  }
#else

  int len = rf.receive(rxBuf, sizeof rxBuf);
  if (len >= 0) {
    Serial.print("OK ");
    for (int i = 0; i < len; ++i) {
      Serial.print(rxBuf[i] >> 4, HEX);
      Serial.print(rxBuf[i] & 0xF, HEX);
    }
    Serial.print(" (");
    Serial.print(rf.rssi);
    Serial.print(rf.afc < 0 ? "" : "+");
    Serial.print(rf.afc);
    Serial.print(":");
    Serial.print(rf.lna);
    Serial.println(")");
  }
 #endif
}

As you can imagine the receiver did not receive anything :-)
Now I am a little stuck here. So any comments ideas are appreciated.

Best,
Wolfgang


Replies (8)

RE: RFM69 native on ATtiny - Added by martynj almost 4 years ago

woelfs,

Can you easily monitor the current drawn by the RF module, preferably with an old style, moving pointer milliammeter?
If you change rf.txPower(15); // 0 = min .. 31 = max to 31, then the idle, receive and transmit modes are easily distinguished by the size of the “kick”.
That could perhaps gives some clues to if you are driving the RF module at all.

RE: RFM69 native on ATtiny - Added by woelfs almost 4 years ago

Thanks martynj. Unfortunately I only have a digital multimeter. I added a 2sec delay in the loop of my sender code, but the current drawn is constant at 4.7mA. Have to check one of my existing RFM12 nodes for reference tonight.

RE: RFM69 native on ATtiny - Added by martynj almost 4 years ago

woelfs,

That would suggest the RF module is not responding to any commands (Rx drain ~16mA, full Tx drain ~45mA, though only briefly while the mode is active, hence the ‘kick’ on a moving pointer meter) If you only have a multimeter, you can check pin 7 (NSS) is pulling down for the SPI transactions (again, only briefly unless you force a loop).
Debugging further without a ’scope gets difficult.

RE: RFM69 native on ATtiny - Added by jcw almost 4 years ago

Or a logic analyser, there are some for \$15 or so on eBay which work quite well (Saleae compatible).

I haven’t looked at your code very much yet. Looks like you can send debugging output to a serial pin. Great. You could insert print’s at various places inside the rf.init() code to see how far it gets. If it gets through the rf.init() call, that would mean that SPI communication is working.

RE: RFM69 native on ATtiny - Added by woelfs almost 4 years ago

With painful toggle pin debugging I found out that the program hangs in RF69::init()

  ...
  writeReg(REG_SYNCVALUE1, 0xAA);
  while (readReg(REG_SYNCVALUE1) != 0xAA);

  pinMode(9,OUTPUT); // this is never reached
  digitalWrite(9,LOW);
  return;
  ...

My readReg looks as following:

uint8_t readReg (uint8_t addr) {
      uint8_t ret =spi.rwReg(addr, 0);
      if (ret == 0xFF || ret == 0)
      {
          pinMode(9,OUTPUT);
          digitalWrite(9,HIGH);
      }
      return ret;

    }

Pin 9 never goes LOW.

Looks like http://jeelabs.net/boards/7/topics/4906 could be related, even though cross reading the thread (more than a 100 posts) I didn’t find what actually fixed the problem yet.

RE: RFM69 native on ATtiny - Added by jcw almost 4 years ago

This means that SPI communication is not working yet on your setup.
The code writes a value in a harmless register and then makes sure it can read it back.
Next it writes a different code and checks again, to be sure SPI commands really work.

Impossible to say what could be causing this, but you could copy just the SPI setup code and these few lines into a standalone sketch and try to debug it that way.
A logic analyser or even an LED can help to figure out if the proper I/O pins are toggling. Just for kicks, try swapping MISO and MOSI.
If nothing else works, try to copy code from the RF12 driver, since it is known to do proper SPI comms on an ATtiny.

But my first suggestion would be to quadruple-check that all the pins and wires are hooked up correctly, and that all pin modes are set right.

RE: RFM69 native on ATtiny - Added by woelfs almost 4 years ago

It works!

[rf69demo]
OK 801D110102030405060708090A0B0C0D0E0F10 (76+10:6)
OK 801D120102030405060708090A0B0C0D0E0F1011 (71+32:6)
OK 801D130102030405060708090A0B0C0D0E0F101112 (67+34:6)
OK 801D140102030405060708090A0B0C0D0E0F10111213 (67-2:6)
OK 801D150102030405060708090A0B0C0D0E0F1011121314 (73+6:6)
OK 801D160102030405060708090A0B0C0D0E0F101112131415

All my bad, but I learned that it can be much much worth than printf debugging ;-)

Pins were right in my initial post but MISO (arduino pin 4) obviously needs to be INPUT and MOSI (arduino pin 5) OUTPUT

pinMode(1, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, OUTPUT);

should have been

pinMode(1, OUTPUT); // SS
pinMode(4, INPUT); // MISO 7
pinMode(5, OUTPUT); // MOSI 8
pinMode(6, OUTPUT); // SCK 9

Thanks for your help!

RE: RFM69 native on ATtiny - Added by jcw almost 4 years ago

Excellent - I’ve added your suggested changes to github.

    (1-8/8)