Semaphore Initialization - Linux and FreeRTOS #89

Merged
gaisser merged 16 commits from KSat/fsfw:mueller_binSemaphoreInit into master 2020-08-28 13:25:07 +02:00
10 changed files with 616 additions and 606 deletions
Showing only changes of commit aacda3afc2 - Show all commits

View File

@ -1,91 +1,95 @@
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h> #include "../../osal/FreeRTOS/BinSemaphUsingTask.h"
#include <framework/osal/FreeRTOS/TaskManagement.h> #include "../../osal/FreeRTOS/TaskManagement.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() { BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
handle = TaskManagement::getCurrentTaskHandle(); handle = TaskManagement::getCurrentTaskHandle();
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "Could not retrieve task handle. Please ensure the" sif::error << "Could not retrieve task handle. Please ensure the"
"constructor was called inside a task." << std::endl; "constructor was called inside a task." << std::endl;
} }
xTaskNotifyGive(handle); xTaskNotifyGive(handle);
} }
BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() { BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
// Clear notification value on destruction. // Clear notification value on destruction.
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr); xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
} }
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphoreUsingTask::acquire(TimeoutType timeoutType,
TickType_t timeout = SemaphoreIF::POLLING; uint32_t timeoutMs) {
if(timeoutMs == SemaphoreIF::BLOCKING) { TickType_t timeout = 0;
timeout = SemaphoreIF::BLOCKING; if(timeoutType == TimeoutType::POLLING) {
} timeout = 0;
else if(timeoutMs > SemaphoreIF::POLLING){ }
timeout = pdMS_TO_TICKS(timeoutMs); else if(timeoutType == TimeoutType::WAITING){
} timeout = pdMS_TO_TICKS(timeoutMs);
return acquireWithTickTimeout(timeout); }
} else {
timeout = portMAX_DELAY;
ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout( }
TickType_t timeoutTicks) { return acquireWithTickTimeout(timeoutType, timeout);
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks); }
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK; ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout(
} TimeoutType timeoutType, TickType_t timeoutTicks) {
else { BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
return SemaphoreIF::SEMAPHORE_TIMEOUT; if (returncode == pdPASS) {
} return HasReturnvaluesIF::RETURN_OK;
} }
else {
ReturnValue_t BinarySemaphoreUsingTask::release() { return SemaphoreIF::SEMAPHORE_TIMEOUT;
return release(this->handle); }
} }
ReturnValue_t BinarySemaphoreUsingTask::release( ReturnValue_t BinarySemaphoreUsingTask::release() {
TaskHandle_t taskHandle) { return release(this->handle);
if(getSemaphoreCounter(taskHandle) == 1) { }
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
} ReturnValue_t BinarySemaphoreUsingTask::release(
BaseType_t returncode = xTaskNotifyGive(taskHandle); TaskHandle_t taskHandle) {
if (returncode == pdPASS) { if(getSemaphoreCounter(taskHandle) == 1) {
return HasReturnvaluesIF::RETURN_OK; return SemaphoreIF::SEMAPHORE_NOT_OWNED;
} }
else { BaseType_t returncode = xTaskNotifyGive(taskHandle);
// This should never happen. if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_OK;
} }
} else {
// This should never happen.
TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() { return HasReturnvaluesIF::RETURN_FAILED;
return handle; }
} }
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const { TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() {
return getSemaphoreCounter(this->handle); return handle;
} }
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter( uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const {
TaskHandle_t taskHandle) { return getSemaphoreCounter(this->handle);
uint32_t notificationValue; }
xTaskNotifyAndQuery(taskHandle, 0, eNoAction, &notificationValue);
return notificationValue; uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter(
} TaskHandle_t taskHandle) {
uint32_t notificationValue;
// Be careful with the stack size here. This is called from an ISR! xTaskNotifyAndQuery(taskHandle, 0, eNoAction, &notificationValue);
ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR( return notificationValue;
TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) { }
if(getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) {
return SemaphoreIF::SEMAPHORE_NOT_OWNED; // Be careful with the stack size here. This is called from an ISR!
} ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR(
vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken); TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) {
return HasReturnvaluesIF::RETURN_OK; if(getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) {
} return SemaphoreIF::SEMAPHORE_NOT_OWNED;
}
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR( vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken);
TaskHandle_t taskHandle, BaseType_t* higherPriorityTaskWoken) { return HasReturnvaluesIF::RETURN_OK;
uint32_t notificationValue = 0; }
xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, &notificationValue,
higherPriorityTaskWoken); uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR(
return notificationValue; TaskHandle_t taskHandle, BaseType_t* higherPriorityTaskWoken) {
} uint32_t notificationValue = 0;
xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, &notificationValue,
higherPriorityTaskWoken);
return notificationValue;
}

