diff --git a/src/arch/pikeos_posix/CMakeLists.txt b/src/arch/pikeos_posix/CMakeLists.txt index e9a45952..6d0f6cd5 100644 --- a/src/arch/pikeos_posix/CMakeLists.txt +++ b/src/arch/pikeos_posix/CMakeLists.txt @@ -39,11 +39,11 @@ if("${FORTE_ARCHITECTURE}" STREQUAL "PikeOS_Posix") forte_add_include_directories(${CMAKE_CURRENT_SOURCE_DIR}) - forte_add_timerhandler(CPCTimerHandler pctimeha ) - forte_add_sourcefile_hcpp(../posix/forte_sync ../utils/timespec_utils ../posix/pctimeha) + forte_add_sourcefile_hcpp(../posix/forte_sync ../utils/timespec_utils) forte_add_sourcefile_cpp(forte_thread.cpp pctimeha.cpp forte_sem.cpp forte_architecture_time.cpp ../genforte_printer.cpp ../genforte_realFunctions.cpp) - forte_add_sourcefile_h(../forte_architecture_time.h forte_thread.h ) + forte_add_sourcefile_h(../forte_architecture_time.h forte_thread.h pctimeha.h) + forte_add_timerhandler(CPCTimerHandler pctimeha) forte_add_sourcefile_cpp(./forte_specific_architecture.cpp) if(FORTE_COM_ETH) diff --git a/src/arch/pikeos_posix/pctimeha.cpp b/src/arch/pikeos_posix/pctimeha.cpp new file mode 100644 index 00000000..fbe52664 --- /dev/null +++ b/src/arch/pikeos_posix/pctimeha.cpp @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2005 - 2018, 2018 ACIN, fortiss GmbH, SYSGO AG + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alois Zoitl - initial API and implementation and/or initial documentation + * Martin Melik-Merkumians - updates timer handler to use monotonic clock + * Agostino Mascitti - PikeOS adaption + *******************************************************************************/ +#include +#include "pctimeha.h" +#include "../../core/devexec.h" +#include +#include +#include "../utils/timespec_utils.h" + +CTimerHandler* CTimerHandler::createTimerHandler(CDeviceExecution& paDeviceExecution){ + return new CPCTimerHandler(paDeviceExecution); +} + +CPCTimerHandler::CPCTimerHandler(CDeviceExecution& paDeviceExecution) : CTimerHandler(paDeviceExecution) { +} + +CPCTimerHandler::~CPCTimerHandler(){ + disableHandler(); +} + +void CPCTimerHandler::run(){ + struct timespec stReq; + stReq.tv_sec = 0; + stReq.tv_nsec = (1000000 / getTicksPerSecond()) * 1000; + + struct timespec stOldTime; + struct timespec stNewTime; + struct timespec stReqTime; + // Timer interval is 1ms + stReqTime.tv_sec = 0; + stReqTime.tv_nsec = (1000000 / getTicksPerSecond()) * 1000; + struct timespec stDiffTime; + struct timespec stRemainingTime = { 0, 0 }; + + clock_gettime(CLOCK_REALTIME, &stOldTime); // in PikeOS time cannot be changed at run-time. So it's equivalent to CLOCK_MONOTONIC + while(isAlive()){ + + nanosleep(&stReq, nullptr); + + clock_gettime(CLOCK_REALTIME, &stNewTime); + + timespecSub(&stNewTime, &stOldTime, &stDiffTime); + + timespecAdd(&stRemainingTime, &stDiffTime, &stRemainingTime); + + while(!timespecLessThan(&stRemainingTime, &stReqTime)){ + nextTick(); + timespecSub(&stRemainingTime, &stReqTime, &stRemainingTime); + } + stOldTime = stNewTime; // in c++ this should work fine + } +} + +void CPCTimerHandler::enableHandler(){ + start(); +} + +void CPCTimerHandler::disableHandler(){ + end(); +} + +void CPCTimerHandler::setPriority(int ){ + //TODO think on hwo to handle this. +} + +int CPCTimerHandler::getPriority() const { + //TODO think on hwo to handle this. + return 1; +} diff --git a/src/arch/pikeos_posix/pctimeha.h b/src/arch/pikeos_posix/pctimeha.h new file mode 100644 index 00000000..27875143 --- /dev/null +++ b/src/arch/pikeos_posix/pctimeha.h @@ -0,0 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2005 - 2011, 2018 ACIN, SYSGO AG + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Alois Zoitl - initial API and implementation and/or initial documentation + * Agostino Mascitti - Adaption to PikeOS 4.2 + *******************************************************************************/ +#ifndef _PIKEOS_PCTIMEHA_H_ +#define _PIKEOS_PCTIMEHA_H_ + +#include "../posix/pctimeha.h" + +#endif /*_PIKEOS_PCTIMEHA_H_*/