tcc_callback.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * \file
  3. *
  4. * \brief SAM TCC - Timer Counter for Control Applications 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 "tcc_callback.h"
  47. void *_tcc_instances[TCC_INST_NUM];
  48. void _tcc_interrupt_handler(uint8_t module_index);
  49. const uint32_t _tcc_intflag[TCC_CALLBACK_N] = {
  50. TCC_INTFLAG_OVF,
  51. TCC_INTFLAG_TRG,
  52. TCC_INTFLAG_CNT,
  53. TCC_INTFLAG_ERR,
  54. TCC_INTFLAG_FAULTA,
  55. TCC_INTFLAG_FAULTB,
  56. TCC_INTFLAG_FAULT0,
  57. TCC_INTFLAG_FAULT1,
  58. #define _TCC_INTFLAG_MC(n,dummy) TCC_INTFLAG_MC##n,
  59. /* TCC_INTFLAG_MC0 ~ ... */
  60. MREPEAT(TCC_NUM_CHANNELS, _TCC_INTFLAG_MC, 0)
  61. #undef _TCC_INTFLAG_MC
  62. };
  63. # define _TCC_INTERRUPT_VECT_NUM(n, unused) \
  64. SYSTEM_INTERRUPT_MODULE_TCC##n,
  65. /**
  66. * \internal Get the interrupt vector for the given device instance
  67. *
  68. * \param[in] The TCC module instance number
  69. *
  70. * \return Interrupt vector for of the given TCC module instance.
  71. */
  72. static enum system_interrupt_vector _tcc_interrupt_get_interrupt_vector(
  73. uint32_t inst_num)
  74. {
  75. static uint8_t tcc_interrupt_vectors[TCC_INST_NUM] = {
  76. MREPEAT(TCC_INST_NUM, _TCC_INTERRUPT_VECT_NUM, 0)
  77. };
  78. return (enum system_interrupt_vector)tcc_interrupt_vectors[inst_num];
  79. }
  80. /**
  81. * \brief Registers a callback
  82. *
  83. * Registers a callback function which is implemented by the user.
  84. *
  85. * \note The callback must be enabled by \ref tcc_enable_callback,
  86. * in order for the interrupt handler to call it when the conditions for the
  87. * callback type is met.
  88. *
  89. * \param[in] module Pointer to TCC software instance struct
  90. * \param[in] callback_func Pointer to callback function
  91. * \param[in] callback_type Callback type given by an enum
  92. */
  93. enum status_code tcc_register_callback(
  94. struct tcc_module *const module,
  95. tcc_callback_t callback_func,
  96. const enum tcc_callback callback_type)
  97. {
  98. /* Sanity check arguments */
  99. Assert(module);
  100. Assert(callback_func);
  101. /* Register callback function */
  102. module->callback[callback_type] = callback_func;
  103. /* Set the bit corresponding to the callback_type */
  104. module->register_callback_mask |= _tcc_intflag[callback_type];
  105. return STATUS_OK;
  106. }
  107. /**
  108. * \brief Unregisters a callback
  109. *
  110. * Unregisters a callback function implemented by the user. The callback should
  111. * be disabled before it is unregistered.
  112. *
  113. * \param[in] module Pointer to TCC software instance struct
  114. * \param[in] callback_type Callback type given by an enum
  115. */
  116. enum status_code tcc_unregister_callback(
  117. struct tcc_module *const module,
  118. const enum tcc_callback callback_type)
  119. {
  120. /* Sanity check arguments */
  121. Assert(module);
  122. /* Unregister callback function */
  123. module->callback[callback_type] = NULL;
  124. /* Clear the bit corresponding to the callback_type */
  125. module->register_callback_mask &= ~_tcc_intflag[callback_type];
  126. return STATUS_OK;
  127. }
  128. /**
  129. * \brief Enables callback
  130. *
  131. * Enables the callback function registered by the \ref
  132. * tcc_register_callback. The callback function will be called from the
  133. * interrupt handler when the conditions for the callback type are
  134. * met. This function will also enable the appropriate interrupts.
  135. *
  136. * \param[in] module Pointer to TCC software instance struct
  137. * \param[in] callback_type Callback type given by an enum
  138. */
  139. void tcc_enable_callback(
  140. struct tcc_module *const module,
  141. const enum tcc_callback callback_type)
  142. {
  143. /* Sanity check arguments */
  144. Assert(module);
  145. Assert(module->hw);
  146. /* Enable interrupts for this TCC module */
  147. system_interrupt_enable(_tcc_interrupt_get_interrupt_vector(
  148. _tcc_get_inst_index(module->hw)));
  149. /* Enable channel or other callbacks */
  150. module->enable_callback_mask |= _tcc_intflag[callback_type];
  151. module->hw->INTENSET.reg = _tcc_intflag[callback_type];
  152. }
  153. /**
  154. * \brief Disables callback
  155. *
  156. * Disables the callback function registered by the \ref
  157. * tcc_register_callback, and the callback will not be called from the
  158. * interrupt routine. The function will also disable the appropriate
  159. * interrupts.
  160. *
  161. * \param[in] module Pointer to TCC software instance struct
  162. * \param[in] callback_type Callback type given by an enum
  163. */
  164. void tcc_disable_callback(
  165. struct tcc_module *const module,
  166. const enum tcc_callback callback_type)
  167. {
  168. /* Sanity check arguments */
  169. Assert(module);
  170. Assert(module->hw);
  171. /* Disable interrupts for this TCC module */
  172. system_interrupt_disable(_tcc_interrupt_get_interrupt_vector(
  173. _tcc_get_inst_index(module->hw)));
  174. /* Disable channel or other callbacks */
  175. module->enable_callback_mask &= ~_tcc_intflag[callback_type];
  176. module->hw->INTENCLR.reg = _tcc_intflag[callback_type];
  177. }
  178. /**
  179. * \internal ISR handler for TCC
  180. *
  181. * Auto-generate a set of interrupt handlers for each TCC in the device.
  182. */
  183. #define _TCC_INTERRUPT_HANDLER(n, m) \
  184. void TCC##n##_Handler(void) \
  185. { \
  186. _tcc_interrupt_handler(n); \
  187. }
  188. MREPEAT(TCC_INST_NUM, _TCC_INTERRUPT_HANDLER, 0)
  189. /**
  190. * \internal Interrupt handler for the TCC module
  191. *
  192. * Handles interrupts as they occur, it will run the callback functions
  193. * that are registered and enabled.
  194. *
  195. * \param[in] module_index ID of the TCC instance calling the interrupt
  196. * handler
  197. */
  198. void _tcc_interrupt_handler(
  199. uint8_t module_index)
  200. {
  201. int i;
  202. uint32_t interrupt_and_callback_status_mask;
  203. struct tcc_module *module =
  204. (struct tcc_module *)_tcc_instances[module_index];
  205. interrupt_and_callback_status_mask = (module->hw->INTFLAG.reg &
  206. module->register_callback_mask &
  207. module->enable_callback_mask);
  208. /* Check if callback interrupt has occured */
  209. for (i = 0; i < TCC_CALLBACK_N; i ++) {
  210. if (interrupt_and_callback_status_mask & _tcc_intflag[i]) {
  211. /* Invoke the registered and enabled callback function */
  212. (module->callback[i])(module);
  213. /* Clear interrupt flag */
  214. module->hw->INTFLAG.reg = _tcc_intflag[i];
  215. }
  216. }
  217. }