Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Timer.h Source File

Timer.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 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 #ifndef MBED_TIMER_H
00018 #define MBED_TIMER_H
00019 
00020 #include "platform/platform.h"
00021 #include "hal/ticker_api.h"
00022 #include "platform/NonCopyable.h"
00023 
00024 namespace mbed {
00025 /**
00026  * \defgroup drivers_Timer Timer class
00027  * \ingroup drivers-public-api-ticker
00028  * @{
00029  */
00030 
00031 /** A general purpose timer
00032  *
00033  * @note Synchronization level: Interrupt safe
00034  *
00035  * Example:
00036  * @code
00037  * // Count the time to toggle an LED
00038  *
00039  * #include "mbed.h"
00040  *
00041  * Timer timer;
00042  * DigitalOut led(LED1);
00043  * int begin, end;
00044  *
00045  * int main() {
00046  *     timer.start();
00047  *     begin = timer.read_us();
00048  *     led = !led;
00049  *     end = timer.read_us();
00050  *     printf("Toggle the led takes %d us", end - begin);
00051  * }
00052  * @endcode
00053  */
00054 class Timer : private NonCopyable<Timer> {
00055 
00056 public:
00057     Timer();
00058     Timer(const ticker_data_t *data);
00059     ~Timer();
00060 
00061     /** Start the timer
00062      */
00063     void start();
00064 
00065     /** Stop the timer
00066      */
00067     void stop();
00068 
00069     /** Reset the timer to 0.
00070      *
00071      * If it was already running, it will continue
00072      */
00073     void reset();
00074 
00075     /** Get the time passed in seconds
00076      *
00077      *  @returns    Time passed in seconds
00078      */
00079     float read();
00080 
00081     /** Get the time passed in milliseconds
00082      *
00083      *  @returns    Time passed in milliseconds
00084      */
00085     int read_ms();
00086 
00087     /** Get the time passed in microseconds
00088      *
00089      *  @returns    Time passed in microseconds
00090      */
00091     int read_us();
00092 
00093     /** An operator shorthand for read()
00094      */
00095     operator float();
00096 
00097     /** Get in a high resolution type the time passed in microseconds.
00098      *  Returns a 64 bit integer.
00099      */
00100     us_timestamp_t read_high_resolution_us();
00101 
00102 #if !defined(DOXYGEN_ONLY)
00103 protected:
00104     us_timestamp_t slicetime();
00105     int _running;            // whether the timer is running
00106     us_timestamp_t _start;   // the start time of the latest slice
00107     us_timestamp_t _time;    // any accumulated time from previous slices
00108     const ticker_data_t *_ticker_data;
00109     bool _lock_deepsleep;    // flag that indicates if deep sleep should be disabled
00110 };
00111 #endif
00112 
00113 /** @}*/
00114 
00115 } // namespace mbed
00116 
00117 #endif