mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
drivers/Timer.cpp@186:707f6e361f3e, 2018-06-22 (annotated)
- Committer:
- Anna Bridge
- Date:
- Fri Jun 22 16:45:37 2018 +0100
- Revision:
- 186:707f6e361f3e
- Parent:
- 175:af195413fb11
mbed-dev library. Release version 162
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "drivers/Timer.h" |
<> | 149:156823d33999 | 17 | #include "hal/ticker_api.h" |
<> | 149:156823d33999 | 18 | #include "hal/us_ticker_api.h" |
<> | 160:d5399cc887bb | 19 | #include "platform/mbed_critical.h" |
AnnaBridge | 175:af195413fb11 | 20 | #include "hal/lp_ticker_api.h" |
<> | 149:156823d33999 | 21 | |
<> | 149:156823d33999 | 22 | namespace mbed { |
<> | 149:156823d33999 | 23 | |
AnnaBridge | 175:af195413fb11 | 24 | Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true) { |
<> | 149:156823d33999 | 25 | reset(); |
<> | 149:156823d33999 | 26 | } |
<> | 149:156823d33999 | 27 | |
AnnaBridge | 175:af195413fb11 | 28 | Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(true) { |
<> | 149:156823d33999 | 29 | reset(); |
Anna Bridge |
186:707f6e361f3e | 30 | #if DEVICE_LPTICKER |
AnnaBridge | 175:af195413fb11 | 31 | _lock_deepsleep = (data != get_lp_ticker_data()); |
AnnaBridge | 175:af195413fb11 | 32 | #endif |
AnnaBridge | 175:af195413fb11 | 33 | } |
AnnaBridge | 175:af195413fb11 | 34 | |
AnnaBridge | 175:af195413fb11 | 35 | Timer::~Timer() { |
AnnaBridge | 175:af195413fb11 | 36 | core_util_critical_section_enter(); |
AnnaBridge | 175:af195413fb11 | 37 | if (_running) { |
AnnaBridge | 175:af195413fb11 | 38 | if(_lock_deepsleep) { |
AnnaBridge | 175:af195413fb11 | 39 | sleep_manager_unlock_deep_sleep(); |
AnnaBridge | 175:af195413fb11 | 40 | } |
AnnaBridge | 175:af195413fb11 | 41 | } |
AnnaBridge | 175:af195413fb11 | 42 | _running = 0; |
AnnaBridge | 175:af195413fb11 | 43 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 44 | } |
<> | 149:156823d33999 | 45 | |
<> | 149:156823d33999 | 46 | void Timer::start() { |
<> | 149:156823d33999 | 47 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 48 | if (!_running) { |
AnnaBridge | 175:af195413fb11 | 49 | if(_lock_deepsleep) { |
AnnaBridge | 175:af195413fb11 | 50 | sleep_manager_lock_deep_sleep(); |
AnnaBridge | 175:af195413fb11 | 51 | } |
AnnaBridge | 167:e84263d55307 | 52 | _start = ticker_read_us(_ticker_data); |
<> | 149:156823d33999 | 53 | _running = 1; |
<> | 149:156823d33999 | 54 | } |
<> | 149:156823d33999 | 55 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 56 | } |
<> | 149:156823d33999 | 57 | |
<> | 149:156823d33999 | 58 | void Timer::stop() { |
<> | 149:156823d33999 | 59 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 60 | _time += slicetime(); |
AnnaBridge | 174:b96e65c34a4d | 61 | if (_running) { |
AnnaBridge | 175:af195413fb11 | 62 | if(_lock_deepsleep) { |
AnnaBridge | 175:af195413fb11 | 63 | sleep_manager_unlock_deep_sleep(); |
AnnaBridge | 175:af195413fb11 | 64 | } |
AnnaBridge | 174:b96e65c34a4d | 65 | } |
<> | 149:156823d33999 | 66 | _running = 0; |
<> | 149:156823d33999 | 67 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 68 | } |
<> | 149:156823d33999 | 69 | |
<> | 149:156823d33999 | 70 | int Timer::read_us() { |
AnnaBridge | 167:e84263d55307 | 71 | return read_high_resolution_us(); |
<> | 149:156823d33999 | 72 | } |
<> | 149:156823d33999 | 73 | |
<> | 149:156823d33999 | 74 | float Timer::read() { |
<> | 149:156823d33999 | 75 | return (float)read_us() / 1000000.0f; |
<> | 149:156823d33999 | 76 | } |
<> | 149:156823d33999 | 77 | |
<> | 149:156823d33999 | 78 | int Timer::read_ms() { |
AnnaBridge | 167:e84263d55307 | 79 | return read_high_resolution_us() / 1000; |
<> | 149:156823d33999 | 80 | } |
<> | 149:156823d33999 | 81 | |
AnnaBridge | 167:e84263d55307 | 82 | us_timestamp_t Timer::read_high_resolution_us() { |
<> | 149:156823d33999 | 83 | core_util_critical_section_enter(); |
AnnaBridge | 167:e84263d55307 | 84 | us_timestamp_t time = _time + slicetime(); |
AnnaBridge | 167:e84263d55307 | 85 | core_util_critical_section_exit(); |
AnnaBridge | 167:e84263d55307 | 86 | return time; |
AnnaBridge | 167:e84263d55307 | 87 | } |
AnnaBridge | 167:e84263d55307 | 88 | |
AnnaBridge | 167:e84263d55307 | 89 | us_timestamp_t Timer::slicetime() { |
AnnaBridge | 167:e84263d55307 | 90 | us_timestamp_t ret = 0; |
AnnaBridge | 167:e84263d55307 | 91 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 92 | if (_running) { |
AnnaBridge | 167:e84263d55307 | 93 | ret = ticker_read_us(_ticker_data) - _start; |
<> | 149:156823d33999 | 94 | } |
<> | 149:156823d33999 | 95 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 96 | return ret; |
<> | 149:156823d33999 | 97 | } |
<> | 149:156823d33999 | 98 | |
<> | 149:156823d33999 | 99 | void Timer::reset() { |
<> | 149:156823d33999 | 100 | core_util_critical_section_enter(); |
AnnaBridge | 167:e84263d55307 | 101 | _start = ticker_read_us(_ticker_data); |
<> | 149:156823d33999 | 102 | _time = 0; |
<> | 149:156823d33999 | 103 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 104 | } |
<> | 149:156823d33999 | 105 | |
<> | 149:156823d33999 | 106 | Timer::operator float() { |
<> | 149:156823d33999 | 107 | return read(); |
<> | 149:156823d33999 | 108 | } |
<> | 149:156823d33999 | 109 | |
<> | 149:156823d33999 | 110 | } // namespace mbed |