Skip to content

Commit 1054277

Browse files
committed
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.
1 parent 8b6a7aa commit 1054277

File tree

3 files changed

+226
-100
lines changed

3 files changed

+226
-100
lines changed

rtos/TARGET_CORTEX/SysTimer.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2012 ARM Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
#include "rtos/TARGET_CORTEX/SysTimer.h"
23+
24+
#if DEVICE_LOWPOWERTIMER
25+
26+
#include "hal/lp_ticker_api.h"
27+
#include "rtx_core_cm.h"
28+
extern "C" {
29+
#include "rtx_lib.h"
30+
}
31+
32+
#if (defined(NO_SYSTICK))
33+
/**
34+
* Return an IRQ number that can be used in the absence of SysTick
35+
*
36+
* @return Free IRQ number that can be used
37+
*/
38+
extern "C" IRQn_Type mbed_get_m0_tick_irqn(void);
39+
#endif
40+
41+
namespace rtos {
42+
namespace internal {
43+
44+
SysTimer::SysTimer() :
45+
TimerEvent(get_lp_ticker_data()), _start_time(0), _tick(0)
46+
{
47+
_start_time = ticker_read_us(_ticker_data);
48+
#if (defined(NO_SYSTICK))
49+
NVIC_SetVector(mbed_get_m0_tick_irqn(), (uint32_t)SysTick_Handler);
50+
NVIC_SetPriority(mbed_get_m0_tick_irqn(), 0xFF); /* RTOS requires lowest priority */
51+
NVIC_EnableIRQ(mbed_get_m0_tick_irqn());
52+
#else
53+
// Ensure SysTick has the correct priority as it is still used
54+
// to trigger software interrupts on each tick. The period does
55+
// not matter since it will never start counting.
56+
OS_Tick_Setup(osRtxConfig.tick_freq, OS_TICK_HANDLER);
57+
#endif
58+
}
59+
60+
void SysTimer::schedule_tick(uint32_t delta)
61+
{
62+
insert_absolute(_start_time + (_tick + delta) * 1000000ULL / OS_TICK_FREQ);
63+
}
64+
65+
void SysTimer::cancel_tick()
66+
{
67+
remove();
68+
}
69+
70+
uint32_t SysTimer::get_tick()
71+
{
72+
return _tick & 0xFFFFFFFF;
73+
}
74+
75+
uint32_t SysTimer::update_tick()
76+
{
77+
uint64_t new_tick = ticker_read_us(_ticker_data) * OS_TICK_FREQ / 1000000;
78+
if (new_tick > _tick) {
79+
// Don't update to the current tick. Instead, update to the
80+
// previous tick and let the SysTick handler increment it
81+
// to the current value. This allows scheduling restart
82+
// successfully after the OS is resumed.
83+
new_tick--;
84+
}
85+
uint32_t elapsed_ticks = new_tick - _tick;
86+
_tick = new_tick;
87+
return elapsed_ticks;
88+
}
89+
90+
us_timestamp_t SysTimer::get_time()
91+
{
92+
return ticker_read_us(_ticker_data);
93+
}
94+
95+
SysTimer::~SysTimer()
96+
{
97+
}
98+
99+
void SysTimer::handler()
100+
{
101+
#if (defined(NO_SYSTICK))
102+
NVIC_SetPendingIRQ(mbed_get_m0_tick_irqn());
103+
#else
104+
SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
105+
#endif
106+
_tick++;
107+
}
108+
109+
}
110+
}
111+
112+
#endif

rtos/TARGET_CORTEX/SysTimer.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2012 ARM Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
#ifndef MBED_SYS_TIMER_H
23+
#define MBED_SYS_TIMER_H
24+
25+
#if defined(DEVICE_LOWPOWERTIMER) || defined(DOXYGEN_ONLY)
26+
27+
#include "platform/NonCopyable.h"
28+
#include "drivers/TimerEvent.h"
29+
30+
namespace rtos {
31+
namespace internal {
32+
33+
/**
34+
* @cond RTOS_INTERNAL
35+
*
36+
* @addtogroup rtos
37+
* @{
38+
*
39+
* @defgroup rtos_SysTimer SysTimer class
40+
* @{
41+
*/
42+
43+
/**
44+
* The SysTimer class is used exclusively by RTX idle loop in TICKLESS mode.
45+
*
46+
* @note SysTimer is not the part of Mbed RTOS API.
47+
*/
48+
class SysTimer: private mbed::TimerEvent, private mbed::NonCopyable<SysTimer> {
49+
public:
50+
51+
SysTimer();
52+
virtual ~SysTimer();
53+
54+
/**
55+
* Schedule an os tick to fire
56+
*
57+
* @param delta Tick to fire at relative to current tick
58+
*
59+
* @warning If a tick is already scheduled it needs to be cancelled first!
60+
*/
61+
void schedule_tick(uint32_t delta = 1);
62+
63+
/**
64+
* Prevent any scheduled ticks from triggering
65+
*/
66+
void cancel_tick();
67+
68+
/** Get the current tick count
69+
*
70+
* @return The number of ticks since timer creation. For the os_timer this
71+
* should match RTX's tick count (the number of ticks since boot).
72+
*/
73+
uint32_t get_tick();
74+
75+
/**
76+
* Update the internal tick count
77+
*
78+
* @return The number of ticks incremented
79+
*
80+
* @note Due to a scheduling issue, the number of ticks returned is decremented
81+
* by 1 so that a handler can be called and update to the current value.
82+
* This allows scheduling restart successfully after the OS is resumed.
83+
*/
84+
uint32_t update_tick();
85+
86+
/**
87+
* Get the time
88+
*
89+
* @return Current time in microseconds
90+
*/
91+
us_timestamp_t get_time();
92+
93+
protected:
94+
virtual void handler();
95+
us_timestamp_t _start_time;
96+
uint64_t _tick;
97+
};
98+
99+
/**
100+
* @}
101+
* @}
102+
* @endcond
103+
*/
104+
105+
}
106+
}
107+
108+
#endif
109+
110+
#endif

