mbed-os
Fork of mbed-os by
drivers/Timer.cpp@1:3deb71413561, 2017-07-20 (annotated)
- Committer:
- xuaner
- Date:
- Thu Jul 20 14:26:57 2017 +0000
- Revision:
- 1:3deb71413561
- Parent:
- 0:f269e3021894
mbed_os
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2013 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | #include "drivers/Timer.h" |
elessair | 0:f269e3021894 | 17 | #include "hal/ticker_api.h" |
elessair | 0:f269e3021894 | 18 | #include "hal/us_ticker_api.h" |
elessair | 0:f269e3021894 | 19 | #include "platform/critical.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | namespace mbed { |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()) { |
elessair | 0:f269e3021894 | 24 | reset(); |
elessair | 0:f269e3021894 | 25 | } |
elessair | 0:f269e3021894 | 26 | |
elessair | 0:f269e3021894 | 27 | Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data) { |
elessair | 0:f269e3021894 | 28 | reset(); |
elessair | 0:f269e3021894 | 29 | } |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | void Timer::start() { |
elessair | 0:f269e3021894 | 32 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 33 | if (!_running) { |
elessair | 0:f269e3021894 | 34 | _start = ticker_read(_ticker_data); |
elessair | 0:f269e3021894 | 35 | _running = 1; |
elessair | 0:f269e3021894 | 36 | } |
elessair | 0:f269e3021894 | 37 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 38 | } |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | void Timer::stop() { |
elessair | 0:f269e3021894 | 41 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 42 | _time += slicetime(); |
elessair | 0:f269e3021894 | 43 | _running = 0; |
elessair | 0:f269e3021894 | 44 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 45 | } |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | int Timer::read_us() { |
elessair | 0:f269e3021894 | 48 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 49 | int time = _time + slicetime(); |
elessair | 0:f269e3021894 | 50 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 51 | return time; |
elessair | 0:f269e3021894 | 52 | } |
elessair | 0:f269e3021894 | 53 | |
elessair | 0:f269e3021894 | 54 | float Timer::read() { |
elessair | 0:f269e3021894 | 55 | return (float)read_us() / 1000000.0f; |
elessair | 0:f269e3021894 | 56 | } |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | int Timer::read_ms() { |
elessair | 0:f269e3021894 | 59 | return read_us() / 1000; |
elessair | 0:f269e3021894 | 60 | } |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | int Timer::slicetime() { |
elessair | 0:f269e3021894 | 63 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 64 | int ret = 0; |
elessair | 0:f269e3021894 | 65 | if (_running) { |
elessair | 0:f269e3021894 | 66 | ret = ticker_read(_ticker_data) - _start; |
elessair | 0:f269e3021894 | 67 | } |
elessair | 0:f269e3021894 | 68 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 69 | return ret; |
elessair | 0:f269e3021894 | 70 | } |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | void Timer::reset() { |
elessair | 0:f269e3021894 | 73 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 74 | _start = ticker_read(_ticker_data); |
elessair | 0:f269e3021894 | 75 | _time = 0; |
elessair | 0:f269e3021894 | 76 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 77 | } |
elessair | 0:f269e3021894 | 78 | |
elessair | 0:f269e3021894 | 79 | Timer::operator float() { |
elessair | 0:f269e3021894 | 80 | return read(); |
elessair | 0:f269e3021894 | 81 | } |
elessair | 0:f269e3021894 | 82 | |
elessair | 0:f269e3021894 | 83 | } // namespace mbed |