Skip to content

Commit 07887f9

Browse files
kjbraceykegilbert
authored andcommitted
Add Kernel::get_ms_count
Give C++ access to the RTOS's absolute timebase, reducing the need to run private Timers and similar. Allows wait_until functionality, and makes it easier to avoid time drift. Place it in a new header and namespace in case we want more kernel functions in future. Try to cover over the breaking API change potentially upcoming in CMSIS-RTOS 2.1.1, when it reduces the tick count from 64-bit to 32-bit. (See ARM-software/CMSIS_5#277) Explicitly state that ticks are milliseconds in mbed OS, despite CMSIS RTOS 2 permitting different tick rates. See also ARMmbed#3648 (wait_until for condition variables) and ARMmbed#5378 (EventQueue should use RTOS tick count).
1 parent be8d3f9 commit 07887f9

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

rtos/Kernel.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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+
23+
#include "rtos/Kernel.h"
24+
25+
#include "mbed.h"
26+
27+
namespace rtos {
28+
29+
uint64_t Kernel::get_ms_count() {
30+
// CMSIS-RTOS 2.1.0 and 2.1.1 differ in the time type. We assume
31+
// our header at least matches the implementation, so we don't try looking
32+
// at the run-time version report. (There's no compile-time version report)
33+
34+
// 2.1.0 uint64_t osKernelGetTickCount(void), not documented as callable from ISR (but RTX does allow)
35+
// 2.1.1 uint32_t osKernelGetTickCount(void), callable from ISR
36+
// 2.1.x who knows? We assume could go back to uint64_t
37+
if (sizeof osKernelGetTickCount() == sizeof(uint64_t)) {
38+
return osKernelGetTickCount();
39+
} else /* assume 32-bit */ {
40+
// Based on suggestion in CMSIS-RTOS 2.1.1 docs, but with reentrancy
41+
// protection for the tick memory. We use critical section rather than a
42+
// mutex, as hopefully this method can be callable from interrupt later -
43+
// only thing currently preventing it is that pre CMSIS RTOS 2.1.1, it's
44+
// not defined as safe.
45+
// We assume this is called multiple times per 32-bit wrap period (49 days).
46+
static uint32_t tick_h, tick_l;
47+
48+
core_util_critical_section_enter();
49+
// The 2.1.1 API says this is legal from an ISR - we assume this means
50+
// it's also legal with interrupts disabled. RTX implementation kind
51+
// of conflates the two.
52+
uint32_t tick32 = osKernelGetTickCount();
53+
if (tick32 < tick_l) {
54+
tick_h++;
55+
}
56+
tick_l = tick32;
57+
uint64_t ret = ((uint64_t) tick_h << 32) | tick_l;
58+
core_util_critical_section_exit();
59+
return ret;
60+
}
61+
}
62+
63+
}

rtos/Kernel.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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 KERNEL_H
23+
#define KERNEL_H
24+
25+
#include <stdint.h>
26+
27+
namespace rtos {
28+
/** \addtogroup rtos */
29+
/** @{*/
30+
31+
/** Functions in the Kernel namespace control RTOS kernel information. */
32+
namespace Kernel {
33+
34+
/** Read the current RTOS kernel millisecond tick count.
35+
The tick count corresponds to the tick count used by the RTOS for timing
36+
purposes. It increments monotonically from 0 at boot, hence effectively
37+
never wraps. If the underlying RTOS only provides a 32-bit tick count,
38+
this method expands it to 64 bits.
39+
@return RTOS kernel current tick count
40+
@note mbed OS always uses millisecond RTOS ticks, and this could only wrap
41+
after half a billion years
42+
@note You cannot call this function from ISR context.
43+
*/
44+
uint64_t get_ms_count();
45+
46+
} // namespace Kernel
47+
48+
} // namespace rtos
49+
#endif
50+
51+
/** @}*/

rtos/rtos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define RTOS_H
2727

2828
#include "mbed_rtos_storage.h"
29+
#include "rtos/Kernel.h"
2930
#include "rtos/Thread.h"
3031
#include "rtos/Mutex.h"
3132
#include "rtos/RtosTimer.h"

0 commit comments

Comments
 (0)