mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
drivers/source/Timer.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 | #include "drivers/Timer.h" |
kenjiArai | 1:9db0e321a9f4 | 18 | #include "hal/ticker_api.h" |
kenjiArai | 1:9db0e321a9f4 | 19 | #include "hal/us_ticker_api.h" |
kenjiArai | 1:9db0e321a9f4 | 20 | #include "platform/mbed_critical.h" |
kenjiArai | 1:9db0e321a9f4 | 21 | #include "platform/mbed_power_mgmt.h" |
kenjiArai | 1:9db0e321a9f4 | 22 | |
kenjiArai | 1:9db0e321a9f4 | 23 | namespace mbed { |
kenjiArai | 1:9db0e321a9f4 | 24 | |
kenjiArai | 1:9db0e321a9f4 | 25 | Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true) |
kenjiArai | 1:9db0e321a9f4 | 26 | { |
kenjiArai | 1:9db0e321a9f4 | 27 | reset(); |
kenjiArai | 1:9db0e321a9f4 | 28 | } |
kenjiArai | 1:9db0e321a9f4 | 29 | |
kenjiArai | 1:9db0e321a9f4 | 30 | Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), |
kenjiArai | 1:9db0e321a9f4 | 31 | _lock_deepsleep(!data->interface->runs_in_deep_sleep) |
kenjiArai | 1:9db0e321a9f4 | 32 | { |
kenjiArai | 1:9db0e321a9f4 | 33 | reset(); |
kenjiArai | 1:9db0e321a9f4 | 34 | } |
kenjiArai | 1:9db0e321a9f4 | 35 | |
kenjiArai | 1:9db0e321a9f4 | 36 | Timer::~Timer() |
kenjiArai | 1:9db0e321a9f4 | 37 | { |
kenjiArai | 1:9db0e321a9f4 | 38 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 39 | if (_running) { |
kenjiArai | 1:9db0e321a9f4 | 40 | if (_lock_deepsleep) { |
kenjiArai | 1:9db0e321a9f4 | 41 | sleep_manager_unlock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 42 | } |
kenjiArai | 1:9db0e321a9f4 | 43 | } |
kenjiArai | 1:9db0e321a9f4 | 44 | _running = 0; |
kenjiArai | 1:9db0e321a9f4 | 45 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 46 | } |
kenjiArai | 1:9db0e321a9f4 | 47 | |
kenjiArai | 1:9db0e321a9f4 | 48 | void Timer::start() |
kenjiArai | 1:9db0e321a9f4 | 49 | { |
kenjiArai | 1:9db0e321a9f4 | 50 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 51 | if (!_running) { |
kenjiArai | 1:9db0e321a9f4 | 52 | if (_lock_deepsleep) { |
kenjiArai | 1:9db0e321a9f4 | 53 | sleep_manager_lock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 54 | } |
kenjiArai | 1:9db0e321a9f4 | 55 | _start = ticker_read_us(_ticker_data); |
kenjiArai | 1:9db0e321a9f4 | 56 | _running = 1; |
kenjiArai | 1:9db0e321a9f4 | 57 | } |
kenjiArai | 1:9db0e321a9f4 | 58 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 59 | } |
kenjiArai | 1:9db0e321a9f4 | 60 | |
kenjiArai | 1:9db0e321a9f4 | 61 | void Timer::stop() |
kenjiArai | 1:9db0e321a9f4 | 62 | { |
kenjiArai | 1:9db0e321a9f4 | 63 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 64 | _time += slicetime(); |
kenjiArai | 1:9db0e321a9f4 | 65 | if (_running) { |
kenjiArai | 1:9db0e321a9f4 | 66 | if (_lock_deepsleep) { |
kenjiArai | 1:9db0e321a9f4 | 67 | sleep_manager_unlock_deep_sleep(); |
kenjiArai | 1:9db0e321a9f4 | 68 | } |
kenjiArai | 1:9db0e321a9f4 | 69 | } |
kenjiArai | 1:9db0e321a9f4 | 70 | _running = 0; |
kenjiArai | 1:9db0e321a9f4 | 71 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 72 | } |
kenjiArai | 1:9db0e321a9f4 | 73 | |
kenjiArai | 1:9db0e321a9f4 | 74 | int Timer::read_us() |
kenjiArai | 1:9db0e321a9f4 | 75 | { |
kenjiArai | 1:9db0e321a9f4 | 76 | return read_high_resolution_us(); |
kenjiArai | 1:9db0e321a9f4 | 77 | } |
kenjiArai | 1:9db0e321a9f4 | 78 | |
kenjiArai | 1:9db0e321a9f4 | 79 | float Timer::read() |
kenjiArai | 1:9db0e321a9f4 | 80 | { |
kenjiArai | 1:9db0e321a9f4 | 81 | return (float)read_high_resolution_us() / 1000000.0f; |
kenjiArai | 1:9db0e321a9f4 | 82 | } |
kenjiArai | 1:9db0e321a9f4 | 83 | |
kenjiArai | 1:9db0e321a9f4 | 84 | int Timer::read_ms() |
kenjiArai | 1:9db0e321a9f4 | 85 | { |
kenjiArai | 1:9db0e321a9f4 | 86 | return read_high_resolution_us() / 1000; |
kenjiArai | 1:9db0e321a9f4 | 87 | } |
kenjiArai | 1:9db0e321a9f4 | 88 | |
kenjiArai | 1:9db0e321a9f4 | 89 | us_timestamp_t Timer::read_high_resolution_us() |
kenjiArai | 1:9db0e321a9f4 | 90 | { |
kenjiArai | 1:9db0e321a9f4 | 91 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 92 | us_timestamp_t time = _time + slicetime(); |
kenjiArai | 1:9db0e321a9f4 | 93 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 94 | return time; |
kenjiArai | 1:9db0e321a9f4 | 95 | } |
kenjiArai | 1:9db0e321a9f4 | 96 | |
kenjiArai | 1:9db0e321a9f4 | 97 | us_timestamp_t Timer::slicetime() |
kenjiArai | 1:9db0e321a9f4 | 98 | { |
kenjiArai | 1:9db0e321a9f4 | 99 | us_timestamp_t ret = 0; |
kenjiArai | 1:9db0e321a9f4 | 100 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 101 | if (_running) { |
kenjiArai | 1:9db0e321a9f4 | 102 | ret = ticker_read_us(_ticker_data) - _start; |
kenjiArai | 1:9db0e321a9f4 | 103 | } |
kenjiArai | 1:9db0e321a9f4 | 104 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 105 | return ret; |
kenjiArai | 1:9db0e321a9f4 | 106 | } |
kenjiArai | 1:9db0e321a9f4 | 107 | |
kenjiArai | 1:9db0e321a9f4 | 108 | void Timer::reset() |
kenjiArai | 1:9db0e321a9f4 | 109 | { |
kenjiArai | 1:9db0e321a9f4 | 110 | core_util_critical_section_enter(); |
kenjiArai | 1:9db0e321a9f4 | 111 | _start = ticker_read_us(_ticker_data); |
kenjiArai | 1:9db0e321a9f4 | 112 | _time = 0; |
kenjiArai | 1:9db0e321a9f4 | 113 | core_util_critical_section_exit(); |
kenjiArai | 1:9db0e321a9f4 | 114 | } |
kenjiArai | 1:9db0e321a9f4 | 115 | |
kenjiArai | 1:9db0e321a9f4 | 116 | Timer::operator float() |
kenjiArai | 1:9db0e321a9f4 | 117 | { |
kenjiArai | 1:9db0e321a9f4 | 118 | return read(); |
kenjiArai | 1:9db0e321a9f4 | 119 | } |
kenjiArai | 1:9db0e321a9f4 | 120 | |
kenjiArai | 1:9db0e321a9f4 | 121 | } // namespace mbed |