mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
platform/source/SysTimer.cpp@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9db0e321a9f4 | 1 | /* mbed Microcontroller Library |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2006-2019 ARM Limited |
kenjiArai | 1:9db0e321a9f4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 1:9db0e321a9f4 | 4 | * |
kenjiArai | 1:9db0e321a9f4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 1:9db0e321a9f4 | 6 | * you may not use this file except in compliance with the License. |
kenjiArai | 1:9db0e321a9f4 | 7 | * You may obtain a copy of the License at |
kenjiArai | 1:9db0e321a9f4 | 8 | * |
kenjiArai | 1:9db0e321a9f4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 1:9db0e321a9f4 | 10 | * |
kenjiArai | 1:9db0e321a9f4 | 11 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 1:9db0e321a9f4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 1:9db0e321a9f4 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 1:9db0e321a9f4 | 14 | * See the License for the specific language governing permissions and |
kenjiArai | 1:9db0e321a9f4 | 15 | * limitations under the License. |
kenjiArai | 1:9db0e321a9f4 | 16 | */ |
kenjiArai | 1:9db0e321a9f4 | 17 | |
kenjiArai | 1:9db0e321a9f4 | 18 | #include "hal/us_ticker_api.h" |
kenjiArai | 1:9db0e321a9f4 | 19 | #include "hal/lp_ticker_api.h" |
kenjiArai | 1:9db0e321a9f4 | 20 | #include "mbed_atomic.h" |
kenjiArai | 1:9db0e321a9f4 | 21 | #include "mbed_critical.h" |
kenjiArai | 1:9db0e321a9f4 | 22 | #include "mbed_assert.h" |
kenjiArai | 1:9db0e321a9f4 | 23 | #include "platform/mbed_power_mgmt.h" |
kenjiArai | 1:9db0e321a9f4 | 24 | #include "platform/CriticalSectionLock.h" |
kenjiArai | 1:9db0e321a9f4 | 25 | #include "platform/source/SysTimer.h" |
kenjiArai | 1:9db0e321a9f4 | 26 | extern "C" { |
kenjiArai | 1:9db0e321a9f4 | 27 | #if MBED_CONF_RTOS_PRESENT |
kenjiArai | 1:9db0e321a9f4 | 28 | #include "rtx_lib.h" |
kenjiArai | 1:9db0e321a9f4 | 29 | #endif |
kenjiArai | 1:9db0e321a9f4 | 30 | } |
kenjiArai | 1:9db0e321a9f4 | 31 | |
kenjiArai | 1:9db0e321a9f4 | 32 | #if (defined(NO_SYSTICK)) |
kenjiArai | 1:9db0e321a9f4 | 33 | /** |
kenjiArai | 1:9db0e321a9f4 | 34 | * Return an IRQ number that can be used in the absence of SysTick |
kenjiArai | 1:9db0e321a9f4 | 35 | * |
kenjiArai | 1:9db0e321a9f4 | 36 | * @return Free IRQ number that can be used |
kenjiArai | 1:9db0e321a9f4 | 37 | */ |
kenjiArai | 1:9db0e321a9f4 | 38 | extern "C" IRQn_Type mbed_get_m0_tick_irqn(void); |
kenjiArai | 1:9db0e321a9f4 | 39 | #endif |
kenjiArai | 1:9db0e321a9f4 | 40 | |
kenjiArai | 1:9db0e321a9f4 | 41 | #if defined(TARGET_CORTEX_A) |
kenjiArai | 1:9db0e321a9f4 | 42 | extern "C" IRQn_ID_t mbed_get_a9_tick_irqn(void); |
kenjiArai | 1:9db0e321a9f4 | 43 | #endif |
kenjiArai | 1:9db0e321a9f4 | 44 | |
kenjiArai | 1:9db0e321a9f4 | 45 | namespace mbed { |
kenjiArai | 1:9db0e321a9f4 | 46 | namespace internal { |
kenjiArai | 1:9db0e321a9f4 | 47 | |
kenjiArai | 1:9db0e321a9f4 | 48 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 49 | SysTimer<US_IN_TICK, IRQ>::SysTimer() : |
kenjiArai | 1:9db0e321a9f4 | 50 | #if DEVICE_LPTICKER |
kenjiArai | 1:9db0e321a9f4 | 51 | TimerEvent(get_lp_ticker_data()), |
kenjiArai | 1:9db0e321a9f4 | 52 | #else |
kenjiArai | 1:9db0e321a9f4 | 53 | TimerEvent(get_us_ticker_data()), |
kenjiArai | 1:9db0e321a9f4 | 54 | #endif |
kenjiArai | 1:9db0e321a9f4 | 55 | _time_us(ticker_read_us(_ticker_data)), |
kenjiArai | 1:9db0e321a9f4 | 56 | _tick(0), |
kenjiArai | 1:9db0e321a9f4 | 57 | _unacknowledged_ticks(0), |
kenjiArai | 1:9db0e321a9f4 | 58 | _wake_time_set(false), |
kenjiArai | 1:9db0e321a9f4 | 59 | _wake_time_passed(false), |
kenjiArai | 1:9db0e321a9f4 | 60 | _wake_early(false), |
kenjiArai | 1:9db0e321a9f4 | 61 | _ticking(false), |
kenjiArai | 1:9db0e321a9f4 | 62 | _deep_sleep_locked(false) |
kenjiArai | 1:9db0e321a9f4 | 63 | { |
kenjiArai | 1:9db0e321a9f4 | 64 | } |
kenjiArai | 1:9db0e321a9f4 | 65 | |
kenjiArai | 1:9db0e321a9f4 | 66 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 67 | SysTimer<US_IN_TICK, IRQ>::SysTimer(const ticker_data_t *data) : |
kenjiArai | 1:9db0e321a9f4 | 68 | TimerEvent(data), |
kenjiArai | 1:9db0e321a9f4 | 69 | _time_us(ticker_read_us(_ticker_data)), |
kenjiArai | 1:9db0e321a9f4 | 70 | _tick(0), |
kenjiArai | 1:9db0e321a9f4 | 71 | _unacknowledged_ticks(0), |
kenjiArai | 1:9db0e321a9f4 | 72 | _wake_time_set(false), |
kenjiArai | 1:9db0e321a9f4 | 73 | _wake_time_passed(false), |
kenjiArai | 1:9db0e321a9f4 | 74 | _wake_early(false), |
kenjiArai | 1:9db0e321a9f4 | 75 | _ticking(false), |
kenjiArai | 1:9db0e321a9f4 | 76 | _deep_sleep_locked(false) |
kenjiArai | 1:9db0e321a9f4 | 77 | { |
kenjiArai | 1:9db0e321a9f4 | 78 | } |
kenjiArai | 1:9db0e321a9f4 | 79 | |
kenjiArai | 1:9db0e321a9f4 | 80 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 81 | SysTimer<US_IN_TICK, IRQ>::~SysTimer() |
kenjiArai | 1:9db0e321a9f4 | 82 | { |
kenjiArai | 1:9db0e321a9f4 | 83 | cancel_tick(); |
kenjiArai | 1:9db0e321a9f4 | 84 | cancel_wake(); |
kenjiArai | 1:9db0e321a9f4 | 85 | } |
kenjiArai | 1:9db0e321a9f4 | 86 | |
kenjiArai | 1:9db0e321a9f4 | 87 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 88 | void SysTimer<US_IN_TICK, IRQ>::set_wake_time(uint64_t at) |
kenjiArai | 1:9db0e321a9f4 | 89 | { |
kenjiArai | 1:9db0e321a9f4 | 90 | // SysTimer must not be active - we must be in suspend state |
kenjiArai | 1:9db0e321a9f4 | 91 | MBED_ASSERT(!_ticking); |
kenjiArai | 1:9db0e321a9f4 | 92 | |
kenjiArai | 1:9db0e321a9f4 | 93 | // There is a potential race here, when called from outside |
kenjiArai | 1:9db0e321a9f4 | 94 | // a critical section. See function documentation for notes on |
kenjiArai | 1:9db0e321a9f4 | 95 | // handling it. |
kenjiArai | 1:9db0e321a9f4 | 96 | if (core_util_atomic_load_bool(&_wake_time_set)) { |
kenjiArai | 1:9db0e321a9f4 | 97 | return; |
kenjiArai | 1:9db0e321a9f4 | 98 | } |
kenjiArai | 1:9db0e321a9f4 | 99 | |
kenjiArai | 1:9db0e321a9f4 | 100 | // Analyse the timers |
kenjiArai | 1:9db0e321a9f4 | 101 | if (update_and_get_tick() >= at) { |
kenjiArai | 1:9db0e321a9f4 | 102 | _wake_time_passed = true; |
kenjiArai | 1:9db0e321a9f4 | 103 | return; |
kenjiArai | 1:9db0e321a9f4 | 104 | } |
kenjiArai | 1:9db0e321a9f4 | 105 | |
kenjiArai | 1:9db0e321a9f4 | 106 | uint64_t ticks_to_sleep = at - _tick; |
kenjiArai | 1:9db0e321a9f4 | 107 | uint64_t wake_time = at * US_IN_TICK; |
kenjiArai | 1:9db0e321a9f4 | 108 | |
kenjiArai | 1:9db0e321a9f4 | 109 | /* Set this first, before attaching the interrupt that can unset it */ |
kenjiArai | 1:9db0e321a9f4 | 110 | _wake_time_set = true; |
kenjiArai | 1:9db0e321a9f4 | 111 | _wake_time_passed = false; |
kenjiArai | 1:9db0e321a9f4 | 112 | |
kenjiArai | 1:9db0e321a9f4 | 113 | if (!_deep_sleep_locked && !_ticker_data->interface->runs_in_deep_sleep) { |
kenjiArai | 1:9db0e321a9f4 | 114 | _deep_sleep_locked = true; |
kenjiArai | 1:9db0e321a9f4 | 115 | sleep_manager_lock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 116 | } |
kenjiArai | 1:9db0e321a9f4 | 117 | /* Consider whether we will need early or precise wake-up */ |
kenjiArai | 1:9db0e321a9f4 | 118 | if (MBED_CONF_TARGET_DEEP_SLEEP_LATENCY > 0 && |
kenjiArai | 1:9db0e321a9f4 | 119 | ticks_to_sleep > MBED_CONF_TARGET_DEEP_SLEEP_LATENCY && |
kenjiArai | 1:9db0e321a9f4 | 120 | !_deep_sleep_locked) { |
kenjiArai | 1:9db0e321a9f4 | 121 | /* If there is deep sleep latency, but we still have enough time, |
kenjiArai | 1:9db0e321a9f4 | 122 | * and we haven't blocked deep sleep ourselves, |
kenjiArai | 1:9db0e321a9f4 | 123 | * allow for that latency by requesting early wake-up. |
kenjiArai | 1:9db0e321a9f4 | 124 | * Actual sleep may or may not be deep, depending on other actors. |
kenjiArai | 1:9db0e321a9f4 | 125 | */ |
kenjiArai | 1:9db0e321a9f4 | 126 | _wake_early = true; |
kenjiArai | 1:9db0e321a9f4 | 127 | insert_absolute(wake_time - MBED_CONF_TARGET_DEEP_SLEEP_LATENCY * US_IN_TICK); |
kenjiArai | 1:9db0e321a9f4 | 128 | } else { |
kenjiArai | 1:9db0e321a9f4 | 129 | /* Otherwise, set up to wake at the precise time. |
kenjiArai | 1:9db0e321a9f4 | 130 | * If there is a deep sleep latency, ensure that we're holding the lock so the sleep |
kenjiArai | 1:9db0e321a9f4 | 131 | * is shallow. (If there is no deep sleep latency, we're fine with it being deep). |
kenjiArai | 1:9db0e321a9f4 | 132 | */ |
kenjiArai | 1:9db0e321a9f4 | 133 | _wake_early = false; |
kenjiArai | 1:9db0e321a9f4 | 134 | if (MBED_CONF_TARGET_DEEP_SLEEP_LATENCY > 0 && !_deep_sleep_locked) { |
kenjiArai | 1:9db0e321a9f4 | 135 | _deep_sleep_locked = true; |
kenjiArai | 1:9db0e321a9f4 | 136 | sleep_manager_lock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 137 | } |
kenjiArai | 1:9db0e321a9f4 | 138 | insert_absolute(wake_time); |
kenjiArai | 1:9db0e321a9f4 | 139 | } |
kenjiArai | 1:9db0e321a9f4 | 140 | } |
kenjiArai | 1:9db0e321a9f4 | 141 | |
kenjiArai | 1:9db0e321a9f4 | 142 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 143 | void SysTimer<US_IN_TICK, IRQ>::cancel_wake() |
kenjiArai | 1:9db0e321a9f4 | 144 | { |
kenjiArai | 1:9db0e321a9f4 | 145 | MBED_ASSERT(!_ticking); |
kenjiArai | 1:9db0e321a9f4 | 146 | // Remove ensures serialized access to SysTimer by stopping timer interrupt |
kenjiArai | 1:9db0e321a9f4 | 147 | remove(); |
kenjiArai | 1:9db0e321a9f4 | 148 | |
kenjiArai | 1:9db0e321a9f4 | 149 | _wake_time_set = false; |
kenjiArai | 1:9db0e321a9f4 | 150 | _wake_time_passed = false; |
kenjiArai | 1:9db0e321a9f4 | 151 | |
kenjiArai | 1:9db0e321a9f4 | 152 | if (_deep_sleep_locked) { |
kenjiArai | 1:9db0e321a9f4 | 153 | _deep_sleep_locked = false; |
kenjiArai | 1:9db0e321a9f4 | 154 | sleep_manager_unlock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 155 | } |
kenjiArai | 1:9db0e321a9f4 | 156 | } |
kenjiArai | 1:9db0e321a9f4 | 157 | |
kenjiArai | 1:9db0e321a9f4 | 158 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 159 | uint64_t SysTimer<US_IN_TICK, IRQ>::_elapsed_ticks() const |
kenjiArai | 1:9db0e321a9f4 | 160 | { |
kenjiArai | 1:9db0e321a9f4 | 161 | uint64_t elapsed_us = ticker_read_us(_ticker_data) - _time_us; |
kenjiArai | 1:9db0e321a9f4 | 162 | if (elapsed_us < US_IN_TICK) { |
kenjiArai | 1:9db0e321a9f4 | 163 | return 0; |
kenjiArai | 1:9db0e321a9f4 | 164 | } else if (elapsed_us < 2 * US_IN_TICK) { |
kenjiArai | 1:9db0e321a9f4 | 165 | return 1; |
kenjiArai | 1:9db0e321a9f4 | 166 | } else if (elapsed_us <= 0xFFFFFFFF) { |
kenjiArai | 1:9db0e321a9f4 | 167 | // Fast common case avoiding 64-bit division |
kenjiArai | 1:9db0e321a9f4 | 168 | return (uint32_t) elapsed_us / US_IN_TICK; |
kenjiArai | 1:9db0e321a9f4 | 169 | } else { |
kenjiArai | 1:9db0e321a9f4 | 170 | return elapsed_us / US_IN_TICK; |
kenjiArai | 1:9db0e321a9f4 | 171 | } |
kenjiArai | 1:9db0e321a9f4 | 172 | } |
kenjiArai | 1:9db0e321a9f4 | 173 | |
kenjiArai | 1:9db0e321a9f4 | 174 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 175 | void SysTimer<US_IN_TICK, IRQ>::start_tick() |
kenjiArai | 1:9db0e321a9f4 | 176 | { |
kenjiArai | 1:9db0e321a9f4 | 177 | _ticking = true; |
kenjiArai | 1:9db0e321a9f4 | 178 | if (_unacknowledged_ticks > 0) { |
kenjiArai | 1:9db0e321a9f4 | 179 | _set_irq_pending(); |
kenjiArai | 1:9db0e321a9f4 | 180 | } |
kenjiArai | 1:9db0e321a9f4 | 181 | _schedule_tick(); |
kenjiArai | 1:9db0e321a9f4 | 182 | } |
kenjiArai | 1:9db0e321a9f4 | 183 | |
kenjiArai | 1:9db0e321a9f4 | 184 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 185 | void SysTimer<US_IN_TICK, IRQ>::_schedule_tick() |
kenjiArai | 1:9db0e321a9f4 | 186 | { |
kenjiArai | 1:9db0e321a9f4 | 187 | insert_absolute(_time_us + US_IN_TICK); |
kenjiArai | 1:9db0e321a9f4 | 188 | } |
kenjiArai | 1:9db0e321a9f4 | 189 | |
kenjiArai | 1:9db0e321a9f4 | 190 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 191 | void SysTimer<US_IN_TICK, IRQ>::acknowledge_tick() |
kenjiArai | 1:9db0e321a9f4 | 192 | { |
kenjiArai | 1:9db0e321a9f4 | 193 | // Try to avoid missed ticks if OS's IRQ level is not keeping |
kenjiArai | 1:9db0e321a9f4 | 194 | // up with our handler. |
kenjiArai | 1:9db0e321a9f4 | 195 | // 8-bit counter to save space, and also make sure it we don't |
kenjiArai | 1:9db0e321a9f4 | 196 | // try TOO hard to resync if something goes really awry - |
kenjiArai | 1:9db0e321a9f4 | 197 | // resync will reset if the count hits 256. |
kenjiArai | 1:9db0e321a9f4 | 198 | if (core_util_atomic_decr_u8(&_unacknowledged_ticks, 1) > 0) { |
kenjiArai | 1:9db0e321a9f4 | 199 | _set_irq_pending(); |
kenjiArai | 1:9db0e321a9f4 | 200 | } |
kenjiArai | 1:9db0e321a9f4 | 201 | } |
kenjiArai | 1:9db0e321a9f4 | 202 | |
kenjiArai | 1:9db0e321a9f4 | 203 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 204 | void SysTimer<US_IN_TICK, IRQ>::cancel_tick() |
kenjiArai | 1:9db0e321a9f4 | 205 | { |
kenjiArai | 1:9db0e321a9f4 | 206 | // Underlying call is interrupt safe |
kenjiArai | 1:9db0e321a9f4 | 207 | |
kenjiArai | 1:9db0e321a9f4 | 208 | remove(); |
kenjiArai | 1:9db0e321a9f4 | 209 | _ticking = false; |
kenjiArai | 1:9db0e321a9f4 | 210 | |
kenjiArai | 1:9db0e321a9f4 | 211 | _clear_irq_pending(); |
kenjiArai | 1:9db0e321a9f4 | 212 | } |
kenjiArai | 1:9db0e321a9f4 | 213 | |
kenjiArai | 1:9db0e321a9f4 | 214 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 215 | uint64_t SysTimer<US_IN_TICK, IRQ>::get_tick() const |
kenjiArai | 1:9db0e321a9f4 | 216 | { |
kenjiArai | 1:9db0e321a9f4 | 217 | // Atomic is necessary as this can be called from any foreground context, |
kenjiArai | 1:9db0e321a9f4 | 218 | // while IRQ can update it. |
kenjiArai | 1:9db0e321a9f4 | 219 | return core_util_atomic_load_u64(&_tick); |
kenjiArai | 1:9db0e321a9f4 | 220 | } |
kenjiArai | 1:9db0e321a9f4 | 221 | |
kenjiArai | 1:9db0e321a9f4 | 222 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 223 | uint64_t SysTimer<US_IN_TICK, IRQ>::update_and_get_tick() |
kenjiArai | 1:9db0e321a9f4 | 224 | { |
kenjiArai | 1:9db0e321a9f4 | 225 | MBED_ASSERT(!_ticking && !_wake_time_set); |
kenjiArai | 1:9db0e321a9f4 | 226 | // Can only be used when no interrupts are scheduled |
kenjiArai | 1:9db0e321a9f4 | 227 | // Update counters to reflect elapsed time |
kenjiArai | 1:9db0e321a9f4 | 228 | uint64_t elapsed_ticks = _elapsed_ticks(); |
kenjiArai | 1:9db0e321a9f4 | 229 | _unacknowledged_ticks = 0; |
kenjiArai | 1:9db0e321a9f4 | 230 | _time_us += elapsed_ticks * US_IN_TICK; |
kenjiArai | 1:9db0e321a9f4 | 231 | _tick += elapsed_ticks; |
kenjiArai | 1:9db0e321a9f4 | 232 | |
kenjiArai | 1:9db0e321a9f4 | 233 | return _tick; |
kenjiArai | 1:9db0e321a9f4 | 234 | } |
kenjiArai | 1:9db0e321a9f4 | 235 | |
kenjiArai | 1:9db0e321a9f4 | 236 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 237 | us_timestamp_t SysTimer<US_IN_TICK, IRQ>::get_time() const |
kenjiArai | 1:9db0e321a9f4 | 238 | { |
kenjiArai | 1:9db0e321a9f4 | 239 | // Underlying call is interrupt safe |
kenjiArai | 1:9db0e321a9f4 | 240 | |
kenjiArai | 1:9db0e321a9f4 | 241 | return ticker_read_us(_ticker_data); |
kenjiArai | 1:9db0e321a9f4 | 242 | } |
kenjiArai | 1:9db0e321a9f4 | 243 | |
kenjiArai | 1:9db0e321a9f4 | 244 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 245 | us_timestamp_t SysTimer<US_IN_TICK, IRQ>::get_time_since_tick() const |
kenjiArai | 1:9db0e321a9f4 | 246 | { |
kenjiArai | 1:9db0e321a9f4 | 247 | // Underlying call is interrupt safe, and _time_us is not updated by IRQ |
kenjiArai | 1:9db0e321a9f4 | 248 | |
kenjiArai | 1:9db0e321a9f4 | 249 | return get_time() - _time_us; |
kenjiArai | 1:9db0e321a9f4 | 250 | } |
kenjiArai | 1:9db0e321a9f4 | 251 | |
kenjiArai | 1:9db0e321a9f4 | 252 | #if (defined(NO_SYSTICK)) |
kenjiArai | 1:9db0e321a9f4 | 253 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 254 | IRQn_Type SysTimer<US_IN_TICK, IRQ>::get_irq_number() |
kenjiArai | 1:9db0e321a9f4 | 255 | { |
kenjiArai | 1:9db0e321a9f4 | 256 | return mbed_get_m0_tick_irqn(); |
kenjiArai | 1:9db0e321a9f4 | 257 | } |
kenjiArai | 1:9db0e321a9f4 | 258 | #elif (TARGET_CORTEX_M) |
kenjiArai | 1:9db0e321a9f4 | 259 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 260 | IRQn_Type SysTimer<US_IN_TICK, IRQ>::get_irq_number() |
kenjiArai | 1:9db0e321a9f4 | 261 | { |
kenjiArai | 1:9db0e321a9f4 | 262 | return SysTick_IRQn; |
kenjiArai | 1:9db0e321a9f4 | 263 | } |
kenjiArai | 1:9db0e321a9f4 | 264 | #elif (TARGET_CORTEX_A) |
kenjiArai | 1:9db0e321a9f4 | 265 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 266 | IRQn_ID_t SysTimer<US_IN_TICK, IRQ>::get_irq_number() |
kenjiArai | 1:9db0e321a9f4 | 267 | { |
kenjiArai | 1:9db0e321a9f4 | 268 | return mbed_get_a9_tick_irqn(); |
kenjiArai | 1:9db0e321a9f4 | 269 | } |
kenjiArai | 1:9db0e321a9f4 | 270 | #endif |
kenjiArai | 1:9db0e321a9f4 | 271 | |
kenjiArai | 1:9db0e321a9f4 | 272 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 273 | void SysTimer<US_IN_TICK, IRQ>::_set_irq_pending() |
kenjiArai | 1:9db0e321a9f4 | 274 | { |
kenjiArai | 1:9db0e321a9f4 | 275 | // Protected function synchronized externally |
kenjiArai | 1:9db0e321a9f4 | 276 | if (!IRQ) { |
kenjiArai | 1:9db0e321a9f4 | 277 | return; |
kenjiArai | 1:9db0e321a9f4 | 278 | } |
kenjiArai | 1:9db0e321a9f4 | 279 | #if (defined(NO_SYSTICK)) |
kenjiArai | 1:9db0e321a9f4 | 280 | NVIC_SetPendingIRQ(mbed_get_m0_tick_irqn()); |
kenjiArai | 1:9db0e321a9f4 | 281 | #elif (TARGET_CORTEX_M) |
kenjiArai | 1:9db0e321a9f4 | 282 | SCB->ICSR = SCB_ICSR_PENDSTSET_Msk; |
kenjiArai | 1:9db0e321a9f4 | 283 | #else |
kenjiArai | 1:9db0e321a9f4 | 284 | IRQ_SetPending(mbed_get_a9_tick_irqn()); |
kenjiArai | 1:9db0e321a9f4 | 285 | #endif |
kenjiArai | 1:9db0e321a9f4 | 286 | } |
kenjiArai | 1:9db0e321a9f4 | 287 | |
kenjiArai | 1:9db0e321a9f4 | 288 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 289 | void SysTimer<US_IN_TICK, IRQ>::_clear_irq_pending() |
kenjiArai | 1:9db0e321a9f4 | 290 | { |
kenjiArai | 1:9db0e321a9f4 | 291 | // Protected function synchronized externally |
kenjiArai | 1:9db0e321a9f4 | 292 | if (!IRQ) { |
kenjiArai | 1:9db0e321a9f4 | 293 | return; |
kenjiArai | 1:9db0e321a9f4 | 294 | } |
kenjiArai | 1:9db0e321a9f4 | 295 | #if (defined(NO_SYSTICK)) |
kenjiArai | 1:9db0e321a9f4 | 296 | NVIC_ClearPendingIRQ(mbed_get_m0_tick_irqn()); |
kenjiArai | 1:9db0e321a9f4 | 297 | #elif (TARGET_CORTEX_M) |
kenjiArai | 1:9db0e321a9f4 | 298 | SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk; |
kenjiArai | 1:9db0e321a9f4 | 299 | #else |
kenjiArai | 1:9db0e321a9f4 | 300 | IRQ_ClearPending(mbed_get_a9_tick_irqn()); |
kenjiArai | 1:9db0e321a9f4 | 301 | #endif |
kenjiArai | 1:9db0e321a9f4 | 302 | } |
kenjiArai | 1:9db0e321a9f4 | 303 | |
kenjiArai | 1:9db0e321a9f4 | 304 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 305 | void SysTimer<US_IN_TICK, IRQ>::_increment_tick() |
kenjiArai | 1:9db0e321a9f4 | 306 | { |
kenjiArai | 1:9db0e321a9f4 | 307 | // Protected function synchronized externally |
kenjiArai | 1:9db0e321a9f4 | 308 | |
kenjiArai | 1:9db0e321a9f4 | 309 | _tick++; |
kenjiArai | 1:9db0e321a9f4 | 310 | _time_us += US_IN_TICK; |
kenjiArai | 1:9db0e321a9f4 | 311 | } |
kenjiArai | 1:9db0e321a9f4 | 312 | |
kenjiArai | 1:9db0e321a9f4 | 313 | template<uint32_t US_IN_TICK, bool IRQ> |
kenjiArai | 1:9db0e321a9f4 | 314 | void SysTimer<US_IN_TICK, IRQ>::handler() |
kenjiArai | 1:9db0e321a9f4 | 315 | { |
kenjiArai | 1:9db0e321a9f4 | 316 | /* To reduce IRQ latency problems, we do not re-arm in the interrupt handler */ |
kenjiArai | 1:9db0e321a9f4 | 317 | if (_wake_time_set) { |
kenjiArai | 1:9db0e321a9f4 | 318 | _wake_time_set = false; |
kenjiArai | 1:9db0e321a9f4 | 319 | if (!_wake_early) { |
kenjiArai | 1:9db0e321a9f4 | 320 | _wake_time_passed = true; |
kenjiArai | 1:9db0e321a9f4 | 321 | } |
kenjiArai | 1:9db0e321a9f4 | 322 | /* If this was an early interrupt, user has the responsibility to check and |
kenjiArai | 1:9db0e321a9f4 | 323 | * note the combination of (!set, !passed), and re-arm the wake timer if |
kenjiArai | 1:9db0e321a9f4 | 324 | * necessary. |
kenjiArai | 1:9db0e321a9f4 | 325 | */ |
kenjiArai | 1:9db0e321a9f4 | 326 | } else if (_ticking) { |
kenjiArai | 1:9db0e321a9f4 | 327 | _unacknowledged_ticks++; |
kenjiArai | 1:9db0e321a9f4 | 328 | _set_irq_pending(); |
kenjiArai | 1:9db0e321a9f4 | 329 | _increment_tick(); |
kenjiArai | 1:9db0e321a9f4 | 330 | // We do this now, rather than in acknowledgement, as we get it "for free" |
kenjiArai | 1:9db0e321a9f4 | 331 | // here - because we're in the ticker handler, the programming gets deferred |
kenjiArai | 1:9db0e321a9f4 | 332 | // until end of dispatch, and the ticker would likely be rescheduling |
kenjiArai | 1:9db0e321a9f4 | 333 | // anyway after dispatch. |
kenjiArai | 1:9db0e321a9f4 | 334 | |
kenjiArai | 1:9db0e321a9f4 | 335 | _schedule_tick(); |
kenjiArai | 1:9db0e321a9f4 | 336 | } |
kenjiArai | 1:9db0e321a9f4 | 337 | } |
kenjiArai | 1:9db0e321a9f4 | 338 | |
kenjiArai | 1:9db0e321a9f4 | 339 | #if MBED_CONF_RTOS_PRESENT |
kenjiArai | 1:9db0e321a9f4 | 340 | /* Whatever the OS wants (in case it isn't 1ms) */ |
kenjiArai | 1:9db0e321a9f4 | 341 | MBED_STATIC_ASSERT(1000000 % OS_TICK_FREQ == 0, "OS_TICK_FREQ must be a divisor of 1000000 for correct tick calculations"); |
kenjiArai | 1:9db0e321a9f4 | 342 | #define OS_TICK_US (1000000 / OS_TICK_FREQ) |
kenjiArai | 1:9db0e321a9f4 | 343 | #if OS_TICK_US != 1000 |
kenjiArai | 1:9db0e321a9f4 | 344 | template class SysTimer<OS_TICK_US>; |
kenjiArai | 1:9db0e321a9f4 | 345 | #endif |
kenjiArai | 1:9db0e321a9f4 | 346 | #endif |
kenjiArai | 1:9db0e321a9f4 | 347 | |
kenjiArai | 1:9db0e321a9f4 | 348 | /* Standard 1ms SysTimer */ |
kenjiArai | 1:9db0e321a9f4 | 349 | template class SysTimer<1000>; |
kenjiArai | 1:9db0e321a9f4 | 350 | |
kenjiArai | 1:9db0e321a9f4 | 351 | /* Standard 1ms SysTimer that doesn't set interrupts, used for Greentea tests */ |
kenjiArai | 1:9db0e321a9f4 | 352 | template class SysTimer<1000, false>; |
kenjiArai | 1:9db0e321a9f4 | 353 | |
kenjiArai | 1:9db0e321a9f4 | 354 | /* Slowed-down SysTimer that doesn't set interrupts, used for Greentea tests */ |
kenjiArai | 1:9db0e321a9f4 | 355 | template class SysTimer<42000, false>; |
kenjiArai | 1:9db0e321a9f4 | 356 | |
kenjiArai | 1:9db0e321a9f4 | 357 | } // namespace internal |
kenjiArai | 1:9db0e321a9f4 | 358 | } // namespace mbed |