View File

@ -1,75 +1,76 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ #ifndef FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
#define FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ #define FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
#include <framework/returnvalues/HasReturnvaluesIF.h> #include "../../returnvalues/HasReturnvaluesIF.h"
#include <framework/tasks/SemaphoreIF.h> #include "../../tasks/SemaphoreIF.h"
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
/** /**
* @brief Binary Semaphore implementation using the task notification value. * @brief Binary Semaphore implementation using the task notification value.
* The notification value should therefore not be used * The notification value should therefore not be used
* for other purposes. * for other purposes.
* @details * @details
* Additional information: https://www.freertos.org/RTOS-task-notifications.html * Additional information: https://www.freertos.org/RTOS-task-notifications.html
* and general semaphore documentation. * and general semaphore documentation.
*/ */
class BinarySemaphoreUsingTask: public SemaphoreIF, class BinarySemaphoreUsingTask: public SemaphoreIF,
public HasReturnvaluesIF { public HasReturnvaluesIF {
public: public:
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF; static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
//! @brief Default ctor //! @brief Default ctor
BinarySemaphoreUsingTask(); BinarySemaphoreUsingTask();
//! @brief Default dtor //! @brief Default dtor
virtual~ BinarySemaphoreUsingTask(); virtual~ BinarySemaphoreUsingTask();
ReturnValue_t acquire(uint32_t timeoutMs = ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
SemaphoreIF::BLOCKING) override; uint32_t timeoutMs = portMAX_DELAY) override;
ReturnValue_t release() override; ReturnValue_t release() override;
uint8_t getSemaphoreCounter() const override; uint8_t getSemaphoreCounter() const override;
static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle); static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle);
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t taskHandle, static uint8_t getSemaphoreCounterFromISR(TaskHandle_t taskHandle,
BaseType_t* higherPriorityTaskWoken); BaseType_t* higherPriorityTaskWoken);
/** /**
* Same as acquire() with timeout in FreeRTOS ticks. * Same as acquire() with timeout in FreeRTOS ticks.
* @param timeoutTicks * @param timeoutTicks
* @return - @c RETURN_OK on success * @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure * - @c RETURN_FAILED on failure
*/ */
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks = ReturnValue_t acquireWithTickTimeout(
SemaphoreIF::BLOCKING); TimeoutType timeoutType = TimeoutType::BLOCKING,
TickType_t timeoutTicks = portMAX_DELAY);
/**
* Get handle to the task related to the semaphore. /**
* @return * Get handle to the task related to the semaphore.
*/ * @return
TaskHandle_t getTaskHandle(); */
TaskHandle_t getTaskHandle();
/**
* Wrapper function to give back semaphore from handle /**
* @param semaphore * Wrapper function to give back semaphore from handle
* @return - @c RETURN_OK on success * @param semaphore
* - @c RETURN_FAILED on failure * @return - @c RETURN_OK on success
*/ * - @c RETURN_FAILED on failure
static ReturnValue_t release(TaskHandle_t taskToNotify); */
static ReturnValue_t release(TaskHandle_t taskToNotify);
/**
* Wrapper function to give back semaphore from handle when called from an ISR /**
* @param semaphore * Wrapper function to give back semaphore from handle when called from an ISR
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with * @param semaphore
* a higher priority was unblocked. A context switch should be requested * @param higherPriorityTaskWoken This will be set to pdPASS if a task with
* from an ISR if this is the case (see TaskManagement functions) * a higher priority was unblocked. A context switch should be requested
* @return - @c RETURN_OK on success * from an ISR if this is the case (see TaskManagement functions)
* - @c RETURN_FAILED on failure * @return - @c RETURN_OK on success
*/ * - @c RETURN_FAILED on failure
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify, */
BaseType_t * higherPriorityTaskWoken); static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
BaseType_t * higherPriorityTaskWoken);
protected:
TaskHandle_t handle; protected:
}; TaskHandle_t handle;
};
#endif /* FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ */
#endif /* FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ */

View File

