Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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