From 15d1cdc52677db61eb2806e649f7bc84a39b4287 Mon Sep 17 00:00:00 2001 From: Adam Novak Date: Wed, 16 Feb 2022 21:02:49 -0500 Subject: [PATCH] Poll for packets --- LoRa.cpp | 55 ++++++++++++++++++++++++++++++---------------- LoRa.h | 2 ++ RNode_Firmware.ino | 6 +++++ 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/LoRa.cpp b/LoRa.cpp index c320f00..f95a92f 100644 --- a/LoRa.cpp +++ b/LoRa.cpp @@ -394,6 +394,19 @@ void LoRaClass::flush() { } +void LoRaClass::pollReceive() +{ + int irqFlags = readRegister(REG_IRQ_FLAGS); + + // clear IRQ's + writeRegister(REG_IRQ_FLAGS, irqFlags); + + if ((irqFlags & IRQ_RX_DONE_MASK) && !(irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK)) { + // received a packet + handleRx(); + } +} + void LoRaClass::onReceive(void(*callback)(int)) { _onReceive = callback; @@ -404,17 +417,15 @@ void LoRaClass::onReceive(void(*callback)(int)) #endif writeRegister(REG_DIO_MAPPING_1, 0x00); - -#if LIBRARY_TYPE == LIBRARY_ARDUINO + +#if MCU_VARIANT != MCU_LINUX && LIBRARY_TYPE == LIBRARY_ARDUINO #ifdef SPI_HAS_NOTUSINGINTERRUPT SPI.usingInterrupt(digitalPinToInterrupt(_dio0)); #endif attachInterrupt(digitalPinToInterrupt(_dio0), LoRaClass::onDio0Rise, RISING); #endif - // TODO: What do we do if we want to use C library with a board that - // actually has dio0 connected? } else { -#if LIBRARY_TYPE == LIBRARY_ARDUINO +#if MCU_VARIANT != MCU_LINUX && LIBRARY_TYPE == LIBRARY_ARDUINO detachInterrupt(digitalPinToInterrupt(_dio0)); #ifdef SPI_HAS_NOTUSINGINTERRUPT SPI.notUsingInterrupt(digitalPinToInterrupt(_dio0)); @@ -670,23 +681,29 @@ void ISR_VECT LoRaClass::handleDio0Rise() if ((irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) { // received a packet - _packetIndex = 0; - - // read packet length - int packetLength = _implicitHeaderMode ? readRegister(REG_PAYLOAD_LENGTH) : readRegister(REG_RX_NB_BYTES); - - // set FIFO address to current RX address - writeRegister(REG_FIFO_ADDR_PTR, readRegister(REG_FIFO_RX_CURRENT_ADDR)); - - if (_onReceive) { - _onReceive(packetLength); - } - - // reset FIFO address - writeRegister(REG_FIFO_ADDR_PTR, 0); + handleRx(); } } +void ISR_VECT LoRaClass::handleRx() +{ + // received a packet + _packetIndex = 0; + + // read packet length + int packetLength = _implicitHeaderMode ? readRegister(REG_PAYLOAD_LENGTH) : readRegister(REG_RX_NB_BYTES); + + // set FIFO address to current RX address + writeRegister(REG_FIFO_ADDR_PTR, readRegister(REG_FIFO_RX_CURRENT_ADDR)); + + if (_onReceive) { + _onReceive(packetLength); + } + + // reset FIFO address + writeRegister(REG_FIFO_ADDR_PTR, 0); +} + uint8_t ISR_VECT LoRaClass::readRegister(uint8_t address) { return singleTransfer(address & 0x7f, 0x00); diff --git a/LoRa.h b/LoRa.h index a418efe..b784f27 100644 --- a/LoRa.h +++ b/LoRa.h @@ -76,6 +76,7 @@ public: virtual int peek(); virtual void flush(); + void pollReceive(); void onReceive(void(*callback)(int)); void receive(int size = 0); @@ -115,6 +116,7 @@ private: void implicitHeaderMode(); void handleDio0Rise(); + void handleRx(); uint8_t readRegister(uint8_t address); void writeRegister(uint8_t address, uint8_t value); diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 5e8af06..b444e64 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -778,6 +778,12 @@ void loop() { kiss_write_packet(); } #endif + + #if MCU_VARIANT == MCU_LINUX + // We don't have interrupts, so we need to poll ofr received packets. + // TODO: Is this fast enough? Or do we need threads or something? + LoRa.pollReceive(); + #endif if (queue_height > 0) { if (!dcd_waiting) updateModemStatus();