mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16 #ifndef MBED_TIMER_H
elessair 0:f269e3021894 17 #define MBED_TIMER_H
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #include "platform/platform.h"
elessair 0:f269e3021894 20 #include "hal/ticker_api.h"
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 namespace mbed {
elessair 0:f269e3021894 23 /** \addtogroup drivers */
elessair 0:f269e3021894 24 /** @{*/
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 /** A general purpose timer
elessair 0:f269e3021894 27 *
elessair 0:f269e3021894 28 * @Note Synchronization level: Interrupt safe
elessair 0:f269e3021894 29 *
elessair 0:f269e3021894 30 * Example:
elessair 0:f269e3021894 31 * @code
elessair 0:f269e3021894 32 * // Count the time to toggle a LED
elessair 0:f269e3021894 33 *
elessair 0:f269e3021894 34 * #include "mbed.h"
elessair 0:f269e3021894 35 *
elessair 0:f269e3021894 36 * Timer timer;
elessair 0:f269e3021894 37 * DigitalOut led(LED1);
elessair 0:f269e3021894 38 * int begin, end;
elessair 0:f269e3021894 39 *
elessair 0:f269e3021894 40 * int main() {
elessair 0:f269e3021894 41 * timer.start();
elessair 0:f269e3021894 42 * begin = timer.read_us();
elessair 0:f269e3021894 43 * led = !led;
elessair 0:f269e3021894 44 * end = timer.read_us();
elessair 0:f269e3021894 45 * printf("Toggle the led takes %d us", end - begin);
elessair 0:f269e3021894 46 * }
elessair 0:f269e3021894 47 * @endcode
elessair 0:f269e3021894 48 */
elessair 0:f269e3021894 49 class Timer {
elessair 0:f269e3021894 50
elessair 0:f269e3021894 51 public:
elessair 0:f269e3021894 52 Timer();
elessair 0:f269e3021894 53 Timer(const ticker_data_t *data);
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 /** Start the timer
elessair 0:f269e3021894 56 */
elessair 0:f269e3021894 57 void start();
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 /** Stop the timer
elessair 0:f269e3021894 60 */
elessair 0:f269e3021894 61 void stop();
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 /** Reset the timer to 0.
elessair 0:f269e3021894 64 *
elessair 0:f269e3021894 65 * If it was already counting, it will continue
elessair 0:f269e3021894 66 */
elessair 0:f269e3021894 67 void reset();
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 /** Get the time passed in seconds
elessair 0:f269e3021894 70 */
elessair 0:f269e3021894 71 float read();
elessair 0:f269e3021894 72
elessair 0:f269e3021894 73 /** Get the time passed in mili-seconds
elessair 0:f269e3021894 74 */
elessair 0:f269e3021894 75 int read_ms();
elessair 0:f269e3021894 76
elessair 0:f269e3021894 77 /** Get the time passed in micro-seconds
elessair 0:f269e3021894 78 */
elessair 0:f269e3021894 79 int read_us();
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 /** An operator shorthand for read()
elessair 0:f269e3021894 82 */
elessair 0:f269e3021894 83 operator float();
elessair 0:f269e3021894 84
elessair 0:f269e3021894 85 protected:
elessair 0:f269e3021894 86 int slicetime();
elessair 0:f269e3021894 87 int _running; // whether the timer is running
elessair 0:f269e3021894 88 unsigned int _start; // the start time of the latest slice
elessair 0:f269e3021894 89 int _time; // any accumulated time from previous slices
elessair 0:f269e3021894 90 const ticker_data_t *_ticker_data;
elessair 0:f269e3021894 91 };
elessair 0:f269e3021894 92
elessair 0:f269e3021894 93 } // namespace mbed
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 #endif
elessair 0:f269e3021894 96
elessair 0:f269e3021894 97 /** @}*/