timer.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * \file
  3. *
  4. * \brief SAM TIMER Driver for SAMB11
  5. *
  6. * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include "timer.h"
  47. static timer_callback_t timer_callback;
  48. /**
  49. * \brief Initializes config with predefined default values.
  50. *
  51. * This function will initialize a given TIMER configuration structure to
  52. * a set of known default values. This function should be called on
  53. * any new instance of the configuration structures before being
  54. * modified by the user application.
  55. *
  56. * The default configuration is as follows:
  57. * \li Timer interrupt set as disable
  58. * \li Set relaod value as 0
  59. *
  60. * \param[out] config Pointer to a TIMER module configuration structure to set
  61. */
  62. void timer_get_config_defaults(struct timer_config *config)
  63. {
  64. config->reload_value = 0;
  65. config->interrupt_enable = true;
  66. }
  67. /**
  68. * \brief Get TIMER0 module current value.
  69. *
  70. * \retval Current value
  71. */
  72. uint32_t timer_get_value(void)
  73. {
  74. return TIMER0->VALUE.reg;
  75. }
  76. /**
  77. * \brief Set TIMER0 module value.
  78. *
  79. * \param[in] value Reload value
  80. */
  81. void timer_set_value(uint32_t value)
  82. {
  83. TIMER0->RELOAD.reg = value;
  84. }
  85. /**
  86. * \brief Get TIMER0 module interrupt status
  87. *
  88. * \retval The status of module
  89. */
  90. uint32_t timer_get_interrupt_status(void)
  91. {
  92. return TIMER0->INTSTATUSCLEAR.reg;
  93. }
  94. /**
  95. * \brief Clear TIMER0 module interrupt status
  96. *
  97. * Clear the TIMER0 module interrupt status
  98. */
  99. void timer_clear_interrupt_status(void)
  100. {
  101. TIMER0->INTSTATUSCLEAR.reg = 1;
  102. /* Wait for operation finish */
  103. while (TIMER0->INTSTATUSCLEAR.reg);
  104. }
  105. /**
  106. * \brief Set TIMER0 module enable
  107. *
  108. * Enable the TIMER0 module
  109. */
  110. void timer_enable(void)
  111. {
  112. TIMER0->CTRL.reg |= TIMER_CTRL_ENABLE;
  113. }
  114. /**
  115. * \brief Set TIMER0 disable
  116. *
  117. * Disable the TIMER0 module
  118. */
  119. void timer_disable(void)
  120. {
  121. TIMER0->CTRL.reg &= (~TIMER_CTRL_ENABLE);
  122. }
  123. /**
  124. * \brief Registers a callback.
  125. *
  126. * Registers and enable a callback function which is implemented by the user.
  127. *
  128. * \param[in] callback_func Pointer to callback function
  129. */
  130. void timer_register_callback(timer_callback_t fun)
  131. {
  132. timer_callback = fun;
  133. }
  134. /**
  135. * \brief Unregisters a callback.
  136. *
  137. * Unregisters and disable a callback function implemented by the user.
  138. *
  139. */
  140. void timer_unregister_callback(void)
  141. {
  142. timer_callback = NULL;
  143. }
  144. /**
  145. * \brief Timer ISR handler.
  146. *
  147. * Timer ISR handler.
  148. *
  149. */
  150. static void timer_isr_handler(void)
  151. {
  152. if (timer_get_interrupt_status()) {
  153. timer_clear_interrupt_status();
  154. if (timer_callback) {
  155. timer_callback();
  156. }
  157. }
  158. }
  159. /**
  160. * \brief Initializes TIMER0 module instance.
  161. *
  162. * Initializes the TIMER0 module, based on the given
  163. * configuration values.
  164. *
  165. * \param[in] config Pointer to the TIMER configuration options struct
  166. *
  167. * \return Status of the initialization procedure.
  168. */
  169. void timer_init(const struct timer_config *config)
  170. {
  171. /* Global reset */
  172. system_peripheral_reset(PERIPHERAL_TIMER);
  173. TIMER0->CTRL.reg = config->interrupt_enable << TIMER_CTRL_INTERRUPT_ENABLE_Pos;
  174. TIMER0->RELOAD.reg = config->reload_value;
  175. timer_callback = NULL;
  176. system_register_isr(RAM_ISR_TABLE_TIMER0_INDEX, (uint32_t)timer_isr_handler);
  177. }