nrf_drv_timer.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "nrf_drv_timer.h"
  13. #include "nrf_drv_common.h"
  14. #include "app_util_platform.h"
  15. #if (TIMER_COUNT == 0)
  16. #error "No TIMER instances enabled in the driver configuration file."
  17. #endif
  18. /**@brief Timer control block. */
  19. typedef struct
  20. {
  21. nrf_timer_event_handler_t handler;
  22. void * context;
  23. nrf_drv_state_t state;
  24. } timer_control_block_t;
  25. static timer_control_block_t m_cb[TIMER_COUNT];
  26. static const nrf_drv_timer_config_t m_default_config[TIMER_COUNT] = {
  27. #if TIMER0_ENABLED
  28. NRF_DRV_TIMER_DEFAULT_CONFIG(0),
  29. #endif
  30. #if TIMER1_ENABLED
  31. NRF_DRV_TIMER_DEFAULT_CONFIG(1),
  32. #endif
  33. #if TIMER2_ENABLED
  34. NRF_DRV_TIMER_DEFAULT_CONFIG(2),
  35. #endif
  36. #if TIMER3_ENABLED
  37. NRF_DRV_TIMER_DEFAULT_CONFIG(3),
  38. #endif
  39. #if TIMER4_ENABLED
  40. NRF_DRV_TIMER_DEFAULT_CONFIG(4),
  41. #endif
  42. };
  43. ret_code_t nrf_drv_timer_init(nrf_drv_timer_t const * const p_instance,
  44. nrf_drv_timer_config_t const * p_config,
  45. nrf_timer_event_handler_t timer_event_handler)
  46. {
  47. timer_control_block_t * p_cb = &m_cb[p_instance->instance_id];
  48. #ifdef SOFTDEVICE_PRESENT
  49. ASSERT(p_instance->p_reg != NRF_TIMER0);
  50. #endif
  51. ASSERT(NRF_TIMER_IS_BIT_WIDTH_VALID(p_instance->p_reg, p_config->bit_width));
  52. if (p_cb->state != NRF_DRV_STATE_UNINITIALIZED)
  53. {
  54. return NRF_ERROR_INVALID_STATE;
  55. }
  56. if (timer_event_handler == NULL)
  57. {
  58. return NRF_ERROR_INVALID_PARAM;
  59. }
  60. if (p_config == NULL)
  61. {
  62. p_config = &m_default_config[p_instance->instance_id];
  63. }
  64. p_cb->handler = timer_event_handler;
  65. p_cb->context = p_config->p_context;
  66. uint8_t i;
  67. for (i = 0; i < p_instance->cc_channel_count; ++i)
  68. {
  69. nrf_timer_event_clear(p_instance->p_reg,
  70. nrf_timer_compare_event_get(i));
  71. }
  72. nrf_drv_common_irq_enable(nrf_drv_get_IRQn(p_instance->p_reg),
  73. p_config->interrupt_priority);
  74. nrf_timer_mode_set(p_instance->p_reg, p_config->mode);
  75. nrf_timer_bit_width_set(p_instance->p_reg, p_config->bit_width);
  76. nrf_timer_frequency_set(p_instance->p_reg, p_config->frequency);
  77. p_cb->state = NRF_DRV_STATE_INITIALIZED;
  78. return NRF_SUCCESS;
  79. }
  80. void nrf_drv_timer_uninit(nrf_drv_timer_t const * const p_instance)
  81. {
  82. nrf_drv_common_irq_disable(nrf_drv_get_IRQn(p_instance->p_reg));
  83. #define DISABLE_ALL UINT32_MAX
  84. nrf_timer_shorts_disable(p_instance->p_reg, DISABLE_ALL);
  85. nrf_timer_int_disable(p_instance->p_reg, DISABLE_ALL);
  86. #undef DISABLE_ALL
  87. nrf_drv_timer_disable(p_instance);
  88. m_cb[p_instance->instance_id].state = NRF_DRV_STATE_UNINITIALIZED;
  89. }
  90. void nrf_drv_timer_enable(nrf_drv_timer_t const * const p_instance)
  91. {
  92. ASSERT(m_cb[p_instance->instance_id].state == NRF_DRV_STATE_INITIALIZED);
  93. nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_START);
  94. m_cb[p_instance->instance_id].state = NRF_DRV_STATE_POWERED_ON;
  95. }
  96. void nrf_drv_timer_disable(nrf_drv_timer_t const * const p_instance)
  97. {
  98. ASSERT(m_cb[p_instance->instance_id].state == NRF_DRV_STATE_POWERED_ON);
  99. nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_SHUTDOWN);
  100. m_cb[p_instance->instance_id].state = NRF_DRV_STATE_INITIALIZED;
  101. }
  102. void nrf_drv_timer_resume(nrf_drv_timer_t const * const p_instance)
  103. {
  104. ASSERT(m_cb[p_instance->instance_id].state == NRF_DRV_STATE_POWERED_ON);
  105. nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_START);
  106. }
  107. void nrf_drv_timer_pause(nrf_drv_timer_t const * const p_instance)
  108. {
  109. ASSERT(m_cb[p_instance->instance_id].state == NRF_DRV_STATE_POWERED_ON);
  110. nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_STOP);
  111. }
  112. void nrf_drv_timer_clear(nrf_drv_timer_t const * const p_instance)
  113. {
  114. ASSERT(m_cb[p_instance->instance_id].state != NRF_DRV_STATE_UNINITIALIZED);
  115. nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_CLEAR);
  116. }
  117. void nrf_drv_timer_increment(nrf_drv_timer_t const * const p_instance)
  118. {
  119. ASSERT(m_cb[p_instance->instance_id].state == NRF_DRV_STATE_POWERED_ON);
  120. ASSERT(nrf_timer_mode_get(p_instance->p_reg) != NRF_TIMER_MODE_TIMER);
  121. nrf_timer_task_trigger(p_instance->p_reg, NRF_TIMER_TASK_COUNT);
  122. }
  123. uint32_t nrf_drv_timer_capture(nrf_drv_timer_t const * const p_instance,
  124. nrf_timer_cc_channel_t cc_channel)
  125. {
  126. ASSERT(m_cb[p_instance->instance_id].state == NRF_DRV_STATE_POWERED_ON);
  127. ASSERT(cc_channel < p_instance->cc_channel_count);
  128. nrf_timer_task_trigger(p_instance->p_reg,
  129. nrf_timer_capture_task_get(cc_channel));
  130. return nrf_timer_cc_read(p_instance->p_reg, cc_channel);
  131. }
  132. void nrf_drv_timer_compare(nrf_drv_timer_t const * const p_instance,
  133. nrf_timer_cc_channel_t cc_channel,
  134. uint32_t cc_value,
  135. bool enable_int)
  136. {
  137. nrf_timer_int_mask_t timer_int = nrf_timer_compare_int_get(cc_channel);
  138. if (enable_int)
  139. {
  140. nrf_timer_int_enable(p_instance->p_reg, timer_int);
  141. }
  142. else
  143. {
  144. nrf_timer_int_disable(p_instance->p_reg, timer_int);
  145. }
  146. nrf_timer_cc_write(p_instance->p_reg, cc_channel, cc_value);
  147. }
  148. void nrf_drv_timer_extended_compare(nrf_drv_timer_t const * const p_instance,
  149. nrf_timer_cc_channel_t cc_channel,
  150. uint32_t cc_value,
  151. nrf_timer_short_mask_t timer_short_mask,
  152. bool enable_int)
  153. {
  154. nrf_timer_shorts_disable(p_instance->p_reg,
  155. (TIMER_SHORTS_COMPARE0_STOP_Msk << cc_channel) |
  156. (TIMER_SHORTS_COMPARE0_CLEAR_Msk << cc_channel));
  157. nrf_timer_shorts_enable(p_instance->p_reg, timer_short_mask);
  158. (void)nrf_drv_timer_compare(p_instance,
  159. cc_channel,
  160. cc_value,
  161. enable_int);
  162. }
  163. void nrf_drv_timer_compare_int_enable(nrf_drv_timer_t const * const p_instance,
  164. uint32_t channel)
  165. {
  166. ASSERT(m_cb[p_instance->instance_id].state != NRF_DRV_STATE_UNINITIALIZED);
  167. ASSERT(channel < p_instance->cc_channel_count);
  168. nrf_timer_event_clear(p_instance->p_reg,
  169. nrf_timer_compare_event_get(channel));
  170. nrf_timer_int_enable(p_instance->p_reg,
  171. nrf_timer_compare_int_get(channel));
  172. }
  173. void nrf_drv_timer_compare_int_disable(nrf_drv_timer_t const * const p_instance,
  174. uint32_t channel)
  175. {
  176. ASSERT(m_cb[p_instance->instance_id].state != NRF_DRV_STATE_UNINITIALIZED);
  177. ASSERT(channel < p_instance->cc_channel_count);
  178. nrf_timer_int_disable(p_instance->p_reg,
  179. nrf_timer_compare_int_get(channel));
  180. }
  181. static void irq_handler(NRF_TIMER_Type * p_reg,
  182. timer_control_block_t * p_cb,
  183. uint8_t channel_count)
  184. {
  185. uint8_t i;
  186. for (i = 0; i < channel_count; ++i)
  187. {
  188. nrf_timer_event_t event = nrf_timer_compare_event_get(i);
  189. nrf_timer_int_mask_t int_mask = nrf_timer_compare_int_get(i);
  190. if (nrf_timer_event_check(p_reg, event) &&
  191. nrf_timer_int_enable_check(p_reg, int_mask))
  192. {
  193. nrf_timer_event_clear(p_reg, event);
  194. p_cb->handler(event, p_cb->context);
  195. }
  196. }
  197. }
  198. #if TIMER0_ENABLED
  199. void TIMER0_IRQHandler(void)
  200. {
  201. irq_handler(NRF_TIMER0, &m_cb[TIMER0_INSTANCE_INDEX],
  202. NRF_TIMER_CC_CHANNEL_COUNT(0));
  203. }
  204. #endif
  205. #if TIMER1_ENABLED
  206. void TIMER1_IRQHandler(void)
  207. {
  208. irq_handler(NRF_TIMER1, &m_cb[TIMER1_INSTANCE_INDEX],
  209. NRF_TIMER_CC_CHANNEL_COUNT(1));
  210. }
  211. #endif
  212. #if TIMER2_ENABLED
  213. void TIMER2_IRQHandler(void)
  214. {
  215. irq_handler(NRF_TIMER2, &m_cb[TIMER2_INSTANCE_INDEX],
  216. NRF_TIMER_CC_CHANNEL_COUNT(2));
  217. }
  218. #endif
  219. #if TIMER3_ENABLED
  220. void TIMER3_IRQHandler(void)
  221. {
  222. irq_handler(NRF_TIMER3, &m_cb[TIMER3_INSTANCE_INDEX],
  223. NRF_TIMER_CC_CHANNEL_COUNT(3));
  224. }
  225. #endif
  226. #if TIMER4_ENABLED
  227. void TIMER4_IRQHandler(void)
  228. {
  229. irq_handler(NRF_TIMER4, &m_cb[TIMER4_INSTANCE_INDEX],
  230. NRF_TIMER_CC_CHANNEL_COUNT(4));
  231. }
  232. #endif