ac_callback.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifndef AC_CALLBACK_H_INCLUDED
  47. #define AC_CALLBACK_H_INCLUDED
  48. #include <compiler.h>
  49. #include <system_interrupt.h>
  50. #include "ac.h"
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. #if (AC_INST_NUM > 1) && !defined(__DOXYGEN__)
  55. /**
  56. * \internal Get the interrupt vector for the given device instance
  57. *
  58. * \param[in] TC module instance number
  59. *
  60. * \return Interrupt vector for of the given TC module instance.
  61. */
  62. static enum system_interrupt_vector _ac_interrupt_get_interrupt_vector(
  63. uint32_t inst_num)
  64. {
  65. static uint8_t ac_interrupt_vectors[AC_INST_NUM] =
  66. {
  67. SYSTEM_INTERRUPT_MODULE_AC,
  68. #if (AC_INST_NUM == 2)
  69. SYSTEM_INTERRUPT_MODULE_AC1,
  70. #endif
  71. #if (AC_INST_NUM >= 3)
  72. # error This driver is not support more than three AC instances.
  73. #endif
  74. };
  75. return ac_interrupt_vectors[inst_num];
  76. }
  77. #endif /* (AC_INST_NUM > 1) !defined(__DOXYGEN__)*/
  78. /**
  79. * \name Callback Management
  80. * {@
  81. */
  82. enum status_code ac_register_callback(
  83. struct ac_module *const module,
  84. ac_callback_t callback_func,
  85. const enum ac_callback callback_type);
  86. enum status_code ac_unregister_callback(
  87. struct ac_module *const module,
  88. const enum ac_callback callback_type);
  89. /**
  90. * \brief Enables callback.
  91. *
  92. * Enables the callback function registered by the \ref
  93. * ac_register_callback. The callback function will be called from the
  94. * interrupt handler when the conditions for the callback type are
  95. * met. This function will also enable the appropriate interrupts.
  96. *
  97. * \param[in] module Pointer to AC software instance struct
  98. * \param[in] callback_type Callback type given by an enum
  99. */
  100. static inline void ac_enable_callback(
  101. struct ac_module *const module,
  102. const enum ac_callback callback_type)
  103. {
  104. /* Sanity check arguments */
  105. Assert(module);
  106. /* Set software flag for the callback */
  107. module->enable_callback_mask |= (1 << callback_type);
  108. uint32_t inenset_temp = 0;
  109. switch (callback_type)
  110. {
  111. case AC_CALLBACK_COMPARATOR_0:
  112. inenset_temp |= AC_INTFLAG_COMP0;
  113. break;
  114. case AC_CALLBACK_COMPARATOR_1:
  115. inenset_temp |= AC_INTFLAG_COMP1;
  116. break;
  117. case AC_CALLBACK_WINDOW_0:
  118. inenset_temp |= AC_INTFLAG_WIN0;
  119. break;
  120. #if (AC_NUM_CMP > 2)
  121. case AC_CALLBACK_COMPARATOR_2:
  122. inenset_temp |= AC_INTFLAG_COMP2;
  123. break;
  124. case AC_CALLBACK_COMPARATOR_3:
  125. inenset_temp |= AC_INTFLAG_COMP3;
  126. break;
  127. # if !(SAMC20)
  128. case AC_CALLBACK_WINDOW_1:
  129. inenset_temp |= AC_INTFLAG_WIN1;
  130. break;
  131. # endif
  132. #endif
  133. default:
  134. break;
  135. }
  136. /* Enable the interrupt for the callback */
  137. module->hw->INTENSET.reg = inenset_temp;
  138. #if (AC_INST_NUM == 1)
  139. /* Enable interrupts for AC module */
  140. system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_AC);
  141. #elif (AC_INST_NUM > 1)
  142. system_interrupt_enable(_ac_interrupt_get_interrupt_vector(_ac_get_inst_index(module->hw)));
  143. #endif /* (AC_INST_NUM > 1) */
  144. }
  145. /**
  146. * \brief Disables callback.
  147. *
  148. * Disables the callback function registered by the \ref
  149. * ac_register_callback, and the callback will not be called from the
  150. * interrupt routine. The function will also disable the appropriate
  151. * interrupts.
  152. *
  153. * \param[in] module Pointer to AC software instance struct
  154. * \param[in] callback_type Callback type given by an enum
  155. */
  156. static inline void ac_disable_callback(
  157. struct ac_module *const module,
  158. const enum ac_callback callback_type)
  159. {
  160. /* Sanity check arguments */
  161. Assert(module);
  162. /* Clear software flag for the callback */
  163. module->enable_callback_mask &= ~(1 << callback_type);
  164. uint32_t inenclr_temp = 0;
  165. switch (callback_type)
  166. {
  167. case AC_CALLBACK_COMPARATOR_0:
  168. inenclr_temp |= AC_INTFLAG_COMP0;
  169. break;
  170. case AC_CALLBACK_COMPARATOR_1:
  171. inenclr_temp |= AC_INTFLAG_COMP1;
  172. break;
  173. case AC_CALLBACK_WINDOW_0:
  174. inenclr_temp |= AC_INTFLAG_WIN0;
  175. break;
  176. #if (AC_NUM_CMP > 2)
  177. case AC_CALLBACK_COMPARATOR_2:
  178. inenclr_temp |= AC_INTFLAG_COMP2;
  179. break;
  180. case AC_CALLBACK_COMPARATOR_3:
  181. inenclr_temp |= AC_INTFLAG_COMP3;
  182. break;
  183. # if !(SAMC20)
  184. case AC_CALLBACK_WINDOW_1:
  185. inenclr_temp |= AC_INTFLAG_WIN1;
  186. break;
  187. # endif
  188. #endif
  189. default:
  190. break;
  191. }
  192. /* Disable the interrupt for the callback */
  193. module->hw->INTENCLR.reg = inenclr_temp;
  194. }
  195. /**
  196. * @}
  197. */
  198. #ifdef __cplusplus
  199. }
  200. #endif
  201. #endif /* AC_CALLBACK_H_INCLUDED */