fsfw/osal/FreeRTOS/BinarySemaphore.h

126 lines
4.2 KiB
C
Raw Normal View History

2020-04-22 19:44:03 +02:00
#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
#include <framework/returnvalues/HasReturnvaluesIF.h>
#include <framework/tasks/SemaphoreIF.h>
2020-04-22 19:44:03 +02:00
extern "C" {
2020-05-18 18:06:40 +02:00
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
2020-04-22 19:44:03 +02:00
}
2020-05-18 18:06:40 +02:00
// TODO: Counting semaphores and implement the new (better)
// task notifications. However, those use task notifications require
// the task handle. Maybe it would be better to make a separate class
// and switch between the classes with #ifdefs.
// Task Notifications require FreeRTOS V8.2 something..
2020-04-22 19:44:03 +02: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-04-22 19:44:03 +02:00
* @details
* Documentation: https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html
*
2020-05-18 18:06:40 +02:00
* @author R. Mueller
2020-04-22 19:44:03 +02:00
* @ingroup osal
*/
2020-05-18 20:39:48 +02:00
class BinarySemaphore: public SemaphoreIF,
public HasReturnvaluesIF {
2020-04-22 19:44:03 +02:00
public:
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
//! No block time, poll the semaphore. Can also be used as tick type.
//! Can be passed as tick type and ms value.
static constexpr uint32_t NO_BLOCK_TIMEOUT = 0;
static constexpr TickType_t NO_BLOCK_TICKS = 0;
//! No block time, poll the semaphore.
//! Can be passed as tick type and ms value.
static constexpr TickType_t BLOCK_TIMEOUT_TICKS = portMAX_DELAY;
static constexpr uint32_t BLOCK_TIMEOUT = portMAX_DELAY;
//! Semaphore timeout
static constexpr ReturnValue_t SEMAPHORE_TIMEOUT = MAKE_RETURN_CODE(1);
/** The current semaphore can not be given, because it is not owned */
static constexpr ReturnValue_t SEMAPHORE_NOT_OWNED = MAKE_RETURN_CODE(2);
static constexpr ReturnValue_t SEMAPHORE_NULLPOINTER = MAKE_RETURN_CODE(3);
2020-05-18 20:35:13 +02:00
//! @brief Default ctor
2020-05-18 20:39:48 +02:00
BinarySemaphore();
2020-05-18 20:35:13 +02:00
//! @brief Copy ctor
2020-05-18 20:39:48 +02:00
BinarySemaphore(const BinarySemaphore&);
2020-05-18 20:35:13 +02:00
//! @brief Copy assignment
2020-05-18 20:39:48 +02:00
BinarySemaphore& operator=(const BinarySemaphore&);
2020-05-18 20:35:13 +02:00
//! @brief Move ctor
2020-05-18 20:39:48 +02:00
BinarySemaphore (BinarySemaphore &&);
2020-05-18 20:35:13 +02:00
//! @brief Move assignment
2020-05-18 20:39:48 +02:00
BinarySemaphore & operator=(BinarySemaphore &&);
2020-05-18 20:35:13 +02:00
//! @brief Destructor
2020-05-18 20:39:48 +02:00
virtual ~BinarySemaphore();
2020-04-22 19:44:03 +02:00
ReturnValue_t acquire(uint32_t timeoutMs =
2020-05-18 20:39:48 +02:00
BinarySemaphore::NO_BLOCK_TIMEOUT) override;
ReturnValue_t release() override;
uint8_t getSemaphoreCounter() override;
2020-04-22 19:44:03 +02: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-04-22 19:44:03 +02:00
* 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 =
2020-05-18 20:39:48 +02:00
BinarySemaphore::NO_BLOCK_TIMEOUT);
2020-04-22 19:44:03 +02:00
/**
* 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 =
2020-05-18 20:39:48 +02:00
BinarySemaphore::NO_BLOCK_TICKS);
2020-04-22 19:44:03 +02:00
/**
* 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
*/
SemaphoreHandle_t getSemaphore();
/**
* Reset the semaphore.
*/
void resetSemaphore();
/**
* 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(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
* @return - @c RETURN_OK on success
* - @c RETURN_FAILED on failure
*/
static ReturnValue_t giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore,
BaseType_t * higherPriorityTaskWoken);
2020-05-18 20:35:13 +02:00
protected:
2020-04-22 19:44:03 +02:00
SemaphoreHandle_t handle;
};
#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */