From 908a6becba0ee226460fb27bbe9b793867a81dad Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Thu, 30 Mar 2023 17:48:53 +0200 Subject: [PATCH] Implement BinarySemaphore freertos wrapper --- .../Firmware/src/FreeRTOS/BinarySemaphore.cpp | 26 +++++++++++++++++++ .../Firmware/src/FreeRTOS/BinarySemaphore.h | 20 ++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 modules/control/Firmware/src/FreeRTOS/BinarySemaphore.cpp create mode 100644 modules/control/Firmware/src/FreeRTOS/BinarySemaphore.h diff --git a/modules/control/Firmware/src/FreeRTOS/BinarySemaphore.cpp b/modules/control/Firmware/src/FreeRTOS/BinarySemaphore.cpp new file mode 100644 index 0000000..96464ae --- /dev/null +++ b/modules/control/Firmware/src/FreeRTOS/BinarySemaphore.cpp @@ -0,0 +1,26 @@ +#include +#include + +freertos::BinarySemaphore::BinarySemaphore() + : handle{xSemaphoreCreateBinary()} {} +freertos::BinarySemaphore::BinarySemaphore(BinarySemaphore &&other) noexcept { + handle = etl::move(other.handle); +} +freertos::BinarySemaphore::~BinarySemaphore() { vSemaphoreDelete(handle); } +auto freertos::BinarySemaphore::operator=( + const BinarySemaphore &&other) noexcept -> BinarySemaphore & { + handle = etl::move(other.handle); + return *this; +} +auto freertos::BinarySemaphore::take(TickType_t timeout) -> bool { + BaseType_t success = xSemaphoreTake(handle, timeout); + return success == pdTRUE ? true : false; +} +auto freertos::BinarySemaphore::give() -> bool { + BaseType_t success = xSemaphoreGive(handle); + return success == pdTRUE ? true : false; +} + +auto freertos::BinarySemaphore::getCount() -> size_t { + return uxSemaphoreGetCount(handle); +} \ No newline at end of file diff --git a/modules/control/Firmware/src/FreeRTOS/BinarySemaphore.h b/modules/control/Firmware/src/FreeRTOS/BinarySemaphore.h new file mode 100644 index 0000000..08fb38d --- /dev/null +++ b/modules/control/Firmware/src/FreeRTOS/BinarySemaphore.h @@ -0,0 +1,20 @@ +#pragma once +#include + +namespace freertos { +class BinarySemaphore { +public: + BinarySemaphore(); + BinarySemaphore(const BinarySemaphore &other) = delete; + BinarySemaphore(BinarySemaphore &&other) noexcept; + ~BinarySemaphore(); + BinarySemaphore &operator=(const BinarySemaphore &other) = delete; + BinarySemaphore &operator=(const BinarySemaphore &&other) noexcept; + bool take(TickType_t timeout = portMAX_DELAY); + bool give(); + size_t getCount(); + +private: + SemaphoreHandle_t handle; +}; +} // namespace freertos