From 623262852587280dfa462c4f3e8db90548a80f7f Mon Sep 17 00:00:00 2001 From: "Samuel C. Auclair" Date: Wed, 24 Apr 2024 21:18:02 -0400 Subject: [PATCH] DIO pin optional --- src/arduino-rfm/RFM95.cpp | 55 +++++++++++++++++++++---- src/arduino-rfm/RFM95.h | 6 +++ src/arduino-rfm/lorawan-arduino-rfm.cpp | 36 +++++++++++----- 3 files changed, 78 insertions(+), 19 deletions(-) diff --git a/src/arduino-rfm/RFM95.cpp b/src/arduino-rfm/RFM95.cpp index 6204ae7..2e0b47f 100644 --- a/src/arduino-rfm/RFM95.cpp +++ b/src/arduino-rfm/RFM95.cpp @@ -603,6 +603,10 @@ void RFM_Set_Tx_Power(int level, int outputPin) } } +bool RFM_isRxDone() +{ + return (RFM_Read(RFM_REG_IRQ_FLAGS & 0x7f) & IRQ_RX_DONE_MASK) != 0; +} void RFM_Set_OCP(uint8_t mA) { @@ -642,7 +646,8 @@ void RFM_Send_Package(sBuffer *RFM_Tx_Package, sSettings *LoRa_Settings) RFM_Change_Channel(LoRa_Settings->Channel_Tx); //Switch DIO0 to TxDone - RFM_Write(RFM_REG_DIO_MAPPING1, 0x40); + if (RFM_pins.DIO0 != -1) + RFM_Write(RFM_REG_DIO_MAPPING1, 0x40); //Set IQ to normal values RFM_Write(RFM_REG_INVERT_IQ,0x27); @@ -667,7 +672,12 @@ void RFM_Send_Package(sBuffer *RFM_Tx_Package, sSettings *LoRa_Settings) RFM_Write(RFM_REG_OP_MODE,0x83); //Wait for TxDone - while(digitalRead(RFM_pins.DIO0) == LOW); + if (RFM_pins.DIO0 != -1) + while(digitalRead(RFM_pins.DIO0) == LOW); + else + { + while ((RFM_Read(RFM_REG_IRQ_FLAGS & 0x7f) & IRQ_TX_DONE_MASK) == 0); + } //Clear interrupt RFM_Write(RFM_REG_IRQ_FLAGS,0x08); @@ -688,7 +698,8 @@ message_t RFM_Single_Receive(sSettings *LoRa_Settings) message_t Message_Status = NO_MESSAGE; //Change DIO 0 back to RxDone - RFM_Write(RFM_REG_DIO_MAPPING1, 0x00); + if (RFM_pins.DIO0 != -1) + RFM_Write(RFM_REG_DIO_MAPPING1, 0x00); //Invert IQ Back RFM_Write(RFM_REG_INVERT_IQ, 0x67); @@ -705,10 +716,29 @@ message_t RFM_Single_Receive(sSettings *LoRa_Settings) //Wait until RxDone or Timeout //Wait until timeout or RxDone interrupt - while((digitalRead(RFM_pins.DIO0) == LOW) && (digitalRead(RFM_pins.DIO1) == LOW)); + byte RegIrqFlags; + if (RFM_pins.DIO0 != -1) + while((digitalRead(RFM_pins.DIO0) == LOW) && (digitalRead(RFM_pins.DIO1) == LOW)); + else + { + RegIrqFlags = RFM_Read(RFM_REG_IRQ_FLAGS & 0x7f); + while ((RegIrqFlags & (IRQ_RX_DONE_MASK | IRQ_RX_TIMEOUT_MASK)) == 0) + { + RegIrqFlags = RFM_Read(RFM_REG_IRQ_FLAGS & 0x7f); + } + } //Check for Timeout - if(digitalRead(RFM_pins.DIO1) == HIGH) + bool isTimeout; + if (RFM_pins.DIO0 != -1) + { + isTimeout = digitalRead(RFM_pins.DIO1) == HIGH; + } + else + { + isTimeout = (RegIrqFlags & IRQ_RX_TIMEOUT_MASK) != 0; + } + if (isTimeout) { //Clear interrupt register RFM_Write(RFM_REG_IRQ_FLAGS,0xE0); @@ -716,7 +746,16 @@ message_t RFM_Single_Receive(sSettings *LoRa_Settings) } //Check for RxDone - if(digitalRead(RFM_pins.DIO0) == HIGH) + bool isRxDone; + if (RFM_pins.DIO0 != -1) + { + isRxDone = digitalRead(RFM_pins.DIO0) == HIGH; + } + else + { + isRxDone = (RegIrqFlags & IRQ_RX_DONE_MASK) != 0; + } + if(isRxDone) { Message_Status = NEW_MESSAGE; } @@ -724,7 +763,6 @@ message_t RFM_Single_Receive(sSettings *LoRa_Settings) return Message_Status; } - /* ***************************************************************************************** * Description : Function to switch RFM to continuous receive mode, used for Class C motes @@ -735,7 +773,8 @@ message_t RFM_Single_Receive(sSettings *LoRa_Settings) void RFM_Continuous_Receive(sSettings *LoRa_Settings) { //Change DIO 0 back to RxDone and DIO 1 to rx timeout - RFM_Write(RFM_REG_DIO_MAPPING1,0x00); + if (RFM_pins.DIO0 != -1) + RFM_Write(RFM_REG_DIO_MAPPING1,0x00); //Invert IQ Back RFM_Write(RFM_REG_INVERT_IQ, 0x67); diff --git a/src/arduino-rfm/RFM95.h b/src/arduino-rfm/RFM95.h index aac651b..dd058ce 100644 --- a/src/arduino-rfm/RFM95.h +++ b/src/arduino-rfm/RFM95.h @@ -94,6 +94,11 @@ typedef enum { RFM_MODE_LORA = 0b10000000 } frm_mode_t; +typedef enum { + IRQ_RX_TIMEOUT_MASK = 0b10000000, + IRQ_RX_DONE_MASK = 0b01000000, + IRQ_TX_DONE_MASK = 0b00001000 + } irq_mask; /* ***************************************************************************************** @@ -109,6 +114,7 @@ message_t RFM_Get_Package(sBuffer *RFM_Rx_Package); void RFM_Write(unsigned char RFM_Address, unsigned char RFM_Data); void RFM_Switch_Mode(unsigned char Mode); void RFM_Set_Tx_Power(int level, int outputPin); +bool RFM_isRxDone(); void RFM_Set_OCP(unsigned char mA); unsigned char RFM_Get_Rssi(); diff --git a/src/arduino-rfm/lorawan-arduino-rfm.cpp b/src/arduino-rfm/lorawan-arduino-rfm.cpp index 48e2008..57b60b4 100644 --- a/src/arduino-rfm/lorawan-arduino-rfm.cpp +++ b/src/arduino-rfm/lorawan-arduino-rfm.cpp @@ -122,23 +122,28 @@ bool LoRaWANClass::init(void) Message_Rx.Direction = 0x01; //Set down direction for Rx message //Initialize I/O pins - pinMode(RFM_pins.DIO0, INPUT); - pinMode(RFM_pins.DIO1, INPUT); + if (RFM_pins.DIO0 != -1) + { + pinMode(RFM_pins.DIO0, INPUT); + pinMode(RFM_pins.DIO1, INPUT); + pinMode(RFM_pins.DIO2, INPUT); + } #ifdef BOARD_DRAGINO_SHIELD pinMode(RFM_pins.DIO5, INPUT); #endif - pinMode(RFM_pins.DIO2, INPUT); pinMode(RFM_pins.CS, OUTPUT); - pinMode(RFM_pins.RST, OUTPUT); - digitalWrite(RFM_pins.CS, HIGH); // Reset - digitalWrite(RFM_pins.RST, HIGH); - delay(10); - digitalWrite(RFM_pins.RST, LOW); - delay(10); - digitalWrite(RFM_pins.RST, HIGH); + if (RFM_pins.RST != -1) + { + pinMode(RFM_pins.RST, OUTPUT); + digitalWrite(RFM_pins.RST, HIGH); + delay(10); + digitalWrite(RFM_pins.RST, LOW); + delay(10); + digitalWrite(RFM_pins.RST, HIGH); + } //Initialise the SPI port SPI.begin(); @@ -495,7 +500,16 @@ void LoRaWANClass::update(void) } //Receive in Class C mode - if (digitalRead(RFM_pins.DIO0) == HIGH ) + bool isRxDone; + if (RFM_pins.DIO0 != -1) + { + isRxDone = digitalRead(RFM_pins.DIO0) == HIGH; + } + else + { + isRxDone = RFM_isRxDone(); + } + if (isRxDone) { LORA_Receive_Data(&Buffer_Rx, &Session_Data, &OTAA_Data, &Message_Rx, &LoRa_Settings); if (Buffer_Rx.Counter != 0x00)