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