Skip to content

Commit

Permalink
fixes for the posix port
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksville committed Oct 11, 2020
1 parent 8330c32 commit 999b292
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 11 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
39 changes: 39 additions & 0 deletions src/concurrency/BinarySemaphoreFreeRTOS.cpp
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions src/concurrency/BinarySemaphoreFreeRTOS.h
Original file line number Diff line number Diff line change
@@ -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

}
36 changes: 36 additions & 0 deletions src/concurrency/BinarySemaphorePosix.cpp
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions src/concurrency/BinarySemaphorePosix.h
Original file line number Diff line number Diff line change
@@ -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

}
8 changes: 3 additions & 5 deletions src/concurrency/InterruptableDelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ namespace concurrency

InterruptableDelay::InterruptableDelay()
{
semaphore = xSemaphoreCreateBinary();
}

InterruptableDelay::~InterruptableDelay()
{
vSemaphoreDelete(semaphore);
}

/**
Expand All @@ -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;
Expand All @@ -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
11 changes: 10 additions & 1 deletion src/concurrency/InterruptableDelay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand All @@ -14,7 +23,7 @@ namespace concurrency
*/
class InterruptableDelay
{
SemaphoreHandle_t semaphore;
BinarySemaphore semaphore;

public:
InterruptableDelay();
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/RadioInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/mesh/RadioInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeshPacket> *rxDest = NULL;
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/RadioLibInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/RadioLibInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
8 changes: 8 additions & 0 deletions src/mesh/TypedQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ template <class T> class TypedQueue
template <class T> class TypedQueue
{
std::queue<T> q;
concurrency::OSThread *reader = NULL;

public:
TypedQueue(int maxElements) {}
Expand All @@ -83,6 +84,11 @@ template <class T> class TypedQueue

bool enqueue(T x, TickType_t maxWait = portMAX_DELAY)
{
if (reader) {
reader->setInterval(0);
concurrency::mainDelay.interrupt();
}

q.push(x);
return true;
}
Expand All @@ -101,5 +107,7 @@ template <class T> class TypedQueue
}

// bool dequeueFromISR(T *p, BaseType_t *higherPriWoken) { return xQueueReceiveFromISR(h, p, higherPriWoken); }

void setReader(concurrency::OSThread *t) { reader = t; }
};
#endif

0 comments on commit 999b292

Please sign in to comment.