RTOS: SysTimer: Extract RtosTimer as SysTimer

RtosTimer class introduced with tickless support in #4991 had to be
renamed because that name was already present in rtos namespace.
pull/5548/head
Filip Jagodzinski 2017-09-27 15:47:58 +02:00
parent 8b6a7aacc5
commit 1054277c65
3 changed files with 226 additions and 100 deletions

View File

@ -0,0 +1,112 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "rtos/TARGET_CORTEX/SysTimer.h"
#if DEVICE_LOWPOWERTIMER
#include "hal/lp_ticker_api.h"
#include "rtx_core_cm.h"
extern "C" {
#include "rtx_lib.h"
}
#if (defined(NO_SYSTICK))
/**
* Return an IRQ number that can be used in the absence of SysTick
*
* @return Free IRQ number that can be used
*/
extern "C" IRQn_Type mbed_get_m0_tick_irqn(void);
#endif
namespace rtos {
namespace internal {
SysTimer::SysTimer() :
TimerEvent(get_lp_ticker_data()), _start_time(0), _tick(0)
{
_start_time = ticker_read_us(_ticker_data);
#if (defined(NO_SYSTICK))
NVIC_SetVector(mbed_get_m0_tick_irqn(), (uint32_t)SysTick_Handler);
NVIC_SetPriority(mbed_get_m0_tick_irqn(), 0xFF); /* RTOS requires lowest priority */
NVIC_EnableIRQ(mbed_get_m0_tick_irqn());
#else
// Ensure SysTick has the correct priority as it is still used
// to trigger software interrupts on each tick. The period does
// not matter since it will never start counting.
OS_Tick_Setup(osRtxConfig.tick_freq, OS_TICK_HANDLER);
#endif
}
void SysTimer::schedule_tick(uint32_t delta)
{
insert_absolute(_start_time + (_tick + delta) * 1000000ULL / OS_TICK_FREQ);
}
void SysTimer::cancel_tick()
{
remove();
}
uint32_t SysTimer::get_tick()
{
return _tick & 0xFFFFFFFF;
}
uint32_t SysTimer::update_tick()
{
uint64_t new_tick = ticker_read_us(_ticker_data) * OS_TICK_FREQ / 1000000;
if (new_tick > _tick) {
// Don't update to the current tick. Instead, update to the
// previous tick and let the SysTick handler increment it
// to the current value. This allows scheduling restart
// successfully after the OS is resumed.
new_tick--;
}
uint32_t elapsed_ticks = new_tick - _tick;
_tick = new_tick;
return elapsed_ticks;
}
us_timestamp_t SysTimer::get_time()
{
return ticker_read_us(_ticker_data);
}
SysTimer::~SysTimer()
{
}
void SysTimer::handler()
{
#if (defined(NO_SYSTICK))
NVIC_SetPendingIRQ(mbed_get_m0_tick_irqn());
#else
SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
#endif
_tick++;
}
}
}
#endif

View File

@ -0,0 +1,110 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MBED_SYS_TIMER_H
#define MBED_SYS_TIMER_H
#if defined(DEVICE_LOWPOWERTIMER) || defined(DOXYGEN_ONLY)
#include "platform/NonCopyable.h"
#include "drivers/TimerEvent.h"
namespace rtos {
namespace internal {
/**
* @cond RTOS_INTERNAL
*
* @addtogroup rtos
* @{
*
* @defgroup rtos_SysTimer SysTimer class
* @{
*/
/**
* The SysTimer class is used exclusively by RTX idle loop in TICKLESS mode.
*
* @note SysTimer is not the part of Mbed RTOS API.
*/
class SysTimer: private mbed::TimerEvent, private mbed::NonCopyable<SysTimer> {
public:
SysTimer();
virtual ~SysTimer();
/**
* Schedule an os tick to fire
*
* @param delta Tick to fire at relative to current tick
*
* @warning If a tick is already scheduled it needs to be cancelled first!
*/
void schedule_tick(uint32_t delta = 1);
/**
* Prevent any scheduled ticks from triggering
*/
void cancel_tick();
/** Get the current tick count
*
* @return The number of ticks since timer creation. For the os_timer this
* should match RTX's tick count (the number of ticks since boot).
*/
uint32_t get_tick();
/**
* Update the internal tick count
*
* @return The number of ticks incremented
*
* @note Due to a scheduling issue, the number of ticks returned is decremented
* by 1 so that a handler can be called and update to the current value.
* This allows scheduling restart successfully after the OS is resumed.
*/
uint32_t update_tick();
/**
* Get the time
*
* @return Current time in microseconds
*/
us_timestamp_t get_time();
protected:
virtual void handler();
us_timestamp_t _start_time;
uint64_t _tick;
};
/**
* @}
* @}
* @endcond
*/
}
}
#endif
#endif

View File

@ -36,113 +36,17 @@ using namespace mbed;
#ifdef MBED_TICKLESS
#if (defined(NO_SYSTICK))
/**
* Return an IRQ number that can be used in the absence of SysTick
*
* @return Free IRQ number that can be used
*/
extern "C" IRQn_Type mbed_get_m0_tick_irqn(void);
#endif
#include "rtos/TARGET_CORTEX/SysTimer.h"
class RtosTimer : private TimerEvent {
public:
RtosTimer(): TimerEvent(get_lp_ticker_data()), _start_time(0), _tick(0) {
_start_time = ticker_read_us(_ticker_data);
#if (defined(NO_SYSTICK))
NVIC_SetVector(mbed_get_m0_tick_irqn(), (uint32_t)SysTick_Handler);
NVIC_SetPriority(mbed_get_m0_tick_irqn(), 0xFF); /* RTOS requires lowest priority */
NVIC_EnableIRQ(mbed_get_m0_tick_irqn());
#else
// Ensure SysTick has the correct priority as it is still used
// to trigger software interrupts on each tick. The period does
// not matter since it will never start counting.
OS_Tick_Setup(osRtxConfig.tick_freq, OS_TICK_HANDLER);
#endif
};
/**
* Schedule an os tick to fire
*
* @param delta Tick to fire at relative to current tick
*/
void schedule_tick(uint32_t delta=1) {
insert_absolute(_start_time + (_tick + delta) * 1000000 / OS_TICK_FREQ);
}
/**
* Prevent any scheduled ticks from triggering
*/
void cancel_tick() {
remove();
}
/**
* Get the current tick count
*
* @return The number of ticks since boot. This should match RTX's tick count
*/
uint32_t get_tick() {
return _tick & 0xFFFFFFFF;
}
/**
* Update the internal tick count
*
* @return The number of ticks incremented
*/
uint32_t update_tick() {
uint64_t new_tick = ticker_read_us(_ticker_data) * OS_TICK_FREQ / 1000000;
if (new_tick > _tick) {
// Don't update to the current tick. Instead, update to the
// previous tick and let the SysTick handler increment it
// to the current value. This allows scheduling restart
// successfully after the OS is resumed.
new_tick--;
}
uint32_t elapsed_ticks = new_tick - _tick;
_tick = new_tick;
return elapsed_ticks;
}
/**
* Get the time
*
* @return Current time in microseconds
*/
us_timestamp_t get_time() {
return ticker_read_us(_ticker_data);
}
~RtosTimer() {
};
protected:
void handler() {
#if (defined(NO_SYSTICK))
NVIC_SetPendingIRQ(mbed_get_m0_tick_irqn());
#else
SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
#endif
_tick++;
}
us_timestamp_t _start_time;
uint64_t _tick;
};
static RtosTimer *os_timer;
static uint64_t os_timer_data[sizeof(RtosTimer) / 8];
static rtos::internal::SysTimer *os_timer;
static uint64_t os_timer_data[sizeof(rtos::internal::SysTimer) / 8];
/// Enable System Timer.
int32_t OS_Tick_Enable (void)
{
// Do not use SingletonPtr since this relies on the RTOS
if (NULL == os_timer) {
os_timer = new (os_timer_data) RtosTimer();
os_timer = new (os_timer_data) rtos::internal::SysTimer();
}
// set to fire interrupt on next tick