@ -1,11 +1,11 @@
#include <framework/osal/FreeRTOS/BinarySemaphore.h> #include "../../osal/FreeRTOS/BinarySemaphore.h"
#include <framework/osal/FreeRTOS/TaskManagement.h> #include "../../osal/FreeRTOS/TaskManagement.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
BinarySemaphore::BinarySemaphore() { BinarySemaphore::BinarySemaphore() {
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "Semaphore: Binary semaph creation failure" << std::endl; sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
} }
// Initiated semaphore must be given before it can be taken. // Initiated semaphore must be given before it can be taken.
xSemaphoreGive(handle); xSemaphoreGive(handle);
@ -35,18 +35,23 @@ BinarySemaphore& BinarySemaphore::operator =(
return *this; return *this;
} }
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphore::acquire(TimeoutType timeoutType,
TickType_t timeout = SemaphoreIF::POLLING; uint32_t timeoutMs) {
if(timeoutMs == SemaphoreIF::BLOCKING) { TickType_t timeout = 0;
timeout = SemaphoreIF::BLOCKING; if(timeoutType == TimeoutType::POLLING) {
timeout = 0;
} }
else if(timeoutMs > SemaphoreIF::POLLING){ else if(timeoutType == TimeoutType::WAITING){
timeout = pdMS_TO_TICKS(timeoutMs); timeout = pdMS_TO_TICKS(timeoutMs);
} }
return acquireWithTickTimeout(timeout); else {
timeout = portMAX_DELAY;
}
return acquireWithTickTimeout(timeoutType, timeout);
} }
ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TickType_t timeoutTicks) { ReturnValue_t BinarySemaphore::acquireWithTickTimeout(TimeoutType timeoutType,
TickType_t timeoutTicks) {
if(handle == nullptr) { if(handle == nullptr) {
return SemaphoreIF::SEMAPHORE_INVALID; return SemaphoreIF::SEMAPHORE_INVALID;
} }
@ -84,7 +89,7 @@ uint8_t BinarySemaphore::getSemaphoreCounter() const {
SemaphoreHandle_t BinarySemaphore::getSemaphore() { SemaphoreHandle_t BinarySemaphore::getSemaphore() {
return handle; return handle;
} }
// Be careful with the stack size here. This is called from an ISR! // Be careful with the stack size here. This is called from an ISR!
ReturnValue_t BinarySemaphore::releaseFromISR( ReturnValue_t BinarySemaphore::releaseFromISR(
@ -100,4 +105,4 @@ ReturnValue_t BinarySemaphore::releaseFromISR(
else { else {
return SemaphoreIF::SEMAPHORE_NOT_OWNED; return SemaphoreIF::SEMAPHORE_NOT_OWNED;
} }
} }

View File

@ -1,8 +1,8 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ #ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ #define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#include <framework/returnvalues/HasReturnvaluesIF.h> #include "../../returnvalues/HasReturnvaluesIF.h"
#include <framework/tasks/SemaphoreIF.h> #include "../../tasks/SemaphoreIF.h"
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/semphr.h> #include <freertos/semphr.h>
@ -52,8 +52,8 @@ public:
* @return -@c RETURN_OK on success * @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/ */
ReturnValue_t acquire(uint32_t timeoutMs = ReturnValue_t acquire(TimeoutType timeoutType =
SemaphoreIF::BLOCKING) override; TimeoutType::BLOCKING, uint32_t timeoutMs = portMAX_DELAY) override;
/** /**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks. * Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
@ -61,8 +61,8 @@ public:
* @return -@c RETURN_OK on success * @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/ */
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks = ReturnValue_t acquireWithTickTimeout(TimeoutType timeoutType =
SemaphoreIF::BLOCKING); TimeoutType::BLOCKING, TickType_t timeoutTicks = portMAX_DELAY);
/** /**
* Release the binary semaphore. * Release the binary semaphore.

View File

@ -1,110 +1,114 @@
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h> #include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
#include <framework/osal/FreeRTOS/TaskManagement.h> #include "../../osal/FreeRTOS/TaskManagement.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount, CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount,
uint8_t initCount): maxCount(maxCount) { uint8_t initCount): maxCount(maxCount) {
if(initCount > maxCount) { if(initCount > maxCount) {
sif::error << "CountingSemaphoreUsingTask: Max count bigger than " sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
"intial cout. Setting initial count to max count." << std::endl; "intial cout. Setting initial count to max count." << std::endl;
initCount = maxCount; initCount = maxCount;
} }
handle = TaskManagement::getCurrentTaskHandle(); handle = TaskManagement::getCurrentTaskHandle();
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "CountingSemaphoreUsingTask: Could not retrieve task " sif::error << "CountingSemaphoreUsingTask: Could not retrieve task "
"handle. Please ensure the constructor was called inside a " "handle. Please ensure the constructor was called inside a "
"task." << std::endl; "task." << std::endl;
} }
uint32_t oldNotificationValue; uint32_t oldNotificationValue;
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite,
&oldNotificationValue); &oldNotificationValue);
if(oldNotificationValue != 0) { if(oldNotificationValue != 0) {
sif::warning << "CountinSemaphoreUsingTask: Semaphore initiated but " sif::warning << "CountinSemaphoreUsingTask: Semaphore initiated but "
"current notification value is not 0. Please ensure the " "current notification value is not 0. Please ensure the "
"notification value is not used for other purposes!" << std::endl; "notification value is not used for other purposes!" << std::endl;
} }
for(int i = 0; i < initCount; i++) { for(int i = 0; i < initCount; i++) {
xTaskNotifyGive(handle); xTaskNotifyGive(handle);
} }
} }
CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() { CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
// Clear notification value on destruction. // Clear notification value on destruction.
// If this is not desired, don't call the destructor // If this is not desired, don't call the destructor
// (or implement a boolean which disables the reset) // (or implement a boolean which disables the reset)
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr); xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
} }
ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) { ReturnValue_t CountingSemaphoreUsingTask::acquire(TimeoutType timeoutType,
TickType_t timeout = SemaphoreIF::POLLING; uint32_t timeoutMs) {
if(timeoutMs == SemaphoreIF::BLOCKING) { TickType_t timeout = 0;
timeout = SemaphoreIF::BLOCKING; if(timeoutType == TimeoutType::POLLING) {
} timeout = 0;
else if(timeoutMs > SemaphoreIF::POLLING){ }
timeout = pdMS_TO_TICKS(timeoutMs); else if(timeoutType == TimeoutType::WAITING){
} timeout = pdMS_TO_TICKS(timeoutMs);
return acquireWithTickTimeout(timeout); }
else {
} timeout = portMAX_DELAY;
}
ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout( return acquireWithTickTimeout(timeoutType, timeout);
TickType_t timeoutTicks) {
// Decrement notfication value without resetting it. }
BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
if (getSemaphoreCounter() == oldCount - 1) { ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout(
return HasReturnvaluesIF::RETURN_OK; TimeoutType timeoutType, TickType_t timeoutTicks) {
} // Decrement notfication value without resetting it.
else { BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
return SemaphoreIF::SEMAPHORE_TIMEOUT; if (getSemaphoreCounter() == oldCount - 1) {
} return HasReturnvaluesIF::RETURN_OK;
} }
else {
ReturnValue_t CountingSemaphoreUsingTask::release() { return SemaphoreIF::SEMAPHORE_TIMEOUT;
if(getSemaphoreCounter() == maxCount) { }
return SemaphoreIF::SEMAPHORE_NOT_OWNED; }
}
return release(handle); ReturnValue_t CountingSemaphoreUsingTask::release() {
} if(getSemaphoreCounter() == maxCount) {
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
ReturnValue_t CountingSemaphoreUsingTask::release( }
TaskHandle_t taskToNotify) { return release(handle);
BaseType_t returncode = xTaskNotifyGive(taskToNotify); }
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK; ReturnValue_t CountingSemaphoreUsingTask::release(
} TaskHandle_t taskToNotify) {
else { BaseType_t returncode = xTaskNotifyGive(taskToNotify);
// This should never happen. if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_OK;
} }
} else {
// This should never happen.
return HasReturnvaluesIF::RETURN_FAILED;
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const { }
uint32_t notificationValue = 0; }
xTaskNotifyAndQuery(handle, 0, eNoAction, &notificationValue);
return notificationValue;
} uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const {
uint32_t notificationValue = 0;
TaskHandle_t CountingSemaphoreUsingTask::getTaskHandle() { xTaskNotifyAndQuery(handle, 0, eNoAction, &notificationValue);
return handle; return notificationValue;
} }
ReturnValue_t CountingSemaphoreUsingTask::releaseFromISR( TaskHandle_t CountingSemaphoreUsingTask::getTaskHandle() {
TaskHandle_t taskToNotify, BaseType_t* higherPriorityTaskWoken) { return handle;
vTaskNotifyGiveFromISR(taskToNotify, higherPriorityTaskWoken); }
return HasReturnvaluesIF::RETURN_OK;
} ReturnValue_t CountingSemaphoreUsingTask::releaseFromISR(
TaskHandle_t taskToNotify, BaseType_t* higherPriorityTaskWoken) {
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounterFromISR( vTaskNotifyGiveFromISR(taskToNotify, higherPriorityTaskWoken);
TaskHandle_t task, BaseType_t* higherPriorityTaskWoken) { return HasReturnvaluesIF::RETURN_OK;
uint32_t notificationValue; }
xTaskNotifyAndQueryFromISR(task, 0, eNoAction, &notificationValue,
higherPriorityTaskWoken); uint8_t CountingSemaphoreUsingTask::getSemaphoreCounterFromISR(
return notificationValue; TaskHandle_t task, BaseType_t* higherPriorityTaskWoken) {
} uint32_t notificationValue;
xTaskNotifyAndQueryFromISR(task, 0, eNoAction, &notificationValue,
uint8_t CountingSemaphoreUsingTask::getMaxCount() const { higherPriorityTaskWoken);
return maxCount; return notificationValue;
} }
uint8_t CountingSemaphoreUsingTask::getMaxCount() const {
return maxCount;
}

View File

@ -1,100 +1,102 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ #ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ #define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h> #include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
#include <framework/tasks/SemaphoreIF.h> #include "../../tasks/SemaphoreIF.h"
extern "C" { extern "C" {
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
} }
/** /**
* @brief Couting Semaphore implementation which uses the notification value * @brief Couting Semaphore implementation which uses the notification value
* of the task. The notification value should therefore not be used * of the task. The notification value should therefore not be used
* for other purposes. * for other purposes.
* @details * @details
* Additional information: https://www.freertos.org/RTOS-task-notifications.html * Additional information: https://www.freertos.org/RTOS-task-notifications.html
* and general semaphore documentation. * and general semaphore documentation.
*/ */
class CountingSemaphoreUsingTask: public SemaphoreIF { class CountingSemaphoreUsingTask: public SemaphoreIF {
public: public:
CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount); CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount);
virtual ~CountingSemaphoreUsingTask(); virtual ~CountingSemaphoreUsingTask();
/** /**
* Acquire the counting semaphore. * Acquire the counting semaphore.
* If no semaphores are available, the task will be blocked * If no semaphores are available, the task will be blocked
* for a maximum of #timeoutMs or until one is given back, * for a maximum of #timeoutMs or until one is given back,
* for example by an ISR or another task. * for example by an ISR or another task.
* @param timeoutMs * @param timeoutMs
* @return -@c RETURN_OK on success * @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/ */
ReturnValue_t acquire(uint32_t timeoutMs = SemaphoreIF::BLOCKING) override; ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
uint32_t timeoutMs = portMAX_DELAY) override;
/**
* Release a semaphore, increasing the number of available counting /**
* semaphores up to the #maxCount value. * Release a semaphore, increasing the number of available counting
* @return -@c RETURN_OK on success * semaphores up to the #maxCount value.
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are * @return -@c RETURN_OK on success
* already available. * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
*/ * already available.
ReturnValue_t release() override; */
ReturnValue_t release() override;
uint8_t getSemaphoreCounter() const override;
/** uint8_t getSemaphoreCounter() const override;
* Get the semaphore counter from an ISR. /**
* @param task * Get the semaphore counter from an ISR.
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with * @param task
* a higher priority was unblocked. A context switch should be requested * @param higherPriorityTaskWoken This will be set to pdPASS if a task with
* from an ISR if this is the case (see TaskManagement functions) * a higher priority was unblocked. A context switch should be requested
* @return * from an ISR if this is the case (see TaskManagement functions)
*/ * @return
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t task, */
BaseType_t* higherPriorityTaskWoken); static uint8_t getSemaphoreCounterFromISR(TaskHandle_t task,
BaseType_t* higherPriorityTaskWoken);
/**
* Acquire with a timeout value in ticks /**
* @param timeoutTicks * Acquire with a timeout value in ticks
* @return -@c RETURN_OK on success * @param timeoutTicks
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout * @return -@c RETURN_OK on success
*/ * -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
ReturnValue_t acquireWithTickTimeout( */
TickType_t timeoutTicks = SemaphoreIF::BLOCKING); ReturnValue_t acquireWithTickTimeout(
TimeoutType timeoutType = TimeoutType::BLOCKING,
/** TickType_t timeoutTicks = portMAX_DELAY);
* Get handle to the task related to the semaphore.
* @return /**
*/ * Get handle to the task related to the semaphore.
TaskHandle_t getTaskHandle(); * @return
*/
/** TaskHandle_t getTaskHandle();
* Release semaphore of task by supplying task handle
* @param taskToNotify /**
* @return -@c RETURN_OK on success * Release semaphore of task by supplying task handle
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are * @param taskToNotify
* already available. * @return -@c RETURN_OK on success
*/ * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
static ReturnValue_t release(TaskHandle_t taskToNotify); * already available.
/** */
* Release seamphore of a task from an ISR. static ReturnValue_t release(TaskHandle_t taskToNotify);
* @param taskToNotify /**
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with * Release seamphore of a task from an ISR.
* a higher priority was unblocked. A context switch should be requested * @param taskToNotify
* from an ISR if this is the case (see TaskManagement functions) * @param higherPriorityTaskWoken This will be set to pdPASS if a task with
* @return -@c RETURN_OK on success * a higher priority was unblocked. A context switch should be requested
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are * from an ISR if this is the case (see TaskManagement functions)
* already available. * @return -@c RETURN_OK on success
*/ * -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify, * already available.
BaseType_t* higherPriorityTaskWoken); */
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
uint8_t getMaxCount() const; BaseType_t* higherPriorityTaskWoken);
private: uint8_t getMaxCount() const;
TaskHandle_t handle;
const uint8_t maxCount; private:
}; TaskHandle_t handle;
const uint8_t maxCount;
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */ };
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */

