fsfw/osal/FreeRTOS/BinarySemaphore.h

106 lines
3.3 KiB
C
Raw Normal View History

2020-02-25 17:04:21 +01:00
#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/tasks/SemaphoreIF.h>
2020-05-27 17:07:35 +02:00
2020-04-22 19:42:42 +02:00
extern "C" {
2020-05-18 18:06:40 +02:00
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
2020-04-22 19:42:42 +02:00
}
2020-02-25 17:04:21 +01:00
/**
2020-05-18 20:35:13 +02:00
* @brief OS Tool to achieve synchronization of between tasks or between
* task and ISR. The default semaphore implementation creates a
* binary semaphore, which can only be taken once.
2020-02-25 17:04:21 +01:00
* @details
* Documentation: https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html
*
* Please note that if the semaphore implementation is only related to
* the synchronization of one task, the new task notifications can be used,
* also see the BinSemaphUsingTask and CountingSemaphUsingTask classes.
* These use the task notification value instead of a queue and are
* faster and more efficient.
*
2020-05-18 18:06:40 +02:00
* @author R. Mueller
2020-02-25 17:04:21 +01:00
* @ingroup osal
*/
2020-05-18 20:39:48 +02:00
class BinarySemaphore: public SemaphoreIF,
2020-05-27 17:42:18 +02:00
public HasReturnvaluesIF {
2020-02-25 17:04:21 +01:00
public:
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
2020-05-27 17:42:18 +02:00
2020-05-18 20:35:13 +02:00
//! @brief Default ctor
2020-02-25 17:04:21 +01:00
BinarySemaphore();
2020-05-20 12:50:56 +02:00
//! @brief Copy ctor, deleted explicitely.
BinarySemaphore(const BinarySemaphore&) = delete;
//! @brief Copy assignment, deleted explicitely.
BinarySemaphore& operator=(const BinarySemaphore&) = delete;
2020-05-18 20:35:13 +02:00
//! @brief Move ctor
BinarySemaphore (BinarySemaphore &&);
2020-05-18 20:35:13 +02:00
//! @brief Move assignment
BinarySemaphore & operator=(BinarySemaphore &&);
2020-05-18 20:35:13 +02:00
//! @brief Destructor
virtual ~BinarySemaphore();
2020-02-25 17:04:21 +01:00
uint8_t getSemaphoreCounter() override;
2020-05-27 17:42:18 +02:00
2020-02-25 17:04:21 +01:00
/**
* 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,
2020-02-25 17:04:21 +01:00
* for example by an ISR or another task.
* @param timeoutMs
* @return -@c RETURN_OK on success
* -@c RETURN_FAILED on failure
*/
2020-05-27 21:27:31 +02:00
ReturnValue_t acquire(uint32_t timeoutMs =
SemaphoreIF::NO_TIMEOUT) override;
2020-02-25 17:04:21 +01:00
/**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
* @param timeoutTicks
2020-04-09 17:56:48 +02:00
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
2020-02-25 17:04:21 +01:00
*/
2020-05-27 21:27:31 +02:00
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks =
2020-05-27 17:07:35 +02:00
BinarySemaphore::NO_TIMEOUT);
2020-02-25 17:04:21 +01:00
/**
* Give back the binary semaphore
2020-04-09 17:56:48 +02:00
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
2020-02-25 17:04:21 +01:00
*/
2020-05-27 21:27:31 +02:00
ReturnValue_t release() override;
2020-02-25 17:04:21 +01:00
/**
* Get Handle to the semaphore.
* @return
*/
SemaphoreHandle_t getSemaphore();
/**
* Wrapper function to give back semaphore from handle
* @param semaphore
2020-04-09 17:56:48 +02:00
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
static ReturnValue_t giveBinarySemaphore(SemaphoreHandle_t semaphore);
/**
* 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
2020-04-09 17:56:48 +02:00
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
static ReturnValue_t giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore,
BaseType_t * higherPriorityTaskWoken);
2020-05-27 17:42:18 +02:00
protected:
2020-02-25 17:04:21 +01:00
SemaphoreHandle_t handle;
};
#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */