Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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