Skip to content

Commit

Permalink
DIO pin optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sacua committed Apr 25, 2024
1 parent c1ff503 commit 6232628
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
55 changes: 47 additions & 8 deletions src/arduino-rfm/RFM95.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -705,26 +716,53 @@ 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);
Message_Status = TIMEOUT;
}

//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;
}

return Message_Status;
}


/*
*****************************************************************************************
* Description : Function to switch RFM to continuous receive mode, used for Class C motes
Expand All @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/arduino-rfm/RFM95.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
*****************************************************************************************
Expand All @@ -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();
Expand Down
36 changes: 25 additions & 11 deletions src/arduino-rfm/lorawan-arduino-rfm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6232628

Please sign in to comment.