sys_time.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef SYS_TIME_H_INCLUDED
  43. #define SYS_TIME_H_INCLUDED
  44. #include <stdint.h>
  45. #include "sys_queue.h"
  46. /** @file
  47. * This file contains declarations of the primitives to work with Time (timers) and necessary types.
  48. *
  49. * @defgroup sys_time Time API
  50. * @ingroup sys_15_4
  51. * @{
  52. * @brief Module for declaring Time API.
  53. * @details The system time module implements some routines to deal with time (timers). The timer can be started by
  54. * sys_timer_start(), stopped by sys_timer_stop(), and adjusted after sleep by sys_timer_adjust(). Some
  55. * information can be acquired by sys_timer_is_started() and sys_time_get(). The correct API for implementing hardware
  56. * delays is sys_time_delay_us(). Note that the module must be initialized by sys_timers_init() which
  57. * is done by sys_init().
  58. */
  59. /**@brief Unsigned type of system time.
  60. */
  61. typedef uint64_t sys_time_t;
  62. /**@brief Signed type of system time.
  63. */
  64. typedef int64_t sys_signed_time_t;
  65. /**@brief Prototype of the user-defined timer callback.
  66. *
  67. * @param p_data Pointer to the data, specific for this callback.
  68. */
  69. typedef void (* sys_timer_callback_t)(void * p_data);
  70. /**@brief System timer type (one-shot or periodic timer).
  71. */
  72. typedef enum
  73. {
  74. SYS_TIMER_ONESHOT, /**< The timer is Oneshot */
  75. SYS_TIMER_PERIODIC /**< The timer is Periodic */
  76. } sys_timer_type_t;
  77. /**@brief Timer descriptor.
  78. */
  79. typedef struct
  80. {
  81. /** Service field. */
  82. sys_queue_item_t item;
  83. /** Service field. */
  84. sys_time_t absolute_time;
  85. /** Relevant time moment, at which this timer is programmed to be triggered,
  86. * measured in microseconds.
  87. */
  88. sys_time_t interval;
  89. /** Periodic or one-shot timer.
  90. *
  91. * @details If type is set to SYS_TIMER_PERIODIC, the timer will restart automatically
  92. * with the same period.
  93. */
  94. sys_timer_type_t type;
  95. /** Timer callback function.
  96. *
  97. * @details This function is to be called, when this timer triggers.
  98. */
  99. sys_timer_callback_t callback;
  100. /** Timer callback parameter.
  101. *
  102. * @details This pointer is to be passed to the timer callback function.
  103. */
  104. void * p_data;
  105. } sys_timer_t;
  106. /**@brief Function for initializing the timers module.
  107. */
  108. void sys_timers_init(void);
  109. /**@brief Function for starting the timer.
  110. *
  111. * @details See the description of \ref sys_timer_t fields for the details
  112. * on how to program the timer.
  113. *
  114. * @param[in] p_timer Pointer to a valid timer descriptor, which is filled by the user,
  115. * according to \ref sys_timer_t fields description.
  116. */
  117. void sys_timer_start(sys_timer_t * p_timer);
  118. /**@brief Function for stopping the timer.
  119. *
  120. * @details This function is used to stop the timer, which was started earlier.
  121. * After this function is called, the timer will not fire.
  122. *
  123. * @param[in] p_timer Pointer to a valid timer descriptor.
  124. */
  125. void sys_timer_stop(sys_timer_t * p_timer);
  126. /**@brief Function for checking if input timer has been started.
  127. *
  128. * @param[in] p_timer Pointer to a timer.
  129. *
  130. * @retval true p_timer has been started and has not been stopped yet.
  131. * @retval false p_timer has never been started or already timed out.
  132. */
  133. bool sys_timer_is_started(sys_timer_t * p_timer);
  134. /**@brief Function for getting the current system time.
  135. *
  136. * @retval The current system timer counter value in microseconds.
  137. */
  138. sys_time_t sys_time_get(void);
  139. /**@brief Function for implementing a delay for short hardware delays.
  140. *
  141. * @warning Interrupts are NOT disabled inside this function.
  142. *
  143. * @param[in] delay_us Number of microseconds to delay.
  144. */
  145. void sys_time_delay_us(uint32_t delay_us);
  146. /**@brief Function for executing expired timers after sleep.
  147. */
  148. void sys_timer_adjust(void);
  149. /** @} */
  150. #endif // SYS_TIME_H_INCLUDED