fsfw/osal/FreeRTOS/BinarySemaphore.cpp

104 lines
2.8 KiB
C++
Raw Normal View History

2020-02-25 17:04:21 +01:00
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
#include <framework/osal/FreeRTOS/TaskManagement.h>
2020-02-25 17:04:21 +01:00
#include <framework/serviceinterface/ServiceInterfaceStream.h>
2020-04-22 19:42:42 +02:00
2020-02-25 17:04:21 +01:00
BinarySemaphore::BinarySemaphore() {
handle = xSemaphoreCreateBinary();
if(handle == nullptr) {
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
2020-02-25 17:04:21 +01:00
}
2020-05-27 21:27:31 +02:00
// Initiated semaphore must be given before it can be taken.
xSemaphoreGive(handle);
2020-02-25 17:04:21 +01:00
}
2020-02-25 17:04:21 +01:00
BinarySemaphore::~BinarySemaphore() {
vSemaphoreDelete(handle);
}
BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) {
handle = xSemaphoreCreateBinary();
if(handle == nullptr) {
2020-04-23 20:04:48 +02:00
sif::error << "Binary semaphore creation failure" << std::endl;
}
xSemaphoreGive(handle);
}
BinarySemaphore& BinarySemaphore::operator =(
BinarySemaphore&& s) {
if(&s != this) {
handle = xSemaphoreCreateBinary();
if(handle == nullptr) {
2020-04-23 20:04:48 +02:00
sif::error << "Binary semaphore creation failure" << std::endl;
}
xSemaphoreGive(handle);
}
return *this;
}
2020-05-27 21:27:31 +02:00
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
TickType_t timeout = SemaphoreIF::POLLING;
if(timeoutMs == SemaphoreIF::BLOCKING) {
timeout = SemaphoreIF::BLOCKING;
}
else if(timeoutMs > SemaphoreIF::POLLING){
timeout = pdMS_TO_TICKS(timeoutMs);
2020-02-25 17:04:21 +01:00
}
return acquireWithTickTimeout(timeout);
2020-02-25 17:04:21 +01:00
}
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TickType_t timeoutTicks) {
if(handle == nullptr) {
2020-05-29 03:03:48 +02:00
return SemaphoreIF::SEMAPHORE_INVALID;
2020-02-25 17:04:21 +01:00
}
BaseType_t returncode = xSemaphoreTake(handle, timeoutTicks);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
2020-05-27 21:30:20 +02:00
return SemaphoreIF::SEMAPHORE_TIMEOUT;
2020-02-25 17:04:21 +01:00
}
}
2020-05-27 21:27:31 +02:00
ReturnValue_t BinarySemaphore::release() {
2020-05-29 03:03:48 +02:00
return release(handle);
}
ReturnValue_t BinarySemaphore::release(SemaphoreHandle_t semaphore) {
if (semaphore == nullptr) {
return SemaphoreIF::SEMAPHORE_INVALID;
2020-02-25 17:04:21 +01:00
}
2020-05-29 03:03:48 +02:00
BaseType_t returncode = xSemaphoreGive(semaphore);
2020-02-25 17:04:21 +01:00
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
2020-02-25 17:04:21 +01:00
}
}
2020-05-27 21:33:34 +02:00
uint8_t BinarySemaphore::getSemaphoreCounter() const {
2020-05-27 21:30:20 +02:00
return uxSemaphoreGetCount(handle);
}
2020-02-25 17:04:21 +01:00
SemaphoreHandle_t BinarySemaphore::getSemaphore() {
return handle;
}
2020-05-29 03:03:48 +02:00
2020-04-06 19:51:45 +02:00
// Be careful with the stack size here. This is called from an ISR!
2020-05-27 23:41:59 +02:00
ReturnValue_t BinarySemaphore::releaseFromISR(
2020-05-27 21:27:31 +02:00
SemaphoreHandle_t semaphore, BaseType_t * higherPriorityTaskWoken) {
if (semaphore == nullptr) {
2020-05-29 03:03:48 +02:00
return SemaphoreIF::SEMAPHORE_INVALID;
2020-02-25 17:04:21 +01:00
}
2020-05-27 21:27:31 +02:00
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore,
higherPriorityTaskWoken);
2020-02-25 17:04:21 +01:00
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
2020-05-27 21:30:20 +02:00
}
else {
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
2020-02-25 17:04:21 +01:00
}
}