2020-02-25 17:04:21 +01:00
|
|
|
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
|
2020-02-28 22:55:25 +01:00
|
|
|
#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-05-27 00:28:13 +02:00
|
|
|
const uint32_t SemaphoreIF::NO_TIMEOUT = 0;
|
|
|
|
|
2020-02-25 17:04:21 +01:00
|
|
|
BinarySemaphore::BinarySemaphore() {
|
2020-04-23 17:54:41 +02:00
|
|
|
handle = xSemaphoreCreateBinary();
|
2020-05-18 20:46:50 +02:00
|
|
|
if(handle == nullptr) {
|
|
|
|
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
2020-04-23 17:54:41 +02:00
|
|
|
xSemaphoreGive(handle);
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
2020-02-25 12:54:28 +01:00
|
|
|
|
2020-02-25 17:04:21 +01:00
|
|
|
BinarySemaphore::~BinarySemaphore() {
|
|
|
|
vSemaphoreDelete(handle);
|
|
|
|
}
|
2020-02-25 12:54:28 +01:00
|
|
|
|
2020-04-08 18:08:14 +02:00
|
|
|
BinarySemaphore::BinarySemaphore(BinarySemaphore&& s) {
|
2020-04-23 17:54:41 +02:00
|
|
|
handle = xSemaphoreCreateBinary();
|
2020-04-08 18:08:14 +02:00
|
|
|
if(handle == nullptr) {
|
2020-04-23 20:04:48 +02:00
|
|
|
sif::error << "Binary semaphore creation failure" << std::endl;
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
2020-04-23 17:54:41 +02:00
|
|
|
xSemaphoreGive(handle);
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BinarySemaphore& BinarySemaphore::operator =(
|
|
|
|
BinarySemaphore&& s) {
|
|
|
|
if(&s != this) {
|
2020-04-23 17:54:41 +02:00
|
|
|
handle = xSemaphoreCreateBinary();
|
2020-04-08 18:08:14 +02:00
|
|
|
if(handle == nullptr) {
|
2020-04-23 20:04:48 +02:00
|
|
|
sif::error << "Binary semaphore creation failure" << std::endl;
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
2020-04-23 17:54:41 +02:00
|
|
|
xSemaphoreGive(handle);
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-25 17:04:21 +01:00
|
|
|
ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) {
|
2020-03-26 19:53:05 +01:00
|
|
|
if(handle == nullptr) {
|
2020-04-08 18:08:14 +02:00
|
|
|
return SEMAPHORE_NULLPOINTER;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
2020-04-08 18:08:14 +02:00
|
|
|
TickType_t timeout = BinarySemaphore::NO_BLOCK_TICKS;
|
|
|
|
if(timeoutMs == BinarySemaphore::BLOCK_TIMEOUT) {
|
|
|
|
timeout = BinarySemaphore::BLOCK_TIMEOUT_TICKS;
|
|
|
|
}
|
|
|
|
else if(timeoutMs > BinarySemaphore::NO_BLOCK_TIMEOUT){
|
|
|
|
timeout = pdMS_TO_TICKS(timeoutMs);
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
2020-02-25 12:54:28 +01:00
|
|
|
|
2020-02-25 17:04:21 +01:00
|
|
|
BaseType_t returncode = xSemaphoreTake(handle, timeout);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return SEMAPHORE_TIMEOUT;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 18:08:14 +02:00
|
|
|
ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout(
|
|
|
|
TickType_t timeoutTicks) {
|
2020-03-26 19:53:05 +01:00
|
|
|
if(handle == nullptr) {
|
2020-04-08 18:08:14 +02:00
|
|
|
return SEMAPHORE_NULLPOINTER;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BaseType_t returncode = xSemaphoreTake(handle, timeoutTicks);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return SEMAPHORE_TIMEOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t BinarySemaphore::giveBinarySemaphore() {
|
2020-03-26 19:53:05 +01:00
|
|
|
if (handle == nullptr) {
|
2020-04-08 18:08:14 +02:00
|
|
|
return SEMAPHORE_NULLPOINTER;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
|
|
|
BaseType_t returncode = xSemaphoreGive(handle);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return SEMAPHORE_NOT_OWNED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SemaphoreHandle_t BinarySemaphore::getSemaphore() {
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:55:25 +01:00
|
|
|
ReturnValue_t BinarySemaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore) {
|
2020-03-26 19:53:05 +01:00
|
|
|
if (semaphore == nullptr) {
|
2020-04-08 18:08:14 +02:00
|
|
|
return SEMAPHORE_NULLPOINTER;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
|
|
|
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() {
|
2020-03-26 19:53:05 +01:00
|
|
|
if(handle != nullptr) {
|
|
|
|
vSemaphoreDelete(handle);
|
2020-04-23 17:54:41 +02:00
|
|
|
handle = xSemaphoreCreateBinary();
|
|
|
|
xSemaphoreGive(handle);
|
2020-03-26 19:53:05 +01:00
|
|
|
}
|
2020-03-06 15:39:42 +01:00
|
|
|
}
|
2020-05-18 20:46:50 +02:00
|
|
|
|
2020-05-18 20:39:48 +02:00
|
|
|
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
|
2020-05-18 19:38:02 +02:00
|
|
|
return takeBinarySemaphore(timeoutMs);
|
|
|
|
}
|
2020-03-06 15:39:42 +01:00
|
|
|
|
2020-05-18 20:39:48 +02:00
|
|
|
ReturnValue_t BinarySemaphore::release() {
|
2020-05-18 19:38:02 +02:00
|
|
|
return giveBinarySemaphore();
|
|
|
|
}
|
|
|
|
|
2020-05-18 20:39:48 +02:00
|
|
|
uint8_t BinarySemaphore::getSemaphoreCounter() {
|
2020-05-18 19:38:02 +02:00
|
|
|
return uxSemaphoreGetCount(handle);
|
2020-05-18 20:46:50 +02:00
|
|
|
}
|
2020-04-08 18:08:14 +02:00
|
|
|
|
2020-04-06 19:51:45 +02:00
|
|
|
// Be careful with the stack size here. This is called from an ISR!
|
2020-02-28 22:55:25 +01:00
|
|
|
ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore,
|
2020-02-25 17:04:21 +01:00
|
|
|
BaseType_t * higherPriorityTaskWoken) {
|
2020-03-26 19:53:05 +01:00
|
|
|
if (semaphore == nullptr) {
|
2020-04-08 18:08:14 +02:00
|
|
|
return SEMAPHORE_NULLPOINTER;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
|
|
|
BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, higherPriorityTaskWoken);
|
|
|
|
if (returncode == pdPASS) {
|
|
|
|
if(*higherPriorityTaskWoken == pdPASS) {
|
2020-04-10 17:06:06 +02:00
|
|
|
// Request context switch because unblocking the semaphore
|
|
|
|
// caused a high priority task unblock.
|
2020-03-02 01:00:17 +01:00
|
|
|
TaskManagement::requestContextSwitch(CallContext::isr);
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
2020-04-08 18:08:14 +02:00
|
|
|
return SEMAPHORE_NOT_OWNED;
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
2020-05-18 20:46:50 +02:00
|
|
|
}
|