Pin change interrupt with RFM69 with the Jeelibs library
I'm trying to use the RFM69CW radio module with a TFT shield on an Arduino Uno, however, both devices require the use of pin 2 on the Arduino. I've researched the possibility of changing the pin assignment for the RFM69 radio to use pin A5 which is free in my setup. I've struggled to get this to work with the pin change interrupt feature of the Jeelibs library. I've made made the changes stated in the RFM12B.cpp file as indicated in the comments:
`// #define OPTIMIZE_SPI 1 // uncomment this to write to the RFM12B @ 8 Mhz
// pin change interrupts are currently only supported on ATmega328's
#define PINCHG_IRQ 1 // uncomment this to use pin-change interrupts
// maximum transmit / receive buffer: 3 header + data + 2 crc bytes
#define RF_MAX (RF12_MAXDATA + 5)
// pins used for the RFM12B interface - yes, there is logic in this madness:
// - leave RFM_IRQ set to the pin which corresponds with INT0, because the
// current driver code will use attachInterrupt() to hook into that
// - (new) you can now change RFM_IRQ, if you also enable PINCHG_IRQ - this
// will switch to pin change interrupts instead of attach/detachInterrupt()
// - use SS_DDR, SS_PORT, and SS_BIT to define the pin you will be using as
// select pin for the RFM12B (you're free to set them to anything you like)
// - please leave SPI_SS, SPI_MOSI, SPI_MISO, and SPI_SCK as is, i.e. pointing
// to the hardware-supported SPI pins on the ATmega, including SPI_SS !
// ATmega168, ATmega328, etc.
#define RFM_IRQ 19 // 2=JeeNode, 18=JeeNode pin change`
I suspect my understanding of the changes needed is lacking and I'm not sure I've made all the changes needed. What settings do I need to allow the IRQ for the RFM69 radio to work with pin A5? (Note that I'm setting #define RF69_COMPAT 1 in my sketch).
Many thanks in advance.
Referencing http://marcusjenkins.com/arduino-pinout-diagrams/ I am seeing A5 silkscreen to be Port C bit 5. Mapping across to the left I see PCINT13 as its pin change reference. My first take would be:
// ATmega168, ATmega328, etc.
//#define RFM_IRQ 2 // 2=JeeNode, 18=JeeNode pin change
//#define RFM_IRQ 1 // PCINT1=JeeNode Block pin change
#define RFM_IRQ 13 // Port C bit 5, Uno A5
#define SS_DDR DDRB
#define SS_PORT PORTB
#define SS_BIT 2 // for PORTB: 2 = d.10, 1 = d.9, 0 = d.8
#define SPI_SS 10 // PB2, pin 16
#define SPI_MOSI 11 // PB3, pin 17
#define SPI_MISO 12 // PB4, pin 18
#define SPI_SCK 13 // PB5, pin 19
Have you considered using INT1?
Thanks very much for your reply JohnO. I tried as you suggest with pin 13 but it doesn't work. If I connect the IRQ pin on the RFM69 back to digital pin 2 it works again though that might be as expected. Is there any reason why it will not work with an RFM69 in compatibility mode but will with a genuine RFM12B?
Regarding INT1, my (limited) understanding is that I would need to use pin 3 on the Arduino - unfortunately that pin is also used by my TFT shield.
Thanks again for your help.
Are you setting the IRQ pin value in the RF69_avr.h file?
Hold that thought.
I am now having doubts that PIN_CHANGE was ever implemented in the Master branch for the RFM69. I have a branch that has it implemented.
Thanks again JohnO. Would that branch be available to me to test with by any chance?
It is available as the RFxConsole-RSSI branch https://github.com/jcw/jeelib/tree/RFxConsole-RSSI
The IRQ number is set in the file RF69_avr.h
It is important to set RF69_COMPAT in the above file in addition to the similar setting in RF12.cpp.
My branch is copy of jeelib master branch heavily mashed about by my untutored hand and does contain bad, ugly and I hope a little good but it comes without any warranty.
Do you have a link to the TFT board you are using as I am surprised it is so interrupt greedy.
Thanks very much for the link - I'll give that a try. I appreciate your efforts in making PCI work with these radio modules.
I'm using the mcufriend_kvb library to drive it. This particular TFT module needed a hardware tweak to make it work with an 8 bit bus. Unfortunately is takes most of the pins on an Arduino. The SPI and A5 pins are free however.
Many thanks again.
NB My branch holds for far too long in the interrupt service code. If you get other interrupt starvation issues for your TFT card etc. you may want to consider porting the pin change code into your own branch or perhaps a pull request on master to implement pin change. From memory it isn't a trivial change.