|
| 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 | +} |
0 commit comments