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