mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
Timer.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 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 "hal/lp_ticker_api.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), _lock_deepsleep(true) 00031 { 00032 reset(); 00033 #if DEVICE_LPTICKER 00034 _lock_deepsleep = (data != get_lp_ticker_data()); 00035 #endif 00036 } 00037 00038 Timer::~Timer() 00039 { 00040 core_util_critical_section_enter(); 00041 if (_running) { 00042 if (_lock_deepsleep) { 00043 sleep_manager_unlock_deep_sleep(); 00044 } 00045 } 00046 _running = 0; 00047 core_util_critical_section_exit(); 00048 } 00049 00050 void Timer::start() 00051 { 00052 core_util_critical_section_enter(); 00053 if (!_running) { 00054 if (_lock_deepsleep) { 00055 sleep_manager_lock_deep_sleep(); 00056 } 00057 _start = ticker_read_us(_ticker_data); 00058 _running = 1; 00059 } 00060 core_util_critical_section_exit(); 00061 } 00062 00063 void Timer::stop() 00064 { 00065 core_util_critical_section_enter(); 00066 _time += slicetime(); 00067 if (_running) { 00068 if (_lock_deepsleep) { 00069 sleep_manager_unlock_deep_sleep(); 00070 } 00071 } 00072 _running = 0; 00073 core_util_critical_section_exit(); 00074 } 00075 00076 int Timer::read_us() 00077 { 00078 return read_high_resolution_us(); 00079 } 00080 00081 float Timer::read() 00082 { 00083 return (float)read_high_resolution_us() / 1000000.0f; 00084 } 00085 00086 int Timer::read_ms() 00087 { 00088 return read_high_resolution_us() / 1000; 00089 } 00090 00091 us_timestamp_t Timer::read_high_resolution_us() 00092 { 00093 core_util_critical_section_enter(); 00094 us_timestamp_t time = _time + slicetime(); 00095 core_util_critical_section_exit(); 00096 return time; 00097 } 00098 00099 us_timestamp_t Timer::slicetime() 00100 { 00101 us_timestamp_t ret = 0; 00102 core_util_critical_section_enter(); 00103 if (_running) { 00104 ret = ticker_read_us(_ticker_data) - _start; 00105 } 00106 core_util_critical_section_exit(); 00107 return ret; 00108 } 00109 00110 void Timer::reset() 00111 { 00112 core_util_critical_section_enter(); 00113 _start = ticker_read_us(_ticker_data); 00114 _time = 0; 00115 core_util_critical_section_exit(); 00116 } 00117 00118 Timer::operator float() 00119 { 00120 return read(); 00121 } 00122 00123 } // namespace mbed
Generated on Tue Jul 12 2022 20:41:16 by 1.7.2