diff --git a/platformio.ini b/platformio.ini index 89ff46e9b1..dbdf1051f4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -69,7 +69,7 @@ lib_deps = https://github.com/meshtastic/AXP202X_Library.git#8404abb6d4b486748636bc6ad72d2a47baaf5460 Wire ; explicitly needed here because the AXP202 library forgets to add it SPI - https://github.com/geeksville/ArduinoThread.git + https://github.com/geeksville/ArduinoThread.git#333ffd09b596977c217ba25da4258f588b462ac6 ; Common settings for conventional (non Portduino) Ardino targets [arduino_base] diff --git a/src/concurrency/BinarySemaphoreFreeRTOS.cpp b/src/concurrency/BinarySemaphoreFreeRTOS.cpp new file mode 100644 index 0000000000..983ee09551 --- /dev/null +++ b/src/concurrency/BinarySemaphoreFreeRTOS.cpp @@ -0,0 +1,39 @@ +#include "concurrency/BinarySemaphoreFreeRTOS.h" +#include "configuration.h" + +#ifdef HAS_FREE_RTOS + +namespace concurrency +{ + +BinarySemaphoreFreeRTOS::BinarySemaphoreFreeRTOS() +{ + semaphore = xSemaphoreCreateBinary(); +} + +BinarySemaphoreFreeRTOS::~BinarySemaphoreFreeRTOS() +{ + vSemaphoreDelete(semaphore); +} + +/** + * Returns false if we were interrupted + */ +bool BinarySemaphoreFreeRTOS::take(uint32_t msec) +{ + return xSemaphoreTake(semaphore, pdMS_TO_TICKS(msec)); +} + +void BinarySemaphoreFreeRTOS::give() +{ + xSemaphoreGive(semaphore); +} + +IRAM_ATTR void BinarySemaphoreFreeRTOS::giveFromISR(BaseType_t *pxHigherPriorityTaskWoken) +{ + xSemaphoreGiveFromISR(semaphore, pxHigherPriorityTaskWoken); +} + +} // namespace concurrency + +#endif \ No newline at end of file diff --git a/src/concurrency/BinarySemaphoreFreeRTOS.h b/src/concurrency/BinarySemaphoreFreeRTOS.h new file mode 100644 index 0000000000..b5e488fd2e --- /dev/null +++ b/src/concurrency/BinarySemaphoreFreeRTOS.h @@ -0,0 +1,31 @@ +#pragma once + +#include "configuration.h" +#include "../freertosinc.h" + +namespace concurrency +{ + +#ifdef HAS_FREE_RTOS + +class BinarySemaphoreFreeRTOS +{ + SemaphoreHandle_t semaphore; + + public: + BinarySemaphoreFreeRTOS(); + ~BinarySemaphoreFreeRTOS(); + + /** + * Returns false if we timed out + */ + bool take(uint32_t msec); + + void give(); + + void giveFromISR(BaseType_t *pxHigherPriorityTaskWoken); +}; + +#endif + +} \ No newline at end of file diff --git a/src/concurrency/BinarySemaphorePosix.cpp b/src/concurrency/BinarySemaphorePosix.cpp new file mode 100644 index 0000000000..44cd741f16 --- /dev/null +++ b/src/concurrency/BinarySemaphorePosix.cpp @@ -0,0 +1,36 @@ +#include "concurrency/BinarySemaphorePosix.h" +#include "configuration.h" + +#ifndef HAS_FREE_RTOS + +namespace concurrency +{ + +BinarySemaphorePosix::BinarySemaphorePosix() +{ +} + +BinarySemaphorePosix::~BinarySemaphorePosix() +{ +} + +/** + * Returns false if we timed out + */ +bool BinarySemaphorePosix::take(uint32_t msec) +{ + delay(msec); // FIXME + return false; +} + +void BinarySemaphorePosix::give() +{ +} + +IRAM_ATTR void BinarySemaphorePosix::giveFromISR(BaseType_t *pxHigherPriorityTaskWoken) +{ +} + +} // namespace concurrency + +#endif \ No newline at end of file diff --git a/src/concurrency/BinarySemaphorePosix.h b/src/concurrency/BinarySemaphorePosix.h new file mode 100644 index 0000000000..8a63686784 --- /dev/null +++ b/src/concurrency/BinarySemaphorePosix.h @@ -0,0 +1,31 @@ +#pragma once + +#include "configuration.h" +#include "../freertosinc.h" + +namespace concurrency +{ + +#ifndef HAS_FREE_RTOS + +class BinarySemaphorePosix +{ + // SemaphoreHandle_t semaphore; + + public: + BinarySemaphorePosix(); + ~BinarySemaphorePosix(); + + /** + * Returns false if we timed out + */ + bool take(uint32_t msec); + + void give(); + + void giveFromISR(BaseType_t *pxHigherPriorityTaskWoken); +}; + +#endif + +} \ No newline at end of file diff --git a/src/concurrency/InterruptableDelay.cpp b/src/concurrency/InterruptableDelay.cpp index 4b4ccc9142..e7538235e0 100644 --- a/src/concurrency/InterruptableDelay.cpp +++ b/src/concurrency/InterruptableDelay.cpp @@ -6,12 +6,10 @@ namespace concurrency InterruptableDelay::InterruptableDelay() { - semaphore = xSemaphoreCreateBinary(); } InterruptableDelay::~InterruptableDelay() { - vSemaphoreDelete(semaphore); } /** @@ -23,7 +21,7 @@ bool InterruptableDelay::delay(uint32_t msec) // DEBUG_MSG("delay %u ", msec); // sem take will return false if we timed out (i.e. were not interrupted) - bool r = xSemaphoreTake(semaphore, pdMS_TO_TICKS(msec)); + bool r = semaphore.take(msec); // DEBUG_MSG("interrupt=%d\n", r); return !r; @@ -34,12 +32,12 @@ bool InterruptableDelay::delay(uint32_t msec) void InterruptableDelay::interrupt() { - xSemaphoreGive(semaphore); + semaphore.give(); } IRAM_ATTR void InterruptableDelay::interruptFromISR(BaseType_t *pxHigherPriorityTaskWoken) { - xSemaphoreGiveFromISR(semaphore, pxHigherPriorityTaskWoken); + semaphore.giveFromISR(pxHigherPriorityTaskWoken); } } // namespace concurrency \ No newline at end of file diff --git a/src/concurrency/InterruptableDelay.h b/src/concurrency/InterruptableDelay.h index b6428b25a5..b0d4b009fb 100644 --- a/src/concurrency/InterruptableDelay.h +++ b/src/concurrency/InterruptableDelay.h @@ -2,6 +2,15 @@ #include "../freertosinc.h" + +#ifdef HAS_FREE_RTOS +#include "concurrency/BinarySemaphoreFreeRTOS.h" +#define BinarySemaphore BinarySemaphoreFreeRTOS +#else +#include "concurrency/BinarySemaphorePosix.h" +#define BinarySemaphore BinarySemaphorePosix +#endif + namespace concurrency { @@ -14,7 +23,7 @@ namespace concurrency */ class InterruptableDelay { - SemaphoreHandle_t semaphore; + BinarySemaphore semaphore; public: InterruptableDelay(); diff --git a/src/mesh/RadioInterface.cpp b/src/mesh/RadioInterface.cpp index 28e6b8c888..8fefd80f88 100644 --- a/src/mesh/RadioInterface.cpp +++ b/src/mesh/RadioInterface.cpp @@ -91,7 +91,7 @@ void printPacket(const char *prefix, const MeshPacket *p) DEBUG_MSG(")\n"); } -RadioInterface::RadioInterface() : NotifiedWorkerThread("RadioIf") +RadioInterface::RadioInterface() { assert(sizeof(PacketHeader) == 4 || sizeof(PacketHeader) == 16); // make sure the compiler did what we expected diff --git a/src/mesh/RadioInterface.h b/src/mesh/RadioInterface.h index 70c5a090ac..1d2e4b7508 100644 --- a/src/mesh/RadioInterface.h +++ b/src/mesh/RadioInterface.h @@ -36,7 +36,7 @@ typedef struct { * * This defines the SOLE API for talking to radios (because soon we will have alternate radio implementations) */ -class RadioInterface : protected concurrency::NotifiedWorkerThread +class RadioInterface { friend class MeshRadio; // for debugging we let that class touch pool PointerQueue *rxDest = NULL; @@ -72,6 +72,8 @@ class RadioInterface : protected concurrency::NotifiedWorkerThread */ RadioInterface(); + virtual ~RadioInterface() {} + /** * Set where to deliver received packets. This method should only be used by the Router class */ diff --git a/src/mesh/RadioLibInterface.cpp b/src/mesh/RadioLibInterface.cpp index 5dab06e001..41d000d693 100644 --- a/src/mesh/RadioLibInterface.cpp +++ b/src/mesh/RadioLibInterface.cpp @@ -19,7 +19,7 @@ void LockingModule::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t *dataOut, uint RadioLibInterface::RadioLibInterface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy, SPIClass &spi, PhysicalLayer *_iface) - : module(cs, irq, rst, busy, spi, spiSettings), iface(_iface) + : NotifiedWorkerThread("RadioIf"), module(cs, irq, rst, busy, spi, spiSettings), iface(_iface) { instance = this; } diff --git a/src/mesh/RadioLibInterface.h b/src/mesh/RadioLibInterface.h index 755935e26f..86dcb9701e 100644 --- a/src/mesh/RadioLibInterface.h +++ b/src/mesh/RadioLibInterface.h @@ -59,7 +59,7 @@ class LockingModule : public Module virtual void SPItransfer(uint8_t cmd, uint8_t reg, uint8_t *dataOut, uint8_t *dataIn, uint8_t numBytes); }; -class RadioLibInterface : public RadioInterface +class RadioLibInterface : public RadioInterface, protected concurrency::NotifiedWorkerThread { /// Used as our notification from the ISR enum PendingISR { ISR_NONE = 0, ISR_RX, ISR_TX, TRANSMIT_DELAY_COMPLETED }; diff --git a/src/mesh/TypedQueue.h b/src/mesh/TypedQueue.h index e35cb34bd8..0b60e6cf74 100644 --- a/src/mesh/TypedQueue.h +++ b/src/mesh/TypedQueue.h @@ -73,6 +73,7 @@ template class TypedQueue template class TypedQueue { std::queue q; + concurrency::OSThread *reader = NULL; public: TypedQueue(int maxElements) {} @@ -83,6 +84,11 @@ template class TypedQueue bool enqueue(T x, TickType_t maxWait = portMAX_DELAY) { + if (reader) { + reader->setInterval(0); + concurrency::mainDelay.interrupt(); + } + q.push(x); return true; } @@ -101,5 +107,7 @@ template class TypedQueue } // bool dequeueFromISR(T *p, BaseType_t *higherPriWoken) { return xQueueReceiveFromISR(h, p, higherPriWoken); } + + void setReader(concurrency::OSThread *t) { reader = t; } }; #endif