View File

@ -1,43 +1,43 @@
#include <framework/osal/FreeRTOS/CountingSemaphore.h> #include "../../osal/FreeRTOS/CountingSemaphore.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
#include <framework/osal/FreeRTOS/TaskManagement.h> #include "../../osal/FreeRTOS/TaskManagement.h"
#include <freertos/semphr.h> #include <freertos/semphr.h>
// Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in // Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in
// free FreeRTOSConfig.h file. // free FreeRTOSConfig.h file.
CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount): CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount):
maxCount(maxCount), initCount(initCount) { maxCount(maxCount), initCount(initCount) {
if(initCount > maxCount) { if(initCount > maxCount) {
sif::error << "CountingSemaphoreUsingTask: Max count bigger than " sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
"intial cout. Setting initial count to max count." << std::endl; "intial cout. Setting initial count to max count." << std::endl;
initCount = maxCount; initCount = maxCount;
} }
handle = xSemaphoreCreateCounting(maxCount, initCount); handle = xSemaphoreCreateCounting(maxCount, initCount);
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "CountingSemaphore: Creation failure" << std::endl; sif::error << "CountingSemaphore: Creation failure" << std::endl;
} }
} }
CountingSemaphore::CountingSemaphore(CountingSemaphore&& other): CountingSemaphore::CountingSemaphore(CountingSemaphore&& other):
maxCount(other.maxCount), initCount(other.initCount) { maxCount(other.maxCount), initCount(other.initCount) {
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount); handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "CountingSemaphore: Creation failure" << std::endl; sif::error << "CountingSemaphore: Creation failure" << std::endl;
} }
} }
CountingSemaphore& CountingSemaphore::operator =( CountingSemaphore& CountingSemaphore::operator =(
CountingSemaphore&& other) { CountingSemaphore&& other) {
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount); handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
if(handle == nullptr) { if(handle == nullptr) {
sif::error << "CountingSemaphore: Creation failure" << std::endl; sif::error << "CountingSemaphore: Creation failure" << std::endl;
} }
return * this; return * this;
} }
uint8_t CountingSemaphore::getMaxCount() const { uint8_t CountingSemaphore::getMaxCount() const {
return maxCount; return maxCount;
} }

