1 /**
2 * \file
3 *
4 * \brief SAM External Interrupt Driver
5 *
6 * Copyright (C) 2012-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 "extint.h"
47 #include "extint_callback.h"
48
49 /**
50 * \internal
51 * Internal driver device instance struct, declared in the main module driver.
52 */
53 extern struct _extint_module _extint_dev;
54
55 /**
56 * \internal
57 * This is the number of the channel whose callback is currently running.
58 */
59 uint8_t _current_channel;
60
61 /**
62 * \brief Registers an asynchronous callback function with the driver.
63 *
64 * Registers an asynchronous callback with the EXTINT driver, fired when a
65 * channel detects the configured channel detection criteria
66 * (e.g. edge or level). Callbacks are fired once for each detected channel.
67 *
68 * \note NMI channel callbacks cannot be registered via this function; the
69 * device's NMI interrupt should be hooked directly in the user
70 * application and the NMI flags manually cleared via
71 * \ref extint_nmi_clear_detected().
72 *
73 * \param[in] callback Pointer to the callback function to register
74 * \param[in] channel Logical channel to register callback for
75 * \param[in] type Type of callback function to register
76 *
77 * \return Status of the registration operation.
78 * \retval STATUS_OK The callback was registered successfully
79 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
80 * \retval STATUS_ERR_ALREADY_INITIALIZED Callback function has been
81 * registered, need unregister first
82 */
extint_register_callback(const extint_callback_t callback,const uint8_t channel,const enum extint_callback_type type)83 enum status_code extint_register_callback(
84 const extint_callback_t callback,
85 const uint8_t channel,
86 const enum extint_callback_type type)
87 {
88 /* Sanity check arguments */
89 Assert(callback);
90
91 if (type != EXTINT_CALLBACK_TYPE_DETECT) {
92 Assert(false);
93 return STATUS_ERR_INVALID_ARG;
94 }
95
96 if (_extint_dev.callbacks[channel] == NULL) {
97 _extint_dev.callbacks[channel] = callback;
98 return STATUS_OK;
99 } else if (_extint_dev.callbacks[channel] == callback) {
100 return STATUS_OK;
101 }
102
103 return STATUS_ERR_ALREADY_INITIALIZED;
104 }
105
106 /**
107 * \brief Unregisters an asynchronous callback function with the driver.
108 *
109 * Unregisters an asynchronous callback with the EXTINT driver, removing it
110 * from the internal callback registration table.
111 *
112 * \param[in] callback Pointer to the callback function to unregister
113 * \param[in] channel Logical channel to unregister callback for
114 * \param[in] type Type of callback function to unregister
115 *
116 * \return Status of the de-registration operation.
117 * \retval STATUS_OK The callback was unregistered successfully
118 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
119 * \retval STATUS_ERR_BAD_ADDRESS No matching entry was found in the
120 * registration table
121 */
extint_unregister_callback(const extint_callback_t callback,const uint8_t channel,const enum extint_callback_type type)122 enum status_code extint_unregister_callback(
123 const extint_callback_t callback,
124 const uint8_t channel,
125 const enum extint_callback_type type)
126 {
127 /* Sanity check arguments */
128 Assert(callback);
129
130 if (type != EXTINT_CALLBACK_TYPE_DETECT) {
131 Assert(false);
132 return STATUS_ERR_INVALID_ARG;
133 }
134
135 if (_extint_dev.callbacks[channel] == callback) {
136 _extint_dev.callbacks[channel] = NULL;
137 return STATUS_OK;
138 }
139
140 return STATUS_ERR_BAD_ADDRESS;
141 }
142
143 /**
144 * \brief Enables asynchronous callback generation for a given channel and type.
145 *
146 * Enables asynchronous callbacks for a given logical external interrupt channel
147 * and type. This must be called before an external interrupt channel will
148 * generate callback events.
149 *
150 * \param[in] channel Logical channel to enable callback generation for
151 * \param[in] type Type of callback function callbacks to enable
152 *
153 * \return Status of the callback enable operation.
154 * \retval STATUS_OK The callback was enabled successfully
155 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
156 */
extint_chan_enable_callback(const uint8_t channel,const enum extint_callback_type type)157 enum status_code extint_chan_enable_callback(
158 const uint8_t channel,
159 const enum extint_callback_type type)
160 {
161 if (type == EXTINT_CALLBACK_TYPE_DETECT) {
162 Eic *const eic = _extint_get_eic_from_channel(channel);
163
164 eic->INTENSET.reg = (1UL << channel);
165 }
166 else {
167 Assert(false);
168 return STATUS_ERR_INVALID_ARG;
169 }
170
171 return STATUS_OK;
172 }
173
174 /**
175 * \brief Disables asynchronous callback generation for a given channel and type.
176 *
177 * Disables asynchronous callbacks for a given logical external interrupt
178 * channel and type.
179 *
180 * \param[in] channel Logical channel to disable callback generation for
181 * \param[in] type Type of callback function callbacks to disable
182 *
183 * \return Status of the callback disable operation.
184 * \retval STATUS_OK The callback was disabled successfully
185 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
186 */
extint_chan_disable_callback(const uint8_t channel,const enum extint_callback_type type)187 enum status_code extint_chan_disable_callback(
188 const uint8_t channel,
189 const enum extint_callback_type type)
190 {
191 if (type == EXTINT_CALLBACK_TYPE_DETECT) {
192 Eic *const eic = _extint_get_eic_from_channel(channel);
193
194 eic->INTENCLR.reg = (1UL << channel);
195 }
196 else {
197 Assert(false);
198 return STATUS_ERR_INVALID_ARG;
199 }
200
201 return STATUS_OK;
202 }
203
204 /**
205 * \brief Find what channel caused the callback.
206 *
207 * Can be used in an EXTINT callback function to find what channel caused
208 * the callback in case the same callback is used by multiple channels.
209 *
210 * \return Channel number.
211 */
extint_get_current_channel(void)212 uint8_t extint_get_current_channel(void)
213 {
214 return _current_channel;
215 }
216
217 /** Handler for the EXTINT hardware module interrupt. */
EIC_Handler(void)218 void EIC_Handler(void)
219 {
220 /* Find any triggered channels, run associated callback handlers */
221 for (_current_channel = 0; _current_channel < EIC_NUMBER_OF_INTERRUPTS ; _current_channel++) {
222 if (extint_chan_is_detected(_current_channel)) {
223 /* Clear flag */
224 extint_chan_clear_detected(_current_channel);
225 /* Find any associated callback entries in the callback table */
226 if (_extint_dev.callbacks[_current_channel] != NULL) {
227 /* Run the registered callback */
228 _extint_dev.callbacks[_current_channel]();
229 }
230 }
231 }
232 }
233