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