tc_interrupt.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef TC_INTERRUPT_H_INCLUDED
  47. #define TC_INTERRUPT_H_INCLUDED
  48. #include "tc.h"
  49. #include <system_interrupt.h>
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. #if !defined(__DOXYGEN__)
  54. extern void *_tc_instances[TC_INST_NUM];
  55. # define _TC_INTERRUPT_VECT_NUM(n, unused) \
  56. SYSTEM_INTERRUPT_MODULE_TC##n,
  57. /**
  58. * \internal Get the interrupt vector for the given device instance
  59. *
  60. * \param[in] TC module instance number
  61. *
  62. * \return Interrupt vector for of the given TC module instance.
  63. */
  64. static enum system_interrupt_vector _tc_interrupt_get_interrupt_vector(
  65. uint32_t inst_num)
  66. {
  67. static uint8_t tc_interrupt_vectors[TC_INST_NUM] =
  68. {
  69. #if (SAML21E) || (SAML21G) || (SAMR30E) || (SAMR30G)
  70. SYSTEM_INTERRUPT_MODULE_TC0,
  71. SYSTEM_INTERRUPT_MODULE_TC1,
  72. SYSTEM_INTERRUPT_MODULE_TC4
  73. #else
  74. MRECURSION(TC_INST_NUM, _TC_INTERRUPT_VECT_NUM, TC_INST_MAX_ID)
  75. #endif
  76. };
  77. return (enum system_interrupt_vector)tc_interrupt_vectors[inst_num];
  78. }
  79. #endif /* !defined(__DOXYGEN__) */
  80. /**
  81. * \name Callback Management
  82. * {@
  83. */
  84. enum status_code tc_register_callback(
  85. struct tc_module *const module,
  86. tc_callback_t callback_func,
  87. const enum tc_callback callback_type);
  88. enum status_code tc_unregister_callback(
  89. struct tc_module *const module,
  90. const enum tc_callback callback_type);
  91. /**
  92. * \brief Enables callback.
  93. *
  94. * Enables the callback function registered by the \ref
  95. * tc_register_callback. The callback function will be called from the
  96. * interrupt handler when the conditions for the callback type are
  97. * met. This function will also enable the appropriate interrupts.
  98. *
  99. * \param[in] module Pointer to TC software instance struct
  100. * \param[in] callback_type Callback type given by an enum
  101. */
  102. static inline void tc_enable_callback(
  103. struct tc_module *const module,
  104. const enum tc_callback callback_type)
  105. {
  106. /* Sanity check arguments */
  107. Assert(module);
  108. /* Enable interrupts for this TC module */
  109. system_interrupt_enable(_tc_interrupt_get_interrupt_vector(_tc_get_inst_index(module->hw)));
  110. /* Enable callback */
  111. if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
  112. module->enable_callback_mask |= TC_INTFLAG_MC(1);
  113. module->hw->COUNT8.INTENSET.reg = TC_INTFLAG_MC(1);
  114. }
  115. else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
  116. module->enable_callback_mask |= TC_INTFLAG_MC(2);
  117. module->hw->COUNT8.INTENSET.reg = TC_INTFLAG_MC(2);
  118. }
  119. else {
  120. module->enable_callback_mask |= (1 << callback_type);
  121. module->hw->COUNT8.INTENSET.reg = (1 << callback_type);
  122. }
  123. }
  124. /**
  125. * \brief Disables callback.
  126. *
  127. * Disables the callback function registered by the \ref
  128. * tc_register_callback, and the callback will not be called from the
  129. * interrupt routine. The function will also disable the appropriate
  130. * interrupts.
  131. *
  132. * \param[in] module Pointer to TC software instance struct
  133. * \param[in] callback_type Callback type given by an enum
  134. */
  135. static inline void tc_disable_callback(
  136. struct tc_module *const module,
  137. const enum tc_callback callback_type){
  138. /* Sanity check arguments */
  139. Assert(module);
  140. /* Disable callback */
  141. if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
  142. module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(1);
  143. module->enable_callback_mask &= ~TC_INTFLAG_MC(1);
  144. }
  145. else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
  146. module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(2);
  147. module->enable_callback_mask &= ~TC_INTFLAG_MC(2);
  148. }
  149. else {
  150. module->hw->COUNT8.INTENCLR.reg = (1 << callback_type);
  151. module->enable_callback_mask &= ~(1 << callback_type);
  152. }
  153. }
  154. /**
  155. * @}
  156. */
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif /* TC_INTERRUPT_H_INCLUDED */