implementing new task notifications

This commit is contained in:
Robin Müller 2020-05-27 17:42:18 +02:00
parent 968d7fad81
commit 2d33274c23
2 changed files with 145 additions and 19 deletions

View File

@ -2,6 +2,8 @@
#include <framework/osal/FreeRTOS/TaskManagement.h> #include <framework/osal/FreeRTOS/TaskManagement.h>
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
#if ( configUSE_OLD_SEMAPHORES == 1 )
BinarySemaphore::BinarySemaphore() { BinarySemaphore::BinarySemaphore() {
handle = xSemaphoreCreateBinary(); handle = xSemaphoreCreateBinary();
if(handle == nullptr) { if(handle == nullptr) {
@ -51,7 +53,7 @@ ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) {
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
else { else {
return SEMAPHORE_TIMEOUT; return SemaphoreIF::SEMAPHORE_TIMEOUT;
} }
} }
@ -97,14 +99,6 @@ ReturnValue_t BinarySemaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore)
} }
} }
void BinarySemaphore::resetSemaphore() {
if(handle != nullptr) {
vSemaphoreDelete(handle);
handle = xSemaphoreCreateBinary();
xSemaphoreGive(handle);
}
}
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) { ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
return takeBinarySemaphore(timeoutMs); return takeBinarySemaphore(timeoutMs);
} }
@ -135,3 +129,60 @@ ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t sema
return SEMAPHORE_NOT_OWNED; return SEMAPHORE_NOT_OWNED;
} }
} }
#else
BinarySemaphore::BinarySemaphore() {
handle = TaskManagement::getCurrentTaskHandle();
}
ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) {
return takeBinarySemaphore(timeoutMs);
}
ReturnValue_t BinarySemaphore::release() {
return giveBinarySemaphore();
}
ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) {
TickType_t timeout = SemaphoreIF::NO_TIMEOUT;
if(timeoutMs == SemaphoreIF::MAX_TIMEOUT) {
timeout = SemaphoreIF::MAX_TIMEOUT;
}
else if(timeoutMs > BinarySemaphore::NO_TIMEOUT){
timeout = pdMS_TO_TICKS(timeoutMs);
}
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeout);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
}
else {
return SemaphoreIF::SEMAPHORE_TIMEOUT;
}
}
ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout(
TickType_t timeoutTicks) {
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_TIMEOUT;
}
}
ReturnValue_t BinarySemaphore::giveBinarySemaphore() {
if (handle == nullptr) {
return SEMAPHORE_NULLPOINTER;
}
BaseType_t returncode = xTaskNotifyGive(handle);
if (returncode == pdPASS) {
return HasReturnvaluesIF::RETURN_OK;
} else {
return SEMAPHORE_NOT_OWNED;
}
}
#endif

View File

@ -6,7 +6,11 @@
extern "C" { extern "C" {
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#if ( configUSE_OLD_SEMAPHORES == 1 )
#include <freertos/semphr.h> #include <freertos/semphr.h>
#else
#include <freertos/task.h>
#endif
} }
// TODO: Implement the new (better) task notifications. // TODO: Implement the new (better) task notifications.
@ -24,6 +28,8 @@ extern "C" {
* @author R. Mueller * @author R. Mueller
* @ingroup osal * @ingroup osal
*/ */
#if ( configUSE_OLD_SEMAPHORES == 1 )
class BinarySemaphore: public SemaphoreIF, class BinarySemaphore: public SemaphoreIF,
public HasReturnvaluesIF { public HasReturnvaluesIF {
public: public:
@ -81,11 +87,6 @@ public:
*/ */
SemaphoreHandle_t getSemaphore(); SemaphoreHandle_t getSemaphore();
/**
* Reset the semaphore.
*/
void resetSemaphore();
/** /**
* Wrapper function to give back semaphore from handle * Wrapper function to give back semaphore from handle
* @param semaphore * @param semaphore
@ -109,4 +110,78 @@ protected:
SemaphoreHandle_t handle; SemaphoreHandle_t handle;
}; };
#else
class BinarySemaphore: public SemaphoreIF,
public HasReturnvaluesIF {
public:
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
//! @brief Default ctor
BinarySemaphore();
ReturnValue_t acquire(uint32_t timeoutMs =
SemaphoreIF::NO_TIMEOUT) override;
ReturnValue_t release() override;
uint8_t getSemaphoreCounter() override;
/**
* Take the binary semaphore.
* If the semaphore has already been taken, the task will be blocked
* for a maximum of #timeoutMs or until the semaphore is given back,
* for example by an ISR or another task.
* @param timeoutMs
* @return -@c RETURN_OK on success
* -@c RETURN_FAILED on failure
*/
ReturnValue_t takeBinarySemaphore(uint32_t timeoutMs =
SemaphoreIF::NO_TIMEOUT);
/**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
* @param timeoutTicks
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
ReturnValue_t takeBinarySemaphoreTickTimeout(TickType_t timeoutTicks =
BinarySemaphore::NO_TIMEOUT);
/**
* Give back the binary semaphore
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
ReturnValue_t giveBinarySemaphore();
/**
* Get Handle to the semaphore.
* @return
*/
TaskHandle_t getTaskHandle();
/**
* Wrapper function to give back semaphore from handle
* @param semaphore
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
static ReturnValue_t giveBinarySemaphore(TaskHandle_t taskToNotify);
/**
* Wrapper function to give back semaphore from handle when called from an ISR
* @param semaphore
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with a higher priority
* was unblocked
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
static ReturnValue_t giveBinarySemaphoreFromISR(TaskHandle_t taskToNotify,
BaseType_t * higherPriorityTaskWoken);
protected:
TaskHandle_t handle;
};
#endif
#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */ #endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */