IRQ in RFM12b library

Started by Lensdigital, August 07, 2014, 07:17:33 PM

Charly86

#15
I think I found the problem, in fact, for SPI the RFM12B Library use this definitions (in rfm12b_arrsi.h)

#define SPI_SS          10  // D10 
#define SPI_MOSI        11  // PB3, pin 17
#define SPI_MISO        12  // PB4, pin 18
#define SPI_SCK         13  // PB5, pin 19


I did not remembered that and I though in my code that these defined values were done by Arduino environment so adapted to SPI hardware depending on the target, but it's not and for the 1284p these pins are not for the hardware SPI and so they are wrong and should be :

#define SPI_SS          4   // PB4, pin 5
#define SPI_MOSI        5   // PB5, pin 6
#define SPI_MISO        6   // PB6, pin 7
#define SPI_SCK         7   // PB7, pin 8


So I added the following definition in the rfm12b_arssi.h for the library
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega88) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__)
  #define RFM_DEFAULT_IRQ 2   // default IRQ PIN D2 (can be changed by SetIRQ)
  #define SPI_SS          10  // D10 
  #define SPI_MOSI        11  // PB3, pin 17
  #define SPI_MISO        12  // PB4, pin 18
  #define SPI_SCK         13  // PB5, pin 19
#elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
  #define RFM_DEFAULT_IRQ 2   // default IRQ PIN D2 -> PB2, pin 3(can be changed by SetIRQ)
  #define SPI_SS          4   // PB4, pin 5
  #define SPI_MOSI        5   // PB5, pin 6
  #define SPI_MISO        6   // PB6, pin 7
  #define SPI_SCK         7   // PB7, pin 8
#else
  #error Target not supported for HW Interrupts
#endif


And I'm sure it should be working much better now, let me know

Library has been updated according the above modifications


Lensdigital

#16
Wow you did it! It works on my breadboard setup and with actual circuit!  I kind of suspected it was issue with PORT names :)
So RFM_CS_PIN and RFM_IRQ_PIN definitions in the sketch are optional?

Charly86

Great that it works  :)
The definition RFM_CS_PIN and RFM_IRQ_PIN are default IRQ used a compilation time, but you can change them according to your wiring in the setup of your sketch (or at any place) using the 2 following lines of code (this has to be done only once) :

// sample setting
uint8_t arduinoCSPin = D7 ;
uint8_t ArduinoIRQPin = D3 ;

// This is for choosing the CS pin where is connected the Chip Select pin of the RFM12B
// Arduino pin is Arduino format (5, 8, ... A2, ..)
// This call should be done before any Initialize(...) standard code
radio.SetCS( arduinoCSPin );

// This is for choosing the Hardware IRQ pin where is connected the IRQ of the RFM12B.
// You can only use with external hardware IRQ of Arduino, so for :
// ATMega328 it's 2 for IRQ Pin 2 (INT0) and 3 for IRQ Pin 3 (INT1).
// ATMega1284 it's 10 for IRQ Pin 10 (INT0), 11 for IRQ Pin 11 (INT1) and 2 for IRQ Pin 2 (INT3)
radio.SetIRQ( ArduinoIRQPin );

// Then Initialize the RFM module
radio.Initialize(....)


You can avoid these 2 calls when trying detecting the Module presence,

Serial.print(F("Trying to detect RFM12B..."))

// Check if RFM12B device is present, return true if RFM12B device is detected using the cspin and irqpin false otherwise (same format as above)
// This call inside call SetCS and SetIRQ so if you call isPresent you can avoid calling SetCS and SetIRQ. 
// If you call isPresent without parameters it assume that the RFM12B module is connected with default values (CS=D10 and IRQ=D2)
if ( radio.isPresent( arduinoCSPin , arduinoIRQPin ) )
{
  Serial.print(F("Found!"))
  radio.Initialize(...)
}
else
{
  Serial.print(F("Not Found, check Wiring)"))
}


Let me know if you have any question. If you want more details, take a look at the documentation