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