Timer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "drivers/Timer.h"
  17. #include "hal/ticker_api.h"
  18. #include "hal/us_ticker_api.h"
  19. #include "platform/mbed_critical.h"
  20. #include "hal/lp_ticker_api.h"
  21. namespace mbed {
  22. Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true)
  23. {
  24. reset();
  25. }
  26. Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(true)
  27. {
  28. reset();
  29. #if DEVICE_LPTICKER
  30. _lock_deepsleep = (data != get_lp_ticker_data());
  31. #endif
  32. }
  33. Timer::~Timer()
  34. {
  35. core_util_critical_section_enter();
  36. if (_running) {
  37. if (_lock_deepsleep) {
  38. sleep_manager_unlock_deep_sleep();
  39. }
  40. }
  41. _running = 0;
  42. core_util_critical_section_exit();
  43. }
  44. void Timer::start()
  45. {
  46. core_util_critical_section_enter();
  47. if (!_running) {
  48. if (_lock_deepsleep) {
  49. sleep_manager_lock_deep_sleep();
  50. }
  51. _start = ticker_read_us(_ticker_data);
  52. _running = 1;
  53. }
  54. core_util_critical_section_exit();
  55. }
  56. void Timer::stop()
  57. {
  58. core_util_critical_section_enter();
  59. _time += slicetime();
  60. if (_running) {
  61. if (_lock_deepsleep) {
  62. sleep_manager_unlock_deep_sleep();
  63. }
  64. }
  65. _running = 0;
  66. core_util_critical_section_exit();
  67. }
  68. int Timer::read_us()
  69. {
  70. return read_high_resolution_us();
  71. }
  72. float Timer::read()
  73. {
  74. return (float)read_us() / 1000000.0f;
  75. }
  76. int Timer::read_ms()
  77. {
  78. return read_high_resolution_us() / 1000;
  79. }
  80. us_timestamp_t Timer::read_high_resolution_us()
  81. {
  82. core_util_critical_section_enter();
  83. us_timestamp_t time = _time + slicetime();
  84. core_util_critical_section_exit();
  85. return time;
  86. }
  87. us_timestamp_t Timer::slicetime()
  88. {
  89. us_timestamp_t ret = 0;
  90. core_util_critical_section_enter();
  91. if (_running) {
  92. ret = ticker_read_us(_ticker_data) - _start;
  93. }
  94. core_util_critical_section_exit();
  95. return ret;
  96. }
  97. void Timer::reset()
  98. {
  99. core_util_critical_section_enter();
  100. _start = ticker_read_us(_ticker_data);
  101. _time = 0;
  102. core_util_critical_section_exit();
  103. }
  104. Timer::operator float()
  105. {
  106. return read();
  107. }
  108. } // namespace mbed