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-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-05-27 21:27:31 +02:00
|
|
|
// Initiated semaphore must be given before it can be taken.
|
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-05-27 21:27:31 +02:00
|
|
|
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
|
2020-06-02 22:19:48 +02:00
|
|
|
TickType_t timeout = SemaphoreIF::POLLING;
|
|
|
|
if(timeoutMs == SemaphoreIF::BLOCKING) {
|
|
|
|
timeout = SemaphoreIF::BLOCKING;
|
2020-04-08 18:08:14 +02:00
|
|
|
}
|
2020-06-02 22:19:48 +02:00
|
|
|
else if(timeoutMs > SemaphoreIF::POLLING){
|
2020-04-08 18:08:14 +02:00
|
|
|
timeout = pdMS_TO_TICKS(timeoutMs);
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
2020-05-27 22:12:52 +02:00
|
|
|
return acquireWithTickTimeout(timeout);
|
2020-02-25 17:04:21 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 22:12:52 +02:00
|
|
|
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TickType_t timeoutTicks) {
|
2020-03-26 19:53:05 +01:00
|
|
|
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;
|
2020-05-27 22:12:52 +02:00
|
|
|
}
|
|
|
|
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;
|
2020-05-27 22:12:52 +02:00
|
|
|
}
|
|
|
|
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-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-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) {
|
2020-03-26 19:53:05 +01:00
|
|
|
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-05-18 20:46:50 +02:00
|
|
|
}
|