2013-02-18 15:32:11 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2006-2013 ARM Limited
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#ifndef MBED_TIMER_H
|
|
|
|
#define MBED_TIMER_H
|
|
|
|
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "platform/platform.h"
|
|
|
|
#include "hal/ticker_api.h"
|
drivers: Mark non identity types as non copyable with the NonCopyable traits.
Classes changed: CAN, Ethernet, FlashIAP, I2C, InterruptIn, LowPowerTicker, LowPowerTimeout, LowPowerTimer, RawSerial, Serial, SerialBase, SPI, SPISlave, Ticker, Timeout, Timer, TimerEvent and UARTSerial.
2017-06-20 13:26:29 +00:00
|
|
|
#include "platform/NonCopyable.h"
|
2017-08-14 15:25:31 +00:00
|
|
|
#include "platform/mbed_sleep.h"
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
namespace mbed {
|
2016-10-04 20:02:44 +00:00
|
|
|
/** \addtogroup drivers */
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** A general purpose timer
|
2016-06-08 12:52:14 +00:00
|
|
|
*
|
2017-04-04 17:40:09 +00:00
|
|
|
* @note Synchronization level: Interrupt safe
|
2013-02-18 15:32:11 +00:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* @code
|
|
|
|
* // Count the time to toggle a LED
|
|
|
|
*
|
|
|
|
* #include "mbed.h"
|
|
|
|
*
|
|
|
|
* Timer timer;
|
|
|
|
* DigitalOut led(LED1);
|
|
|
|
* int begin, end;
|
|
|
|
*
|
|
|
|
* int main() {
|
|
|
|
* timer.start();
|
|
|
|
* begin = timer.read_us();
|
|
|
|
* led = !led;
|
|
|
|
* end = timer.read_us();
|
|
|
|
* printf("Toggle the led takes %d us", end - begin);
|
|
|
|
* }
|
|
|
|
* @endcode
|
2017-04-04 19:21:53 +00:00
|
|
|
* @ingroup drivers
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
drivers: Mark non identity types as non copyable with the NonCopyable traits.
Classes changed: CAN, Ethernet, FlashIAP, I2C, InterruptIn, LowPowerTicker, LowPowerTimeout, LowPowerTimer, RawSerial, Serial, SerialBase, SPI, SPISlave, Ticker, Timeout, Timer, TimerEvent and UARTSerial.
2017-06-20 13:26:29 +00:00
|
|
|
class Timer : private NonCopyable<Timer> {
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Timer();
|
2015-05-21 07:36:58 +00:00
|
|
|
Timer(const ticker_data_t *data);
|
2017-09-14 01:05:40 +00:00
|
|
|
~Timer();
|
2013-02-18 15:32:11 +00:00
|
|
|
|
|
|
|
/** Start the timer
|
|
|
|
*/
|
|
|
|
void start();
|
|
|
|
|
|
|
|
/** Stop the timer
|
|
|
|
*/
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
/** Reset the timer to 0.
|
|
|
|
*
|
|
|
|
* If it was already counting, it will continue
|
|
|
|
*/
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/** Get the time passed in seconds
|
2017-06-05 16:55:27 +00:00
|
|
|
*
|
|
|
|
* @returns Time passed in seconds
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
float read();
|
|
|
|
|
2017-06-05 16:55:27 +00:00
|
|
|
/** Get the time passed in milli-seconds
|
|
|
|
*
|
|
|
|
* @returns Time passed in milli seconds
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
int read_ms();
|
|
|
|
|
|
|
|
/** Get the time passed in micro-seconds
|
2017-06-05 16:55:27 +00:00
|
|
|
*
|
|
|
|
* @returns Time passed in micro seconds
|
2013-02-18 15:32:11 +00:00
|
|
|
*/
|
|
|
|
int read_us();
|
|
|
|
|
2016-07-10 18:47:13 +00:00
|
|
|
/** An operator shorthand for read()
|
|
|
|
*/
|
2013-02-18 15:32:11 +00:00
|
|
|
operator float();
|
|
|
|
|
2017-05-15 14:21:35 +00:00
|
|
|
/** Get in a high resolution type the time passed in micro-seconds.
|
|
|
|
*/
|
|
|
|
us_timestamp_t read_high_resolution_us();
|
|
|
|
|
2013-02-18 15:32:11 +00:00
|
|
|
protected:
|
2017-03-31 14:10:57 +00:00
|
|
|
us_timestamp_t slicetime();
|
|
|
|
int _running; // whether the timer is running
|
|
|
|
us_timestamp_t _start; // the start time of the latest slice
|
|
|
|
us_timestamp_t _time; // any accumulated time from previous slices
|
2015-05-21 07:36:58 +00:00
|
|
|
const ticker_data_t *_ticker_data;
|
2017-09-20 09:07:42 +00:00
|
|
|
bool _lock_deepsleep; // flag which indicates if deep-sleep should be disabled
|
2013-02-18 15:32:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mbed
|
|
|
|
|
|
|
|
#endif
|