fsfw/osal/FreeRTOS/BinarySemaphore.h

108 lines
3.6 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_
2020-08-18 13:09:15 +02:00
#include "../../returnvalues/HasReturnvaluesIF.h"
#include "../../tasks/SemaphoreIF.h"
2020-05-27 17:07:35 +02:00
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
2020-05-27 21:33:34 +02:00
uint8_t getSemaphoreCounter() const 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
2020-05-27 23:41:59 +02:00
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
2020-02-25 17:04:21 +01:00
*/
2020-08-27 15:52:55 +02:00
ReturnValue_t acquire(TimeoutType timeoutType =
TimeoutType::BLOCKING, uint32_t timeoutMs = portMAX_DELAY) override;
2020-02-25 17:04:21 +01:00
/**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
* @param timeoutTicks
2020-05-27 23:41:59 +02:00
* @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
2020-02-25 17:04:21 +01:00
*/
2020-08-27 15:52:55 +02:00
ReturnValue_t acquireWithTickTimeout(TimeoutType timeoutType =
TimeoutType::BLOCKING, TickType_t timeoutTicks = portMAX_DELAY);
2020-02-25 17:04:21 +01:00
/**
2020-05-27 23:41:59 +02:00
* Release the binary semaphore.
* @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
* already available.
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-05-27 23:41:59 +02:00
* @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
* already available.
*/
2020-05-27 23:41:59 +02:00
static ReturnValue_t release(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. A context switch from an ISR should
* then be requested (see TaskManagement functions)
2020-05-27 23:41:59 +02:00
* @return -@c RETURN_OK on success
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
* already available.
*/
2020-05-27 23:41:59 +02:00
static ReturnValue_t releaseFromISR(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_ */