fsfw/osal/FreeRTOS/BinarySemaphore.cpp

103 lines
2.6 KiB
C++
Raw Normal View History

/**
* @file BinarySemaphore.cpp
*
* @date 25.02.2020
*/
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>
#include "portmacro.h"
#include "task.h"
2020-02-25 17:04:21 +01:00
BinarySemaphore::BinarySemaphore() {
vSemaphoreCreateBinary(handle);
if(handle == NULL) {
error << "Binary semaphore creation failure" << std::endl;
}
}
2020-02-25 17:04:21 +01:00
BinarySemaphore::~BinarySemaphore() {
vSemaphoreDelete(handle);
}
2020-02-25 17:04:21 +01:00
ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) {
if(handle == NULL) {
return HasReturnvaluesIF::RETURN_FAILED;
}
TickType_t timeout = portMAX_DELAY;
if(timeoutMs != 0) {
timeout = pdMS_TO_TICKS(timeoutMs);
}
2020-02-25 17:04:21 +01:00
BaseType_t returncode = xSemaphoreTake(handle, timeout);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_NOT_FOUND;
}
}
ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout(TickType_t timeoutTicks) {
if(handle == NULL) {
return SEMAPHORE_NOT_FOUND;
}
BaseType_t returncode = xSemaphoreTake(handle, timeoutTicks);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_TIMEOUT;
}
}
ReturnValue_t BinarySemaphore::giveBinarySemaphore() {
if (handle == NULL) {
return HasReturnvaluesIF::RETURN_FAILED;
}
BaseType_t returncode = xSemaphoreGive(handle);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_NOT_OWNED;
}
}
SemaphoreHandle_t BinarySemaphore::getSemaphore() {
return handle;
}
ReturnValue_t BinarySemaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore) {
2020-02-25 17:04:21 +01:00
if (semaphore == NULL) {
return HasReturnvaluesIF::RETURN_FAILED;
}
BaseType_t returncode = xSemaphoreGive(semaphore);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return HasReturnvaluesIF::RETURN_FAILED;
}
}
2020-03-06 15:39:42 +01:00
void BinarySemaphore::resetSemaphore() {
vSemaphoreDelete(handle);
vSemaphoreCreateBinary(handle);
}
ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore,
2020-02-25 17:04:21 +01:00
BaseType_t * higherPriorityTaskWoken) {
if (semaphore == NULL) {
return HasReturnvaluesIF::RETURN_FAILED;
}
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, higherPriorityTaskWoken);
if (returncode == pdPASS) {
if(*higherPriorityTaskWoken == pdPASS) {
// Request context switch
TaskManagement::requestContextSwitch(CallContext::isr);
2020-02-25 17:04:21 +01:00
}
return HasReturnvaluesIF::RETURN_OK;
} else {
return HasReturnvaluesIF::RETURN_FAILED;
}
}