fsfw/src/fsfw/osal/freertos/BinSemaphUsingTask.cpp

97 lines
3.3 KiB
C++
Raw Normal View History

2021-07-14 00:54:39 +02:00
#include "fsfw/osal/freertos/BinSemaphUsingTask.h"
2022-02-02 10:29:30 +01:00
2021-07-14 00:54:39 +02:00
#include "fsfw/osal/freertos/TaskManagement.h"
#include "fsfw/serviceinterface/ServiceInterface.h"
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
#if (tskKERNEL_VERSION_MAJOR == 8 && tskKERNEL_VERSION_MINOR > 2) || tskKERNEL_VERSION_MAJOR > 8
2020-12-14 23:56:44 +01:00
2020-09-18 13:15:14 +02:00
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
2022-02-02 10:29:30 +01:00
handle = TaskManagement::getCurrentTaskHandle();
if (handle == nullptr) {
2021-01-03 14:16:52 +01:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "Could not retrieve task handle. Please ensure the"
"constructor was called inside a task."
<< std::endl;
#endif
2022-02-02 10:29:30 +01:00
}
xTaskNotifyGive(handle);
2020-09-18 13:15:14 +02:00
}
BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
2022-02-02 10:29:30 +01:00
// Clear notification value on destruction.
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
2020-09-18 13:15:14 +02:00
}
2020-12-14 11:17:22 +01:00
void BinarySemaphoreUsingTask::refreshTaskHandle() {
2022-02-02 10:29:30 +01:00
handle = TaskManagement::getCurrentTaskHandle();
2020-12-14 11:17:22 +01:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t BinarySemaphoreUsingTask::acquire(TimeoutType timeoutType, uint32_t timeoutMs) {
TickType_t timeout = 0;
if (timeoutType == TimeoutType::POLLING) {
timeout = 0;
} else if (timeoutType == TimeoutType::WAITING) {
timeout = pdMS_TO_TICKS(timeoutMs);
} else {
timeout = portMAX_DELAY;
}
return acquireWithTickTimeout(timeoutType, timeout);
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout(TimeoutType timeoutType,
TickType_t timeoutTicks) {
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
if (returncode == pdPASS) {
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
} else {
return SemaphoreIF::SEMAPHORE_TIMEOUT;
}
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t BinarySemaphoreUsingTask::release() { return release(this->handle); }
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
ReturnValue_t BinarySemaphoreUsingTask::release(TaskHandle_t taskHandle) {
if (getSemaphoreCounter(taskHandle) == 1) {
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
}
BaseType_t returncode = xTaskNotifyGive(taskHandle);
if (returncode == pdPASS) {
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
} else {
// This should never happen.
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() { return handle; }
2020-09-18 13:15:14 +02:00
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const {
2022-02-02 10:29:30 +01:00
return getSemaphoreCounter(this->handle);
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter(TaskHandle_t taskHandle) {
uint32_t notificationValue;
xTaskNotifyAndQuery(taskHandle, 0, eNoAction, &notificationValue);
return notificationValue;
2020-09-18 13:15:14 +02:00
}
// Be careful with the stack size here. This is called from an ISR!
2022-02-02 10:29:30 +01:00
ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR(TaskHandle_t taskHandle,
BaseType_t* higherPriorityTaskWoken) {
if (getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) {
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
}
vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken);
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR(TaskHandle_t taskHandle,
BaseType_t* higherPriorityTaskWoken) {
uint32_t notificationValue = 0;
xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, &notificationValue, higherPriorityTaskWoken);
return notificationValue;
2020-09-18 13:15:14 +02:00
}
2020-12-14 23:56:44 +01:00
#endif /* (tskKERNEL_VERSION_MAJOR == 8 && tskKERNEL_VERSION_MINOR > 2) || \
tskKERNEL_VERSION_MAJOR > 8 */