app_simple_timer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 app_simple_timer Simple Timer
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Simple timer module.
  47. *
  48. * Supported features and limitations:
  49. * - Two modes: single shot mode and repeated mode.
  50. * - No more than one timer can run simultaneously.
  51. * - The timer is hard-coded to use the TIMER1 peripheral and compare channel 0.
  52. */
  53. #ifndef TIMER_H__
  54. #define TIMER_H__
  55. #include <stdint.h>
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /**@brief Timer time-out handler type. */
  60. typedef void (*app_simple_timer_timeout_handler_t)(void * p_context);
  61. /**@brief Timer modes. */
  62. typedef enum
  63. {
  64. APP_SIMPLE_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */
  65. APP_SIMPLE_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
  66. } app_simple_timer_mode_t;
  67. /**@brief Function for configuring and setting up the timer hardware.
  68. *
  69. * @note Timer frequency is configured statically.
  70. *
  71. * @retval NRF_SUCCESS If the operation is successful.
  72. * @retval NRF_ERROR_INVALID_STATE If the operation fails because the timer is already initialized.
  73. * @retval NRF_ERROR_INVALID_PARAM If the operation fails because some configuration parameter is
  74. * not valid.
  75. */
  76. uint32_t app_simple_timer_init(void);
  77. /**@brief Function for starting a timer.
  78. *
  79. * @note If this function is called for a timer that is already running, the currently running
  80. * timer is stopped before starting the new one.
  81. *
  82. * @param[in] mode Timer mode (see @ref app_simple_timer_mode_t).
  83. * @param[in] timeout_handler Function to be executed when the timer expires
  84. * (see @ref app_simple_timer_timeout_handler_t).
  85. * @param[in] timeout_ticks Number of timer ticks to time-out event.
  86. * @param[in] p_context General purpose pointer. Will be passed to the time-out handler
  87. * when the timer expires.
  88. *
  89. * @retval NRF_SUCCESS If the operation is successful.
  90. * @retval NRF_ERROR_INVALID_STATE If the operation fails because @ref app_simple_timer_init has not
  91. * been called and the operation is not allowed in this state.
  92. * @retval NRF_ERROR_NULL If the operation fails because timeout_handler is NULL.
  93. * @retval NRF_ERROR_INVALID_PARAM If the operation fails because "mode" parameter is not valid.
  94. */
  95. uint32_t app_simple_timer_start(app_simple_timer_mode_t mode,
  96. app_simple_timer_timeout_handler_t timeout_handler,
  97. uint16_t timeout_ticks,
  98. void * p_context);
  99. /**@brief Function for stopping the timer.
  100. *
  101. * @retval NRF_SUCCESS If the operation is successful.
  102. */
  103. uint32_t app_simple_timer_stop(void);
  104. /**@brief Function for uninitializing the timer. Should be called also when the timer is not used
  105. * anymore to reach lowest power consumption in system.
  106. *
  107. * @note The function switches off the internal core of the timer to reach lowest power consumption
  108. * in system. The startup time from this state may be longer compared to starting the timer
  109. * from the stopped state.
  110. *
  111. * @retval NRF_SUCCESS If the operation is successful.
  112. */
  113. uint32_t app_simple_timer_uninit(void);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif // TIMER_H__
  118. /** @} */