rtos/TARGET_CORTEX/mbed_rtx_idle.cpp

Lines changed: 4 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -36,113 +36,17 @@ using namespace mbed;
3636

3737
#ifdef MBED_TICKLESS
3838

39-
#if (defined(NO_SYSTICK))
40-
/**
41-
* Return an IRQ number that can be used in the absence of SysTick
42-
*
43-
* @return Free IRQ number that can be used
44-
*/
45-
extern "C" IRQn_Type mbed_get_m0_tick_irqn(void);
46-
#endif
47-
48-
class RtosTimer : private TimerEvent {
49-
public:
50-
RtosTimer(): TimerEvent(get_lp_ticker_data()), _start_time(0), _tick(0) {
51-
_start_time = ticker_read_us(_ticker_data);
52-
#if (defined(NO_SYSTICK))
53-
NVIC_SetVector(mbed_get_m0_tick_irqn(), (uint32_t)SysTick_Handler);
54-
NVIC_SetPriority(mbed_get_m0_tick_irqn(), 0xFF); /* RTOS requires lowest priority */
55-
NVIC_EnableIRQ(mbed_get_m0_tick_irqn());
56-
#else
57-
// Ensure SysTick has the correct priority as it is still used
58-
// to trigger software interrupts on each tick. The period does
59-
// not matter since it will never start counting.
60-
OS_Tick_Setup(osRtxConfig.tick_freq, OS_TICK_HANDLER);
61-
#endif
62-
};
63-
64-
/**
65-
* Schedule an os tick to fire
66-
*
67-
* @param delta Tick to fire at relative to current tick
68-
*/
69-
void schedule_tick(uint32_t delta=1) {
70-
insert_absolute(_start_time + (_tick + delta) * 1000000 / OS_TICK_FREQ);
71-
}
72-
73-
74-
/**
75-
* Prevent any scheduled ticks from triggering
76-
*/
77-
void cancel_tick() {
78-
remove();
79-
}
80-
81-
/**
82-
* Get the current tick count
83-
*
84-
* @return The number of ticks since boot. This should match RTX's tick count
85-
*/
86-
uint32_t get_tick() {
87-
return _tick & 0xFFFFFFFF;
88-
}
89-
90-
/**
91-
* Update the internal tick count
92-
*
93-
* @return The number of ticks incremented
94-
*/
95-
uint32_t update_tick() {
96-
uint64_t new_tick = ticker_read_us(_ticker_data) * OS_TICK_FREQ / 1000000;
97-
if (new_tick > _tick) {
98-
// Don't update to the current tick. Instead, update to the
99-
// previous tick and let the SysTick handler increment it
100-
// to the current value. This allows scheduling restart
101-
// successfully after the OS is resumed.
102-
new_tick--;
103-
}
104-
uint32_t elapsed_ticks = new_tick - _tick;
105-
_tick = new_tick;
106-
return elapsed_ticks;
107-
}
108-
109-
/**
110-
* Get the time
111-
*
112-
* @return Current time in microseconds
113-
*/
114-
us_timestamp_t get_time() {
115-
return ticker_read_us(_ticker_data);
116-
}
117-
118-
~RtosTimer() {
119-
120-
};
121-
122-
protected:
123-
124-
void handler() {
125-
#if (defined(NO_SYSTICK))
126-
NVIC_SetPendingIRQ(mbed_get_m0_tick_irqn());
127-
#else
128-
SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
129-
#endif
130-
_tick++;
131-
}
132-
133-
us_timestamp_t _start_time;
134-
uint64_t _tick;
135-
};
39+
#include "rtos/TARGET_CORTEX/SysTimer.h"
13640

137-
static RtosTimer *os_timer;
138-
static uint64_t os_timer_data[sizeof(RtosTimer) / 8];
41+
static rtos::internal::SysTimer *os_timer;
42+
static uint64_t os_timer_data[sizeof(rtos::internal::SysTimer) / 8];
13943

14044
/// Enable System Timer.
14145
int32_t OS_Tick_Enable (void)
14246
{
14347
// Do not use SingletonPtr since this relies on the RTOS
14448
if (NULL == os_timer) {
145-
os_timer = new (os_timer_data) RtosTimer();
49+
os_timer = new (os_timer_data) rtos::internal::SysTimer();
14650
}
14751

14852
// set to fire interrupt on next tick

0 commit comments

Comments
 (0)