View File

@ -1,34 +1,34 @@
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ #ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ #define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
#include <framework/osal/FreeRTOS/BinarySemaphore.h> #include "../../osal/FreeRTOS/BinarySemaphore.h"
/** /**
* @brief Counting semaphores, which can be acquire more than once. * @brief Counting semaphores, which can be acquire more than once.
* @details * @details
* See: https://www.freertos.org/CreateCounting.html * See: https://www.freertos.org/CreateCounting.html
* API of counting semaphores is almost identical to binary semaphores, * API of counting semaphores is almost identical to binary semaphores,
* so we just inherit from binary semaphore and provide the respective * so we just inherit from binary semaphore and provide the respective
* constructors. * constructors.
*/ */
class CountingSemaphore: public BinarySemaphore { class CountingSemaphore: public BinarySemaphore {
public: public:
CountingSemaphore(const uint8_t maxCount, uint8_t initCount); CountingSemaphore(const uint8_t maxCount, uint8_t initCount);
//! @brief Copy ctor, disabled //! @brief Copy ctor, disabled
CountingSemaphore(const CountingSemaphore&) = delete; CountingSemaphore(const CountingSemaphore&) = delete;
//! @brief Copy assignment, disabled //! @brief Copy assignment, disabled
CountingSemaphore& operator=(const CountingSemaphore&) = delete; CountingSemaphore& operator=(const CountingSemaphore&) = delete;
//! @brief Move ctor //! @brief Move ctor
CountingSemaphore (CountingSemaphore &&); CountingSemaphore (CountingSemaphore &&);
//! @brief Move assignment //! @brief Move assignment
CountingSemaphore & operator=(CountingSemaphore &&); CountingSemaphore & operator=(CountingSemaphore &&);
/* Same API as binary semaphore otherwise. acquire() can be called /* Same API as binary semaphore otherwise. acquire() can be called
* until there are not semaphores left and release() can be called * until there are not semaphores left and release() can be called
* until maxCount is reached. */ * until maxCount is reached. */
uint8_t getMaxCount() const; uint8_t getMaxCount() const;
private: private:
const uint8_t maxCount; const uint8_t maxCount;
uint8_t initCount = 0; uint8_t initCount = 0;
}; };
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */ #endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */

