mbed library sources. Supersedes mbed-src.
Dependents: LPCXpresso1769_blinky
Fork of mbed-dev by
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 "Timer.h" 00017 #include "ticker_api.h" 00018 #include "us_ticker_api.h" 00019 #include "critical.h" 00020 00021 namespace mbed { 00022 00023 Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()) { 00024 reset(); 00025 } 00026 00027 Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data) { 00028 reset(); 00029 } 00030 00031 void Timer::start() { 00032 core_util_critical_section_enter(); 00033 if (!_running) { 00034 _start = ticker_read(_ticker_data); 00035 _running = 1; 00036 } 00037 core_util_critical_section_exit(); 00038 } 00039 00040 void Timer::stop() { 00041 core_util_critical_section_enter(); 00042 _time += slicetime(); 00043 _running = 0; 00044 core_util_critical_section_exit(); 00045 } 00046 00047 int Timer::read_us() { 00048 core_util_critical_section_enter(); 00049 int time = _time + slicetime(); 00050 core_util_critical_section_exit(); 00051 return time; 00052 } 00053 00054 float Timer::read() { 00055 return (float)read_us() / 1000000.0f; 00056 } 00057 00058 int Timer::read_ms() { 00059 return read_us() / 1000; 00060 } 00061 00062 int Timer::slicetime() { 00063 core_util_critical_section_enter(); 00064 int ret = 0; 00065 if (_running) { 00066 ret = ticker_read(_ticker_data) - _start; 00067 } 00068 core_util_critical_section_exit(); 00069 return ret; 00070 } 00071 00072 void Timer::reset() { 00073 core_util_critical_section_enter(); 00074 _start = ticker_read(_ticker_data); 00075 _time = 0; 00076 core_util_critical_section_exit(); 00077 } 00078 00079 Timer::operator float() { 00080 return read(); 00081 } 00082 00083 } // namespace mbed
Generated on Tue Jul 12 2022 16:04:26 by 1.7.2