app_simple_timer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(SIMPLE_TIMER)
  42. #include "app_simple_timer.h"
  43. #include "nrf.h"
  44. #include "app_util_platform.h"
  45. #include "app_error.h"
  46. #include "nrf_timer.h"
  47. #include "nrf_drv_timer.h"
  48. /**@brief States of simple timer state machine.
  49. */
  50. typedef enum
  51. {
  52. SIMPLE_TIMER_STATE_IDLE = 0,
  53. SIMPLE_TIMER_STATE_INITIALIZED,
  54. SIMPLE_TIMER_STATE_STOPPED,
  55. SIMPLE_TIMER_STATE_STARTED
  56. }simple_timer_states_t;
  57. static app_simple_timer_mode_t m_mode; /**< Registered timer mode. */
  58. static app_simple_timer_timeout_handler_t m_timeout_handler = NULL; /**< Registered time-out handler. */
  59. static void * mp_timeout_handler_context = NULL; /**< Registered time-out handler context. */
  60. static simple_timer_states_t m_simple_timer_state = SIMPLE_TIMER_STATE_IDLE; /**< State machine state. */
  61. const nrf_drv_timer_t SIMPLE_TIMER = NRF_DRV_TIMER_INSTANCE(SIMPLE_TIMER_CONFIG_INSTANCE);
  62. /**
  63. * @brief Handler for timer events.
  64. */
  65. static void app_simple_timer_event_handler(nrf_timer_event_t event_type, void * p_context)
  66. {
  67. switch (event_type)
  68. {
  69. case NRF_TIMER_EVENT_COMPARE0:
  70. if (m_mode == APP_SIMPLE_TIMER_MODE_SINGLE_SHOT)
  71. {
  72. m_simple_timer_state = SIMPLE_TIMER_STATE_STOPPED;
  73. }
  74. //@note: No NULL check required as performed in timer_start(...).
  75. m_timeout_handler(mp_timeout_handler_context);
  76. break;
  77. default:
  78. //Do nothing.
  79. break;
  80. }
  81. }
  82. uint32_t app_simple_timer_init(void)
  83. {
  84. uint32_t err_code = NRF_SUCCESS;
  85. nrf_drv_timer_config_t t_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
  86. t_cfg.mode = NRF_TIMER_MODE_TIMER;
  87. t_cfg.bit_width = NRF_TIMER_BIT_WIDTH_16;
  88. t_cfg.frequency = (nrf_timer_frequency_t)SIMPLE_TIMER_CONFIG_FREQUENCY;
  89. err_code = nrf_drv_timer_init(&SIMPLE_TIMER, &t_cfg, app_simple_timer_event_handler);
  90. if (NRF_SUCCESS == err_code)
  91. {
  92. m_simple_timer_state = SIMPLE_TIMER_STATE_INITIALIZED;
  93. }
  94. return err_code;
  95. }
  96. uint32_t app_simple_timer_start(app_simple_timer_mode_t mode,
  97. app_simple_timer_timeout_handler_t timeout_handler,
  98. uint16_t timeout_ticks,
  99. void * p_context)
  100. {
  101. uint32_t err_code = NRF_SUCCESS;
  102. nrf_timer_short_mask_t timer_short;
  103. VERIFY_PARAM_NOT_NULL(timeout_handler);
  104. if (APP_SIMPLE_TIMER_MODE_REPEATED == mode)
  105. {
  106. timer_short = NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK;
  107. }
  108. else if (APP_SIMPLE_TIMER_MODE_SINGLE_SHOT == mode)
  109. {
  110. timer_short = NRF_TIMER_SHORT_COMPARE0_STOP_MASK;
  111. }
  112. else
  113. {
  114. return NRF_ERROR_INVALID_PARAM;
  115. }
  116. if (SIMPLE_TIMER_STATE_IDLE == m_simple_timer_state)
  117. {
  118. return NRF_ERROR_INVALID_STATE;
  119. }
  120. if (SIMPLE_TIMER_STATE_STARTED == m_simple_timer_state)
  121. {
  122. err_code = app_simple_timer_stop();
  123. APP_ERROR_CHECK(err_code);
  124. }
  125. if (SIMPLE_TIMER_STATE_STOPPED == m_simple_timer_state)
  126. {
  127. nrf_drv_timer_clear(&SIMPLE_TIMER);
  128. }
  129. m_mode = mode;
  130. m_timeout_handler = timeout_handler;
  131. mp_timeout_handler_context = p_context;
  132. nrf_drv_timer_extended_compare(
  133. &SIMPLE_TIMER, NRF_TIMER_CC_CHANNEL0, (uint32_t)timeout_ticks, timer_short, true);
  134. if (m_simple_timer_state == SIMPLE_TIMER_STATE_STOPPED)
  135. {
  136. nrf_drv_timer_resume(&SIMPLE_TIMER);
  137. }
  138. else
  139. {
  140. nrf_drv_timer_enable(&SIMPLE_TIMER);
  141. }
  142. m_simple_timer_state = SIMPLE_TIMER_STATE_STARTED;
  143. return NRF_SUCCESS;
  144. }
  145. uint32_t app_simple_timer_stop(void)
  146. {
  147. if (SIMPLE_TIMER_STATE_STARTED == m_simple_timer_state)
  148. {
  149. nrf_drv_timer_pause(&SIMPLE_TIMER);
  150. m_simple_timer_state = SIMPLE_TIMER_STATE_STOPPED;
  151. }
  152. return NRF_SUCCESS;
  153. }
  154. uint32_t app_simple_timer_uninit(void)
  155. {
  156. uint32_t err_code = NRF_SUCCESS;
  157. if (SIMPLE_TIMER_STATE_IDLE != m_simple_timer_state)
  158. {
  159. nrf_drv_timer_uninit(&SIMPLE_TIMER);
  160. m_simple_timer_state = SIMPLE_TIMER_STATE_IDLE;
  161. }
  162. return err_code;
  163. }
  164. #endif //NRF_MODULE_ENABLED(SIMPLE_TIMER)