Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Kernel.cpp Source File

Kernel.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 
00023 #include "rtos/Kernel.h"
00024 
00025 #include "mbed.h"
00026 
00027 namespace rtos {
00028 
00029 uint64_t Kernel::get_ms_count() {
00030     // CMSIS-RTOS 2.1.0 and 2.1.1 differ in the time type. We assume
00031     // our header at least matches the implementation, so we don't try looking
00032     // at the run-time version report. (There's no compile-time version report)
00033 
00034     // 2.1.0 uint64_t osKernelGetTickCount(void), not documented as callable from ISR (but RTX does allow)
00035     // 2.1.1 uint32_t osKernelGetTickCount(void), callable from ISR
00036     // 2.1.x who knows? We assume could go back to uint64_t
00037     if (sizeof osKernelGetTickCount() == sizeof(uint64_t)) {
00038         return osKernelGetTickCount();
00039     } else /* assume 32-bit */ {
00040         // Based on suggestion in CMSIS-RTOS 2.1.1 docs, but with reentrancy
00041         // protection for the tick memory. We use critical section rather than a
00042         // mutex, as hopefully this method can be callable from interrupt later -
00043         // only thing currently preventing it is that pre CMSIS RTOS 2.1.1, it's
00044         // not defined as safe.
00045         // We assume this is called multiple times per 32-bit wrap period (49 days).
00046         static uint32_t tick_h, tick_l;
00047 
00048         core_util_critical_section_enter();
00049         // The 2.1.1 API says this is legal from an ISR - we assume this means
00050         // it's also legal with interrupts disabled. RTX implementation kind
00051         // of conflates the two.
00052         uint32_t tick32 = osKernelGetTickCount();
00053         if (tick32 < tick_l) {
00054             tick_h++;
00055         }
00056         tick_l = tick32;
00057         uint64_t ret = ((uint64_t) tick_h << 32) | tick_l;
00058         core_util_critical_section_exit();
00059         return ret;
00060     }
00061 }
00062 
00063 }