00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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