mbed library sources
Dependents: STM32F103C8T6-RangoTec STM32-103C8_Plantilla_USB
api/Timer.h@441:8a0b45cd594f, 2014-12-16 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Dec 16 08:15:08 2014 +0000
- Revision:
- 441:8a0b45cd594f
- Parent:
- 13:0645d8841f51
Synchronized with git revision 67fbbf0b635d0c0d93fbe433306c537c2ad206aa
Full URL: https://github.com/mbedmicro/mbed/commit/67fbbf0b635d0c0d93fbe433306c537c2ad206aa/
Targets: nrf51 - updating app_timer.c from Norid'c SDKv7.1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 13:0645d8841f51 | 1 | /* mbed Microcontroller Library |
bogdanm | 13:0645d8841f51 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 13:0645d8841f51 | 3 | * |
bogdanm | 13:0645d8841f51 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 13:0645d8841f51 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 13:0645d8841f51 | 6 | * You may obtain a copy of the License at |
bogdanm | 13:0645d8841f51 | 7 | * |
bogdanm | 13:0645d8841f51 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 13:0645d8841f51 | 9 | * |
bogdanm | 13:0645d8841f51 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 13:0645d8841f51 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 13:0645d8841f51 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 13:0645d8841f51 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 13:0645d8841f51 | 14 | * limitations under the License. |
bogdanm | 13:0645d8841f51 | 15 | */ |
bogdanm | 13:0645d8841f51 | 16 | #ifndef MBED_TIMER_H |
bogdanm | 13:0645d8841f51 | 17 | #define MBED_TIMER_H |
bogdanm | 13:0645d8841f51 | 18 | |
bogdanm | 13:0645d8841f51 | 19 | #include "platform.h" |
bogdanm | 13:0645d8841f51 | 20 | |
bogdanm | 13:0645d8841f51 | 21 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 22 | |
bogdanm | 13:0645d8841f51 | 23 | /** A general purpose timer |
bogdanm | 13:0645d8841f51 | 24 | * |
bogdanm | 13:0645d8841f51 | 25 | * Example: |
bogdanm | 13:0645d8841f51 | 26 | * @code |
bogdanm | 13:0645d8841f51 | 27 | * // Count the time to toggle a LED |
bogdanm | 13:0645d8841f51 | 28 | * |
bogdanm | 13:0645d8841f51 | 29 | * #include "mbed.h" |
bogdanm | 13:0645d8841f51 | 30 | * |
bogdanm | 13:0645d8841f51 | 31 | * Timer timer; |
bogdanm | 13:0645d8841f51 | 32 | * DigitalOut led(LED1); |
bogdanm | 13:0645d8841f51 | 33 | * int begin, end; |
bogdanm | 13:0645d8841f51 | 34 | * |
bogdanm | 13:0645d8841f51 | 35 | * int main() { |
bogdanm | 13:0645d8841f51 | 36 | * timer.start(); |
bogdanm | 13:0645d8841f51 | 37 | * begin = timer.read_us(); |
bogdanm | 13:0645d8841f51 | 38 | * led = !led; |
bogdanm | 13:0645d8841f51 | 39 | * end = timer.read_us(); |
bogdanm | 13:0645d8841f51 | 40 | * printf("Toggle the led takes %d us", end - begin); |
bogdanm | 13:0645d8841f51 | 41 | * } |
bogdanm | 13:0645d8841f51 | 42 | * @endcode |
bogdanm | 13:0645d8841f51 | 43 | */ |
bogdanm | 13:0645d8841f51 | 44 | class Timer { |
bogdanm | 13:0645d8841f51 | 45 | |
bogdanm | 13:0645d8841f51 | 46 | public: |
bogdanm | 13:0645d8841f51 | 47 | Timer(); |
bogdanm | 13:0645d8841f51 | 48 | |
bogdanm | 13:0645d8841f51 | 49 | /** Start the timer |
bogdanm | 13:0645d8841f51 | 50 | */ |
bogdanm | 13:0645d8841f51 | 51 | void start(); |
bogdanm | 13:0645d8841f51 | 52 | |
bogdanm | 13:0645d8841f51 | 53 | /** Stop the timer |
bogdanm | 13:0645d8841f51 | 54 | */ |
bogdanm | 13:0645d8841f51 | 55 | void stop(); |
bogdanm | 13:0645d8841f51 | 56 | |
bogdanm | 13:0645d8841f51 | 57 | /** Reset the timer to 0. |
bogdanm | 13:0645d8841f51 | 58 | * |
bogdanm | 13:0645d8841f51 | 59 | * If it was already counting, it will continue |
bogdanm | 13:0645d8841f51 | 60 | */ |
bogdanm | 13:0645d8841f51 | 61 | void reset(); |
bogdanm | 13:0645d8841f51 | 62 | |
bogdanm | 13:0645d8841f51 | 63 | /** Get the time passed in seconds |
bogdanm | 13:0645d8841f51 | 64 | */ |
bogdanm | 13:0645d8841f51 | 65 | float read(); |
bogdanm | 13:0645d8841f51 | 66 | |
bogdanm | 13:0645d8841f51 | 67 | /** Get the time passed in mili-seconds |
bogdanm | 13:0645d8841f51 | 68 | */ |
bogdanm | 13:0645d8841f51 | 69 | int read_ms(); |
bogdanm | 13:0645d8841f51 | 70 | |
bogdanm | 13:0645d8841f51 | 71 | /** Get the time passed in micro-seconds |
bogdanm | 13:0645d8841f51 | 72 | */ |
bogdanm | 13:0645d8841f51 | 73 | int read_us(); |
bogdanm | 13:0645d8841f51 | 74 | |
bogdanm | 13:0645d8841f51 | 75 | #ifdef MBED_OPERATORS |
bogdanm | 13:0645d8841f51 | 76 | operator float(); |
bogdanm | 13:0645d8841f51 | 77 | #endif |
bogdanm | 13:0645d8841f51 | 78 | |
bogdanm | 13:0645d8841f51 | 79 | protected: |
bogdanm | 13:0645d8841f51 | 80 | int slicetime(); |
bogdanm | 13:0645d8841f51 | 81 | int _running; // whether the timer is running |
bogdanm | 13:0645d8841f51 | 82 | unsigned int _start; // the start time of the latest slice |
bogdanm | 13:0645d8841f51 | 83 | int _time; // any accumulated time from previous slices |
bogdanm | 13:0645d8841f51 | 84 | }; |
bogdanm | 13:0645d8841f51 | 85 | |
bogdanm | 13:0645d8841f51 | 86 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 87 | |
bogdanm | 13:0645d8841f51 | 88 | #endif |