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