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.
Fork of Timer64 by
Timer64Simple.cpp
- Committer:
- andriym
- Date:
- 2017-06-09
- Revision:
- 9:79633fe7d95b
- Parent:
- Timer64.cpp@ 8:5d17ff4f9c23
File content as of revision 9:79633fe7d95b:
/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "Timer64Simple.h" #include "ticker_api.h" #include "us_ticker_api.h" Timer64Simple::Timer64Simple() : _timerRunning(false), _tickerLastTimeUsec(0u), _totalTimeUsec(TIMER64_TOTAL_TIME_INIT), _ticker_data(get_us_ticker_data()) { ; } Timer64Simple::~Timer64Simple() { ; } void Timer64Simple::start(void) { if (!_timerRunning) { _tickerLastTimeUsec = ticker_read(_ticker_data); _timerRunning = true; } } void Timer64Simple::stop(void) { if (_timerRunning) { _read_us(); _timerRunning = false; } } void Timer64Simple::reset(void) { if (_timerRunning) { _tickerLastTimeUsec = ticker_read(_ticker_data); } _totalTimeUsec = TIMER64_TOTAL_TIME_INIT; } uint64_t Timer64Simple::read_us() { if (_timerRunning) { return _read_us(); } return _totalTimeUsec; } uint64_t Timer64Simple::read_ms() { if (_timerRunning) { return _read_us()/1000LU; } return _totalTimeUsec/1000LU; } double Timer64Simple::read() { if (_timerRunning) { return (double)_read_us()/1000000.0L; } return (double)_totalTimeUsec/1000000.0L; } bool Timer64Simple::is_running() { return(_timerRunning); } uint64_t Timer64Simple::_read_us() { timestamp_t tickerCurrentTimeUsec = ticker_read(_ticker_data); // check for ticker time rollover if (tickerCurrentTimeUsec >= _tickerLastTimeUsec) { _totalTimeUsec += (uint64_t)(tickerCurrentTimeUsec - _tickerLastTimeUsec); } else { // rollover! _totalTimeUsec += (uint64_t)(4294967296U - _tickerLastTimeUsec) + (uint64_t)tickerCurrentTimeUsec; } _tickerLastTimeUsec = tickerCurrentTimeUsec; return(_totalTimeUsec); }