fsfw/osal/FreeRTOS/BinarySemaphore.cpp

109 lines
2.9 KiB
C++
Raw Normal View History

2020-08-18 13:09:15 +02:00
#include "../../osal/FreeRTOS/BinarySemaphore.h"
#include "../../osal/FreeRTOS/TaskManagement.h"
#include "../../serviceinterface/ServiceInterfaceStream.h"
2020-04-22 19:42:42 +02:00
2020-02-25 17:04:21 +01:00
BinarySemaphore::BinarySemaphore() {
handle = xSemaphoreCreateBinary();
2020-08-18 13:09:15 +02:00
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-08-27 15:52:55 +02:00
ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType,
uint32_t timeoutMs) {
TickType_t timeout = 0;
if(timeoutType == TimeoutType::POLLING) {
timeout = 0;
}
2020-08-27 15:52:55 +02:00
else if(timeoutType == TimeoutType::WAITING){
timeout = pdMS_TO_TICKS(timeoutMs);
2020-02-25 17:04:21 +01:00
}
2020-08-27 15:52:55 +02:00
else {
timeout = portMAX_DELAY;
}
return acquireWithTickTimeout(timeoutType, timeout);
2020-02-25 17:04:21 +01:00
}
2020-08-27 15:52:55 +02:00
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TimeoutType timeoutType,
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-08-18 13:09:15 +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
}
2020-08-18 13:09:15 +02:00
}