Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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 #include "rtos_idle.h"
00025 #include "rtos_handlers.h"
00026 #include "platform/mbed_critical.h"
00027 #include "platform/source/mbed_os_timer.h"
00028 
00029 #if !MBED_CONF_RTOS_PRESENT
00030 /* If the RTOS is not present, we call mbed_thread.cpp to do the work */
00031 /* If the RTOS is present, mbed_thread.cpp calls us to do the work */
00032 #include "platform/mbed_thread.h"
00033 #endif
00034 
00035 namespace rtos {
00036 
00037 uint64_t Kernel::get_ms_count()
00038 {
00039 #if MBED_CONF_RTOS_PRESENT
00040     // CMSIS-RTOS 2.1.0 and 2.1.1 differ in the time type. We assume
00041     // our header at least matches the implementation, so we don't try looking
00042     // at the run-time version report. (There's no compile-time version report)
00043 
00044     // 2.1.0 uint64_t osKernelGetTickCount(void), not documented as callable from ISR (but RTX does allow)
00045     // 2.1.1 uint32_t osKernelGetTickCount(void), callable from ISR
00046     // 2.1.x who knows? We assume could go back to uint64_t
00047     if (sizeof osKernelGetTickCount() == sizeof(uint64_t)) {
00048         return osKernelGetTickCount();
00049     } else { /* assume 32-bit */
00050         // Based on suggestion in CMSIS-RTOS 2.1.1 docs, but with reentrancy
00051         // protection for the tick memory. We use critical section rather than a
00052         // mutex, as hopefully this method can be callable from interrupt later -
00053         // only thing currently preventing it is that pre CMSIS RTOS 2.1.1, it's
00054         // not defined as safe.
00055         // We assume this is called multiple times per 32-bit wrap period (49 days).
00056         static uint32_t tick_h, tick_l;
00057 
00058         core_util_critical_section_enter();
00059         // The 2.1.1 API says this is legal from an ISR - we assume this means
00060         // it's also legal with interrupts disabled. RTX implementation kind
00061         // of conflates the two.
00062         uint32_t tick32 = osKernelGetTickCount();
00063         if (tick32 < tick_l) {
00064             tick_h++;
00065         }
00066         tick_l = tick32;
00067         uint64_t ret = ((uint64_t) tick_h << 32) | tick_l;
00068         core_util_critical_section_exit();
00069         return ret;
00070     }
00071 #else
00072     return ::get_ms_count();
00073 #endif
00074 }
00075 
00076 #if MBED_CONF_RTOS_PRESENT
00077 void Kernel::attach_idle_hook(void (*fptr)(void))
00078 {
00079     rtos_attach_idle_hook (fptr);
00080 }
00081 
00082 void Kernel::attach_thread_terminate_hook(void (*fptr)(osThreadId_t id))
00083 {
00084     rtos_attach_thread_terminate_hook (fptr);
00085 }
00086 #endif
00087 
00088 }