Timer.h 2.5 KB

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