fsfw/src/fsfw/osal/freertos/BinarySemaphore.h

106 lines
3.6 KiB
C
Raw Normal View History

2020-12-14 11:17:22 +01:00
#ifndef FSFW_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#define FSFW_OSAL_FREERTOS_BINARYSEMPAHORE_H_
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
#include "FreeRTOS.h"
2022-08-16 12:48:22 +02:00
#include "fsfw/returnvalues/returnvalue.h"
2021-07-14 00:54:39 +02:00
#include "fsfw/tasks/SemaphoreIF.h"
#include "semphr.h"
2020-09-18 13:15:14 +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.
* @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.
*
* @author R. Mueller
* @ingroup osal
*/
2022-08-16 12:48:22 +02:00
class BinarySemaphore : public SemaphoreIF {
2022-02-02 10:29:30 +01:00
public:
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
//! @brief Default ctor
BinarySemaphore();
//! @brief Copy ctor, deleted explicitely.
BinarySemaphore(const BinarySemaphore &) = delete;
//! @brief Copy assignment, deleted explicitely.
BinarySemaphore &operator=(const BinarySemaphore &) = delete;
//! @brief Move ctor
BinarySemaphore(BinarySemaphore &&);
//! @brief Move assignment
BinarySemaphore &operator=(BinarySemaphore &&);
//! @brief Destructor
virtual ~BinarySemaphore();
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
uint8_t getSemaphoreCounter() const override;
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +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,
* for example by an ISR or another task.
* @param timeoutMs
2022-08-16 12:12:21 +02:00
* @return -@c returnvalue::OK on success
2022-02-02 10:29:30 +01:00
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/
ReturnValue_t acquire(TimeoutType timeoutType = TimeoutType::BLOCKING,
uint32_t timeoutMs = portMAX_DELAY) override;
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
/**
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
* @param timeoutTicks
2022-08-16 12:12:21 +02:00
* @return -@c returnvalue::OK on success
2022-02-02 10:29:30 +01:00
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
*/
ReturnValue_t acquireWithTickTimeout(TimeoutType timeoutType = TimeoutType::BLOCKING,
TickType_t timeoutTicks = portMAX_DELAY);
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
/**
* Release the binary semaphore.
2022-08-16 12:12:21 +02:00
* @return -@c returnvalue::OK on success
2022-02-02 10:29:30 +01:00
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
* already available.
*/
ReturnValue_t release() override;
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
/**
* Get Handle to the semaphore.
* @return
*/
SemaphoreHandle_t getSemaphore();
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
/**
* Wrapper function to give back semaphore from handle
* @param semaphore
2022-08-16 12:12:21 +02:00
* @return -@c returnvalue::OK on success
2022-02-02 10:29:30 +01:00
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
* already available.
*/
static ReturnValue_t release(SemaphoreHandle_t semaphore);
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
/**
* 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)
2022-08-16 12:12:21 +02:00
* @return -@c returnvalue::OK on success
2022-02-02 10:29:30 +01:00
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if the semaphores is
* already available.
*/
static ReturnValue_t releaseFromISR(SemaphoreHandle_t semaphore,
BaseType_t *higherPriorityTaskWoken);
2020-09-18 13:15:14 +02:00
2022-02-02 10:29:30 +01:00
protected:
SemaphoreHandle_t handle;
2020-09-18 13:15:14 +02:00
};
2020-12-14 11:17:22 +01:00
#endif /* FSFW_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */