CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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