app_simple_timer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "app_simple_timer.h"
  13. #include "nrf.h"
  14. #include "app_util_platform.h"
  15. #include "app_error.h"
  16. #include "nrf_timer.h"
  17. #include "nrf_drv_timer.h"
  18. #include "sdk_common.h"
  19. /**@brief States of simple timer state machine.
  20. */
  21. typedef enum
  22. {
  23. SIMPLE_TIMER_STATE_IDLE = 0,
  24. SIMPLE_TIMER_STATE_INITIALIZED,
  25. SIMPLE_TIMER_STATE_STOPPED,
  26. SIMPLE_TIMER_STATE_STARTED
  27. }simple_timer_states_t;
  28. static app_simple_timer_mode_t m_mode; /**< Registered timer mode. */
  29. static app_simple_timer_timeout_handler_t m_timeout_handler = NULL; /**< Registered time-out handler. */
  30. static void * mp_timeout_handler_context = NULL; /**< Registered time-out handler context. */
  31. static simple_timer_states_t m_simple_timer_state = SIMPLE_TIMER_STATE_IDLE; /**< State machine state. */
  32. #define APP_SIMPLE_TIMER_INSTANCE 1
  33. #if (APP_SIMPLE_TIMER_INSTANCE == 0)
  34. #if (TIMER_CONFIG_MODE(0) != TIMER_MODE_MODE_Timer)
  35. #error "Unsupported timer mode."
  36. #endif
  37. #if (TIMER_CONFIG_BIT_WIDTH(0) != TIMER_BITMODE_BITMODE_16Bit)
  38. #error "Unsupported timer bit width."
  39. #endif
  40. const nrf_drv_timer_t SIMPLE_TIMER = NRF_DRV_TIMER_INSTANCE(0);
  41. #elif (APP_SIMPLE_TIMER_INSTANCE == 1)
  42. #if (TIMER_CONFIG_MODE(1) != TIMER_MODE_MODE_Timer)
  43. #error "Unsupported timer mode."
  44. #endif
  45. #if (TIMER_CONFIG_BIT_WIDTH(1) != TIMER_BITMODE_BITMODE_16Bit)
  46. #error "Unsupported timer bit width."
  47. #endif
  48. const nrf_drv_timer_t SIMPLE_TIMER = NRF_DRV_TIMER_INSTANCE(1);
  49. #elif (APP_SIMPLE_TIMER_INSTANCE == 2)
  50. #if (TIMER_CONFIG_MODE(2) != TIMER_MODE_MODE_Timer)
  51. #error "Unsupported timer mode."
  52. #endif
  53. #if (TIMER_CONFIG_BIT_WIDTH(2) != TIMER_BITMODE_BITMODE_16Bit)
  54. #error "Unsupported timer bit width."
  55. #endif
  56. const nrf_drv_timer_t SIMPLE_TIMER = NRF_DRV_TIMER_INSTANCE(2);
  57. #else
  58. #error "Wrong timer instance id."
  59. #endif
  60. /**
  61. * @brief Handler for timer events.
  62. */
  63. static void app_simple_timer_event_handler(nrf_timer_event_t event_type, void * p_context)
  64. {
  65. switch(event_type)
  66. {
  67. case NRF_TIMER_EVENT_COMPARE0:
  68. if (m_mode == APP_SIMPLE_TIMER_MODE_SINGLE_SHOT)
  69. {
  70. m_simple_timer_state = SIMPLE_TIMER_STATE_STOPPED;
  71. }
  72. //@note: No NULL check required as performed in timer_start(...).
  73. m_timeout_handler(mp_timeout_handler_context);
  74. break;
  75. default:
  76. //Do nothing.
  77. break;
  78. }
  79. }
  80. uint32_t app_simple_timer_init(void)
  81. {
  82. uint32_t err_code = NRF_SUCCESS;
  83. err_code = nrf_drv_timer_init(&SIMPLE_TIMER, NULL, app_simple_timer_event_handler);
  84. if(NRF_SUCCESS == err_code)
  85. {
  86. m_simple_timer_state = SIMPLE_TIMER_STATE_INITIALIZED;
  87. }
  88. return err_code;
  89. }
  90. uint32_t app_simple_timer_start(app_simple_timer_mode_t mode,
  91. app_simple_timer_timeout_handler_t timeout_handler,
  92. uint16_t timeout_ticks,
  93. void * p_context)
  94. {
  95. uint32_t err_code = NRF_SUCCESS;
  96. nrf_timer_short_mask_t timer_short;
  97. VERIFY_PARAM_NOT_NULL(timeout_handler);
  98. if (APP_SIMPLE_TIMER_MODE_REPEATED == mode)
  99. {
  100. timer_short = NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK;
  101. }
  102. else if(APP_SIMPLE_TIMER_MODE_SINGLE_SHOT == mode)
  103. {
  104. timer_short = NRF_TIMER_SHORT_COMPARE0_STOP_MASK;
  105. }
  106. else
  107. {
  108. return NRF_ERROR_INVALID_PARAM;
  109. }
  110. if(SIMPLE_TIMER_STATE_IDLE == m_simple_timer_state)
  111. {
  112. return NRF_ERROR_INVALID_STATE;
  113. }
  114. if(SIMPLE_TIMER_STATE_STARTED == m_simple_timer_state)
  115. {
  116. err_code = app_simple_timer_stop();
  117. APP_ERROR_CHECK(err_code);
  118. }
  119. if(SIMPLE_TIMER_STATE_STOPPED == m_simple_timer_state)
  120. {
  121. nrf_drv_timer_clear(&SIMPLE_TIMER);
  122. }
  123. m_mode = mode;
  124. m_timeout_handler = timeout_handler;
  125. mp_timeout_handler_context = p_context;
  126. nrf_drv_timer_extended_compare(
  127. &SIMPLE_TIMER, NRF_TIMER_CC_CHANNEL0, (uint32_t)timeout_ticks, timer_short, true);
  128. if (m_simple_timer_state == SIMPLE_TIMER_STATE_STOPPED)
  129. {
  130. nrf_drv_timer_resume(&SIMPLE_TIMER);
  131. }
  132. else
  133. {
  134. nrf_drv_timer_enable(&SIMPLE_TIMER);
  135. }
  136. m_simple_timer_state = SIMPLE_TIMER_STATE_STARTED;
  137. return NRF_SUCCESS;
  138. }
  139. uint32_t app_simple_timer_stop(void)
  140. {
  141. if(SIMPLE_TIMER_STATE_STARTED == m_simple_timer_state)
  142. {
  143. nrf_drv_timer_pause(&SIMPLE_TIMER);
  144. m_simple_timer_state = SIMPLE_TIMER_STATE_STOPPED;
  145. }
  146. return NRF_SUCCESS;
  147. }
  148. uint32_t app_simple_timer_uninit(void)
  149. {
  150. uint32_t err_code = NRF_SUCCESS;
  151. if(SIMPLE_TIMER_STATE_IDLE != m_simple_timer_state)
  152. {
  153. nrf_drv_timer_uninit(&SIMPLE_TIMER);
  154. m_simple_timer_state = SIMPLE_TIMER_STATE_IDLE;
  155. }
  156. return err_code;
  157. }