takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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