ac_callback.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * \file
  3. *
  4. * \brief SAM AC - Analog Comparator Callback Driver
  5. *
  6. * Copyright (C) 2013-2015 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 "ac_callback.h"
  47. struct ac_module *_ac_instance[AC_INST_NUM];
  48. void _ac_interrupt_handler(const uint32_t instance_index);
  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 ac_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 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. * \retval STATUS_OK The function exited successfully
  63. */
  64. enum status_code ac_register_callback(
  65. struct ac_module *const module,
  66. ac_callback_t callback_func,
  67. const enum ac_callback callback_type)
  68. {
  69. /* Sanity check arguments */
  70. Assert(module);
  71. Assert(callback_func);
  72. /* Register callback function */
  73. module->callback[callback_type] = callback_func;
  74. /* Set software flag for callback */
  75. module->register_callback_mask |= (1 << callback_type);
  76. return STATUS_OK;
  77. }
  78. /**
  79. * \brief Unregisters a callback.
  80. *
  81. * Unregisters a callback function implemented by the user.
  82. *
  83. * \param[in] module Pointer to AC software instance struct
  84. * \param[in] callback_type Callback type given by an enum
  85. *
  86. * \retval STATUS_OK The function exited successfully
  87. */
  88. enum status_code ac_unregister_callback(
  89. struct ac_module *const module,
  90. const enum ac_callback callback_type)
  91. {
  92. /* Sanity check arguments */
  93. Assert(module);
  94. /* Unregister callback function */
  95. module->callback[callback_type] = NULL;
  96. /* Clear software flag for callback */
  97. module->register_callback_mask &= ~(1 << callback_type);
  98. return STATUS_OK;
  99. }
  100. /**
  101. * \internal ISR handler for AC
  102. */
  103. #if (AC_INST_NUM == 1)
  104. void AC_Handler(void)
  105. {
  106. _ac_interrupt_handler(0);
  107. }
  108. #elif (AC_INST_NUM == 2)
  109. void AC_Handler(void)
  110. {
  111. _ac_interrupt_handler(0);
  112. }
  113. void AC1_Handler(void)
  114. {
  115. _ac_interrupt_handler(1);
  116. }
  117. #else
  118. # error This driver is not support more than three AC instances.
  119. #endif
  120. /**
  121. * \brief Interrupt Handler for AC module.
  122. *
  123. * Handles interrupts as they occur, it will run the callback functions
  124. * that are registered and enabled.
  125. *
  126. * \param [in] instance_index Default value 0
  127. */
  128. void _ac_interrupt_handler(const uint32_t instance_index)
  129. {
  130. /* Temporary variable */
  131. uint8_t interrupt_and_callback_status_mask;
  132. /* Get device instance from the look-up table */
  133. struct ac_module *module = _ac_instance[instance_index];
  134. /* Read and mask interrupt flag register */
  135. interrupt_and_callback_status_mask =
  136. _ac_instance[instance_index]->hw->INTFLAG.reg &
  137. (module->register_callback_mask & module->enable_callback_mask);
  138. /* Check if comparator channel 0 needs to be serviced */
  139. if (interrupt_and_callback_status_mask & AC_INTFLAG_COMP0) {
  140. /* Invoke registered and enabled callback function */
  141. (module->callback[AC_CALLBACK_COMPARATOR_0])(module);
  142. /* Clear interrupt flag */
  143. module->hw->INTFLAG.reg = AC_INTFLAG_COMP0;
  144. }
  145. /* Check if comparator channel 1 needs to be serviced */
  146. if (interrupt_and_callback_status_mask & AC_INTFLAG_COMP1) {
  147. /* Invoke registered and enabled callback function */
  148. (module->callback[AC_CALLBACK_COMPARATOR_1])(module);
  149. /* Clear interrupt flag */
  150. module->hw->INTFLAG.reg = AC_INTFLAG_COMP1;
  151. }
  152. /* Check if window 0 needs to be serviced */
  153. if (interrupt_and_callback_status_mask & AC_INTFLAG_WIN0) {
  154. /* Invoke registered and enabled callback function */
  155. (module->callback[AC_CALLBACK_WINDOW_0])(module);
  156. /* Clear interrupt flag */
  157. module->hw->INTFLAG.reg = AC_INTFLAG_WIN0;
  158. }
  159. #if (AC_NUM_CMP > 2)
  160. /* Check if comparator channel 2 needs to be serviced */
  161. if (interrupt_and_callback_status_mask & AC_INTFLAG_COMP2) {
  162. /* Invoke registered and enabled callback function */
  163. (module->callback[AC_CALLBACK_COMPARATOR_2])(module);
  164. /* Clear interrupt flag */
  165. module->hw->INTFLAG.reg = AC_INTFLAG_COMP2;
  166. }
  167. /* Check if comparator channel 3 needs to be serviced */
  168. if (interrupt_and_callback_status_mask & AC_INTFLAG_COMP3) {
  169. /* Invoke registered and enabled callback function */
  170. (module->callback[AC_CALLBACK_COMPARATOR_3])(module);
  171. /* Clear interrupt flag */
  172. module->hw->INTFLAG.reg = AC_INTFLAG_COMP3;
  173. }
  174. # if !(SAMC20)
  175. /* Check if window 1 needs to be serviced */
  176. if (interrupt_and_callback_status_mask & AC_INTFLAG_WIN1) {
  177. /* Invoke registered and enabled callback function */
  178. (module->callback[AC_CALLBACK_WINDOW_1])(module);
  179. /* Clear interrupt flag */
  180. module->hw->INTFLAG.reg = AC_INTFLAG_WIN1;
  181. }
  182. # endif
  183. #endif /* (AC_NUM_CMP > 2) */
  184. }