Skip to content
Snippets Groups Projects
Commit b8ad7dfe authored by Federico's avatar Federico
Browse files

Refactored cstimer.h, removed getNextInterrupt() and destructor

parent 5d482302
No related branches found
No related tags found
2 merge requests!40Update to Miosix 2.7,!17Draft: Improved miosix build system and fixed cmake scripts
......@@ -45,25 +45,28 @@ namespace miosix {
return instance;
}
void ContextSwitchTimer::IRQsetNextInterrupt(long long ns){
void ContextSwitchTimer::IRQsetNextInterrupt(long long ns)
{
pImpl->b.IRQsetNextInterruptCS(pImpl->b.removeBasicCorrection(vht->corrected2uncorrected(vt->corrected2uncorrected(tc->ns2tick(ns)))));
}
long long ContextSwitchTimer::getNextInterrupt() const{
return tc->tick2ns(vt->uncorrected2corrected(vht->uncorrected2corrected(pImpl->b.addBasicCorrection(pImpl->b.IRQgetSetTimeCS()))));
}
// long long ContextSwitchTimer::getNextInterrupt() const
// {
// return tc->tick2ns(vt->uncorrected2corrected(vht->uncorrected2corrected(pImpl->b.addBasicCorrection(pImpl->b.IRQgetSetTimeCS()))));
// }
long long ContextSwitchTimer::getCurrentTime() const{
long long ContextSwitchTimer::getCurrentTime() const
{
return tc->tick2ns(vt->uncorrected2corrected(vht->uncorrected2corrected(pImpl->b.addBasicCorrection(pImpl->b.getCurrentTick()))));
}
long long ContextSwitchTimer::IRQgetCurrentTime() const{
long long ContextSwitchTimer::IRQgetCurrentTime() const
{
return tc->tick2ns(vt->uncorrected2corrected(vht->uncorrected2corrected(pImpl->b.addBasicCorrection(pImpl->b.IRQgetCurrentTick()))));
}
ContextSwitchTimer::~ContextSwitchTimer(){}
ContextSwitchTimer::ContextSwitchTimer(){
ContextSwitchTimer::ContextSwitchTimer()
{
pImpl=new ContextSwitchTimerImpl();
timerFreq=pImpl->b.getTimerFrequency();
tc = new TimeConversion(timerFreq);
......@@ -71,4 +74,5 @@ namespace miosix {
vt=&VirtualClock::instance();
}
}
} //namespace miosix
......@@ -105,11 +105,6 @@ void ContextSwitchTimer::IRQsetNextInterrupt(long long ns)
}
}
long long ContextSwitchTimer::getNextInterrupt() const
{
return tc->tick2ns(nextInterrupt());
}
long long ContextSwitchTimer::getCurrentTime() const
{
bool interrupts=areInterruptsEnabled();
......@@ -127,8 +122,6 @@ long long ContextSwitchTimer::IRQgetCurrentTime() const
return tc->tick2ns(IRQgetTick());
}
ContextSwitchTimer::~ContextSwitchTimer() {}
ContextSwitchTimer::ContextSwitchTimer()
{
// TIM2 Source Clock (from APB1) Enable
......
......@@ -105,11 +105,6 @@ void ContextSwitchTimer::IRQsetNextInterrupt(long long ns)
}
}
long long ContextSwitchTimer::getNextInterrupt() const
{
return tc->tick2ns(nextInterrupt());
}
long long ContextSwitchTimer::getCurrentTime() const
{
bool interrupts=areInterruptsEnabled();
......@@ -127,8 +122,6 @@ long long ContextSwitchTimer::IRQgetCurrentTime() const
return tc->tick2ns(IRQgetTick());
}
ContextSwitchTimer::~ContextSwitchTimer() {}
ContextSwitchTimer::ContextSwitchTimer()
{
// TIM2 Source Clock (from APB1) Enable
......@@ -176,4 +169,5 @@ ContextSwitchTimer::ContextSwitchTimer()
static TimeConversion stc(timerFreq);
tc = &stc;
}
} //namespace miosix
......@@ -112,11 +112,6 @@ void ContextSwitchTimer::IRQsetNextInterrupt(long long ns)
}
}
long long ContextSwitchTimer::getNextInterrupt() const
{
return tc->tick2ns(nextInterrupt()) + set_offset;
}
long long ContextSwitchTimer::getCurrentTime() const
{
bool interrupts=areInterruptsEnabled();
......@@ -136,7 +131,7 @@ long long ContextSwitchTimer::IRQgetCurrentTime() const
void ContextSwitchTimer::IRQsetCurrentTime(long long ns)
{
long long nextInterrupt = getNextInterrupt();
long long nextInterrupt = tc->tick2ns(nextInterrupt()) + set_offset;
long long currentTime = tc->tick2ns(IRQgetTick());
//NOTE: can only move time forward, the OS can't tolerate a backward jump
set_offset = std::max(ns - currentTime, 0LL);
......@@ -144,8 +139,6 @@ void ContextSwitchTimer::IRQsetCurrentTime(long long ns)
IRQsetNextInterrupt(nextInterrupt);
}
ContextSwitchTimer::~ContextSwitchTimer() {}
ContextSwitchTimer::ContextSwitchTimer()
{
// TIM2 Source Clock (from APB1) Enable
......
#ifndef CSTIMER_H
#define CSTIMER_H
/***************************************************************************
* Copyright (C) 2015-2021 by Terraneo Federico, Sasan Golchin *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or use *
* macros or inline functions from this file, or you compile this file *
* and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with the GNU General *
* Public License. This exception does not invalidate any other reasons *
* why a work based on this file might be covered by the GNU General *
* Public License. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#pragma once
namespace miosix {
class ContextSwitchTimerImpl;
/**
* This class is a low level interface to a hardware timer, that is used as
* the basis for the Miosix timing infrastructure. In detail, it allows to
* set interrupts used both for thread wakeup from sleep, and for preemption.
* Please note that although the HW timer may operate in ticks, the class should
* provide the user (which is kernel/scheduler codes) with a timing scheme
* in nanoseconds. It is highly recommended to use TimeConversion class for
* this purpose.
* \internal
* This class is meant to be used only by the kernel, not by application code!
*
* This class is a low level interface to a hardware timer, that is used to:
* - measure time durations
* - set interrupts used both preemption and to handle sleeping threads wakeup
*
* Please note that this class should provide the kernel with time information
* in nanoseconds. It is highly recommended to use TimeConversion class
* to convert the underlying hardware timer ticks to nanoseconds.
*/
class ContextSwitchTimer
{
......@@ -25,18 +54,19 @@ public:
/**
* Set the next interrupt.
* Can be called with interrupts disabled or within an interrupt.
* \param ns the time when the interrupt will be fired, in nanoseconds
* The hardware timer handles only one outstading interrupt request at a
* time, so a new call before the interrupt expires cancels the previous one.
* \param ns the time when the interrupt will be fired, in nanoseconds.
* When the interrupt fires, it shall call the
* \code
* void IRQtimerInterrupt(long long currentTime);
* \endcode
* function defined in kernel/scheduler/timer_interrupt.h
*/
void IRQsetNextInterrupt(long long ns);
/**
* \return the time in nanoseconds when the next interrupt will be fired.
* That is, the last value passed to setNextInterrupt().
*/
long long getNextInterrupt() const;
/**
* Could be call both when the interrupts are enabled/disabled!
* Can be called both when the interrupts are enabled/disabled!
* TODO: investigate if it's possible to remove the possibility to call
* this with IRQ disabled and use IRQgetCurrentTime() instead
* \return the current time in nanoseconds as read from the timer
......@@ -50,12 +80,15 @@ public:
long long IRQgetCurrentTime() const;
/**
* Set the current system time.
* Set the current system time. Used to adjust the time for example if the
* system clock was stopped due to entering deep sleep.
* Can be called with interrupts disabled or within an interrupt.
* Used to adjust the time for example if the system clock was stopped
* due to entering deep sleep.
* FIXME: specify what should happen to an already set interrupt when time
* is changed
* \param ns value to set the hardware timer to. Note that the timer can
* only be set to a higher value, never to a lower one, as the OS timer
* needs to be monotonic.
* If an interrupt has been set with IRQsetNextInterrupt, it needs to be
* moved accordingly or fired immediately if the timer advance causes it
* to be in the past.
*/
void IRQsetCurrentTime(long long ns);
......@@ -70,11 +103,6 @@ public:
return timerFreq;
}
/**
* Destructor
*/
virtual ~ContextSwitchTimer();
ContextSwitchTimer(const ContextSwitchTimer&) = delete;
ContextSwitchTimer& operator=(const ContextSwitchTimer&) = delete;
private:
......@@ -87,5 +115,3 @@ private:
};
} //namespace miosix
#endif //CSTIMER_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment