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