Merge branch 'mueller/master' of https://egit.irs.uni-stuttgart.de/KSat/fsfw into mueller/master
This commit is contained in:
@ -1,91 +1,91 @@
|
||||
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h>
|
||||
#include <framework/osal/FreeRTOS/TaskManagement.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
|
||||
handle = TaskManagement::getCurrentTaskHandle();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Could not retrieve task handle. Please ensure the"
|
||||
"constructor was called inside a task." << std::endl;
|
||||
}
|
||||
xTaskNotifyGive(handle);
|
||||
}
|
||||
|
||||
BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
|
||||
// Clear notification value on destruction.
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) {
|
||||
TickType_t timeout = SemaphoreIF::POLLING;
|
||||
if(timeoutMs == SemaphoreIF::BLOCKING) {
|
||||
timeout = SemaphoreIF::BLOCKING;
|
||||
}
|
||||
else if(timeoutMs > SemaphoreIF::POLLING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
return acquireWithTickTimeout(timeout);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout(
|
||||
TickType_t timeoutTicks) {
|
||||
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::release() {
|
||||
return release(this->handle);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::release(
|
||||
TaskHandle_t taskHandle) {
|
||||
if(getSemaphoreCounter(taskHandle) == 1) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
BaseType_t returncode = xTaskNotifyGive(taskHandle);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
// This should never happen.
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const {
|
||||
return getSemaphoreCounter(this->handle);
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter(
|
||||
TaskHandle_t taskHandle) {
|
||||
uint32_t notificationValue;
|
||||
xTaskNotifyAndQuery(taskHandle, 0, eNoAction, ¬ificationValue);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
// Be careful with the stack size here. This is called from an ISR!
|
||||
ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR(
|
||||
TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) {
|
||||
if(getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR(
|
||||
TaskHandle_t taskHandle, BaseType_t* higherPriorityTaskWoken) {
|
||||
uint32_t notificationValue = 0;
|
||||
xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, ¬ificationValue,
|
||||
higherPriorityTaskWoken);
|
||||
return notificationValue;
|
||||
}
|
||||
#include "../../osal/FreeRTOS/BinSemaphUsingTask.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() {
|
||||
handle = TaskManagement::getCurrentTaskHandle();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Could not retrieve task handle. Please ensure the"
|
||||
"constructor was called inside a task." << std::endl;
|
||||
}
|
||||
xTaskNotifyGive(handle);
|
||||
}
|
||||
|
||||
BinarySemaphoreUsingTask::~BinarySemaphoreUsingTask() {
|
||||
// Clear notification value on destruction.
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) {
|
||||
TickType_t timeout = SemaphoreIF::POLLING;
|
||||
if(timeoutMs == SemaphoreIF::BLOCKING) {
|
||||
timeout = SemaphoreIF::BLOCKING;
|
||||
}
|
||||
else if(timeoutMs > SemaphoreIF::POLLING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
return acquireWithTickTimeout(timeout);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::acquireWithTickTimeout(
|
||||
TickType_t timeoutTicks) {
|
||||
BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::release() {
|
||||
return release(this->handle);
|
||||
}
|
||||
|
||||
ReturnValue_t BinarySemaphoreUsingTask::release(
|
||||
TaskHandle_t taskHandle) {
|
||||
if(getSemaphoreCounter(taskHandle) == 1) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
BaseType_t returncode = xTaskNotifyGive(taskHandle);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
// This should never happen.
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
TaskHandle_t BinarySemaphoreUsingTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter() const {
|
||||
return getSemaphoreCounter(this->handle);
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounter(
|
||||
TaskHandle_t taskHandle) {
|
||||
uint32_t notificationValue;
|
||||
xTaskNotifyAndQuery(taskHandle, 0, eNoAction, ¬ificationValue);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
// Be careful with the stack size here. This is called from an ISR!
|
||||
ReturnValue_t BinarySemaphoreUsingTask::releaseFromISR(
|
||||
TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) {
|
||||
if(getSemaphoreCounterFromISR(taskHandle, higherPriorityTaskWoken) == 1) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t BinarySemaphoreUsingTask::getSemaphoreCounterFromISR(
|
||||
TaskHandle_t taskHandle, BaseType_t* higherPriorityTaskWoken) {
|
||||
uint32_t notificationValue = 0;
|
||||
xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, ¬ificationValue,
|
||||
higherPriorityTaskWoken);
|
||||
return notificationValue;
|
||||
}
|
||||
|
@ -1,75 +1,75 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
|
||||
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
#include <framework/tasks/SemaphoreIF.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
/**
|
||||
* @brief Binary Semaphore implementation using the task notification value.
|
||||
* The notification value should therefore not be used
|
||||
* for other purposes.
|
||||
* @details
|
||||
* Additional information: https://www.freertos.org/RTOS-task-notifications.html
|
||||
* and general semaphore documentation.
|
||||
*/
|
||||
class BinarySemaphoreUsingTask: public SemaphoreIF,
|
||||
public HasReturnvaluesIF {
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
|
||||
|
||||
//! @brief Default ctor
|
||||
BinarySemaphoreUsingTask();
|
||||
//! @brief Default dtor
|
||||
virtual~ BinarySemaphoreUsingTask();
|
||||
|
||||
ReturnValue_t acquire(uint32_t timeoutMs =
|
||||
SemaphoreIF::BLOCKING) override;
|
||||
ReturnValue_t release() override;
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle);
|
||||
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t taskHandle,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
/**
|
||||
* Same as acquire() with timeout in FreeRTOS ticks.
|
||||
* @param timeoutTicks
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks =
|
||||
SemaphoreIF::BLOCKING);
|
||||
|
||||
/**
|
||||
* Get handle to the task related to the semaphore.
|
||||
* @return
|
||||
*/
|
||||
TaskHandle_t getTaskHandle();
|
||||
|
||||
/**
|
||||
* Wrapper function to give back semaphore from handle
|
||||
* @param semaphore
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
static ReturnValue_t release(TaskHandle_t taskToNotify);
|
||||
|
||||
/**
|
||||
* 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 should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
|
||||
BaseType_t * higherPriorityTaskWoken);
|
||||
|
||||
protected:
|
||||
TaskHandle_t handle;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ */
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_
|
||||
|
||||
#include "../../returnvalues/HasReturnvaluesIF.h"
|
||||
#include "../../tasks/SemaphoreIF.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
/**
|
||||
* @brief Binary Semaphore implementation using the task notification value.
|
||||
* The notification value should therefore not be used
|
||||
* for other purposes.
|
||||
* @details
|
||||
* Additional information: https://www.freertos.org/RTOS-task-notifications.html
|
||||
* and general semaphore documentation.
|
||||
*/
|
||||
class BinarySemaphoreUsingTask: public SemaphoreIF,
|
||||
public HasReturnvaluesIF {
|
||||
public:
|
||||
static const uint8_t INTERFACE_ID = CLASS_ID::SEMAPHORE_IF;
|
||||
|
||||
//! @brief Default ctor
|
||||
BinarySemaphoreUsingTask();
|
||||
//! @brief Default dtor
|
||||
virtual~ BinarySemaphoreUsingTask();
|
||||
|
||||
ReturnValue_t acquire(uint32_t timeoutMs =
|
||||
SemaphoreIF::BLOCKING) override;
|
||||
ReturnValue_t release() override;
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
static uint8_t getSemaphoreCounter(TaskHandle_t taskHandle);
|
||||
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t taskHandle,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
/**
|
||||
* Same as acquire() with timeout in FreeRTOS ticks.
|
||||
* @param timeoutTicks
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks =
|
||||
SemaphoreIF::BLOCKING);
|
||||
|
||||
/**
|
||||
* Get handle to the task related to the semaphore.
|
||||
* @return
|
||||
*/
|
||||
TaskHandle_t getTaskHandle();
|
||||
|
||||
/**
|
||||
* Wrapper function to give back semaphore from handle
|
||||
* @param semaphore
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
static ReturnValue_t release(TaskHandle_t taskToNotify);
|
||||
|
||||
/**
|
||||
* 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 should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return - @c RETURN_OK on success
|
||||
* - @c RETURN_FAILED on failure
|
||||
*/
|
||||
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
|
||||
BaseType_t * higherPriorityTaskWoken);
|
||||
|
||||
protected:
|
||||
TaskHandle_t handle;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_BINSEMAPHUSINGTASK_H_ */
|
||||
|
@ -1,11 +1,11 @@
|
||||
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
|
||||
#include <framework/osal/FreeRTOS/TaskManagement.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include "../../osal/FreeRTOS/BinarySemaphore.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
BinarySemaphore::BinarySemaphore() {
|
||||
handle = xSemaphoreCreateBinary();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Semaphore: Binary semaph creation failure" << std::endl;
|
||||
}
|
||||
// Initiated semaphore must be given before it can be taken.
|
||||
xSemaphoreGive(handle);
|
||||
@ -84,7 +84,7 @@ uint8_t BinarySemaphore::getSemaphoreCounter() const {
|
||||
SemaphoreHandle_t BinarySemaphore::getSemaphore() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Be careful with the stack size here. This is called from an ISR!
|
||||
ReturnValue_t BinarySemaphore::releaseFromISR(
|
||||
@ -100,4 +100,4 @@ ReturnValue_t BinarySemaphore::releaseFromISR(
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_BINARYSEMPAHORE_H_
|
||||
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
#include <framework/tasks/SemaphoreIF.h>
|
||||
#include "../../returnvalues/HasReturnvaluesIF.h"
|
||||
#include "../../tasks/SemaphoreIF.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
@ -1,196 +1,196 @@
|
||||
#include <framework/timemanager/Clock.h>
|
||||
#include <framework/globalfunctions/timevalOperations.h>
|
||||
#include <framework/osal/FreeRTOS/Timekeeper.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
//TODO sanitize input?
|
||||
//TODO much of this code can be reused for tick-only systems
|
||||
|
||||
uint16_t Clock::leapSeconds = 0;
|
||||
MutexIF* Clock::timeMutex = nullptr;
|
||||
|
||||
uint32_t Clock::getTicksPerSecond(void) {
|
||||
return 1000;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setClock(const TimeOfDay_t* time) {
|
||||
|
||||
timeval time_timeval;
|
||||
|
||||
ReturnValue_t result = convertTimeOfDayToTimeval(time, &time_timeval);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK){
|
||||
return result;
|
||||
}
|
||||
|
||||
return setClock(&time_timeval);
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setClock(const timeval* time) {
|
||||
timeval uptime = getUptime();
|
||||
|
||||
timeval offset = *time - uptime;
|
||||
|
||||
Timekeeper::instance()->setOffset(offset);
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getClock_timeval(timeval* time) {
|
||||
timeval uptime = getUptime();
|
||||
|
||||
timeval offset = Timekeeper::instance()->getOffset();
|
||||
|
||||
*time = offset + uptime;
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getUptime(timeval* uptime) {
|
||||
*uptime = getUptime();
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
timeval Clock::getUptime() {
|
||||
TickType_t ticksSinceStart = xTaskGetTickCount();
|
||||
return Timekeeper::ticksToTimeval(ticksSinceStart);
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) {
|
||||
timeval uptime = getUptime();
|
||||
*uptimeMs = uptime.tv_sec * 1000 + uptime.tv_usec / 1000;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
|
||||
timeval time_timeval;
|
||||
ReturnValue_t result = getClock_timeval(&time_timeval);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
*time = time_timeval.tv_sec * 1000000 + time_timeval.tv_usec;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) {
|
||||
timeval time_timeval;
|
||||
ReturnValue_t result = getClock_timeval(&time_timeval);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
struct tm time_tm;
|
||||
|
||||
gmtime_r(&time_timeval.tv_sec,&time_tm);
|
||||
|
||||
time->year = time_tm.tm_year + 1900;
|
||||
time->month = time_tm.tm_mon + 1;
|
||||
time->day = time_tm.tm_mday;
|
||||
|
||||
time->hour = time_tm.tm_hour;
|
||||
time->minute = time_tm.tm_min;
|
||||
time->second = time_tm.tm_sec;
|
||||
|
||||
time->usecond = time_timeval.tv_usec;
|
||||
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from,
|
||||
timeval* to) {
|
||||
struct tm time_tm;
|
||||
|
||||
time_tm.tm_year = from->year - 1900;
|
||||
time_tm.tm_mon = from->month - 1;
|
||||
time_tm.tm_mday = from->day;
|
||||
|
||||
time_tm.tm_hour = from->hour;
|
||||
time_tm.tm_min = from->minute;
|
||||
time_tm.tm_sec = from->second;
|
||||
|
||||
time_t seconds = mktime(&time_tm);
|
||||
|
||||
to->tv_sec = seconds;
|
||||
to->tv_usec = from->usecond;
|
||||
//Fails in 2038..
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) {
|
||||
*JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24.
|
||||
/ 3600.;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) {
|
||||
//SHOULDDO: works not for dates in the past (might have less leap seconds)
|
||||
if (timeMutex == nullptr) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
uint16_t leapSeconds;
|
||||
ReturnValue_t result = getLeapSeconds(&leapSeconds);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
timeval leapSeconds_timeval = { 0, 0 };
|
||||
leapSeconds_timeval.tv_sec = leapSeconds;
|
||||
|
||||
//initial offset between UTC and TAI
|
||||
timeval UTCtoTAI1972 = { 10, 0 };
|
||||
|
||||
timeval TAItoTT = { 32, 184000 };
|
||||
|
||||
*tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT;
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) {
|
||||
if (checkOrCreateClockMutex() != HasReturnvaluesIF::RETURN_OK) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
leapSeconds = leapSeconds_;
|
||||
|
||||
result = timeMutex->unlockMutex();
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) {
|
||||
if (timeMutex == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
*leapSeconds_ = leapSeconds;
|
||||
|
||||
result = timeMutex->unlockMutex();
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::checkOrCreateClockMutex() {
|
||||
if (timeMutex == NULL) {
|
||||
MutexFactory* mutexFactory = MutexFactory::instance();
|
||||
if (mutexFactory == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
timeMutex = mutexFactory->createMutex();
|
||||
if (timeMutex == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
#include "../../timemanager/Clock.h"
|
||||
#include "../../globalfunctions/timevalOperations.h"
|
||||
#include "../../osal/FreeRTOS/Timekeeper.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
//TODO sanitize input?
|
||||
//TODO much of this code can be reused for tick-only systems
|
||||
|
||||
uint16_t Clock::leapSeconds = 0;
|
||||
MutexIF* Clock::timeMutex = nullptr;
|
||||
|
||||
uint32_t Clock::getTicksPerSecond(void) {
|
||||
return 1000;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setClock(const TimeOfDay_t* time) {
|
||||
|
||||
timeval time_timeval;
|
||||
|
||||
ReturnValue_t result = convertTimeOfDayToTimeval(time, &time_timeval);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK){
|
||||
return result;
|
||||
}
|
||||
|
||||
return setClock(&time_timeval);
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setClock(const timeval* time) {
|
||||
timeval uptime = getUptime();
|
||||
|
||||
timeval offset = *time - uptime;
|
||||
|
||||
Timekeeper::instance()->setOffset(offset);
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getClock_timeval(timeval* time) {
|
||||
timeval uptime = getUptime();
|
||||
|
||||
timeval offset = Timekeeper::instance()->getOffset();
|
||||
|
||||
*time = offset + uptime;
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getUptime(timeval* uptime) {
|
||||
*uptime = getUptime();
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
timeval Clock::getUptime() {
|
||||
TickType_t ticksSinceStart = xTaskGetTickCount();
|
||||
return Timekeeper::ticksToTimeval(ticksSinceStart);
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) {
|
||||
timeval uptime = getUptime();
|
||||
*uptimeMs = uptime.tv_sec * 1000 + uptime.tv_usec / 1000;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getClock_usecs(uint64_t* time) {
|
||||
timeval time_timeval;
|
||||
ReturnValue_t result = getClock_timeval(&time_timeval);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
*time = time_timeval.tv_sec * 1000000 + time_timeval.tv_usec;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getDateAndTime(TimeOfDay_t* time) {
|
||||
timeval time_timeval;
|
||||
ReturnValue_t result = getClock_timeval(&time_timeval);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
struct tm time_tm;
|
||||
|
||||
gmtime_r(&time_timeval.tv_sec,&time_tm);
|
||||
|
||||
time->year = time_tm.tm_year + 1900;
|
||||
time->month = time_tm.tm_mon + 1;
|
||||
time->day = time_tm.tm_mday;
|
||||
|
||||
time->hour = time_tm.tm_hour;
|
||||
time->minute = time_tm.tm_min;
|
||||
time->second = time_tm.tm_sec;
|
||||
|
||||
time->usecond = time_timeval.tv_usec;
|
||||
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertTimeOfDayToTimeval(const TimeOfDay_t* from,
|
||||
timeval* to) {
|
||||
struct tm time_tm;
|
||||
|
||||
time_tm.tm_year = from->year - 1900;
|
||||
time_tm.tm_mon = from->month - 1;
|
||||
time_tm.tm_mday = from->day;
|
||||
|
||||
time_tm.tm_hour = from->hour;
|
||||
time_tm.tm_min = from->minute;
|
||||
time_tm.tm_sec = from->second;
|
||||
|
||||
time_t seconds = mktime(&time_tm);
|
||||
|
||||
to->tv_sec = seconds;
|
||||
to->tv_usec = from->usecond;
|
||||
//Fails in 2038..
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertTimevalToJD2000(timeval time, double* JD2000) {
|
||||
*JD2000 = (time.tv_sec - 946728000. + time.tv_usec / 1000000.) / 24.
|
||||
/ 3600.;
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::convertUTCToTT(timeval utc, timeval* tt) {
|
||||
//SHOULDDO: works not for dates in the past (might have less leap seconds)
|
||||
if (timeMutex == nullptr) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
uint16_t leapSeconds;
|
||||
ReturnValue_t result = getLeapSeconds(&leapSeconds);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
timeval leapSeconds_timeval = { 0, 0 };
|
||||
leapSeconds_timeval.tv_sec = leapSeconds;
|
||||
|
||||
//initial offset between UTC and TAI
|
||||
timeval UTCtoTAI1972 = { 10, 0 };
|
||||
|
||||
timeval TAItoTT = { 32, 184000 };
|
||||
|
||||
*tt = utc + leapSeconds_timeval + UTCtoTAI1972 + TAItoTT;
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::setLeapSeconds(const uint16_t leapSeconds_) {
|
||||
if (checkOrCreateClockMutex() != HasReturnvaluesIF::RETURN_OK) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
leapSeconds = leapSeconds_;
|
||||
|
||||
result = timeMutex->unlockMutex();
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::getLeapSeconds(uint16_t* leapSeconds_) {
|
||||
if (timeMutex == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
ReturnValue_t result = timeMutex->lockMutex(MutexIF::BLOCKING);
|
||||
if (result != HasReturnvaluesIF::RETURN_OK) {
|
||||
return result;
|
||||
}
|
||||
|
||||
*leapSeconds_ = leapSeconds;
|
||||
|
||||
result = timeMutex->unlockMutex();
|
||||
return result;
|
||||
}
|
||||
|
||||
ReturnValue_t Clock::checkOrCreateClockMutex() {
|
||||
if (timeMutex == NULL) {
|
||||
MutexFactory* mutexFactory = MutexFactory::instance();
|
||||
if (mutexFactory == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
timeMutex = mutexFactory->createMutex();
|
||||
if (timeMutex == NULL) {
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
@ -1,110 +1,110 @@
|
||||
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h>
|
||||
#include <framework/osal/FreeRTOS/TaskManagement.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount,
|
||||
uint8_t initCount): maxCount(maxCount) {
|
||||
if(initCount > maxCount) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
||||
"intial cout. Setting initial count to max count." << std::endl;
|
||||
initCount = maxCount;
|
||||
}
|
||||
|
||||
handle = TaskManagement::getCurrentTaskHandle();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Could not retrieve task "
|
||||
"handle. Please ensure the constructor was called inside a "
|
||||
"task." << std::endl;
|
||||
}
|
||||
|
||||
uint32_t oldNotificationValue;
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite,
|
||||
&oldNotificationValue);
|
||||
if(oldNotificationValue != 0) {
|
||||
sif::warning << "CountinSemaphoreUsingTask: Semaphore initiated but "
|
||||
"current notification value is not 0. Please ensure the "
|
||||
"notification value is not used for other purposes!" << std::endl;
|
||||
}
|
||||
for(int i = 0; i < initCount; i++) {
|
||||
xTaskNotifyGive(handle);
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
|
||||
// Clear notification value on destruction.
|
||||
// If this is not desired, don't call the destructor
|
||||
// (or implement a boolean which disables the reset)
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) {
|
||||
TickType_t timeout = SemaphoreIF::POLLING;
|
||||
if(timeoutMs == SemaphoreIF::BLOCKING) {
|
||||
timeout = SemaphoreIF::BLOCKING;
|
||||
}
|
||||
else if(timeoutMs > SemaphoreIF::POLLING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
return acquireWithTickTimeout(timeout);
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout(
|
||||
TickType_t timeoutTicks) {
|
||||
// Decrement notfication value without resetting it.
|
||||
BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
|
||||
if (getSemaphoreCounter() == oldCount - 1) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::release() {
|
||||
if(getSemaphoreCounter() == maxCount) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
return release(handle);
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::release(
|
||||
TaskHandle_t taskToNotify) {
|
||||
BaseType_t returncode = xTaskNotifyGive(taskToNotify);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
// This should never happen.
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const {
|
||||
uint32_t notificationValue = 0;
|
||||
xTaskNotifyAndQuery(handle, 0, eNoAction, ¬ificationValue);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
TaskHandle_t CountingSemaphoreUsingTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::releaseFromISR(
|
||||
TaskHandle_t taskToNotify, BaseType_t* higherPriorityTaskWoken) {
|
||||
vTaskNotifyGiveFromISR(taskToNotify, higherPriorityTaskWoken);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounterFromISR(
|
||||
TaskHandle_t task, BaseType_t* higherPriorityTaskWoken) {
|
||||
uint32_t notificationValue;
|
||||
xTaskNotifyAndQueryFromISR(task, 0, eNoAction, ¬ificationValue,
|
||||
higherPriorityTaskWoken);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getMaxCount() const {
|
||||
return maxCount;
|
||||
}
|
||||
#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(const uint8_t maxCount,
|
||||
uint8_t initCount): maxCount(maxCount) {
|
||||
if(initCount > maxCount) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
||||
"intial cout. Setting initial count to max count." << std::endl;
|
||||
initCount = maxCount;
|
||||
}
|
||||
|
||||
handle = TaskManagement::getCurrentTaskHandle();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Could not retrieve task "
|
||||
"handle. Please ensure the constructor was called inside a "
|
||||
"task." << std::endl;
|
||||
}
|
||||
|
||||
uint32_t oldNotificationValue;
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite,
|
||||
&oldNotificationValue);
|
||||
if(oldNotificationValue != 0) {
|
||||
sif::warning << "CountinSemaphoreUsingTask: Semaphore initiated but "
|
||||
"current notification value is not 0. Please ensure the "
|
||||
"notification value is not used for other purposes!" << std::endl;
|
||||
}
|
||||
for(int i = 0; i < initCount; i++) {
|
||||
xTaskNotifyGive(handle);
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphoreUsingTask::~CountingSemaphoreUsingTask() {
|
||||
// Clear notification value on destruction.
|
||||
// If this is not desired, don't call the destructor
|
||||
// (or implement a boolean which disables the reset)
|
||||
xTaskNotifyAndQuery(handle, 0, eSetValueWithOverwrite, nullptr);
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::acquire(uint32_t timeoutMs) {
|
||||
TickType_t timeout = SemaphoreIF::POLLING;
|
||||
if(timeoutMs == SemaphoreIF::BLOCKING) {
|
||||
timeout = SemaphoreIF::BLOCKING;
|
||||
}
|
||||
else if(timeoutMs > SemaphoreIF::POLLING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
return acquireWithTickTimeout(timeout);
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::acquireWithTickTimeout(
|
||||
TickType_t timeoutTicks) {
|
||||
// Decrement notfication value without resetting it.
|
||||
BaseType_t oldCount = ulTaskNotifyTake(pdFALSE, timeoutTicks);
|
||||
if (getSemaphoreCounter() == oldCount - 1) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
return SemaphoreIF::SEMAPHORE_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::release() {
|
||||
if(getSemaphoreCounter() == maxCount) {
|
||||
return SemaphoreIF::SEMAPHORE_NOT_OWNED;
|
||||
}
|
||||
return release(handle);
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::release(
|
||||
TaskHandle_t taskToNotify) {
|
||||
BaseType_t returncode = xTaskNotifyGive(taskToNotify);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
else {
|
||||
// This should never happen.
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounter() const {
|
||||
uint32_t notificationValue = 0;
|
||||
xTaskNotifyAndQuery(handle, 0, eNoAction, ¬ificationValue);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
TaskHandle_t CountingSemaphoreUsingTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
ReturnValue_t CountingSemaphoreUsingTask::releaseFromISR(
|
||||
TaskHandle_t taskToNotify, BaseType_t* higherPriorityTaskWoken) {
|
||||
vTaskNotifyGiveFromISR(taskToNotify, higherPriorityTaskWoken);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getSemaphoreCounterFromISR(
|
||||
TaskHandle_t task, BaseType_t* higherPriorityTaskWoken) {
|
||||
uint32_t notificationValue;
|
||||
xTaskNotifyAndQueryFromISR(task, 0, eNoAction, ¬ificationValue,
|
||||
higherPriorityTaskWoken);
|
||||
return notificationValue;
|
||||
}
|
||||
|
||||
uint8_t CountingSemaphoreUsingTask::getMaxCount() const {
|
||||
return maxCount;
|
||||
}
|
||||
|
@ -1,100 +1,100 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
|
||||
|
||||
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h>
|
||||
#include <framework/tasks/SemaphoreIF.h>
|
||||
|
||||
extern "C" {
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Couting Semaphore implementation which uses the notification value
|
||||
* of the task. The notification value should therefore not be used
|
||||
* for other purposes.
|
||||
* @details
|
||||
* Additional information: https://www.freertos.org/RTOS-task-notifications.html
|
||||
* and general semaphore documentation.
|
||||
*/
|
||||
class CountingSemaphoreUsingTask: public SemaphoreIF {
|
||||
public:
|
||||
CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount);
|
||||
virtual ~CountingSemaphoreUsingTask();
|
||||
|
||||
/**
|
||||
* Acquire the counting semaphore.
|
||||
* If no semaphores are available, the task will be blocked
|
||||
* for a maximum of #timeoutMs or until one is given back,
|
||||
* for example by an ISR or another task.
|
||||
* @param timeoutMs
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquire(uint32_t timeoutMs = SemaphoreIF::BLOCKING) override;
|
||||
|
||||
/**
|
||||
* Release a semaphore, increasing the number of available counting
|
||||
* semaphores up to the #maxCount value.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
ReturnValue_t release() override;
|
||||
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
/**
|
||||
* Get the semaphore counter from an ISR.
|
||||
* @param task
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return
|
||||
*/
|
||||
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t task,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
/**
|
||||
* Acquire with a timeout value in ticks
|
||||
* @param timeoutTicks
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquireWithTickTimeout(
|
||||
TickType_t timeoutTicks = SemaphoreIF::BLOCKING);
|
||||
|
||||
/**
|
||||
* Get handle to the task related to the semaphore.
|
||||
* @return
|
||||
*/
|
||||
TaskHandle_t getTaskHandle();
|
||||
|
||||
/**
|
||||
* Release semaphore of task by supplying task handle
|
||||
* @param taskToNotify
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t release(TaskHandle_t taskToNotify);
|
||||
/**
|
||||
* Release seamphore of a task from an ISR.
|
||||
* @param taskToNotify
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
uint8_t getMaxCount() const;
|
||||
|
||||
private:
|
||||
TaskHandle_t handle;
|
||||
const uint8_t maxCount;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_
|
||||
|
||||
#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
|
||||
#include "../../tasks/SemaphoreIF.h"
|
||||
|
||||
extern "C" {
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Couting Semaphore implementation which uses the notification value
|
||||
* of the task. The notification value should therefore not be used
|
||||
* for other purposes.
|
||||
* @details
|
||||
* Additional information: https://www.freertos.org/RTOS-task-notifications.html
|
||||
* and general semaphore documentation.
|
||||
*/
|
||||
class CountingSemaphoreUsingTask: public SemaphoreIF {
|
||||
public:
|
||||
CountingSemaphoreUsingTask(const uint8_t maxCount, uint8_t initCount);
|
||||
virtual ~CountingSemaphoreUsingTask();
|
||||
|
||||
/**
|
||||
* Acquire the counting semaphore.
|
||||
* If no semaphores are available, the task will be blocked
|
||||
* for a maximum of #timeoutMs or until one is given back,
|
||||
* for example by an ISR or another task.
|
||||
* @param timeoutMs
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquire(uint32_t timeoutMs = SemaphoreIF::BLOCKING) override;
|
||||
|
||||
/**
|
||||
* Release a semaphore, increasing the number of available counting
|
||||
* semaphores up to the #maxCount value.
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
ReturnValue_t release() override;
|
||||
|
||||
uint8_t getSemaphoreCounter() const override;
|
||||
/**
|
||||
* Get the semaphore counter from an ISR.
|
||||
* @param task
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return
|
||||
*/
|
||||
static uint8_t getSemaphoreCounterFromISR(TaskHandle_t task,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
/**
|
||||
* Acquire with a timeout value in ticks
|
||||
* @param timeoutTicks
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_TIMEOUT on timeout
|
||||
*/
|
||||
ReturnValue_t acquireWithTickTimeout(
|
||||
TickType_t timeoutTicks = SemaphoreIF::BLOCKING);
|
||||
|
||||
/**
|
||||
* Get handle to the task related to the semaphore.
|
||||
* @return
|
||||
*/
|
||||
TaskHandle_t getTaskHandle();
|
||||
|
||||
/**
|
||||
* Release semaphore of task by supplying task handle
|
||||
* @param taskToNotify
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t release(TaskHandle_t taskToNotify);
|
||||
/**
|
||||
* Release seamphore of a task from an ISR.
|
||||
* @param taskToNotify
|
||||
* @param higherPriorityTaskWoken This will be set to pdPASS if a task with
|
||||
* a higher priority was unblocked. A context switch should be requested
|
||||
* from an ISR if this is the case (see TaskManagement functions)
|
||||
* @return -@c RETURN_OK on success
|
||||
* -@c SemaphoreIF::SEMAPHORE_NOT_OWNED if #maxCount semaphores are
|
||||
* already available.
|
||||
*/
|
||||
static ReturnValue_t releaseFromISR(TaskHandle_t taskToNotify,
|
||||
BaseType_t* higherPriorityTaskWoken);
|
||||
|
||||
uint8_t getMaxCount() const;
|
||||
|
||||
private:
|
||||
TaskHandle_t handle;
|
||||
const uint8_t maxCount;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */
|
||||
|
@ -1,43 +1,43 @@
|
||||
#include <framework/osal/FreeRTOS/CountingSemaphore.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include <framework/osal/FreeRTOS/TaskManagement.h>
|
||||
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
// Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in
|
||||
// free FreeRTOSConfig.h file.
|
||||
CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount):
|
||||
maxCount(maxCount), initCount(initCount) {
|
||||
if(initCount > maxCount) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
||||
"intial cout. Setting initial count to max count." << std::endl;
|
||||
initCount = maxCount;
|
||||
}
|
||||
|
||||
handle = xSemaphoreCreateCounting(maxCount, initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphore::CountingSemaphore(CountingSemaphore&& other):
|
||||
maxCount(other.maxCount), initCount(other.initCount) {
|
||||
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphore& CountingSemaphore::operator =(
|
||||
CountingSemaphore&& other) {
|
||||
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
return * this;
|
||||
}
|
||||
|
||||
|
||||
uint8_t CountingSemaphore::getMaxCount() const {
|
||||
return maxCount;
|
||||
}
|
||||
#include "../../osal/FreeRTOS/CountingSemaphore.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
// Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in
|
||||
// free FreeRTOSConfig.h file.
|
||||
CountingSemaphore::CountingSemaphore(const uint8_t maxCount, uint8_t initCount):
|
||||
maxCount(maxCount), initCount(initCount) {
|
||||
if(initCount > maxCount) {
|
||||
sif::error << "CountingSemaphoreUsingTask: Max count bigger than "
|
||||
"intial cout. Setting initial count to max count." << std::endl;
|
||||
initCount = maxCount;
|
||||
}
|
||||
|
||||
handle = xSemaphoreCreateCounting(maxCount, initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphore::CountingSemaphore(CountingSemaphore&& other):
|
||||
maxCount(other.maxCount), initCount(other.initCount) {
|
||||
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
CountingSemaphore& CountingSemaphore::operator =(
|
||||
CountingSemaphore&& other) {
|
||||
handle = xSemaphoreCreateCounting(other.maxCount, other.initCount);
|
||||
if(handle == nullptr) {
|
||||
sif::error << "CountingSemaphore: Creation failure" << std::endl;
|
||||
}
|
||||
return * this;
|
||||
}
|
||||
|
||||
|
||||
uint8_t CountingSemaphore::getMaxCount() const {
|
||||
return maxCount;
|
||||
}
|
||||
|
@ -1,34 +1,34 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
|
||||
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
|
||||
|
||||
/**
|
||||
* @brief Counting semaphores, which can be acquire more than once.
|
||||
* @details
|
||||
* See: https://www.freertos.org/CreateCounting.html
|
||||
* API of counting semaphores is almost identical to binary semaphores,
|
||||
* so we just inherit from binary semaphore and provide the respective
|
||||
* constructors.
|
||||
*/
|
||||
class CountingSemaphore: public BinarySemaphore {
|
||||
public:
|
||||
CountingSemaphore(const uint8_t maxCount, uint8_t initCount);
|
||||
//! @brief Copy ctor, disabled
|
||||
CountingSemaphore(const CountingSemaphore&) = delete;
|
||||
//! @brief Copy assignment, disabled
|
||||
CountingSemaphore& operator=(const CountingSemaphore&) = delete;
|
||||
//! @brief Move ctor
|
||||
CountingSemaphore (CountingSemaphore &&);
|
||||
//! @brief Move assignment
|
||||
CountingSemaphore & operator=(CountingSemaphore &&);
|
||||
|
||||
/* Same API as binary semaphore otherwise. acquire() can be called
|
||||
* until there are not semaphores left and release() can be called
|
||||
* until maxCount is reached. */
|
||||
uint8_t getMaxCount() const;
|
||||
private:
|
||||
const uint8_t maxCount;
|
||||
uint8_t initCount = 0;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_
|
||||
#include "../../osal/FreeRTOS/BinarySemaphore.h"
|
||||
|
||||
/**
|
||||
* @brief Counting semaphores, which can be acquire more than once.
|
||||
* @details
|
||||
* See: https://www.freertos.org/CreateCounting.html
|
||||
* API of counting semaphores is almost identical to binary semaphores,
|
||||
* so we just inherit from binary semaphore and provide the respective
|
||||
* constructors.
|
||||
*/
|
||||
class CountingSemaphore: public BinarySemaphore {
|
||||
public:
|
||||
CountingSemaphore(const uint8_t maxCount, uint8_t initCount);
|
||||
//! @brief Copy ctor, disabled
|
||||
CountingSemaphore(const CountingSemaphore&) = delete;
|
||||
//! @brief Copy assignment, disabled
|
||||
CountingSemaphore& operator=(const CountingSemaphore&) = delete;
|
||||
//! @brief Move ctor
|
||||
CountingSemaphore (CountingSemaphore &&);
|
||||
//! @brief Move assignment
|
||||
CountingSemaphore & operator=(CountingSemaphore &&);
|
||||
|
||||
/* Same API as binary semaphore otherwise. acquire() can be called
|
||||
* until there are not semaphores left and release() can be called
|
||||
* until maxCount is reached. */
|
||||
uint8_t getMaxCount() const;
|
||||
private:
|
||||
const uint8_t maxCount;
|
||||
uint8_t initCount = 0;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHORE_H_ */
|
||||
|
@ -1,162 +1,162 @@
|
||||
#include "FixedTimeslotTask.h"
|
||||
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
uint32_t FixedTimeslotTask::deadlineMissedCount = 0;
|
||||
const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = configMINIMAL_STACK_SIZE;
|
||||
|
||||
FixedTimeslotTask::FixedTimeslotTask(TaskName name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod overallPeriod,
|
||||
void (*setDeadlineMissedFunc)()) :
|
||||
started(false), handle(NULL), pst(overallPeriod * 1000) {
|
||||
configSTACK_DEPTH_TYPE stackSize = setStack / sizeof(configSTACK_DEPTH_TYPE);
|
||||
xTaskCreate(taskEntryPoint, name, stackSize, this, setPriority, &handle);
|
||||
// All additional attributes are applied to the object.
|
||||
this->deadlineMissedFunc = setDeadlineMissedFunc;
|
||||
}
|
||||
|
||||
FixedTimeslotTask::~FixedTimeslotTask() {
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::taskEntryPoint(void* argument) {
|
||||
|
||||
// The argument is re-interpreted as FixedTimeslotTask. The Task object is
|
||||
// global, so it is found from any place.
|
||||
FixedTimeslotTask *originalTask(reinterpret_cast<FixedTimeslotTask*>(argument));
|
||||
/* Task should not start until explicitly requested,
|
||||
* but in FreeRTOS, tasks start as soon as they are created if the scheduler
|
||||
* is running but not if the scheduler is not running.
|
||||
* To be able to accommodate both cases we check a member which is set in
|
||||
* #startTask(). If it is not set and we get here, the scheduler was started
|
||||
* before #startTask() was called and we need to suspend if it is set,
|
||||
* the scheduler was not running before #startTask() was called and we
|
||||
* can continue */
|
||||
|
||||
if (not originalTask->started) {
|
||||
vTaskSuspend(NULL);
|
||||
}
|
||||
|
||||
originalTask->taskFunctionality();
|
||||
sif::debug << "Polling task " << originalTask->handle
|
||||
<< " returned from taskFunctionality." << std::endl;
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::missedDeadlineCounter() {
|
||||
FixedTimeslotTask::deadlineMissedCount++;
|
||||
if (FixedTimeslotTask::deadlineMissedCount % 10 == 0) {
|
||||
sif::error << "PST missed " << FixedTimeslotTask::deadlineMissedCount
|
||||
<< " deadlines." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::startTask() {
|
||||
started = true;
|
||||
|
||||
// We must not call resume if scheduler is not started yet
|
||||
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
|
||||
vTaskResume(handle);
|
||||
}
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId,
|
||||
uint32_t slotTimeMs, int8_t executionStep) {
|
||||
if (objectManager->get<ExecutableObjectIF>(componentId) != nullptr) {
|
||||
pst.addSlot(componentId, slotTimeMs, executionStep, this);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
sif::error << "Component " << std::hex << componentId <<
|
||||
" not found, not adding it to pst" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
uint32_t FixedTimeslotTask::getPeriodMs() const {
|
||||
return pst.getLengthMs();
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::checkSequence() const {
|
||||
return pst.checkSequence();
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::taskFunctionality() {
|
||||
// A local iterator for the Polling Sequence Table is created to find the
|
||||
// start time for the first entry.
|
||||
auto slotListIter = pst.current;
|
||||
|
||||
//The start time for the first entry is read.
|
||||
uint32_t intervalMs = slotListIter->pollingTimeMs;
|
||||
TickType_t interval = pdMS_TO_TICKS(intervalMs);
|
||||
|
||||
TickType_t xLastWakeTime;
|
||||
/* The xLastWakeTime variable needs to be initialized with the current tick
|
||||
count. Note that this is the only time the variable is written to
|
||||
explicitly. After this assignment, xLastWakeTime is updated automatically
|
||||
internally within vTaskDelayUntil(). */
|
||||
xLastWakeTime = xTaskGetTickCount();
|
||||
|
||||
// wait for first entry's start time
|
||||
if(interval > 0) {
|
||||
vTaskDelayUntil(&xLastWakeTime, interval);
|
||||
}
|
||||
|
||||
/* Enter the loop that defines the task behavior. */
|
||||
for (;;) {
|
||||
//The component for this slot is executed and the next one is chosen.
|
||||
this->pst.executeAndAdvance();
|
||||
if (not pst.slotFollowsImmediately()) {
|
||||
// Get the interval till execution of the next slot.
|
||||
intervalMs = this->pst.getIntervalToPreviousSlotMs();
|
||||
interval = pdMS_TO_TICKS(intervalMs);
|
||||
|
||||
checkMissedDeadline(xLastWakeTime, interval);
|
||||
|
||||
// Wait for the interval. This exits immediately if a deadline was
|
||||
// missed while also updating the last wake time.
|
||||
vTaskDelayUntil(&xLastWakeTime, interval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval) {
|
||||
/* Check whether deadline was missed while also taking overflows
|
||||
* into account. Drawing this on paper with a timeline helps to understand
|
||||
* it. */
|
||||
TickType_t currentTickCount = xTaskGetTickCount();
|
||||
TickType_t timeToWake = xLastWakeTime + interval;
|
||||
// Time to wake has not overflown.
|
||||
if(timeToWake > xLastWakeTime) {
|
||||
/* If the current time has overflown exclusively or the current
|
||||
* tick count is simply larger than the time to wake, a deadline was
|
||||
* missed */
|
||||
if((currentTickCount < xLastWakeTime) or (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
/* Time to wake has overflown. A deadline was missed if the current time
|
||||
* is larger than the time to wake */
|
||||
else if((timeToWake < xLastWakeTime) and (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::handleMissedDeadline() {
|
||||
#ifdef DEBUG
|
||||
sif::warning << "FixedTimeslotTask: " << pcTaskGetName(NULL) <<
|
||||
" missed deadline!\n" << std::flush;
|
||||
#endif
|
||||
if(deadlineMissedFunc != nullptr) {
|
||||
this->deadlineMissedFunc();
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::sleepFor(uint32_t ms) {
|
||||
vTaskDelay(pdMS_TO_TICKS(ms));
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
TaskHandle_t FixedTimeslotTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
#include "FixedTimeslotTask.h"
|
||||
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
uint32_t FixedTimeslotTask::deadlineMissedCount = 0;
|
||||
const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = configMINIMAL_STACK_SIZE;
|
||||
|
||||
FixedTimeslotTask::FixedTimeslotTask(TaskName name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod overallPeriod,
|
||||
void (*setDeadlineMissedFunc)()) :
|
||||
started(false), handle(NULL), pst(overallPeriod * 1000) {
|
||||
configSTACK_DEPTH_TYPE stackSize = setStack / sizeof(configSTACK_DEPTH_TYPE);
|
||||
xTaskCreate(taskEntryPoint, name, stackSize, this, setPriority, &handle);
|
||||
// All additional attributes are applied to the object.
|
||||
this->deadlineMissedFunc = setDeadlineMissedFunc;
|
||||
}
|
||||
|
||||
FixedTimeslotTask::~FixedTimeslotTask() {
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::taskEntryPoint(void* argument) {
|
||||
|
||||
// The argument is re-interpreted as FixedTimeslotTask. The Task object is
|
||||
// global, so it is found from any place.
|
||||
FixedTimeslotTask *originalTask(reinterpret_cast<FixedTimeslotTask*>(argument));
|
||||
/* Task should not start until explicitly requested,
|
||||
* but in FreeRTOS, tasks start as soon as they are created if the scheduler
|
||||
* is running but not if the scheduler is not running.
|
||||
* To be able to accommodate both cases we check a member which is set in
|
||||
* #startTask(). If it is not set and we get here, the scheduler was started
|
||||
* before #startTask() was called and we need to suspend if it is set,
|
||||
* the scheduler was not running before #startTask() was called and we
|
||||
* can continue */
|
||||
|
||||
if (not originalTask->started) {
|
||||
vTaskSuspend(NULL);
|
||||
}
|
||||
|
||||
originalTask->taskFunctionality();
|
||||
sif::debug << "Polling task " << originalTask->handle
|
||||
<< " returned from taskFunctionality." << std::endl;
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::missedDeadlineCounter() {
|
||||
FixedTimeslotTask::deadlineMissedCount++;
|
||||
if (FixedTimeslotTask::deadlineMissedCount % 10 == 0) {
|
||||
sif::error << "PST missed " << FixedTimeslotTask::deadlineMissedCount
|
||||
<< " deadlines." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::startTask() {
|
||||
started = true;
|
||||
|
||||
// We must not call resume if scheduler is not started yet
|
||||
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
|
||||
vTaskResume(handle);
|
||||
}
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId,
|
||||
uint32_t slotTimeMs, int8_t executionStep) {
|
||||
if (objectManager->get<ExecutableObjectIF>(componentId) != nullptr) {
|
||||
pst.addSlot(componentId, slotTimeMs, executionStep, this);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
sif::error << "Component " << std::hex << componentId <<
|
||||
" not found, not adding it to pst" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
|
||||
uint32_t FixedTimeslotTask::getPeriodMs() const {
|
||||
return pst.getLengthMs();
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::checkSequence() const {
|
||||
return pst.checkSequence();
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::taskFunctionality() {
|
||||
// A local iterator for the Polling Sequence Table is created to find the
|
||||
// start time for the first entry.
|
||||
auto slotListIter = pst.current;
|
||||
|
||||
//The start time for the first entry is read.
|
||||
uint32_t intervalMs = slotListIter->pollingTimeMs;
|
||||
TickType_t interval = pdMS_TO_TICKS(intervalMs);
|
||||
|
||||
TickType_t xLastWakeTime;
|
||||
/* The xLastWakeTime variable needs to be initialized with the current tick
|
||||
count. Note that this is the only time the variable is written to
|
||||
explicitly. After this assignment, xLastWakeTime is updated automatically
|
||||
internally within vTaskDelayUntil(). */
|
||||
xLastWakeTime = xTaskGetTickCount();
|
||||
|
||||
// wait for first entry's start time
|
||||
if(interval > 0) {
|
||||
vTaskDelayUntil(&xLastWakeTime, interval);
|
||||
}
|
||||
|
||||
/* Enter the loop that defines the task behavior. */
|
||||
for (;;) {
|
||||
//The component for this slot is executed and the next one is chosen.
|
||||
this->pst.executeAndAdvance();
|
||||
if (not pst.slotFollowsImmediately()) {
|
||||
// Get the interval till execution of the next slot.
|
||||
intervalMs = this->pst.getIntervalToPreviousSlotMs();
|
||||
interval = pdMS_TO_TICKS(intervalMs);
|
||||
|
||||
checkMissedDeadline(xLastWakeTime, interval);
|
||||
|
||||
// Wait for the interval. This exits immediately if a deadline was
|
||||
// missed while also updating the last wake time.
|
||||
vTaskDelayUntil(&xLastWakeTime, interval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval) {
|
||||
/* Check whether deadline was missed while also taking overflows
|
||||
* into account. Drawing this on paper with a timeline helps to understand
|
||||
* it. */
|
||||
TickType_t currentTickCount = xTaskGetTickCount();
|
||||
TickType_t timeToWake = xLastWakeTime + interval;
|
||||
// Time to wake has not overflown.
|
||||
if(timeToWake > xLastWakeTime) {
|
||||
/* If the current time has overflown exclusively or the current
|
||||
* tick count is simply larger than the time to wake, a deadline was
|
||||
* missed */
|
||||
if((currentTickCount < xLastWakeTime) or (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
/* Time to wake has overflown. A deadline was missed if the current time
|
||||
* is larger than the time to wake */
|
||||
else if((timeToWake < xLastWakeTime) and (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
|
||||
void FixedTimeslotTask::handleMissedDeadline() {
|
||||
#ifdef DEBUG
|
||||
sif::warning << "FixedTimeslotTask: " << pcTaskGetName(NULL) <<
|
||||
" missed deadline!\n" << std::flush;
|
||||
#endif
|
||||
if(deadlineMissedFunc != nullptr) {
|
||||
this->deadlineMissedFunc();
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t FixedTimeslotTask::sleepFor(uint32_t ms) {
|
||||
vTaskDelay(pdMS_TO_TICKS(ms));
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
TaskHandle_t FixedTimeslotTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
@ -1,101 +1,101 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_
|
||||
|
||||
#include <framework/osal/FreeRTOS/FreeRTOSTaskIF.h>
|
||||
#include <framework/tasks/FixedSlotSequence.h>
|
||||
#include <framework/tasks/FixedTimeslotTaskIF.h>
|
||||
#include <framework/tasks/Typedef.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
class FixedTimeslotTask: public FixedTimeslotTaskIF, public FreeRTOSTaskIF {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Keep in mind that you need to call before vTaskStartScheduler()!
|
||||
* A lot of task parameters are set in "FreeRTOSConfig.h".
|
||||
* @param name Name of the task, lenght limited by configMAX_TASK_NAME_LEN
|
||||
* @param setPriority Number of priorities specified by
|
||||
* configMAX_PRIORITIES. High taskPriority_ number means high priority.
|
||||
* @param setStack Stack size in words (not bytes!).
|
||||
* Lower limit specified by configMINIMAL_STACK_SIZE
|
||||
* @param overallPeriod Period in seconds.
|
||||
* @param setDeadlineMissedFunc Callback if a deadline was missed.
|
||||
* @return Pointer to the newly created task.
|
||||
*/
|
||||
FixedTimeslotTask(TaskName name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod overallPeriod,
|
||||
void (*setDeadlineMissedFunc)());
|
||||
|
||||
/**
|
||||
* @brief The destructor of the class.
|
||||
* @details
|
||||
* The destructor frees all heap memory that was allocated on thread
|
||||
* initialization for the PST and the device handlers. This is done by
|
||||
* calling the PST's destructor.
|
||||
*/
|
||||
virtual ~FixedTimeslotTask(void);
|
||||
|
||||
ReturnValue_t startTask(void);
|
||||
/**
|
||||
* This static function can be used as #deadlineMissedFunc.
|
||||
* It counts missedDeadlines and prints the number of missed deadlines
|
||||
* every 10th time.
|
||||
*/
|
||||
static void missedDeadlineCounter();
|
||||
/**
|
||||
* A helper variable to count missed deadlines.
|
||||
*/
|
||||
static uint32_t deadlineMissedCount;
|
||||
|
||||
ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs,
|
||||
int8_t executionStep) override;
|
||||
|
||||
uint32_t getPeriodMs() const override;
|
||||
|
||||
ReturnValue_t checkSequence() const override;
|
||||
|
||||
ReturnValue_t sleepFor(uint32_t ms) override;
|
||||
|
||||
TaskHandle_t getTaskHandle() override;
|
||||
|
||||
protected:
|
||||
bool started;
|
||||
TaskHandle_t handle;
|
||||
|
||||
FixedSlotSequence pst;
|
||||
|
||||
/**
|
||||
* @brief This attribute holds a function pointer that is executed when
|
||||
* a deadline was missed.
|
||||
* @details
|
||||
* Another function may be announced to determine the actions to perform
|
||||
* when a deadline was missed. Currently, only one function for missing
|
||||
* any deadline is allowed. If not used, it shall be declared NULL.
|
||||
*/
|
||||
void (*deadlineMissedFunc)(void);
|
||||
/**
|
||||
* @brief This is the entry point for a new task.
|
||||
* @details
|
||||
* This method starts the task by calling taskFunctionality(), as soon as
|
||||
* all requirements (task scheduler has started and startTask()
|
||||
* has been called) are met.
|
||||
*/
|
||||
static void taskEntryPoint(void* argument);
|
||||
|
||||
/**
|
||||
* @brief This function holds the main functionality of the thread.
|
||||
* @details
|
||||
* Core function holding the main functionality of the task
|
||||
* It links the functionalities provided by FixedSlotSequence with the
|
||||
* OS's System Calls to keep the timing of the periods.
|
||||
*/
|
||||
void taskFunctionality(void);
|
||||
|
||||
void checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval);
|
||||
void handleMissedDeadline();
|
||||
};
|
||||
|
||||
#endif /* POLLINGTASK_H_ */
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_FIXEDTIMESLOTTASK_H_
|
||||
|
||||
#include "../../osal/FreeRTOS/FreeRTOSTaskIF.h"
|
||||
#include "../../tasks/FixedSlotSequence.h"
|
||||
#include "../../tasks/FixedTimeslotTaskIF.h"
|
||||
#include "../../tasks/Typedef.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
class FixedTimeslotTask: public FixedTimeslotTaskIF, public FreeRTOSTaskIF {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Keep in mind that you need to call before vTaskStartScheduler()!
|
||||
* A lot of task parameters are set in "FreeRTOSConfig.h".
|
||||
* @param name Name of the task, lenght limited by configMAX_TASK_NAME_LEN
|
||||
* @param setPriority Number of priorities specified by
|
||||
* configMAX_PRIORITIES. High taskPriority_ number means high priority.
|
||||
* @param setStack Stack size in words (not bytes!).
|
||||
* Lower limit specified by configMINIMAL_STACK_SIZE
|
||||
* @param overallPeriod Period in seconds.
|
||||
* @param setDeadlineMissedFunc Callback if a deadline was missed.
|
||||
* @return Pointer to the newly created task.
|
||||
*/
|
||||
FixedTimeslotTask(TaskName name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod overallPeriod,
|
||||
void (*setDeadlineMissedFunc)());
|
||||
|
||||
/**
|
||||
* @brief The destructor of the class.
|
||||
* @details
|
||||
* The destructor frees all heap memory that was allocated on thread
|
||||
* initialization for the PST and the device handlers. This is done by
|
||||
* calling the PST's destructor.
|
||||
*/
|
||||
virtual ~FixedTimeslotTask(void);
|
||||
|
||||
ReturnValue_t startTask(void);
|
||||
/**
|
||||
* This static function can be used as #deadlineMissedFunc.
|
||||
* It counts missedDeadlines and prints the number of missed deadlines
|
||||
* every 10th time.
|
||||
*/
|
||||
static void missedDeadlineCounter();
|
||||
/**
|
||||
* A helper variable to count missed deadlines.
|
||||
*/
|
||||
static uint32_t deadlineMissedCount;
|
||||
|
||||
ReturnValue_t addSlot(object_id_t componentId, uint32_t slotTimeMs,
|
||||
int8_t executionStep) override;
|
||||
|
||||
uint32_t getPeriodMs() const override;
|
||||
|
||||
ReturnValue_t checkSequence() const override;
|
||||
|
||||
ReturnValue_t sleepFor(uint32_t ms) override;
|
||||
|
||||
TaskHandle_t getTaskHandle() override;
|
||||
|
||||
protected:
|
||||
bool started;
|
||||
TaskHandle_t handle;
|
||||
|
||||
FixedSlotSequence pst;
|
||||
|
||||
/**
|
||||
* @brief This attribute holds a function pointer that is executed when
|
||||
* a deadline was missed.
|
||||
* @details
|
||||
* Another function may be announced to determine the actions to perform
|
||||
* when a deadline was missed. Currently, only one function for missing
|
||||
* any deadline is allowed. If not used, it shall be declared NULL.
|
||||
*/
|
||||
void (*deadlineMissedFunc)(void);
|
||||
/**
|
||||
* @brief This is the entry point for a new task.
|
||||
* @details
|
||||
* This method starts the task by calling taskFunctionality(), as soon as
|
||||
* all requirements (task scheduler has started and startTask()
|
||||
* has been called) are met.
|
||||
*/
|
||||
static void taskEntryPoint(void* argument);
|
||||
|
||||
/**
|
||||
* @brief This function holds the main functionality of the thread.
|
||||
* @details
|
||||
* Core function holding the main functionality of the task
|
||||
* It links the functionalities provided by FixedSlotSequence with the
|
||||
* OS's System Calls to keep the timing of the periods.
|
||||
*/
|
||||
void taskFunctionality(void);
|
||||
|
||||
void checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval);
|
||||
void handleMissedDeadline();
|
||||
};
|
||||
|
||||
#endif /* POLLINGTASK_H_ */
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <framework/osal/FreeRTOS/MessageQueue.h>
|
||||
#include "../../osal/FreeRTOS/MessageQueue.h"
|
||||
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
// TODO I guess we should have a way of checking if we are in an ISR and then
|
||||
// use the "fromISR" versions of all calls
|
||||
|
@ -1,212 +1,212 @@
|
||||
#ifndef MESSAGEQUEUE_H_
|
||||
#define MESSAGEQUEUE_H_
|
||||
|
||||
#include <framework/internalError/InternalErrorReporterIF.h>
|
||||
#include <framework/ipc/MessageQueueIF.h>
|
||||
#include <framework/ipc/MessageQueueMessageIF.h>
|
||||
#include <framework/osal/FreeRTOS/TaskManagement.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
// TODO: this class assumes that MessageQueueId_t is the same size as void*
|
||||
// (the FreeRTOS handle type), compiler will catch this but it might be nice
|
||||
// to have something checking or even an always working solution
|
||||
// https://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/
|
||||
|
||||
/**
|
||||
* @brief This class manages sending and receiving of
|
||||
* message queue messages.
|
||||
* @details
|
||||
* Message queues are used to pass asynchronous messages between processes.
|
||||
* They work like post boxes, where all incoming messages are stored in FIFO
|
||||
* order. This class creates a new receiving queue and provides methods to fetch
|
||||
* received messages. Being a child of MessageQueueSender, this class also
|
||||
* provides methods to send a message to a user-defined or a default destination.
|
||||
* In addition it also provides a reply method to answer to the queue it
|
||||
* received its last message from.
|
||||
*
|
||||
* The MessageQueue should be used as "post box" for a single owning object.
|
||||
* So all message queue communication is "n-to-one".
|
||||
* For creating the queue, as well as sending and receiving messages, the class
|
||||
* makes use of the operating system calls provided.
|
||||
*
|
||||
* Please keep in mind that FreeRTOS offers different calls for message queue
|
||||
* operations if called from an ISR.
|
||||
* For now, the system context needs to be switched manually.
|
||||
* @ingroup osal
|
||||
* @ingroup message_queue
|
||||
*/
|
||||
class MessageQueue : public MessageQueueIF {
|
||||
friend class MessageQueueSenderIF;
|
||||
public:
|
||||
/**
|
||||
* @brief The constructor initializes and configures the message queue.
|
||||
* @details
|
||||
* By making use of the according operating system call, a message queue is created
|
||||
* and initialized. The message depth - the maximum number of messages to be
|
||||
* buffered - may be set with the help of a parameter, whereas the message size is
|
||||
* automatically set to the maximum message queue message size. The operating system
|
||||
* sets the message queue id, or i case of failure, it is set to zero.
|
||||
* @param message_depth
|
||||
* The number of messages to be buffered before passing an error to the
|
||||
* sender. Default is three.
|
||||
* @param max_message_size
|
||||
* With this parameter, the maximum message size can be adjusted.
|
||||
* This should be left default.
|
||||
*/
|
||||
MessageQueue( size_t messageDepth = 3,
|
||||
size_t maxMessageSize = MessageQueueMessage::MAX_MESSAGE_SIZE );
|
||||
|
||||
/** Copying message queues forbidden */
|
||||
MessageQueue(const MessageQueue&) = delete;
|
||||
MessageQueue& operator=(const MessageQueue&) = delete;
|
||||
|
||||
/**
|
||||
* @brief The destructor deletes the formerly created message queue.
|
||||
* @details This is accomplished by using the delete call provided
|
||||
* by the operating system.
|
||||
*/
|
||||
virtual ~MessageQueue();
|
||||
|
||||
/**
|
||||
* This function is used to switch the call context. This has to be called
|
||||
* if a message is sent or received from an ISR!
|
||||
* @param callContext
|
||||
*/
|
||||
void switchSystemContext(CallContext callContext);
|
||||
|
||||
/**
|
||||
* @brief This operation sends a message to the given destination.
|
||||
* @details It directly uses the sendMessage call of the MessageQueueSender parent, but passes its
|
||||
* queue id as "sentFrom" parameter.
|
||||
* @param sendTo This parameter specifies the message queue id of the destination message queue.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
* @param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full.
|
||||
*/
|
||||
ReturnValue_t sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, bool ignoreFault = false) override;
|
||||
/**
|
||||
* @brief This operation sends a message to the default destination.
|
||||
* @details As in the sendMessage method, this function uses the sendToDefault call of the
|
||||
* MessageQueueSender parent class and adds its queue id as "sentFrom" information.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
*/
|
||||
ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override;
|
||||
/**
|
||||
* @brief This operation sends a message to the last communication partner.
|
||||
* @details This operation simplifies answering an incoming message by using the stored
|
||||
* lastParnter information as destination. If there was no message received yet
|
||||
* (i.e. lastPartner is zero), an error code is returned.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
*/
|
||||
ReturnValue_t reply(MessageQueueMessageIF* message) override;
|
||||
|
||||
/**
|
||||
* @brief With the sendMessage call, a queue message is sent to a receiving queue.
|
||||
* @details This method takes the message provided, adds the sentFrom information and passes
|
||||
* it on to the destination provided with an operating system call. The OS's return
|
||||
* value is returned.
|
||||
* @param sendTo This parameter specifies the message queue id to send the message to.
|
||||
* @param message This is a pointer to a previously created message, which is sent.
|
||||
* @param sentFrom The sentFrom information can be set to inject the sender's queue id into the message.
|
||||
* This variable is set to zero by default.
|
||||
* @param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full.
|
||||
*/
|
||||
virtual ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault = false) override;
|
||||
|
||||
/**
|
||||
* @brief The sendToDefault method sends a queue message to the default destination.
|
||||
* @details In all other aspects, it works identical to the sendMessage method.
|
||||
* @param message This is a pointer to a previously created message, which is sent.
|
||||
* @param sentFrom The sentFrom information can be set to inject the sender's queue id into the message.
|
||||
* This variable is set to zero by default.
|
||||
*/
|
||||
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault = false) override;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue and returns the sender.
|
||||
* @details It works identically to the other receiveMessage call, but in addition returns the
|
||||
* sender's queue id.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
* @param receivedFrom A pointer to a queue id in which the sender's id is stored.
|
||||
*/
|
||||
ReturnValue_t receiveMessage(MessageQueueMessageIF* message,
|
||||
MessageQueueId_t *receivedFrom) override;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue.
|
||||
* @details If data is available it is stored in the passed message pointer. The message's
|
||||
* original content is overwritten and the sendFrom information is stored in the
|
||||
* lastPartner attribute. Else, the lastPartner information remains untouched, the
|
||||
* message's content is cleared and the function returns immediately.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
*/
|
||||
ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override;
|
||||
/**
|
||||
* Deletes all pending messages in the queue.
|
||||
* @param count The number of flushed messages.
|
||||
* @return RETURN_OK on success.
|
||||
*/
|
||||
ReturnValue_t flush(uint32_t* count);
|
||||
/**
|
||||
* @brief This method returns the message queue id of the last communication partner.
|
||||
*/
|
||||
MessageQueueId_t getLastPartner() const;
|
||||
/**
|
||||
* @brief This method returns the message queue id of this class's message queue.
|
||||
*/
|
||||
MessageQueueId_t getId() const;
|
||||
|
||||
/**
|
||||
* @brief This method is a simple setter for the default destination.
|
||||
*/
|
||||
void setDefaultDestination(MessageQueueId_t defaultDestination);
|
||||
/**
|
||||
* @brief This method is a simple getter for the default destination.
|
||||
*/
|
||||
MessageQueueId_t getDefaultDestination() const;
|
||||
|
||||
bool isDefaultDestinationSet() const;
|
||||
protected:
|
||||
/**
|
||||
* @brief Implementation to be called from any send Call within
|
||||
* MessageQueue and MessageQueueSenderIF.
|
||||
* @details
|
||||
* This method takes the message provided, adds the sentFrom information and
|
||||
* passes it on to the destination provided with an operating system call.
|
||||
* The OS's return value is returned.
|
||||
* @param sendTo
|
||||
* This parameter specifies the message queue id to send the message to.
|
||||
* @param message
|
||||
* This is a pointer to a previously created message, which is sent.
|
||||
* @param sentFrom
|
||||
* The sentFrom information can be set to inject the sender's queue id into
|
||||
* the message. This variable is set to zero by default.
|
||||
* @param ignoreFault
|
||||
* If set to true, the internal software fault counter is not incremented
|
||||
* if queue is full.
|
||||
* @param context Specify whether call is made from task or from an ISR.
|
||||
*/
|
||||
static ReturnValue_t sendMessageFromMessageQueue(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault=false, CallContext callContext = CallContext::TASK);
|
||||
|
||||
static ReturnValue_t handleSendResult(BaseType_t result, bool ignoreFault);
|
||||
|
||||
private:
|
||||
bool defaultDestinationSet = false;
|
||||
QueueHandle_t handle;
|
||||
MessageQueueId_t defaultDestination = 0;
|
||||
MessageQueueId_t lastPartner = 0;
|
||||
const size_t maxMessageSize;
|
||||
//!< Stores the current system context
|
||||
CallContext callContext = CallContext::TASK;
|
||||
};
|
||||
|
||||
#endif /* MESSAGEQUEUE_H_ */
|
||||
#ifndef MESSAGEQUEUE_H_
|
||||
#define MESSAGEQUEUE_H_
|
||||
|
||||
#include "../../internalError/InternalErrorReporterIF.h"
|
||||
#include "../../ipc/MessageQueueIF.h"
|
||||
#include "../../ipc/MessageQueueMessageIF.h"
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
|
||||
// TODO: this class assumes that MessageQueueId_t is the same size as void*
|
||||
// (the FreeRTOS handle type), compiler will catch this but it might be nice
|
||||
// to have something checking or even an always working solution
|
||||
// https://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/
|
||||
|
||||
/**
|
||||
* @brief This class manages sending and receiving of
|
||||
* message queue messages.
|
||||
* @details
|
||||
* Message queues are used to pass asynchronous messages between processes.
|
||||
* They work like post boxes, where all incoming messages are stored in FIFO
|
||||
* order. This class creates a new receiving queue and provides methods to fetch
|
||||
* received messages. Being a child of MessageQueueSender, this class also
|
||||
* provides methods to send a message to a user-defined or a default destination.
|
||||
* In addition it also provides a reply method to answer to the queue it
|
||||
* received its last message from.
|
||||
*
|
||||
* The MessageQueue should be used as "post box" for a single owning object.
|
||||
* So all message queue communication is "n-to-one".
|
||||
* For creating the queue, as well as sending and receiving messages, the class
|
||||
* makes use of the operating system calls provided.
|
||||
*
|
||||
* Please keep in mind that FreeRTOS offers different calls for message queue
|
||||
* operations if called from an ISR.
|
||||
* For now, the system context needs to be switched manually.
|
||||
* @ingroup osal
|
||||
* @ingroup message_queue
|
||||
*/
|
||||
class MessageQueue : public MessageQueueIF {
|
||||
friend class MessageQueueSenderIF;
|
||||
public:
|
||||
/**
|
||||
* @brief The constructor initializes and configures the message queue.
|
||||
* @details
|
||||
* By making use of the according operating system call, a message queue is created
|
||||
* and initialized. The message depth - the maximum number of messages to be
|
||||
* buffered - may be set with the help of a parameter, whereas the message size is
|
||||
* automatically set to the maximum message queue message size. The operating system
|
||||
* sets the message queue id, or i case of failure, it is set to zero.
|
||||
* @param message_depth
|
||||
* The number of messages to be buffered before passing an error to the
|
||||
* sender. Default is three.
|
||||
* @param max_message_size
|
||||
* With this parameter, the maximum message size can be adjusted.
|
||||
* This should be left default.
|
||||
*/
|
||||
MessageQueue( size_t messageDepth = 3,
|
||||
size_t maxMessageSize = MessageQueueMessage::MAX_MESSAGE_SIZE );
|
||||
|
||||
/** Copying message queues forbidden */
|
||||
MessageQueue(const MessageQueue&) = delete;
|
||||
MessageQueue& operator=(const MessageQueue&) = delete;
|
||||
|
||||
/**
|
||||
* @brief The destructor deletes the formerly created message queue.
|
||||
* @details This is accomplished by using the delete call provided
|
||||
* by the operating system.
|
||||
*/
|
||||
virtual ~MessageQueue();
|
||||
|
||||
/**
|
||||
* This function is used to switch the call context. This has to be called
|
||||
* if a message is sent or received from an ISR!
|
||||
* @param callContext
|
||||
*/
|
||||
void switchSystemContext(CallContext callContext);
|
||||
|
||||
/**
|
||||
* @brief This operation sends a message to the given destination.
|
||||
* @details It directly uses the sendMessage call of the MessageQueueSender parent, but passes its
|
||||
* queue id as "sentFrom" parameter.
|
||||
* @param sendTo This parameter specifies the message queue id of the destination message queue.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
* @param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full.
|
||||
*/
|
||||
ReturnValue_t sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, bool ignoreFault = false) override;
|
||||
/**
|
||||
* @brief This operation sends a message to the default destination.
|
||||
* @details As in the sendMessage method, this function uses the sendToDefault call of the
|
||||
* MessageQueueSender parent class and adds its queue id as "sentFrom" information.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
*/
|
||||
ReturnValue_t sendToDefault(MessageQueueMessageIF* message) override;
|
||||
/**
|
||||
* @brief This operation sends a message to the last communication partner.
|
||||
* @details This operation simplifies answering an incoming message by using the stored
|
||||
* lastParnter information as destination. If there was no message received yet
|
||||
* (i.e. lastPartner is zero), an error code is returned.
|
||||
* @param message A pointer to a previously created message, which is sent.
|
||||
*/
|
||||
ReturnValue_t reply(MessageQueueMessageIF* message) override;
|
||||
|
||||
/**
|
||||
* @brief With the sendMessage call, a queue message is sent to a receiving queue.
|
||||
* @details This method takes the message provided, adds the sentFrom information and passes
|
||||
* it on to the destination provided with an operating system call. The OS's return
|
||||
* value is returned.
|
||||
* @param sendTo This parameter specifies the message queue id to send the message to.
|
||||
* @param message This is a pointer to a previously created message, which is sent.
|
||||
* @param sentFrom The sentFrom information can be set to inject the sender's queue id into the message.
|
||||
* This variable is set to zero by default.
|
||||
* @param ignoreFault If set to true, the internal software fault counter is not incremented if queue is full.
|
||||
*/
|
||||
virtual ReturnValue_t sendMessageFrom(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault = false) override;
|
||||
|
||||
/**
|
||||
* @brief The sendToDefault method sends a queue message to the default destination.
|
||||
* @details In all other aspects, it works identical to the sendMessage method.
|
||||
* @param message This is a pointer to a previously created message, which is sent.
|
||||
* @param sentFrom The sentFrom information can be set to inject the sender's queue id into the message.
|
||||
* This variable is set to zero by default.
|
||||
*/
|
||||
virtual ReturnValue_t sendToDefaultFrom( MessageQueueMessageIF* message,
|
||||
MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault = false) override;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue and returns the sender.
|
||||
* @details It works identically to the other receiveMessage call, but in addition returns the
|
||||
* sender's queue id.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
* @param receivedFrom A pointer to a queue id in which the sender's id is stored.
|
||||
*/
|
||||
ReturnValue_t receiveMessage(MessageQueueMessageIF* message,
|
||||
MessageQueueId_t *receivedFrom) override;
|
||||
|
||||
/**
|
||||
* @brief This function reads available messages from the message queue.
|
||||
* @details If data is available it is stored in the passed message pointer. The message's
|
||||
* original content is overwritten and the sendFrom information is stored in the
|
||||
* lastPartner attribute. Else, the lastPartner information remains untouched, the
|
||||
* message's content is cleared and the function returns immediately.
|
||||
* @param message A pointer to a message in which the received data is stored.
|
||||
*/
|
||||
ReturnValue_t receiveMessage(MessageQueueMessageIF* message) override;
|
||||
/**
|
||||
* Deletes all pending messages in the queue.
|
||||
* @param count The number of flushed messages.
|
||||
* @return RETURN_OK on success.
|
||||
*/
|
||||
ReturnValue_t flush(uint32_t* count);
|
||||
/**
|
||||
* @brief This method returns the message queue id of the last communication partner.
|
||||
*/
|
||||
MessageQueueId_t getLastPartner() const;
|
||||
/**
|
||||
* @brief This method returns the message queue id of this class's message queue.
|
||||
*/
|
||||
MessageQueueId_t getId() const;
|
||||
|
||||
/**
|
||||
* @brief This method is a simple setter for the default destination.
|
||||
*/
|
||||
void setDefaultDestination(MessageQueueId_t defaultDestination);
|
||||
/**
|
||||
* @brief This method is a simple getter for the default destination.
|
||||
*/
|
||||
MessageQueueId_t getDefaultDestination() const;
|
||||
|
||||
bool isDefaultDestinationSet() const;
|
||||
protected:
|
||||
/**
|
||||
* @brief Implementation to be called from any send Call within
|
||||
* MessageQueue and MessageQueueSenderIF.
|
||||
* @details
|
||||
* This method takes the message provided, adds the sentFrom information and
|
||||
* passes it on to the destination provided with an operating system call.
|
||||
* The OS's return value is returned.
|
||||
* @param sendTo
|
||||
* This parameter specifies the message queue id to send the message to.
|
||||
* @param message
|
||||
* This is a pointer to a previously created message, which is sent.
|
||||
* @param sentFrom
|
||||
* The sentFrom information can be set to inject the sender's queue id into
|
||||
* the message. This variable is set to zero by default.
|
||||
* @param ignoreFault
|
||||
* If set to true, the internal software fault counter is not incremented
|
||||
* if queue is full.
|
||||
* @param context Specify whether call is made from task or from an ISR.
|
||||
*/
|
||||
static ReturnValue_t sendMessageFromMessageQueue(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom = NO_QUEUE,
|
||||
bool ignoreFault=false, CallContext callContext = CallContext::TASK);
|
||||
|
||||
static ReturnValue_t handleSendResult(BaseType_t result, bool ignoreFault);
|
||||
|
||||
private:
|
||||
bool defaultDestinationSet = false;
|
||||
QueueHandle_t handle;
|
||||
MessageQueueId_t defaultDestination = 0;
|
||||
MessageQueueId_t lastPartner = 0;
|
||||
const size_t maxMessageSize;
|
||||
//!< Stores the current system context
|
||||
CallContext callContext = CallContext::TASK;
|
||||
};
|
||||
|
||||
#endif /* MESSAGEQUEUE_H_ */
|
||||
|
@ -1,51 +1,51 @@
|
||||
#include <framework/osal/FreeRTOS/Mutex.h>
|
||||
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
Mutex::Mutex() {
|
||||
handle = xSemaphoreCreateMutex();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Mutex::Mutex(FreeRTOS): Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Mutex::~Mutex() {
|
||||
if (handle != nullptr) {
|
||||
vSemaphoreDelete(handle);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) {
|
||||
if (handle == nullptr) {
|
||||
return MutexIF::MUTEX_NOT_FOUND;
|
||||
}
|
||||
// If the timeout type is BLOCKING, this will be the correct value.
|
||||
uint32_t timeout = portMAX_DELAY;
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
timeout = 0;
|
||||
}
|
||||
else if(timeoutType == TimeoutType::WAITING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
|
||||
BaseType_t returncode = xSemaphoreTake(handle, timeout);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return MutexIF::MUTEX_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t Mutex::unlockMutex() {
|
||||
if (handle == nullptr) {
|
||||
return MutexIF::MUTEX_NOT_FOUND;
|
||||
}
|
||||
BaseType_t returncode = xSemaphoreGive(handle);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
|
||||
}
|
||||
}
|
||||
#include "../../osal/FreeRTOS/Mutex.h"
|
||||
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
Mutex::Mutex() {
|
||||
handle = xSemaphoreCreateMutex();
|
||||
if(handle == nullptr) {
|
||||
sif::error << "Mutex::Mutex(FreeRTOS): Creation failure" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Mutex::~Mutex() {
|
||||
if (handle != nullptr) {
|
||||
vSemaphoreDelete(handle);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ReturnValue_t Mutex::lockMutex(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) {
|
||||
if (handle == nullptr) {
|
||||
return MutexIF::MUTEX_NOT_FOUND;
|
||||
}
|
||||
// If the timeout type is BLOCKING, this will be the correct value.
|
||||
uint32_t timeout = portMAX_DELAY;
|
||||
if(timeoutType == TimeoutType::POLLING) {
|
||||
timeout = 0;
|
||||
}
|
||||
else if(timeoutType == TimeoutType::WAITING){
|
||||
timeout = pdMS_TO_TICKS(timeoutMs);
|
||||
}
|
||||
|
||||
BaseType_t returncode = xSemaphoreTake(handle, timeout);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return MutexIF::MUTEX_TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t Mutex::unlockMutex() {
|
||||
if (handle == nullptr) {
|
||||
return MutexIF::MUTEX_NOT_FOUND;
|
||||
}
|
||||
BaseType_t returncode = xSemaphoreGive(handle);
|
||||
if (returncode == pdPASS) {
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
return MutexIF::CURR_THREAD_DOES_NOT_OWN_MUTEX;
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +1,29 @@
|
||||
#ifndef FRAMEWORK_FREERTOS_MUTEX_H_
|
||||
#define FRAMEWORK_FREERTOS_MUTEX_H_
|
||||
|
||||
#include <framework/ipc/MutexIF.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
/**
|
||||
* @brief OS component to implement MUTual EXclusion
|
||||
*
|
||||
* @details
|
||||
* Mutexes are binary semaphores which include a priority inheritance mechanism.
|
||||
* Documentation: https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html
|
||||
* @ingroup osal
|
||||
*/
|
||||
class Mutex : public MutexIF {
|
||||
public:
|
||||
Mutex();
|
||||
~Mutex();
|
||||
ReturnValue_t lockMutex(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) override;
|
||||
ReturnValue_t unlockMutex() override;
|
||||
|
||||
private:
|
||||
SemaphoreHandle_t handle;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_FREERTOS_MUTEX_H_ */
|
||||
#ifndef FRAMEWORK_FREERTOS_MUTEX_H_
|
||||
#define FRAMEWORK_FREERTOS_MUTEX_H_
|
||||
|
||||
#include "../../ipc/MutexIF.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
/**
|
||||
* @brief OS component to implement MUTual EXclusion
|
||||
*
|
||||
* @details
|
||||
* Mutexes are binary semaphores which include a priority inheritance mechanism.
|
||||
* Documentation: https://www.freertos.org/Real-time-embedded-RTOS-mutexes.html
|
||||
* @ingroup osal
|
||||
*/
|
||||
class Mutex : public MutexIF {
|
||||
public:
|
||||
Mutex();
|
||||
~Mutex();
|
||||
ReturnValue_t lockMutex(TimeoutType timeoutType,
|
||||
uint32_t timeoutMs) override;
|
||||
ReturnValue_t unlockMutex() override;
|
||||
|
||||
private:
|
||||
SemaphoreHandle_t handle;
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_FREERTOS_MUTEX_H_ */
|
||||
|
@ -1,28 +1,28 @@
|
||||
#include <framework/ipc/MutexFactory.h>
|
||||
#include <framework/osal/FreeRTOS/Mutex.h>
|
||||
|
||||
//TODO: Different variant than the lazy loading in QueueFactory.
|
||||
//What's better and why? -> one is on heap the other on bss/data
|
||||
//MutexFactory* MutexFactory::factoryInstance = new MutexFactory();
|
||||
MutexFactory* MutexFactory::factoryInstance = nullptr;
|
||||
|
||||
MutexFactory::MutexFactory() {
|
||||
}
|
||||
|
||||
MutexFactory::~MutexFactory() {
|
||||
}
|
||||
|
||||
MutexFactory* MutexFactory::instance() {
|
||||
if (factoryInstance == nullptr){
|
||||
factoryInstance = new MutexFactory();
|
||||
}
|
||||
return MutexFactory::factoryInstance;
|
||||
}
|
||||
|
||||
MutexIF* MutexFactory::createMutex() {
|
||||
return new Mutex();
|
||||
}
|
||||
|
||||
void MutexFactory::deleteMutex(MutexIF* mutex) {
|
||||
delete mutex;
|
||||
}
|
||||
#include "../../ipc/MutexFactory.h"
|
||||
#include "../../osal/FreeRTOS/Mutex.h"
|
||||
|
||||
//TODO: Different variant than the lazy loading in QueueFactory.
|
||||
//What's better and why? -> one is on heap the other on bss/data
|
||||
//MutexFactory* MutexFactory::factoryInstance = new MutexFactory();
|
||||
MutexFactory* MutexFactory::factoryInstance = nullptr;
|
||||
|
||||
MutexFactory::MutexFactory() {
|
||||
}
|
||||
|
||||
MutexFactory::~MutexFactory() {
|
||||
}
|
||||
|
||||
MutexFactory* MutexFactory::instance() {
|
||||
if (factoryInstance == nullptr){
|
||||
factoryInstance = new MutexFactory();
|
||||
}
|
||||
return MutexFactory::factoryInstance;
|
||||
}
|
||||
|
||||
MutexIF* MutexFactory::createMutex() {
|
||||
return new Mutex();
|
||||
}
|
||||
|
||||
void MutexFactory::deleteMutex(MutexIF* mutex) {
|
||||
delete mutex;
|
||||
}
|
||||
|
@ -1,138 +1,138 @@
|
||||
#include "PeriodicTask.h"
|
||||
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
#include <framework/tasks/ExecutableObjectIF.h>
|
||||
|
||||
PeriodicTask::PeriodicTask(const char *name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod setPeriod,
|
||||
TaskDeadlineMissedFunction deadlineMissedFunc) :
|
||||
started(false), handle(NULL), period(setPeriod), deadlineMissedFunc(
|
||||
deadlineMissedFunc)
|
||||
{
|
||||
configSTACK_DEPTH_TYPE stackSize = setStack / sizeof(configSTACK_DEPTH_TYPE);
|
||||
BaseType_t status = xTaskCreate(taskEntryPoint, name,
|
||||
stackSize, this, setPriority, &handle);
|
||||
if(status != pdPASS){
|
||||
sif::debug << "PeriodicTask Insufficient heap memory remaining. "
|
||||
"Status: " << status << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PeriodicTask::~PeriodicTask(void) {
|
||||
//Do not delete objects, we were responsible for ptrs only.
|
||||
}
|
||||
|
||||
void PeriodicTask::taskEntryPoint(void* argument) {
|
||||
// The argument is re-interpreted as PeriodicTask. The Task object is
|
||||
// global, so it is found from any place.
|
||||
PeriodicTask *originalTask(reinterpret_cast<PeriodicTask*>(argument));
|
||||
/* Task should not start until explicitly requested,
|
||||
* but in FreeRTOS, tasks start as soon as they are created if the scheduler
|
||||
* is running but not if the scheduler is not running.
|
||||
* To be able to accommodate both cases we check a member which is set in
|
||||
* #startTask(). If it is not set and we get here, the scheduler was started
|
||||
* before #startTask() was called and we need to suspend if it is set,
|
||||
* the scheduler was not running before #startTask() was called and we
|
||||
* can continue */
|
||||
|
||||
if (not originalTask->started) {
|
||||
vTaskSuspend(NULL);
|
||||
}
|
||||
|
||||
originalTask->taskFunctionality();
|
||||
sif::debug << "Polling task " << originalTask->handle
|
||||
<< " returned from taskFunctionality." << std::endl;
|
||||
}
|
||||
|
||||
ReturnValue_t PeriodicTask::startTask() {
|
||||
started = true;
|
||||
|
||||
// We must not call resume if scheduler is not started yet
|
||||
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
|
||||
vTaskResume(handle);
|
||||
}
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t PeriodicTask::sleepFor(uint32_t ms) {
|
||||
vTaskDelay(pdMS_TO_TICKS(ms));
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
void PeriodicTask::taskFunctionality() {
|
||||
TickType_t xLastWakeTime;
|
||||
const TickType_t xPeriod = pdMS_TO_TICKS(this->period * 1000.);
|
||||
/* The xLastWakeTime variable needs to be initialized with the current tick
|
||||
count. Note that this is the only time the variable is written to
|
||||
explicitly. After this assignment, xLastWakeTime is updated automatically
|
||||
internally within vTaskDelayUntil(). */
|
||||
xLastWakeTime = xTaskGetTickCount();
|
||||
/* Enter the loop that defines the task behavior. */
|
||||
for (;;) {
|
||||
for (auto const& object: objectList) {
|
||||
object->performOperation();
|
||||
}
|
||||
|
||||
checkMissedDeadline(xLastWakeTime, xPeriod);
|
||||
|
||||
vTaskDelayUntil(&xLastWakeTime, xPeriod);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t PeriodicTask::addComponent(object_id_t object) {
|
||||
ExecutableObjectIF* newObject = objectManager->get<ExecutableObjectIF>(
|
||||
object);
|
||||
if (newObject == nullptr) {
|
||||
sif::error << "PeriodicTask::addComponent: Invalid object. Make sure"
|
||||
"it implement ExecutableObjectIF" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
objectList.push_back(newObject);
|
||||
newObject->setTaskIF(this);
|
||||
|
||||
return newObject->initializeAfterTaskCreation();
|
||||
}
|
||||
|
||||
uint32_t PeriodicTask::getPeriodMs() const {
|
||||
return period * 1000;
|
||||
}
|
||||
|
||||
void PeriodicTask::checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval) {
|
||||
/* Check whether deadline was missed while also taking overflows
|
||||
* into account. Drawing this on paper with a timeline helps to understand
|
||||
* it. */
|
||||
TickType_t currentTickCount = xTaskGetTickCount();
|
||||
TickType_t timeToWake = xLastWakeTime + interval;
|
||||
// Time to wake has not overflown.
|
||||
if(timeToWake > xLastWakeTime) {
|
||||
/* If the current time has overflown exclusively or the current
|
||||
* tick count is simply larger than the time to wake, a deadline was
|
||||
* missed */
|
||||
if((currentTickCount < xLastWakeTime) or (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
/* Time to wake has overflown. A deadline was missed if the current time
|
||||
* is larger than the time to wake */
|
||||
else if((timeToWake < xLastWakeTime) and (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
|
||||
TaskHandle_t PeriodicTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
void PeriodicTask::handleMissedDeadline() {
|
||||
#ifdef DEBUG
|
||||
sif::warning << "PeriodicTask: " << pcTaskGetName(NULL) <<
|
||||
" missed deadline!\n" << std::flush;
|
||||
#endif
|
||||
if(deadlineMissedFunc != nullptr) {
|
||||
this->deadlineMissedFunc();
|
||||
}
|
||||
}
|
||||
#include "PeriodicTask.h"
|
||||
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
#include "../../tasks/ExecutableObjectIF.h"
|
||||
|
||||
PeriodicTask::PeriodicTask(const char *name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod setPeriod,
|
||||
TaskDeadlineMissedFunction deadlineMissedFunc) :
|
||||
started(false), handle(NULL), period(setPeriod), deadlineMissedFunc(
|
||||
deadlineMissedFunc)
|
||||
{
|
||||
configSTACK_DEPTH_TYPE stackSize = setStack / sizeof(configSTACK_DEPTH_TYPE);
|
||||
BaseType_t status = xTaskCreate(taskEntryPoint, name,
|
||||
stackSize, this, setPriority, &handle);
|
||||
if(status != pdPASS){
|
||||
sif::debug << "PeriodicTask Insufficient heap memory remaining. "
|
||||
"Status: " << status << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PeriodicTask::~PeriodicTask(void) {
|
||||
//Do not delete objects, we were responsible for ptrs only.
|
||||
}
|
||||
|
||||
void PeriodicTask::taskEntryPoint(void* argument) {
|
||||
// The argument is re-interpreted as PeriodicTask. The Task object is
|
||||
// global, so it is found from any place.
|
||||
PeriodicTask *originalTask(reinterpret_cast<PeriodicTask*>(argument));
|
||||
/* Task should not start until explicitly requested,
|
||||
* but in FreeRTOS, tasks start as soon as they are created if the scheduler
|
||||
* is running but not if the scheduler is not running.
|
||||
* To be able to accommodate both cases we check a member which is set in
|
||||
* #startTask(). If it is not set and we get here, the scheduler was started
|
||||
* before #startTask() was called and we need to suspend if it is set,
|
||||
* the scheduler was not running before #startTask() was called and we
|
||||
* can continue */
|
||||
|
||||
if (not originalTask->started) {
|
||||
vTaskSuspend(NULL);
|
||||
}
|
||||
|
||||
originalTask->taskFunctionality();
|
||||
sif::debug << "Polling task " << originalTask->handle
|
||||
<< " returned from taskFunctionality." << std::endl;
|
||||
}
|
||||
|
||||
ReturnValue_t PeriodicTask::startTask() {
|
||||
started = true;
|
||||
|
||||
// We must not call resume if scheduler is not started yet
|
||||
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
|
||||
vTaskResume(handle);
|
||||
}
|
||||
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
ReturnValue_t PeriodicTask::sleepFor(uint32_t ms) {
|
||||
vTaskDelay(pdMS_TO_TICKS(ms));
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
void PeriodicTask::taskFunctionality() {
|
||||
TickType_t xLastWakeTime;
|
||||
const TickType_t xPeriod = pdMS_TO_TICKS(this->period * 1000.);
|
||||
/* The xLastWakeTime variable needs to be initialized with the current tick
|
||||
count. Note that this is the only time the variable is written to
|
||||
explicitly. After this assignment, xLastWakeTime is updated automatically
|
||||
internally within vTaskDelayUntil(). */
|
||||
xLastWakeTime = xTaskGetTickCount();
|
||||
/* Enter the loop that defines the task behavior. */
|
||||
for (;;) {
|
||||
for (auto const& object: objectList) {
|
||||
object->performOperation();
|
||||
}
|
||||
|
||||
checkMissedDeadline(xLastWakeTime, xPeriod);
|
||||
|
||||
vTaskDelayUntil(&xLastWakeTime, xPeriod);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t PeriodicTask::addComponent(object_id_t object) {
|
||||
ExecutableObjectIF* newObject = objectManager->get<ExecutableObjectIF>(
|
||||
object);
|
||||
if (newObject == nullptr) {
|
||||
sif::error << "PeriodicTask::addComponent: Invalid object. Make sure"
|
||||
"it implement ExecutableObjectIF" << std::endl;
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
objectList.push_back(newObject);
|
||||
newObject->setTaskIF(this);
|
||||
|
||||
return newObject->initializeAfterTaskCreation();
|
||||
}
|
||||
|
||||
uint32_t PeriodicTask::getPeriodMs() const {
|
||||
return period * 1000;
|
||||
}
|
||||
|
||||
void PeriodicTask::checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval) {
|
||||
/* Check whether deadline was missed while also taking overflows
|
||||
* into account. Drawing this on paper with a timeline helps to understand
|
||||
* it. */
|
||||
TickType_t currentTickCount = xTaskGetTickCount();
|
||||
TickType_t timeToWake = xLastWakeTime + interval;
|
||||
// Time to wake has not overflown.
|
||||
if(timeToWake > xLastWakeTime) {
|
||||
/* If the current time has overflown exclusively or the current
|
||||
* tick count is simply larger than the time to wake, a deadline was
|
||||
* missed */
|
||||
if((currentTickCount < xLastWakeTime) or (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
/* Time to wake has overflown. A deadline was missed if the current time
|
||||
* is larger than the time to wake */
|
||||
else if((timeToWake < xLastWakeTime) and (currentTickCount > timeToWake)) {
|
||||
handleMissedDeadline();
|
||||
}
|
||||
}
|
||||
|
||||
TaskHandle_t PeriodicTask::getTaskHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
void PeriodicTask::handleMissedDeadline() {
|
||||
#ifdef DEBUG
|
||||
sif::warning << "PeriodicTask: " << pcTaskGetName(NULL) <<
|
||||
" missed deadline!\n" << std::flush;
|
||||
#endif
|
||||
if(deadlineMissedFunc != nullptr) {
|
||||
this->deadlineMissedFunc();
|
||||
}
|
||||
}
|
||||
|
@ -1,128 +1,128 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
|
||||
#include <framework/osal/FreeRTOS/FreeRTOSTaskIF.h>
|
||||
#include <framework/objectmanager/ObjectManagerIF.h>
|
||||
#include <framework/tasks/PeriodicTaskIF.h>
|
||||
#include <framework/tasks/Typedef.h>
|
||||
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
class ExecutableObjectIF;
|
||||
|
||||
/**
|
||||
* @brief This class represents a specialized task for
|
||||
* periodic activities of multiple objects.
|
||||
* @ingroup task_handling
|
||||
*/
|
||||
class PeriodicTask: public PeriodicTaskIF, public FreeRTOSTaskIF {
|
||||
public:
|
||||
/**
|
||||
* Keep in Mind that you need to call before this vTaskStartScheduler()!
|
||||
* A lot of task parameters are set in "FreeRTOSConfig.h".
|
||||
* TODO: why does this need to be called before vTaskStartScheduler?
|
||||
* @details
|
||||
* The class is initialized without allocated objects.
|
||||
* These need to be added with #addComponent.
|
||||
* @param priority
|
||||
* Sets the priority of a task. Values depend on freeRTOS configuration,
|
||||
* high number means high priority.
|
||||
* @param stack_size
|
||||
* The stack size reserved by the operating system for the task.
|
||||
* @param setPeriod
|
||||
* The length of the period with which the task's
|
||||
* functionality will be executed. It is expressed in clock ticks.
|
||||
* @param setDeadlineMissedFunc
|
||||
* The function pointer to the deadline missed function that shall
|
||||
* be assigned.
|
||||
*/
|
||||
PeriodicTask(TaskName name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod setPeriod,
|
||||
TaskDeadlineMissedFunction deadlineMissedFunc);
|
||||
/**
|
||||
* @brief Currently, the executed object's lifetime is not coupled with
|
||||
* the task object's lifetime, so the destructor is empty.
|
||||
*/
|
||||
virtual ~PeriodicTask(void);
|
||||
|
||||
/**
|
||||
* @brief The method to start the task.
|
||||
* @details The method starts the task with the respective system call.
|
||||
* Entry point is the taskEntryPoint method described below.
|
||||
* The address of the task object is passed as an argument
|
||||
* to the system call.
|
||||
*/
|
||||
ReturnValue_t startTask() override;
|
||||
/**
|
||||
* Adds an object to the list of objects to be executed.
|
||||
* The objects are executed in the order added.
|
||||
* @param object Id of the object to add.
|
||||
* @return
|
||||
* -@c RETURN_OK on success
|
||||
* -@c RETURN_FAILED if the object could not be added.
|
||||
*/
|
||||
ReturnValue_t addComponent(object_id_t object) override;
|
||||
|
||||
uint32_t getPeriodMs() const override;
|
||||
|
||||
ReturnValue_t sleepFor(uint32_t ms) override;
|
||||
|
||||
TaskHandle_t getTaskHandle() override;
|
||||
protected:
|
||||
bool started;
|
||||
TaskHandle_t handle;
|
||||
|
||||
//! Typedef for the List of objects.
|
||||
typedef std::vector<ExecutableObjectIF*> ObjectList;
|
||||
/**
|
||||
* @brief This attribute holds a list of objects to be executed.
|
||||
*/
|
||||
ObjectList objectList;
|
||||
/**
|
||||
* @brief The period of the task.
|
||||
* @details
|
||||
* The period determines the frequency of the task's execution.
|
||||
* It is expressed in clock ticks.
|
||||
*/
|
||||
TaskPeriod period;
|
||||
/**
|
||||
* @brief The pointer to the deadline-missed function.
|
||||
* @details
|
||||
* This pointer stores the function that is executed if the task's deadline
|
||||
* is missed so each may react individually on a timing failure.
|
||||
* The pointer may be NULL, then nothing happens on missing the deadline.
|
||||
* The deadline is equal to the next execution of the periodic task.
|
||||
*/
|
||||
void (*deadlineMissedFunc)(void);
|
||||
/**
|
||||
* @brief This is the function executed in the new task's context.
|
||||
* @details
|
||||
* It converts the argument back to the thread object type and copies the
|
||||
* class instance to the task context. The taskFunctionality method is
|
||||
* called afterwards.
|
||||
* @param A pointer to the task object itself is passed as argument.
|
||||
*/
|
||||
|
||||
static void taskEntryPoint(void* argument);
|
||||
/**
|
||||
* @brief The function containing the actual functionality of the task.
|
||||
* @details
|
||||
* The method sets and starts the task's period, then enters a loop that is
|
||||
* repeated as long as the isRunning attribute is true. Within the loop,
|
||||
* all performOperation methods of the added objects are called.
|
||||
* Afterwards the checkAndRestartPeriod system call blocks the task until
|
||||
* the next period.
|
||||
* On missing the deadline, the deadlineMissedFunction is executed.
|
||||
*/
|
||||
void taskFunctionality(void);
|
||||
|
||||
void checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval);
|
||||
void handleMissedDeadline();
|
||||
};
|
||||
|
||||
#endif /* PERIODICTASK_H_ */
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_PERIODICTASK_H_
|
||||
|
||||
#include "../../osal/FreeRTOS/FreeRTOSTaskIF.h"
|
||||
#include "../../objectmanager/ObjectManagerIF.h"
|
||||
#include "../../tasks/PeriodicTaskIF.h"
|
||||
#include "../../tasks/Typedef.h"
|
||||
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
class ExecutableObjectIF;
|
||||
|
||||
/**
|
||||
* @brief This class represents a specialized task for
|
||||
* periodic activities of multiple objects.
|
||||
* @ingroup task_handling
|
||||
*/
|
||||
class PeriodicTask: public PeriodicTaskIF, public FreeRTOSTaskIF {
|
||||
public:
|
||||
/**
|
||||
* Keep in Mind that you need to call before this vTaskStartScheduler()!
|
||||
* A lot of task parameters are set in "FreeRTOSConfig.h".
|
||||
* TODO: why does this need to be called before vTaskStartScheduler?
|
||||
* @details
|
||||
* The class is initialized without allocated objects.
|
||||
* These need to be added with #addComponent.
|
||||
* @param priority
|
||||
* Sets the priority of a task. Values depend on freeRTOS configuration,
|
||||
* high number means high priority.
|
||||
* @param stack_size
|
||||
* The stack size reserved by the operating system for the task.
|
||||
* @param setPeriod
|
||||
* The length of the period with which the task's
|
||||
* functionality will be executed. It is expressed in clock ticks.
|
||||
* @param setDeadlineMissedFunc
|
||||
* The function pointer to the deadline missed function that shall
|
||||
* be assigned.
|
||||
*/
|
||||
PeriodicTask(TaskName name, TaskPriority setPriority,
|
||||
TaskStackSize setStack, TaskPeriod setPeriod,
|
||||
TaskDeadlineMissedFunction deadlineMissedFunc);
|
||||
/**
|
||||
* @brief Currently, the executed object's lifetime is not coupled with
|
||||
* the task object's lifetime, so the destructor is empty.
|
||||
*/
|
||||
virtual ~PeriodicTask(void);
|
||||
|
||||
/**
|
||||
* @brief The method to start the task.
|
||||
* @details The method starts the task with the respective system call.
|
||||
* Entry point is the taskEntryPoint method described below.
|
||||
* The address of the task object is passed as an argument
|
||||
* to the system call.
|
||||
*/
|
||||
ReturnValue_t startTask() override;
|
||||
/**
|
||||
* Adds an object to the list of objects to be executed.
|
||||
* The objects are executed in the order added.
|
||||
* @param object Id of the object to add.
|
||||
* @return
|
||||
* -@c RETURN_OK on success
|
||||
* -@c RETURN_FAILED if the object could not be added.
|
||||
*/
|
||||
ReturnValue_t addComponent(object_id_t object) override;
|
||||
|
||||
uint32_t getPeriodMs() const override;
|
||||
|
||||
ReturnValue_t sleepFor(uint32_t ms) override;
|
||||
|
||||
TaskHandle_t getTaskHandle() override;
|
||||
protected:
|
||||
bool started;
|
||||
TaskHandle_t handle;
|
||||
|
||||
//! Typedef for the List of objects.
|
||||
typedef std::vector<ExecutableObjectIF*> ObjectList;
|
||||
/**
|
||||
* @brief This attribute holds a list of objects to be executed.
|
||||
*/
|
||||
ObjectList objectList;
|
||||
/**
|
||||
* @brief The period of the task.
|
||||
* @details
|
||||
* The period determines the frequency of the task's execution.
|
||||
* It is expressed in clock ticks.
|
||||
*/
|
||||
TaskPeriod period;
|
||||
/**
|
||||
* @brief The pointer to the deadline-missed function.
|
||||
* @details
|
||||
* This pointer stores the function that is executed if the task's deadline
|
||||
* is missed so each may react individually on a timing failure.
|
||||
* The pointer may be NULL, then nothing happens on missing the deadline.
|
||||
* The deadline is equal to the next execution of the periodic task.
|
||||
*/
|
||||
void (*deadlineMissedFunc)(void);
|
||||
/**
|
||||
* @brief This is the function executed in the new task's context.
|
||||
* @details
|
||||
* It converts the argument back to the thread object type and copies the
|
||||
* class instance to the task context. The taskFunctionality method is
|
||||
* called afterwards.
|
||||
* @param A pointer to the task object itself is passed as argument.
|
||||
*/
|
||||
|
||||
static void taskEntryPoint(void* argument);
|
||||
/**
|
||||
* @brief The function containing the actual functionality of the task.
|
||||
* @details
|
||||
* The method sets and starts the task's period, then enters a loop that is
|
||||
* repeated as long as the isRunning attribute is true. Within the loop,
|
||||
* all performOperation methods of the added objects are called.
|
||||
* Afterwards the checkAndRestartPeriod system call blocks the task until
|
||||
* the next period.
|
||||
* On missing the deadline, the deadlineMissedFunction is executed.
|
||||
*/
|
||||
void taskFunctionality(void);
|
||||
|
||||
void checkMissedDeadline(const TickType_t xLastWakeTime,
|
||||
const TickType_t interval);
|
||||
void handleMissedDeadline();
|
||||
};
|
||||
|
||||
#endif /* PERIODICTASK_H_ */
|
||||
|
@ -1,36 +1,36 @@
|
||||
#include <framework/ipc/QueueFactory.h>
|
||||
|
||||
#include <framework/osal/FreeRTOS/MessageQueue.h>
|
||||
|
||||
|
||||
QueueFactory* QueueFactory::factoryInstance = nullptr;
|
||||
|
||||
|
||||
ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
return MessageQueue::sendMessageFromMessageQueue(sendTo,message,
|
||||
sentFrom,ignoreFault);
|
||||
}
|
||||
|
||||
QueueFactory* QueueFactory::instance() {
|
||||
if (factoryInstance == nullptr) {
|
||||
factoryInstance = new QueueFactory;
|
||||
}
|
||||
return factoryInstance;
|
||||
}
|
||||
|
||||
QueueFactory::QueueFactory() {
|
||||
}
|
||||
|
||||
QueueFactory::~QueueFactory() {
|
||||
}
|
||||
|
||||
MessageQueueIF* QueueFactory::createMessageQueue(uint32_t messageDepth,
|
||||
size_t maxMessageSize) {
|
||||
return new MessageQueue(messageDepth, maxMessageSize);
|
||||
}
|
||||
|
||||
void QueueFactory::deleteMessageQueue(MessageQueueIF* queue) {
|
||||
delete queue;
|
||||
}
|
||||
#include "../../ipc/QueueFactory.h"
|
||||
|
||||
#include "../../osal/FreeRTOS/MessageQueue.h"
|
||||
|
||||
|
||||
QueueFactory* QueueFactory::factoryInstance = nullptr;
|
||||
|
||||
|
||||
ReturnValue_t MessageQueueSenderIF::sendMessage(MessageQueueId_t sendTo,
|
||||
MessageQueueMessageIF* message, MessageQueueId_t sentFrom,
|
||||
bool ignoreFault) {
|
||||
return MessageQueue::sendMessageFromMessageQueue(sendTo,message,
|
||||
sentFrom,ignoreFault);
|
||||
}
|
||||
|
||||
QueueFactory* QueueFactory::instance() {
|
||||
if (factoryInstance == nullptr) {
|
||||
factoryInstance = new QueueFactory;
|
||||
}
|
||||
return factoryInstance;
|
||||
}
|
||||
|
||||
QueueFactory::QueueFactory() {
|
||||
}
|
||||
|
||||
QueueFactory::~QueueFactory() {
|
||||
}
|
||||
|
||||
MessageQueueIF* QueueFactory::createMessageQueue(uint32_t messageDepth,
|
||||
size_t maxMessageSize) {
|
||||
return new MessageQueue(messageDepth, maxMessageSize);
|
||||
}
|
||||
|
||||
void QueueFactory::deleteMessageQueue(MessageQueueIF* queue) {
|
||||
delete queue;
|
||||
}
|
||||
|
@ -1,61 +1,61 @@
|
||||
#include <framework/osal/FreeRTOS/BinarySemaphore.h>
|
||||
#include <framework/osal/FreeRTOS/BinSemaphUsingTask.h>
|
||||
#include <framework/osal/FreeRTOS/CountingSemaphore.h>
|
||||
#include <framework/osal/FreeRTOS/CountingSemaphUsingTask.h>
|
||||
#include <framework/tasks/SemaphoreFactory.h>
|
||||
#include <framework/serviceinterface/ServiceInterfaceStream.h>
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr;
|
||||
const uint32_t SemaphoreIF::POLLING = 0;
|
||||
const uint32_t SemaphoreIF::BLOCKING = portMAX_DELAY;
|
||||
|
||||
static const uint32_t USE_REGULAR_SEMAPHORES = 0;
|
||||
static const uint32_t USE_TASK_NOTIFICATIONS = 1;
|
||||
|
||||
SemaphoreFactory::SemaphoreFactory() {
|
||||
}
|
||||
|
||||
SemaphoreFactory::~SemaphoreFactory() {
|
||||
delete factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::instance() {
|
||||
if (factoryInstance == nullptr){
|
||||
factoryInstance = new SemaphoreFactory();
|
||||
}
|
||||
return SemaphoreFactory::factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) {
|
||||
if(argument == USE_REGULAR_SEMAPHORES) {
|
||||
return new BinarySemaphore();
|
||||
}
|
||||
else if(argument == USE_TASK_NOTIFICATIONS) {
|
||||
return new BinarySemaphoreUsingTask();
|
||||
}
|
||||
else {
|
||||
sif::warning << "SemaphoreFactory: Invalid argument, return regular"
|
||||
"binary semaphore" << std::endl;
|
||||
return new BinarySemaphore();
|
||||
}
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount,
|
||||
uint8_t initCount, uint32_t argument) {
|
||||
if(argument == USE_REGULAR_SEMAPHORES) {
|
||||
return new CountingSemaphore(maxCount, initCount);
|
||||
}
|
||||
else if(argument == USE_TASK_NOTIFICATIONS) {
|
||||
return new CountingSemaphoreUsingTask(maxCount, initCount);
|
||||
}
|
||||
else {
|
||||
sif::warning << "SemaphoreFactory: Invalid argument, return regular"
|
||||
"binary semaphore" << std::endl;
|
||||
return new CountingSemaphore(maxCount, initCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) {
|
||||
delete semaphore;
|
||||
}
|
||||
#include "../../osal/FreeRTOS/BinarySemaphore.h"
|
||||
#include "../../osal/FreeRTOS/BinSemaphUsingTask.h"
|
||||
#include "../../osal/FreeRTOS/CountingSemaphore.h"
|
||||
#include "../../osal/FreeRTOS/CountingSemaphUsingTask.h"
|
||||
#include "../../tasks/SemaphoreFactory.h"
|
||||
#include "../../serviceinterface/ServiceInterfaceStream.h"
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::factoryInstance = nullptr;
|
||||
const uint32_t SemaphoreIF::POLLING = 0;
|
||||
const uint32_t SemaphoreIF::BLOCKING = portMAX_DELAY;
|
||||
|
||||
static const uint32_t USE_REGULAR_SEMAPHORES = 0;
|
||||
static const uint32_t USE_TASK_NOTIFICATIONS = 1;
|
||||
|
||||
SemaphoreFactory::SemaphoreFactory() {
|
||||
}
|
||||
|
||||
SemaphoreFactory::~SemaphoreFactory() {
|
||||
delete factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreFactory* SemaphoreFactory::instance() {
|
||||
if (factoryInstance == nullptr){
|
||||
factoryInstance = new SemaphoreFactory();
|
||||
}
|
||||
return SemaphoreFactory::factoryInstance;
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) {
|
||||
if(argument == USE_REGULAR_SEMAPHORES) {
|
||||
return new BinarySemaphore();
|
||||
}
|
||||
else if(argument == USE_TASK_NOTIFICATIONS) {
|
||||
return new BinarySemaphoreUsingTask();
|
||||
}
|
||||
else {
|
||||
sif::warning << "SemaphoreFactory: Invalid argument, return regular"
|
||||
"binary semaphore" << std::endl;
|
||||
return new BinarySemaphore();
|
||||
}
|
||||
}
|
||||
|
||||
SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount,
|
||||
uint8_t initCount, uint32_t argument) {
|
||||
if(argument == USE_REGULAR_SEMAPHORES) {
|
||||
return new CountingSemaphore(maxCount, initCount);
|
||||
}
|
||||
else if(argument == USE_TASK_NOTIFICATIONS) {
|
||||
return new CountingSemaphoreUsingTask(maxCount, initCount);
|
||||
}
|
||||
else {
|
||||
sif::warning << "SemaphoreFactory: Invalid argument, return regular"
|
||||
"binary semaphore" << std::endl;
|
||||
return new CountingSemaphore(maxCount, initCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SemaphoreFactory::deleteSemaphore(SemaphoreIF* semaphore) {
|
||||
delete semaphore;
|
||||
}
|
||||
|
@ -1,53 +1,53 @@
|
||||
#include <framework/tasks/TaskFactory.h>
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
|
||||
#include "PeriodicTask.h"
|
||||
#include "FixedTimeslotTask.h"
|
||||
|
||||
|
||||
TaskFactory* TaskFactory::factoryInstance = new TaskFactory();
|
||||
|
||||
TaskFactory::~TaskFactory() {
|
||||
}
|
||||
|
||||
TaskFactory* TaskFactory::instance() {
|
||||
return TaskFactory::factoryInstance;
|
||||
}
|
||||
|
||||
PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_,
|
||||
TaskPriority taskPriority_, TaskStackSize stackSize_,
|
||||
TaskPeriod period_,
|
||||
TaskDeadlineMissedFunction deadLineMissedFunction_) {
|
||||
return dynamic_cast<PeriodicTaskIF*>(new PeriodicTask(name_, taskPriority_,
|
||||
stackSize_, period_, deadLineMissedFunction_));
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep in Mind that you need to call before this vTaskStartScheduler()!
|
||||
*/
|
||||
FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_,
|
||||
TaskPriority taskPriority_, TaskStackSize stackSize_,
|
||||
TaskPeriod period_,
|
||||
TaskDeadlineMissedFunction deadLineMissedFunction_) {
|
||||
return dynamic_cast<FixedTimeslotTaskIF*>(new FixedTimeslotTask(name_,
|
||||
taskPriority_,stackSize_, period_, deadLineMissedFunction_));
|
||||
}
|
||||
|
||||
ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) {
|
||||
if (task == nullptr) {
|
||||
//delete self
|
||||
vTaskDelete(nullptr);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
//TODO not implemented
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t TaskFactory::delayTask(uint32_t delayMs) {
|
||||
vTaskDelay(pdMS_TO_TICKS(delayMs));
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
TaskFactory::TaskFactory() {
|
||||
}
|
||||
#include "../../tasks/TaskFactory.h"
|
||||
#include "../../returnvalues/HasReturnvaluesIF.h"
|
||||
|
||||
#include "PeriodicTask.h"
|
||||
#include "FixedTimeslotTask.h"
|
||||
|
||||
|
||||
TaskFactory* TaskFactory::factoryInstance = new TaskFactory();
|
||||
|
||||
TaskFactory::~TaskFactory() {
|
||||
}
|
||||
|
||||
TaskFactory* TaskFactory::instance() {
|
||||
return TaskFactory::factoryInstance;
|
||||
}
|
||||
|
||||
PeriodicTaskIF* TaskFactory::createPeriodicTask(TaskName name_,
|
||||
TaskPriority taskPriority_, TaskStackSize stackSize_,
|
||||
TaskPeriod period_,
|
||||
TaskDeadlineMissedFunction deadLineMissedFunction_) {
|
||||
return dynamic_cast<PeriodicTaskIF*>(new PeriodicTask(name_, taskPriority_,
|
||||
stackSize_, period_, deadLineMissedFunction_));
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep in Mind that you need to call before this vTaskStartScheduler()!
|
||||
*/
|
||||
FixedTimeslotTaskIF* TaskFactory::createFixedTimeslotTask(TaskName name_,
|
||||
TaskPriority taskPriority_, TaskStackSize stackSize_,
|
||||
TaskPeriod period_,
|
||||
TaskDeadlineMissedFunction deadLineMissedFunction_) {
|
||||
return dynamic_cast<FixedTimeslotTaskIF*>(new FixedTimeslotTask(name_,
|
||||
taskPriority_,stackSize_, period_, deadLineMissedFunction_));
|
||||
}
|
||||
|
||||
ReturnValue_t TaskFactory::deleteTask(PeriodicTaskIF* task) {
|
||||
if (task == nullptr) {
|
||||
//delete self
|
||||
vTaskDelete(nullptr);
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
} else {
|
||||
//TODO not implemented
|
||||
return HasReturnvaluesIF::RETURN_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
ReturnValue_t TaskFactory::delayTask(uint32_t delayMs) {
|
||||
vTaskDelay(pdMS_TO_TICKS(delayMs));
|
||||
return HasReturnvaluesIF::RETURN_OK;
|
||||
}
|
||||
|
||||
TaskFactory::TaskFactory() {
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
#include <framework/osal/FreeRTOS/TaskManagement.h>
|
||||
|
||||
#include "../../osal/FreeRTOS/TaskManagement.h"
|
||||
|
||||
void TaskManagement::vRequestContextSwitchFromTask() {
|
||||
vTaskDelay(0);
|
||||
}
|
||||
|
||||
|
||||
void TaskManagement::requestContextSwitch(
|
||||
CallContext callContext = CallContext::TASK) {
|
||||
CallContext callContext = CallContext::TASK) {
|
||||
if(callContext == CallContext::ISR) {
|
||||
// This function depends on the partmacro.h definition for the specific device
|
||||
vRequestContextSwitchFromISR();
|
||||
@ -13,7 +13,7 @@ void TaskManagement::requestContextSwitch(
|
||||
vRequestContextSwitchFromTask();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TaskHandle_t TaskManagement::getCurrentTaskHandle() {
|
||||
return xTaskGetCurrentTaskHandle();
|
||||
}
|
||||
@ -21,4 +21,4 @@ TaskHandle_t TaskManagement::getCurrentTaskHandle() {
|
||||
size_t TaskManagement::getTaskStackHighWatermark(
|
||||
TaskHandle_t task) {
|
||||
return uxTaskGetStackHighWaterMark(task) * sizeof(StackType_t);
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_TASKMANAGEMENT_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_TASKMANAGEMENT_H_
|
||||
|
||||
#include <framework/returnvalues/HasReturnvaluesIF.h>
|
||||
#include "../../returnvalues/HasReturnvaluesIF.h"
|
||||
|
||||
extern "C" {
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
}
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
/**
|
||||
* Architecture dependant portmacro.h function call.
|
||||
* Should be implemented in bsp.
|
||||
@ -17,9 +17,9 @@ extern void vRequestContextSwitchFromISR();
|
||||
|
||||
/*!
|
||||
* Used by functions to tell if they are being called from
|
||||
* within an ISR or from a regular task. This is required because FreeRTOS
|
||||
* within an ISR or from a regular task. This is required because FreeRTOS
|
||||
* has different functions for handling semaphores and messages from within
|
||||
* an ISR and task.
|
||||
* an ISR and task.
|
||||
*/
|
||||
enum class CallContext {
|
||||
TASK = 0x00,//!< task_context
|
||||
@ -29,19 +29,19 @@ enum class CallContext {
|
||||
|
||||
class TaskManagement {
|
||||
public:
|
||||
/**
|
||||
/**
|
||||
* @brief In this function, a function dependant on the portmacro.h header
|
||||
* function calls to request a context switch can be specified.
|
||||
* This can be used if sending to the queue from an ISR caused a task
|
||||
* to unblock and a context switch is required.
|
||||
* to unblock and a context switch is required.
|
||||
*/
|
||||
static void requestContextSwitch(CallContext callContext);
|
||||
|
||||
/**
|
||||
* If task preemption in FreeRTOS is disabled, a context switch
|
||||
* can be requested manually by calling this function.
|
||||
*/
|
||||
static void vRequestContextSwitchFromTask(void);
|
||||
*/
|
||||
static void vRequestContextSwitchFromTask(void);
|
||||
|
||||
/**
|
||||
* @return The current task handle
|
||||
@ -56,9 +56,9 @@ public:
|
||||
* E.g. on a 32 bit machine, a value of 200 means 800 bytes.
|
||||
* @return Smallest value of stack remaining since the task was started in
|
||||
* words.
|
||||
*/
|
||||
*/
|
||||
static size_t getTaskStackHighWatermark(
|
||||
TaskHandle_t task = nullptr);
|
||||
TaskHandle_t task = nullptr);
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_TASKMANAGEMENT_H_ */
|
||||
|
@ -1,41 +1,41 @@
|
||||
#include <framework/osal/FreeRTOS/Timekeeper.h>
|
||||
|
||||
#include "FreeRTOSConfig.h"
|
||||
|
||||
Timekeeper * Timekeeper::myinstance = nullptr;
|
||||
|
||||
Timekeeper::Timekeeper() : offset( { 0, 0 } ) {}
|
||||
|
||||
Timekeeper::~Timekeeper() {}
|
||||
|
||||
const timeval& Timekeeper::getOffset() const {
|
||||
return offset;
|
||||
}
|
||||
|
||||
Timekeeper* Timekeeper::instance() {
|
||||
if (myinstance == nullptr) {
|
||||
myinstance = new Timekeeper();
|
||||
}
|
||||
return myinstance;
|
||||
}
|
||||
|
||||
void Timekeeper::setOffset(const timeval& offset) {
|
||||
this->offset = offset;
|
||||
}
|
||||
|
||||
timeval Timekeeper::ticksToTimeval(TickType_t ticks) {
|
||||
timeval uptime;
|
||||
uptime.tv_sec = ticks / configTICK_RATE_HZ;
|
||||
|
||||
//TODO explain, think about overflow
|
||||
uint32_t subsecondTicks = ticks % configTICK_RATE_HZ;
|
||||
uint64_t usecondTicks = subsecondTicks * 1000000;
|
||||
|
||||
uptime.tv_usec = usecondTicks / configTICK_RATE_HZ;
|
||||
|
||||
return uptime;
|
||||
}
|
||||
|
||||
TickType_t Timekeeper::getTicks() {
|
||||
return xTaskGetTickCount();
|
||||
}
|
||||
#include "../../osal/FreeRTOS/Timekeeper.h"
|
||||
|
||||
#include "FreeRTOSConfig.h"
|
||||
|
||||
Timekeeper * Timekeeper::myinstance = nullptr;
|
||||
|
||||
Timekeeper::Timekeeper() : offset( { 0, 0 } ) {}
|
||||
|
||||
Timekeeper::~Timekeeper() {}
|
||||
|
||||
const timeval& Timekeeper::getOffset() const {
|
||||
return offset;
|
||||
}
|
||||
|
||||
Timekeeper* Timekeeper::instance() {
|
||||
if (myinstance == nullptr) {
|
||||
myinstance = new Timekeeper();
|
||||
}
|
||||
return myinstance;
|
||||
}
|
||||
|
||||
void Timekeeper::setOffset(const timeval& offset) {
|
||||
this->offset = offset;
|
||||
}
|
||||
|
||||
timeval Timekeeper::ticksToTimeval(TickType_t ticks) {
|
||||
timeval uptime;
|
||||
uptime.tv_sec = ticks / configTICK_RATE_HZ;
|
||||
|
||||
//TODO explain, think about overflow
|
||||
uint32_t subsecondTicks = ticks % configTICK_RATE_HZ;
|
||||
uint64_t usecondTicks = subsecondTicks * 1000000;
|
||||
|
||||
uptime.tv_usec = usecondTicks / configTICK_RATE_HZ;
|
||||
|
||||
return uptime;
|
||||
}
|
||||
|
||||
TickType_t Timekeeper::getTicks() {
|
||||
return xTaskGetTickCount();
|
||||
}
|
||||
|
@ -1,40 +1,40 @@
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_
|
||||
|
||||
#include <framework/timemanager/Clock.h>
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
|
||||
/**
|
||||
* A Class to basically store the time difference between uptime and UTC
|
||||
* so the "time-agnostic" FreeRTOS can keep an UTC Time
|
||||
*
|
||||
* Implemented as Singleton, so the FSFW Clock Implementation (see Clock.cpp)
|
||||
* can use it without having a member.
|
||||
*/
|
||||
|
||||
class Timekeeper {
|
||||
private:
|
||||
Timekeeper();
|
||||
|
||||
timeval offset;
|
||||
|
||||
static Timekeeper * myinstance;
|
||||
public:
|
||||
static Timekeeper * instance();
|
||||
virtual ~Timekeeper();
|
||||
|
||||
static timeval ticksToTimeval(TickType_t ticks);
|
||||
/**
|
||||
* Get elapsed time in system ticks.
|
||||
* @return
|
||||
*/
|
||||
static TickType_t getTicks();
|
||||
|
||||
const timeval& getOffset() const;
|
||||
void setOffset(const timeval& offset);
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ */
|
||||
#ifndef FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_
|
||||
#define FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_
|
||||
|
||||
#include "../../timemanager/Clock.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
|
||||
/**
|
||||
* A Class to basically store the time difference between uptime and UTC
|
||||
* so the "time-agnostic" FreeRTOS can keep an UTC Time
|
||||
*
|
||||
* Implemented as Singleton, so the FSFW Clock Implementation (see Clock.cpp)
|
||||
* can use it without having a member.
|
||||
*/
|
||||
|
||||
class Timekeeper {
|
||||
private:
|
||||
Timekeeper();
|
||||
|
||||
timeval offset;
|
||||
|
||||
static Timekeeper * myinstance;
|
||||
public:
|
||||
static Timekeeper * instance();
|
||||
virtual ~Timekeeper();
|
||||
|
||||
static timeval ticksToTimeval(TickType_t ticks);
|
||||
/**
|
||||
* Get elapsed time in system ticks.
|
||||
* @return
|
||||
*/
|
||||
static TickType_t getTicks();
|
||||
|
||||
const timeval& getOffset() const;
|
||||
void setOffset(const timeval& offset);
|
||||
};
|
||||
|
||||
#endif /* FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ */
|
||||
|
Reference in New Issue
Block a user