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