2020-02-25 17:04:21 +01:00
|
|
|
/**
|
|
|
|
* @file BinarySempahore.h
|
|
|
|
*
|
|
|
|
* @date 25.02.2020
|
|
|
|
*/
|
|
|
|
#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
|
|
|
|
#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
|
|
|
|
|
|
|
|
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include "semphr.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief OS Tool to achieve synchronization of between tasks or between task and ISR
|
|
|
|
* @details
|
|
|
|
* Documentation: https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html
|
|
|
|
* @ingroup osal
|
|
|
|
*/
|
|
|
|
class BinarySemaphore: public HasReturnvaluesIF {
|
|
|
|
public:
|
|
|
|
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
|
|
|
|
|
2020-04-08 18:08:14 +02:00
|
|
|
//! 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);
|
2020-02-25 17:04:21 +01:00
|
|
|
/** The current semaphore can not be given, because it is not owned */
|
2020-04-08 18:08:14 +02:00
|
|
|
static constexpr ReturnValue_t SEMAPHORE_NOT_OWNED = MAKE_RETURN_CODE(2);
|
|
|
|
static constexpr ReturnValue_t SEMAPHORE_NULLPOINTER = MAKE_RETURN_CODE(3);
|
2020-02-25 17:04:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a binary semaphore
|
|
|
|
*/
|
|
|
|
BinarySemaphore();
|
|
|
|
|
2020-04-08 18:08:14 +02:00
|
|
|
/**
|
|
|
|
* Copy ctor
|
|
|
|
* @param
|
|
|
|
*/
|
|
|
|
BinarySemaphore(const BinarySemaphore&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy assignment
|
|
|
|
*/
|
|
|
|
BinarySemaphore& operator=(const BinarySemaphore&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move constructor
|
|
|
|
*/
|
|
|
|
BinarySemaphore (BinarySemaphore &&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move assignment
|
|
|
|
*/
|
|
|
|
BinarySemaphore & operator=(BinarySemaphore &&);
|
|
|
|
|
2020-02-25 17:04:21 +01:00
|
|
|
/**
|
|
|
|
* Delete the binary semaphore to prevent a memory leak
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
virtual ~BinarySemaphore();
|
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,
|
|
|
|
* for example by an ISR or another task.
|
|
|
|
* @param timeoutMs
|
|
|
|
* @return -@c RETURN_OK on success
|
|
|
|
* -@c RETURN_FAILED on failure
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
ReturnValue_t takeBinarySemaphore(uint32_t timeoutMs =
|
|
|
|
BinarySemaphore::NO_BLOCK_TIMEOUT);
|
2020-02-25 17:04:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as lockBinarySemaphore() with timeout in FreeRTOS ticks.
|
|
|
|
* @param timeoutTicks
|
|
|
|
* @return -@c RETURN_OK on success
|
|
|
|
* -@c RETURN_FAILED on failure
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
ReturnValue_t takeBinarySemaphoreTickTimeout(TickType_t timeoutTicks =
|
|
|
|
BinarySemaphore::NO_BLOCK_TICKS);
|
2020-02-25 17:04:21 +01: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();
|
2020-02-28 22:55:25 +01:00
|
|
|
|
2020-03-06 15:39:42 +01:00
|
|
|
/**
|
|
|
|
* Reset the semaphore.
|
|
|
|
*/
|
|
|
|
void resetSemaphore();
|
|
|
|
|
2020-02-28 22:55:25 +01:00
|
|
|
/**
|
|
|
|
* 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-02-25 17:04:21 +01:00
|
|
|
private:
|
|
|
|
SemaphoreHandle_t handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_ */
|