iot_timer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
  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. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup iot_timer IoT Timer
  43. * @{
  44. * @ingroup iot_sdk_common
  45. * @brief Timekeeping for other modules.
  46. *
  47. * @details The IoT Timer stores the value of the wall clock, which represents the time elapsed
  48. * since startup (or an integer overflow). The unit of timekeeping is milliseconds.
  49. * The IoT Timer module is conceived to be platform independent, therefore, it does not
  50. * have an internal tick source. An external source has to update the wall clock of the
  51. * IoT Timer at regular intervals.
  52. * Other modules can query the current value of the wall clock and/or can act as clients
  53. * of the IoT Timer by subscribing to callbacks that are repeated at regular intervals.
  54. * You can configure the module by changing the @c sdk_config.h configuration file.
  55. */
  56. #ifndef IOT_TIMER_H__
  57. #define IOT_TIMER_H__
  58. #include <stdint.h>
  59. #include "nrf_error.h"
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. #define SEC_TO_MILLISEC(PARAM) (PARAM * 1000)
  64. /**@brief The type of an instant in milliseconds. */
  65. typedef uint32_t iot_timer_time_in_ms_t;
  66. /**@brief IoT Timer client callback type.
  67. *
  68. * @param[in] wall_clock_value The value of the wall clock that triggered the callback.
  69. *
  70. * @retval None.
  71. *
  72. */
  73. typedef void (*iot_timer_tick_cb)(iot_timer_time_in_ms_t wall_clock_value);
  74. /**@brief IoT Timer client structure.
  75. *
  76. * @note @ref cb_interval cannot be zero.
  77. * @note @ref cb_interval must be greater or equal to @ref IOT_TIMER_RESOLUTION_IN_MS.
  78. * @note If greater, @ref cb_interval must be an integral multiple of @ref IOT_TIMER_RESOLUTION_IN_MS.
  79. */
  80. typedef struct
  81. {
  82. iot_timer_tick_cb iot_timer_callback; /**< Callback procedure of the client. */
  83. iot_timer_time_in_ms_t cb_interval; /**< Interval between repeated callbacks to the client. */
  84. } iot_timer_client_t;
  85. /**@brief IoT Timer client list structure. */
  86. typedef struct
  87. {
  88. uint8_t client_list_length; /**< Total number of clients. */
  89. const iot_timer_client_t * p_client_list; /**< Pointer to the constant array of clients or NULL. */
  90. } iot_timer_clients_list_t;
  91. /**@brief Function for setting the list of clients that subscribe for repeated callbacks from
  92. * the module.
  93. *
  94. * @note To minimize drift between client callbacks, the callback function of each client
  95. * should be designed in a way that the duration of execution does not vary and is as short
  96. * as possible.
  97. *
  98. * @param[in] p_list_of_clients Address of the client list. Can be NULL to cancel all subscriptions.
  99. * To see what parameters are valid, please see the description
  100. * of @ref iot_timer_client_t.
  101. *
  102. * @retval NRF_SUCCESS Address of the list of clients successfully updated.
  103. * @retval NRF_ERROR_INVALID_PARAM If any member of the client list has NULL as a callback
  104. * procedure or the interval for repeated callbacks is smaller
  105. * or equal to @ref IOT_TIMER_RESOLUTION_IN_MS or it is not
  106. * an integral multiple of @ref IOT_TIMER_RESOLUTION_IN_MS.
  107. *
  108. */
  109. uint32_t iot_timer_client_list_set(const iot_timer_clients_list_t * p_list_of_clients);
  110. /**@brief Function for updating the wall clock.
  111. *
  112. * @details The application designer must ensure that this function is called at regular intervals,
  113. * which is set in the @c sdk_config.h configuration file.
  114. * If the updated value of the wall clock is an integral multiple of the callback interval
  115. * of any clients of the module, the callback procedure of the client is executed.
  116. *
  117. * @note The interrupt that triggers the update of the wall clock should have a high relative
  118. * priority to minimize inaccuracy.
  119. *
  120. * @retval NRF_SUCCESS Wall clock successfully updated.
  121. *
  122. */
  123. uint32_t iot_timer_update(void);
  124. /**@brief Function for getting the current wall clock value.
  125. *
  126. * @note The accuracy of timekeeping is limited by the resolution, as set in the @c sdk_config.h
  127. * configuration file.
  128. *
  129. * @param[out] p_elapsed_time Value of the wall clock. Time in milliseconds elapsed since startup
  130. * (or an integer overflow).
  131. *
  132. * @retval NRF_SUCCESS Query successful.
  133. * @retval NRF_ERROR_NULL If @b p_elapsed_time is a NULL pointer.
  134. *
  135. */
  136. uint32_t iot_timer_wall_clock_get(iot_timer_time_in_ms_t * p_elapsed_time);
  137. /**@brief Function for getting the difference between the current and an older wall clock value.
  138. *
  139. * @note The accuracy of calculation is limited by the wall clock resolution, as set in
  140. * the @c sdk_config.h configuration file.
  141. * @note The time difference can only be calculated correctly if only at most one integer overflow
  142. * of the wall clock has occured since the past wall clock value was obtained.
  143. *
  144. * @param[in] p_past_time Past value of the wall clock. Has to be an integral multiple of
  145. * @ref IOT_TIMER_RESOLUTION_IN_MS or zero.
  146. * @param[out] p_delta_time Time elapsed since @b p_past_time in milliseconds.
  147. *
  148. * @retval NRF_SUCCESS Query successful.
  149. * @retval NRF_ERROR_NULL If @b p_past_time or @b p_delta_time is a NULL pointer.
  150. * @retval NRF_ERROR_INVALID_PARAM If @b p_past_time points to a value that is not an integral
  151. * multiple of @ref IOT_TIMER_RESOLUTION_IN_MS.
  152. *
  153. */
  154. uint32_t iot_timer_wall_clock_delta_get(iot_timer_time_in_ms_t * p_past_time, \
  155. iot_timer_time_in_ms_t * p_delta_time);
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif // IOT_TIMER_H__
  160. /**@} */