diff --git a/RFM69.cpp b/RFM69.cpp index 4c15c20..52501bc 100644 --- a/RFM69.cpp +++ b/RFM69.cpp @@ -39,8 +39,30 @@ volatile int16_t RFM69::RSSI; // most accurate RSSI during reception (c volatile bool RFM69::_inISR; RFM69* RFM69::selfPointer; +RFM69::RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW) +{ + _slaveSelectPin = slaveSelectPin; + _interruptPin = interruptPin; + _mode = RF69_MODE_STANDBY; + _promiscuousMode = false; + _powerLevel = 31; + _isRFM69HW = isRFM69HW; + #if defined(RF69_LISTENMODE_ENABLE) + _isHighSpeed = true; + _haveEncryptKey = false; + uint32_t rxDuration = DEFAULT_LISTEN_RX_US; + uint32_t idleDuration = DEFAULT_LISTEN_IDLE_US; + listenModeSetDurations(rxDuration, idleDuration); +#endif +} + bool RFM69::initialize(uint8_t freqBand, uint8_t nodeID, uint8_t networkID) { + _interruptNum = digitalPinToInterrupt(_interruptPin); + if (_interruptNum == NOT_AN_INTERRUPT) return false; +#ifdef RF69_ATTACHINTERRUPT_TAKES_PIN_NUMBER + _interruptNum = _interruptPin; +#endif const uint8_t CONFIG[][2] = { /* 0x01 */ { REG_OPMODE, RF_OPMODE_SEQUENCER_ON | RF_OPMODE_LISTEN_OFF | RF_OPMODE_STANDBY }, diff --git a/RFM69.h b/RFM69.h index a48413e..d07893e 100644 --- a/RFM69.h +++ b/RFM69.h @@ -26,29 +26,106 @@ #ifndef RFM69_h #define RFM69_h #include // assumes Arduino IDE v1.0 or greater +#include + +////////////////////////////////////////////////////////////////////// +//Platform and digitalPinToInterrupt definitions credit to RadioHead// +////////////////////////////////////////////////////////////////////// +// Select platform automatically, if possible +#ifndef RF69_PLATFORM + #if (MPIDE>=150 && defined(ARDUINO)) + // Using ChipKIT Core on Arduino IDE + #define RF69_PLATFORM RF69_PLATFORM_CHIPKIT_CORE + #elif defined(MPIDE) + // Uno32 under old MPIDE, which has been discontinued: + #define RF69_PLATFORM RF69_PLATFORM_UNO32 +#elif defined(NRF51) + #define RF69_PLATFORM RF69_PLATFORM_NRF51 +#elif defined(NRF52) + #define RF69_PLATFORM RF69_PLATFORM_NRF52 + #elif defined(ESP8266) + #define RF69_PLATFORM RF69_PLATFORM_ESP8266 + #elif defined(ESP32) + #define RF69_PLATFORM RF69_PLATFORM_ESP32 + #elif defined(ARDUINO) + #define RF69_PLATFORM RF69_PLATFORM_ARDUINO + #elif defined(__MSP430G2452__) || defined(__MSP430G2553__) + #define RF69_PLATFORM RF69_PLATFORM_MSP430 + #elif defined(MCU_STM32F103RE) + #define RF69_PLATFORM RF69_PLATFORM_STM32 + #elif defined(STM32F2XX) + #define RF69_PLATFORM RF69_PLATFORM_STM32F2 + #elif defined(USE_STDPERIPH_DRIVER) + #define RF69_PLATFORM RF69_PLATFORM_STM32STD + #elif defined(RASPBERRY_PI) + #define RF69_PLATFORM RF69_PLATFORM_RASPI +#elif defined(__unix__) // Linux + #define RF69_PLATFORM RF69_PLATFORM_UNIX +#elif defined(__APPLE__) // OSX + #define RF69_PLATFORM RF69_PLATFORM_UNIX + #else + #error Platform not defined! + #endif +#endif + +// digitalPinToInterrupt is not available prior to Arduino 1.5.6 and 1.0.6 +// See http://arduino.cc/en/Reference/attachInterrupt +#ifndef NOT_AN_INTERRUPT + #define NOT_AN_INTERRUPT -1 +#endif +#ifndef digitalPinToInterrupt + #if (RF69_PLATFORM == RF69_PLATFORM_ARDUINO) && !defined(__arm__) + #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + // Arduino Mega, Mega ADK, Mega Pro + // 2->0, 3->1, 21->2, 20->3, 19->4, 18->5 + #define digitalPinToInterrupt(p) ((p) == 2 ? 0 : ((p) == 3 ? 1 : ((p) >= 18 && (p) <= 21 ? 23 - (p) : NOT_AN_INTERRUPT))) + #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) + // Arduino 1284 and 1284P - See Manicbug and Optiboot + // 10->0, 11->1, 2->2 + #define digitalPinToInterrupt(p) ((p) == 10 ? 0 : ((p) == 11 ? 1 : ((p) == 2 ? 2 : NOT_AN_INTERRUPT))) + #elif defined(__AVR_ATmega32U4__) + // Leonardo, Yun, Micro, Pro Micro, Flora, Esplora + // 3->0, 2->1, 0->2, 1->3, 7->4 + #define digitalPinToInterrupt(p) ((p) == 0 ? 2 : ((p) == 1 ? 3 : ((p) == 2 ? 1 : ((p) == 3 ? 0 : ((p) == 7 ? 4 : NOT_AN_INTERRUPT))))) + #else + // All other arduino except Due: + // Serial Arduino, Extreme, NG, BT, Uno, Diecimila, Duemilanove, Nano, Menta, Pro, Mini 04, Fio, LilyPad, Ethernet etc + // 2->0, 3->1 + #define digitalPinToInterrupt(p) ((p) == 2 ? 0 : ((p) == 3 ? 1 : NOT_AN_INTERRUPT)) + #endif + #elif (RF69_PLATFORM == RF69_PLATFORM_UNO32) || (RF69_PLATFORM == RF69_PLATFORM_CHIPKIT_CORE) + // Hmmm, this is correct for Uno32, but what about other boards on ChipKIT Core? + #define digitalPinToInterrupt(p) ((p) == 38 ? 0 : ((p) == 2 ? 1 : ((p) == 7 ? 2 : ((p) == 8 ? 3 : ((p) == 735 ? 4 : NOT_AN_INTERRUPT))))) + #else + // Everything else (including Due and Teensy) interrupt number the same as the interrupt pin number + #define digitalPinToInterrupt(p) (p) + #endif +#endif + +// On some platforms, attachInterrupt() takes a pin number, not an interrupt number +#if (RF69_PLATFORM == RF69_PLATFORM_ARDUINO) && defined (__arm__) && (defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_SAM_DUE)) + #define RF69_ATTACHINTERRUPT_TAKES_PIN_NUMBER +#endif +//////////////////////////////////////////////////// -#define RF69_MAX_DATA_LEN 61 // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead - 2 bytes crc) #define RF69_SPI_CS SS // SS is the SPI slave select pin, for instance D10 on ATmega328 // INT0 on AVRs should be connected to RFM69's DIO0 (ex on ATmega328 it's D2, on ATmega644/1284 it's D2) #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega88) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__) #define RF69_IRQ_PIN 2 - #define RF69_IRQ_NUM 0 #elif defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) #define RF69_IRQ_PIN 2 - #define RF69_IRQ_NUM 2 #elif defined(__AVR_ATmega32U4__) - #define RF69_IRQ_PIN 3 - #define RF69_IRQ_NUM 0 -#elif defined(__arm__)//Use pin 10 or any pin you want + #define RF69_IRQ_PIN 7 +#elif defined(__STM32F1__) #define RF69_IRQ_PIN PA3 - #define RF69_IRQ_NUM 3 -#else +#elif defined(ARDUINO_SAMD_ZERO) //includes Feather SAMD + #define RF69_IRQ_PIN 3 +#else #define RF69_IRQ_PIN 2 - #define RF69_IRQ_NUM 0 #endif - +#define RF69_MAX_DATA_LEN 61 // to take advantage of the built in AES/CRC we want to limit the frame size to the internal FIFO size (66 bytes - 3 bytes overhead - 2 bytes crc) #define CSMA_LIMIT -90 // upper RX signal sensitivity threshold in dBm for carrier sense access #define RF69_MODE_SLEEP 0 // XTAL OFF #define RF69_MODE_STANDBY 1 // XTAL ON @@ -85,15 +162,10 @@ class RFM69 { static volatile int16_t RSSI; // most accurate RSSI during reception (closest to the reception) static volatile uint8_t _mode; // should be protected? - RFM69(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false, uint8_t interruptNum=RF69_IRQ_NUM) { - _slaveSelectPin = slaveSelectPin; - _interruptPin = interruptPin; - _interruptNum = interruptNum; - _mode = RF69_MODE_STANDBY; - _promiscuousMode = false; - _powerLevel = 31; - _isRFM69HW = isRFM69HW; - } + RFM69(uint8_t slaveSelectPin, uint8_t interruptPin, bool isRFM69HW, uint8_t interruptNum) //interruptNum is now deprecated + : RFM69(slaveSelectPin, interruptPin, isRFM69HW){}; + + RFM69(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false); bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1); void setAddress(uint8_t addr); diff --git a/RFM69_ATC.h b/RFM69_ATC.h index f24bf90..4d8c988 100644 --- a/RFM69_ATC.h +++ b/RFM69_ATC.h @@ -35,8 +35,8 @@ class RFM69_ATC: public RFM69 { public: static volatile uint8_t ACK_RSSI_REQUESTED; // new flag in CTL byte to request RSSI with ACK (could potentially be merged with ACK_REQUESTED) - RFM69_ATC(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false, uint8_t interruptNum=RF69_IRQ_NUM) : - RFM69(slaveSelectPin, interruptPin, isRFM69HW, interruptNum) { + RFM69_ATC(uint8_t slaveSelectPin=RF69_SPI_CS, uint8_t interruptPin=RF69_IRQ_PIN, bool isRFM69HW=false, uint8_t interruptNum=0) : + RFM69(slaveSelectPin, interruptPin, isRFM69HW) { } bool initialize(uint8_t freqBand, uint8_t ID, uint8_t networkID=1); diff --git a/RFM69_OTA.cpp b/RFM69_OTA.cpp index de268ec..467a936 100644 --- a/RFM69_OTA.cpp +++ b/RFM69_OTA.cpp @@ -30,7 +30,7 @@ // Please maintain this license information along with authorship // and copyright notices in any redistribution of this code // ********************************************************************************** -#ifndef __arm__ +#ifdef __AVR__ #include #include #include diff --git a/RFM69_OTA.h b/RFM69_OTA.h index 012f43e..7656c34 100644 --- a/RFM69_OTA.h +++ b/RFM69_OTA.h @@ -30,7 +30,7 @@ // Please maintain this license information along with authorship // and copyright notices in any redistribution of this code // ********************************************************************************** -#ifndef __arm__ +#ifdef __AVR__ #ifndef RFM69_OTA_H #define RFM69_OTA_H