tc_interrupt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * \file
  3. *
  4. * \brief SAM TC - Timer Counter Callback Driver
  5. *
  6. * Copyright (C) 2013-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 "tc_interrupt.h"
  47. void *_tc_instances[TC_INST_NUM];
  48. void _tc_interrupt_handler(uint8_t instance);
  49. /**
  50. * \brief Registers a callback.
  51. *
  52. * Registers a callback function which is implemented by the user.
  53. *
  54. * \note The callback must be enabled by \ref tc_enable_callback,
  55. * in order for the interrupt handler to call it when the conditions for the
  56. * callback type is met.
  57. *
  58. * \param[in] module Pointer to TC software instance struct
  59. * \param[in] callback_func Pointer to callback function
  60. * \param[in] callback_type Callback type given by an enum
  61. */
  62. enum status_code tc_register_callback(
  63. struct tc_module *const module,
  64. tc_callback_t callback_func,
  65. const enum tc_callback callback_type)
  66. {
  67. /* Sanity check arguments */
  68. Assert(module);
  69. Assert(callback_func);
  70. /* Register callback function */
  71. module->callback[callback_type] = callback_func;
  72. /* Set the bit corresponding to the callback_type */
  73. if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
  74. module->register_callback_mask |= TC_INTFLAG_MC(1);
  75. }
  76. else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
  77. module->register_callback_mask |= TC_INTFLAG_MC(2);
  78. }
  79. else {
  80. module->register_callback_mask |= (1 << callback_type);
  81. }
  82. return STATUS_OK;
  83. }
  84. /**
  85. * \brief Unregisters a callback.
  86. *
  87. * Unregisters a callback function implemented by the user. The callback should be
  88. * disabled before it is unregistered.
  89. *
  90. * \param[in] module Pointer to TC software instance struct
  91. * \param[in] callback_type Callback type given by an enum
  92. */
  93. enum status_code tc_unregister_callback(
  94. struct tc_module *const module,
  95. const enum tc_callback callback_type)
  96. {
  97. /* Sanity check arguments */
  98. Assert(module);
  99. /* Unregister callback function */
  100. module->callback[callback_type] = NULL;
  101. /* Clear the bit corresponding to the callback_type */
  102. if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
  103. module->register_callback_mask &= ~TC_INTFLAG_MC(1);
  104. }
  105. else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
  106. module->register_callback_mask &= ~TC_INTFLAG_MC(2);
  107. }
  108. else {
  109. module->register_callback_mask &= ~(1 << callback_type);
  110. }
  111. return STATUS_OK;
  112. }
  113. /**
  114. * \internal ISR handler for TC
  115. *
  116. * Auto-generate a set of interrupt handlers for each TC in the device.
  117. */
  118. #define _TC_INTERRUPT_HANDLER(n, m) \
  119. void TC##n##_Handler(void) \
  120. { \
  121. _tc_interrupt_handler(m); \
  122. }
  123. #if (SAML21E) || (SAML21G) || (SAMR30E) || (SAMR30G)
  124. _TC_INTERRUPT_HANDLER(0,0)
  125. _TC_INTERRUPT_HANDLER(1,1)
  126. _TC_INTERRUPT_HANDLER(4,2)
  127. #else
  128. MRECURSION(TC_INST_NUM, _TC_INTERRUPT_HANDLER, TC_INST_MAX_ID)
  129. #endif
  130. /**
  131. * \internal Interrupt Handler for TC module
  132. *
  133. * Handles interrupts as they occur, it will run the callback functions
  134. * that are registered and enabled.
  135. *
  136. * \param[in] instance ID of the TC instance calling the interrupt
  137. * handler
  138. */
  139. void _tc_interrupt_handler(
  140. uint8_t instance)
  141. {
  142. /* Temporary variable */
  143. uint8_t interrupt_and_callback_status_mask;
  144. /* Get device instance from the look-up table */
  145. struct tc_module *module
  146. = (struct tc_module *)_tc_instances[instance];
  147. /* Read and mask interrupt flag register */
  148. interrupt_and_callback_status_mask = module->hw->COUNT8.INTFLAG.reg &
  149. module->register_callback_mask &
  150. module->enable_callback_mask;
  151. /* Check if an Overflow interrupt has occurred */
  152. if (interrupt_and_callback_status_mask & TC_INTFLAG_OVF) {
  153. /* Invoke registered and enabled callback function */
  154. (module->callback[TC_CALLBACK_OVERFLOW])(module);
  155. /* Clear interrupt flag */
  156. module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_OVF;
  157. }
  158. /* Check if an Error interrupt has occurred */
  159. if (interrupt_and_callback_status_mask & TC_INTFLAG_ERR) {
  160. /* Invoke registered and enabled callback function */
  161. (module->callback[TC_CALLBACK_ERROR])(module);
  162. /* Clear interrupt flag */
  163. module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_ERR;
  164. }
  165. /* Check if an Match/Capture Channel 0 interrupt has occurred */
  166. if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(1)) {
  167. /* Invoke registered and enabled callback function */
  168. (module->callback[TC_CALLBACK_CC_CHANNEL0])(module);
  169. /* Clear interrupt flag */
  170. module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(1);
  171. }
  172. /* Check if an Match/Capture Channel 1 interrupt has occurred */
  173. if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(2)) {
  174. /* Invoke registered and enabled callback function */
  175. (module->callback[TC_CALLBACK_CC_CHANNEL1])(module);
  176. /* Clear interrupt flag */
  177. module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(2);
  178. }
  179. }