View File

@ -1,61 +1,59 @@
#include <framework/osal/FreeRTOS/BinarySemaphore.h> #include "../../osal/FreeRTOS/BinarySemaphore.h"
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h> #include "../../osal/FreeRTOS/BinSemaphUsingTask.h"
#include <framework/osal/FreeRTOS/CountingSemaphore.h> #include "../../osal/FreeRTOS/CountingSemaphore.h"
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h> #include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
#include <framework/tasks/SemaphoreFactory.h> #include "../../tasks/SemaphoreFactory.h"
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include "../../serviceinterface/ServiceInterfaceStream.h"
SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr; SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr;
const uint32_t SemaphoreIF::POLLING = 0;
const uint32_t SemaphoreIF::BLOCKING = portMAX_DELAY; static const uint32_t USE_REGULAR_SEMAPHORES = 0;
static const uint32_t USE_TASK_NOTIFICATIONS = 1;
static const uint32_t USE_REGULAR_SEMAPHORES = 0;
static const uint32_t USE_TASK_NOTIFICATIONS = 1; SemaphoreFactory::SemaphoreFactory() {
}
SemaphoreFactory::SemaphoreFactory() {
} SemaphoreFactory::~SemaphoreFactory() {
delete factoryInstance;
SemaphoreFactory::~SemaphoreFactory() { }
delete factoryInstance;
} SemaphoreFactory* SemaphoreFactory::instance() {
if (factoryInstance == nullptr){
SemaphoreFactory* SemaphoreFactory::instance() { factoryInstance = new SemaphoreFactory();
if (factoryInstance == nullptr){ }
factoryInstance = new SemaphoreFactory(); return SemaphoreFactory::factoryInstance;
} }
return SemaphoreFactory::factoryInstance;
} SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) {
if(argument == USE_REGULAR_SEMAPHORES) {
SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) { return new BinarySemaphore();
if(argument == USE_REGULAR_SEMAPHORES) { }
return new BinarySemaphore(); else if(argument == USE_TASK_NOTIFICATIONS) {
} return new BinarySemaphoreUsingTask();
else if(argument == USE_TASK_NOTIFICATIONS) { }
return new BinarySemaphoreUsingTask(); else {
} sif::warning << "SemaphoreFactory: Invalid argument, return regular"
else { "binary semaphore" << std::endl;
sif::warning << "SemaphoreFactory: Invalid argument, return regular" return new BinarySemaphore();
"binary semaphore" << std::endl; }
return new BinarySemaphore(); }
}
} SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount,
uint8_t initCount, uint32_t argument) {
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount, if(argument == USE_REGULAR_SEMAPHORES) {
uint8_t initCount, uint32_t argument) { return new CountingSemaphore(maxCount, initCount);
if(argument == USE_REGULAR_SEMAPHORES) { }
return new CountingSemaphore(maxCount, initCount); else if(argument == USE_TASK_NOTIFICATIONS) {
} return new CountingSemaphoreUsingTask(maxCount, initCount);
else if(argument == USE_TASK_NOTIFICATIONS) { }
return new CountingSemaphoreUsingTask(maxCount, initCount); else {
} sif::warning << "SemaphoreFactory: Invalid argument, return regular"
else { "binary semaphore" << std::endl;
sif::warning << "SemaphoreFactory: Invalid argument, return regular" return new CountingSemaphore(maxCount, initCount);
"binary semaphore" << std::endl; }
return new CountingSemaphore(maxCount, initCount);
} }
} void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) {
delete semaphore;
void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) { }
delete semaphore;
}

