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.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
Timer.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2019 ARM Limited 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "drivers/Timer.h" 00018 #include "hal/ticker_api.h" 00019 #include "hal/us_ticker_api.h" 00020 #include "platform/mbed_critical.h" 00021 #include "platform/mbed_power_mgmt.h" 00022 00023 namespace mbed { 00024 00025 Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true) 00026 { 00027 reset(); 00028 } 00029 00030 Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), 00031 _lock_deepsleep(!data->interface->runs_in_deep_sleep) 00032 { 00033 reset(); 00034 } 00035 00036 Timer::~Timer() 00037 { 00038 core_util_critical_section_enter(); 00039 if (_running) { 00040 if (_lock_deepsleep) { 00041 sleep_manager_unlock_deep_sleep(); 00042 } 00043 } 00044 _running = 0; 00045 core_util_critical_section_exit(); 00046 } 00047 00048 void Timer::start() 00049 { 00050 core_util_critical_section_enter(); 00051 if (!_running) { 00052 if (_lock_deepsleep) { 00053 sleep_manager_lock_deep_sleep(); 00054 } 00055 _start = ticker_read_us(_ticker_data); 00056 _running = 1; 00057 } 00058 core_util_critical_section_exit(); 00059 } 00060 00061 void Timer::stop() 00062 { 00063 core_util_critical_section_enter(); 00064 _time += slicetime(); 00065 if (_running) { 00066 if (_lock_deepsleep) { 00067 sleep_manager_unlock_deep_sleep(); 00068 } 00069 } 00070 _running = 0; 00071 core_util_critical_section_exit(); 00072 } 00073 00074 int Timer::read_us() 00075 { 00076 return read_high_resolution_us(); 00077 } 00078 00079 float Timer::read() 00080 { 00081 return (float)read_high_resolution_us() / 1000000.0f; 00082 } 00083 00084 int Timer::read_ms() 00085 { 00086 return read_high_resolution_us() / 1000; 00087 } 00088 00089 us_timestamp_t Timer::read_high_resolution_us() 00090 { 00091 core_util_critical_section_enter(); 00092 us_timestamp_t time = _time + slicetime(); 00093 core_util_critical_section_exit(); 00094 return time; 00095 } 00096 00097 us_timestamp_t Timer::slicetime() 00098 { 00099 us_timestamp_t ret = 0; 00100 core_util_critical_section_enter(); 00101 if (_running) { 00102 ret = ticker_read_us(_ticker_data) - _start; 00103 } 00104 core_util_critical_section_exit(); 00105 return ret; 00106 } 00107 00108 void Timer::reset() 00109 { 00110 core_util_critical_section_enter(); 00111 _start = ticker_read_us(_ticker_data); 00112 _time = 0; 00113 core_util_critical_section_exit(); 00114 } 00115 00116 Timer::operator float() 00117 { 00118 return read(); 00119 } 00120 00121 } // namespace mbed
Generated on Tue Jul 12 2022 13:55:00 by
1.7.2