ticker_api.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /** \addtogroup hal */
  2. /** @{*/
  3. /* mbed Microcontroller Library
  4. * Copyright (c) 2015 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MBED_TICKER_API_H
  19. #define MBED_TICKER_API_H
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include "device.h"
  23. /**
  24. * Legacy format representing a timestamp in us.
  25. * Given it is modeled as a 32 bit integer, this type can represent timestamp
  26. * up to 4294 seconds (71 minutes).
  27. * Prefer using us_timestamp_t which store timestamp as 64 bits integer.
  28. */
  29. typedef uint32_t timestamp_t;
  30. /**
  31. * A us timestamp stored in a 64 bit integer.
  32. * Can store timestamp up to 584810 years.
  33. */
  34. typedef uint64_t us_timestamp_t;
  35. /** Ticker's event structure
  36. */
  37. typedef struct ticker_event_s {
  38. us_timestamp_t timestamp; /**< Event's timestamp */
  39. uint32_t id; /**< TimerEvent object */
  40. struct ticker_event_s *next; /**< Next event in the queue */
  41. } ticker_event_t;
  42. typedef void (*ticker_event_handler)(uint32_t id);
  43. /** Information about the ticker implementation
  44. */
  45. typedef struct {
  46. uint32_t frequency; /**< Frequency in Hz this ticker runs at */
  47. uint32_t bits; /**< Number of bits this ticker supports */
  48. } ticker_info_t;
  49. /** Ticker's interface structure - required API for a ticker
  50. */
  51. typedef struct {
  52. void (*init)(void); /**< Init function */
  53. uint32_t (*read)(void); /**< Read function */
  54. void (*disable_interrupt)(void); /**< Disable interrupt function */
  55. void (*clear_interrupt)(void); /**< Clear interrupt function */
  56. void (*set_interrupt)(timestamp_t timestamp); /**< Set interrupt function */
  57. void (*fire_interrupt)(void); /**< Fire interrupt right-away */
  58. const ticker_info_t *(*get_info)(void); /**< Return info about this ticker's implementation */
  59. } ticker_interface_t;
  60. /** Ticker's event queue structure
  61. */
  62. typedef struct {
  63. ticker_event_handler event_handler; /**< Event handler */
  64. ticker_event_t *head; /**< A pointer to head */
  65. uint32_t frequency; /**< Frequency of the timer in Hz */
  66. uint32_t bitmask; /**< Mask to be applied to time values read */
  67. uint32_t max_delta; /**< Largest delta in ticks that can be used when scheduling */
  68. uint64_t max_delta_us; /**< Largest delta in us that can be used when scheduling */
  69. uint32_t tick_last_read; /**< Last tick read */
  70. uint64_t tick_remainder; /**< Ticks that have not been added to base_time */
  71. us_timestamp_t present_time; /**< Store the timestamp used for present time */
  72. bool initialized; /**< Indicate if the instance is initialized */
  73. bool dispatching; /**< The function ticker_irq_handler is dispatching */
  74. uint8_t frequency_shifts; /**< If frequency is a value of 2^n, this is n, otherwise 0 */
  75. } ticker_event_queue_t;
  76. /** Ticker's data structure
  77. */
  78. typedef struct {
  79. const ticker_interface_t *interface; /**< Ticker's interface */
  80. ticker_event_queue_t *queue; /**< Ticker's event queue */
  81. } ticker_data_t;
  82. #ifdef __cplusplus
  83. extern "C" {
  84. #endif
  85. /**
  86. * \defgroup hal_ticker Ticker HAL functions
  87. * @{
  88. */
  89. /** Initialize a ticker and set the event handler
  90. *
  91. * @param ticker The ticker object.
  92. * @param handler A handler to be set
  93. */
  94. void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
  95. /** IRQ handler that goes through the events to trigger overdue events.
  96. *
  97. * @param ticker The ticker object.
  98. */
  99. void ticker_irq_handler(const ticker_data_t *const ticker);
  100. /** Remove an event from the queue
  101. *
  102. * @param ticker The ticker object.
  103. * @param obj The event object to be removed from the queue
  104. */
  105. void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
  106. /** Insert an event to the queue
  107. *
  108. * The event will be executed in timestamp - ticker_read().
  109. *
  110. * @warning This function does not consider timestamp in the past. If an event
  111. * is inserted with a timestamp less than the current timestamp then the event
  112. * will be executed in timestamp - ticker_read() us.
  113. * The internal counter wrap very quickly it is hard to decide weither an
  114. * event is in the past or in 1 hour.
  115. *
  116. * @note prefer the use of ticker_insert_event_us which allows registration of
  117. * absolute timestamp.
  118. *
  119. * @param ticker The ticker object.
  120. * @param obj The event object to be inserted to the queue
  121. * @param timestamp The event's timestamp
  122. * @param id The event object
  123. */
  124. void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
  125. /** Insert an event to the queue
  126. *
  127. * The event will be executed in timestamp - ticker_read_us() us.
  128. *
  129. * @note If an event is inserted with a timestamp less than the current
  130. * timestamp then the event will be scheduled immediately resulting in
  131. * an instant call to event handler.
  132. *
  133. * @param ticker The ticker object.
  134. * @param obj The event object to be inserted to the queue
  135. * @param timestamp The event's timestamp
  136. * @param id The event object
  137. */
  138. void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id);
  139. /** Read the current (relative) ticker's timestamp
  140. *
  141. * @warning Return a relative timestamp because the counter wrap every 4294
  142. * seconds.
  143. *
  144. * @param ticker The ticker object.
  145. * @return The current timestamp
  146. */
  147. timestamp_t ticker_read(const ticker_data_t *const ticker);
  148. /** Read the current (absolute) ticker's timestamp
  149. *
  150. * @warning Return an absolute timestamp counting from the initialization of the
  151. * ticker.
  152. *
  153. * @param ticker The ticker object.
  154. * @return The current timestamp
  155. */
  156. us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
  157. /** Read the next event's timestamp
  158. *
  159. * @param ticker The ticker object.
  160. * @param timestamp The timestamp object.
  161. * @return 1 if timestamp is pending event, 0 if there's no event pending
  162. */
  163. int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
  164. /* Private functions
  165. *
  166. * @cond PRIVATE
  167. *
  168. */
  169. int _ticker_match_interval_passed(timestamp_t prev_tick, timestamp_t cur_tick, timestamp_t match_tick);
  170. /*
  171. * @endcond PRIVATE
  172. *
  173. */
  174. /**@}*/
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif
  179. /** @}*/