View File

@ -1,72 +1,68 @@
#ifndef FRAMEWORK_TASKS_SEMAPHOREIF_H_ #ifndef FRAMEWORK_TASKS_SEMAPHOREIF_H_
#define FRAMEWORK_TASKS_SEMAPHOREIF_H_ #define FRAMEWORK_TASKS_SEMAPHOREIF_H_
#include <framework/returnvalues/FwClassIds.h> #include "../returnvalues/FwClassIds.h"
#include <framework/returnvalues/HasReturnvaluesIF.h> #include "../returnvalues/HasReturnvaluesIF.h"
#include <cstdint> #include <cstdint>
/** /**
* @brief Generic interface for semaphores, which can be used to achieve * @brief Generic interface for semaphores, which can be used to achieve
* task synchronization. This is a generic interface which can be * task synchronization. This is a generic interface which can be
* used for both binary semaphores and counting semaphores. * used for both binary semaphores and counting semaphores.
* @details * @details
* A semaphore is a synchronization primitive. * A semaphore is a synchronization primitive.
* See: https://en.wikipedia.org/wiki/Semaphore_(programming) * See: https://en.wikipedia.org/wiki/Semaphore_(programming)
* A semaphore can be used to achieve task synchonization and track the * A semaphore can be used to achieve task synchonization and track the
* availability of resources by using either the binary or the counting * availability of resources by using either the binary or the counting
* semaphore types. * semaphore types.
* *
* If mutual exlcusion of a resource is desired, a mutex should be used, * If mutual exlcusion of a resource is desired, a mutex should be used,
* which is a special form of a semaphore and has an own interface. * which is a special form of a semaphore and has an own interface.
*/ */
class SemaphoreIF { class SemaphoreIF {
public: public:
virtual~ SemaphoreIF() {}; /**
/** * Different types of timeout for the mutex lock.
* @brief Timeout value used for polling lock attempt. */
* @details enum TimeoutType {
* If the lock is not successfull, MUTEX_TIMEOUT will be returned POLLING, //!< If mutex is not available, return immediately
* immediately. Value needs to be defined in implementation. WAITING, //!< Wait a specified time for the mutex to become available
*/ BLOCKING //!< Block indefinitely until the mutex becomes available.
static const uint32_t POLLING; };
/**
* @brief Timeout value used for permanent blocking lock attempt. virtual~ SemaphoreIF() {};
* @details
* The task will be blocked (indefinitely) until the mutex is unlocked. static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
gaisser marked this conversation as resolved
Review

I don't see the addition of the class id in FwClassIds. Could you add this change as well?

I don't see the addition of the class id in FwClassIds. Could you add this change as well?
Review

It's actually in there, I did a compile check

It's actually in there, I did a compile check
Review

Interesting, I don't find it in the commit view.

Interesting, I don't find it in the commit view.
Review

Oh it's part of the master.

Oh it's part of the master.
* Value needs to be defined in implementation. //! Semaphore timeout
*/ static constexpr ReturnValue_t SEMAPHORE_TIMEOUT = MAKE_RETURN_CODE(1);
static const uint32_t BLOCKING; //! The current semaphore can not be given, because it is not owned
static constexpr ReturnValue_t SEMAPHORE_NOT_OWNED = MAKE_RETURN_CODE(2);
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF; static constexpr ReturnValue_t SEMAPHORE_INVALID = MAKE_RETURN_CODE(3);
//! Semaphore timeout
static constexpr ReturnValue_t SEMAPHORE_TIMEOUT = MAKE_RETURN_CODE(1); /**
//! The current semaphore can not be given, because it is not owned * Generic call to acquire a semaphore.
static constexpr ReturnValue_t SEMAPHORE_NOT_OWNED = MAKE_RETURN_CODE(2); * If there are no more semaphores to be taken (for a counting semaphore,
static constexpr ReturnValue_t SEMAPHORE_INVALID = MAKE_RETURN_CODE(3); * a semaphore may be taken more than once), the taks will block
* for a maximum of timeoutMs while trying to acquire the semaphore.
/** * This can be used to achieve task synchrnization.
* Generic call to acquire a semaphore. * @param timeoutMs
* If there are no more semaphores to be taken (for a counting semaphore, * @return - c RETURN_OK for successfull acquisition
* a semaphore may be taken more than once), the taks will block */
* for a maximum of timeoutMs while trying to acquire the semaphore. virtual ReturnValue_t acquire(TimeoutType timeoutType =
* This can be used to achieve task synchrnization. TimeoutType::BLOCKING, uint32_t timeoutMs = 0) = 0;
* @param timeoutMs
* @return - c RETURN_OK for successfull acquisition /**
*/ * Corrensponding call to release a semaphore.
virtual ReturnValue_t acquire(uint32_t timeoutMs) = 0; * @return -@c RETURN_OK for successfull release
*/
/** virtual ReturnValue_t release() = 0;
* Corrensponding call to release a semaphore.
* @return -@c RETURN_OK for successfull release /**
*/ * If the semaphore is a counting semaphore then the semaphores current
virtual ReturnValue_t release() = 0; * count value is returned. If the semaphore is a binary semaphore then 1
* is returned if the semaphore is available, and 0 is returned if the
/** * semaphore is not available.
* If the semaphore is a counting semaphore then the semaphores current */
* count value is returned. If the semaphore is a binary semaphore then 1 virtual uint8_t getSemaphoreCounter() const = 0;
* is returned if the semaphore is available, and 0 is returned if the };
* semaphore is not available.
*/ #endif /* FRAMEWORK_TASKS_SEMAPHOREIF_H_ */
virtual uint8_t getSemaphoreCounter() const = 0;
};
#endif /* FRAMEWORK_TASKS_SEMAPHOREIF_H_ */