diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 000000000..a866d56a9 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,20 @@ +#include "debug.h" + +#include + +#include +#include + +#include "configuration.h" + +namespace meshtastic +{ + +void printThreadInfo(const char *extra) +{ + uint32_t taskHandle = reinterpret_cast(xTaskGetCurrentTaskHandle()); + DEBUG_MSG("printThreadInfo(%s) task: %" PRIx32 " core id: %u min free stack: %u\n", extra, taskHandle, xPortGetCoreID(), + uxTaskGetStackHighWaterMark(nullptr)); +} + +} // namespace meshtastic diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 000000000..74a1875e4 --- /dev/null +++ b/src/debug.h @@ -0,0 +1,10 @@ +#pragma once + +namespace meshtastic +{ + +/// Dumps out which core we are running on, and min level of remaining stack +/// seen. +void printThreadInfo(const char *extra); + +} // namespace meshtastic diff --git a/src/lock.cpp b/src/lock.cpp new file mode 100644 index 000000000..8041799ea --- /dev/null +++ b/src/lock.cpp @@ -0,0 +1,35 @@ +#include "lock.h" + +#include + +namespace meshtastic +{ + +Lock::Lock() +{ + handle = xSemaphoreCreateBinary(); + assert(handle); + assert(xSemaphoreGive(handle)); +} + +void Lock::lock() +{ + assert(xSemaphoreTake(handle, portMAX_DELAY)); +} + +void Lock::unlock() +{ + assert(xSemaphoreGive(handle)); +} + +LockGuard::LockGuard(Lock *lock) : lock(lock) +{ + lock->lock(); +} + +LockGuard::~LockGuard() +{ + lock->unlock(); +} + +} // namespace meshtastic diff --git a/src/lock.h b/src/lock.h new file mode 100644 index 000000000..bffaeee37 --- /dev/null +++ b/src/lock.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include + +namespace meshtastic +{ + +// Simple wrapper around FreeRTOS API for implementing a mutex lock. +class Lock +{ + public: + Lock(); + + Lock(const Lock&) = delete; + Lock& operator=(const Lock&) = delete; + + /// Locks the lock. + // + // Must not be called from an ISR. + void lock(); + + // Unlocks the lock. + // + // Must not be called from an ISR. + void unlock(); + + private: + SemaphoreHandle_t handle; +}; + +// RAII lock guard. +class LockGuard +{ + public: + LockGuard(Lock *lock); + ~LockGuard(); + + LockGuard(const LockGuard&) = delete; + LockGuard& operator=(const LockGuard&) = delete; + + private: + Lock* lock; +}; + + +} // namespace meshtastic