mbed_lp_ticker_wrapper.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2018 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 "hal/lp_ticker_api.h"
  17. #if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
  18. #include "Timeout.h"
  19. #include "mbed_critical.h"
  20. static const timestamp_t min_delta = LPTICKER_DELAY_TICKS;
  21. static bool init = false;
  22. static bool pending = false;
  23. static bool timeout_pending = false;
  24. static timestamp_t last_set_interrupt = 0;
  25. static timestamp_t last_request = 0;
  26. static timestamp_t next = 0;
  27. static timestamp_t mask;
  28. static timestamp_t reschedule_us;
  29. // Do not use SingletonPtr since this must be initialized in a critical section
  30. static mbed::Timeout *timeout;
  31. static uint64_t timeout_data[sizeof(mbed::Timeout) / 8];
  32. /**
  33. * Initialize variables
  34. */
  35. static void init_local()
  36. {
  37. MBED_ASSERT(core_util_in_critical_section());
  38. const ticker_info_t *info = lp_ticker_get_info();
  39. if (info->bits >= 32) {
  40. mask = 0xffffffff;
  41. } else {
  42. mask = ((uint64_t)1 << info->bits) - 1;
  43. }
  44. // Round us_per_tick up
  45. timestamp_t us_per_tick = (1000000 + info->frequency - 1) / info->frequency;
  46. // Add 1 tick to the min delta for the case where the clock transitions after you read it
  47. // Add 4 microseconds to round up the micro second ticker time (which has a frequency of at least 250KHz - 4us period)
  48. reschedule_us = (min_delta + 1) * us_per_tick + 4;
  49. timeout = new (timeout_data) mbed::Timeout();
  50. }
  51. /**
  52. * Call lp_ticker_set_interrupt with a value that is guaranteed to fire
  53. *
  54. * Assumptions
  55. * -Only one low power clock tick can pass from the last read (last_read)
  56. * -The closest an interrupt can fire is max_delta + 1
  57. *
  58. * @param last_read The last value read from lp_ticker_read
  59. * @param timestamp The timestamp to trigger the interrupt at
  60. */
  61. static void set_interrupt_safe(timestamp_t last_read, timestamp_t timestamp)
  62. {
  63. MBED_ASSERT(core_util_in_critical_section());
  64. uint32_t delta = (timestamp - last_read) & mask;
  65. if (delta < min_delta + 2) {
  66. timestamp = (last_read + min_delta + 2) & mask;
  67. }
  68. lp_ticker_set_interrupt(timestamp);
  69. }
  70. /**
  71. * Set the low power ticker match time when hardware is ready
  72. *
  73. * This event is scheduled to set the lp timer after the previous write
  74. * has taken effect and it is safe to write a new value without blocking.
  75. * If the time has already passed then this function fires and interrupt
  76. * immediately.
  77. */
  78. static void set_interrupt_later()
  79. {
  80. core_util_critical_section_enter();
  81. timestamp_t current = lp_ticker_read();
  82. if (_ticker_match_interval_passed(last_request, current, next)) {
  83. lp_ticker_fire_interrupt();
  84. } else {
  85. set_interrupt_safe(current, next);
  86. last_set_interrupt = lp_ticker_read();
  87. }
  88. timeout_pending = false;
  89. core_util_critical_section_exit();
  90. }
  91. /**
  92. * Wrapper around lp_ticker_set_interrupt to prevent blocking
  93. *
  94. * Problems this function is solving:
  95. * 1. Interrupt may not fire if set earlier than LPTICKER_DELAY_TICKS low power clock cycles
  96. * 2. Setting the interrupt back-to-back will block
  97. *
  98. * This wrapper function prevents lp_ticker_set_interrupt from being called
  99. * back-to-back and blocking while the first write is in progress. This function
  100. * avoids that problem by scheduling a timeout event if the lp ticker is in the
  101. * middle of a write operation.
  102. *
  103. * @param timestamp Time to call ticker irq
  104. * @note this is a utility function and it's not required part of HAL implementation
  105. */
  106. extern "C" void lp_ticker_set_interrupt_wrapper(timestamp_t timestamp)
  107. {
  108. core_util_critical_section_enter();
  109. if (!init) {
  110. init_local();
  111. init = true;
  112. }
  113. timestamp_t current = lp_ticker_read();
  114. if (pending) {
  115. // Check if pending should be cleared
  116. if (((current - last_set_interrupt) & mask) >= min_delta) {
  117. pending = false;
  118. }
  119. }
  120. if (pending || timeout_pending) {
  121. next = timestamp;
  122. last_request = current;
  123. if (!timeout_pending) {
  124. timeout->attach_us(set_interrupt_later, reschedule_us);
  125. timeout_pending = true;
  126. }
  127. } else {
  128. // Schedule immediately if nothing is pending
  129. set_interrupt_safe(current, timestamp);
  130. last_set_interrupt = lp_ticker_read();
  131. pending = true;
  132. }
  133. core_util_critical_section_exit();
  134. }
  135. #endif