1 /**
2 * \file
3 *
4 * \brief External interrupt functionality imkplementation.
5 *
6 * Copyright (C) 2015-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 #include "hal_ext_irq.h"
45
46 #define EXT_IRQ_AMOUNT 4
47
48 /**
49 * \brief Driver version
50 */
51 #define DRIVER_VERSION 0x00000001u
52
53 /**
54 * \brief External IRQ struct
55 */
56 struct ext_irq {
57 ext_irq_cb_t cb;
58 uint32_t pin;
59 };
60
61 /* Remove KEIL compiling error in case no IRQ line selected */
62 #if EXT_IRQ_AMOUNT == 0
63 #undef EXT_IRQ_AMOUNT
64 #define EXT_IRQ_AMOUNT 1
65 #endif
66
67 /**
68 * \brief Array of external IRQs callbacks
69 */
70 static struct ext_irq ext_irqs[EXT_IRQ_AMOUNT];
71
72 static void process_ext_irq(const uint32_t pin);
73
74 /**
75 * \brief Initialize external irq component if any
76 */
ext_irq_init(void)77 int32_t ext_irq_init(void)
78 {
79 uint16_t i;
80
81 for (i = 0; i < EXT_IRQ_AMOUNT; i++) {
82 ext_irqs[i].pin = 0xFFFFFFFF;
83 ext_irqs[i].cb = NULL;
84 }
85
86 return _ext_irq_init(process_ext_irq);
87 }
88
89 /**
90 * \brief Deinitialize external irq if any
91 */
ext_irq_deinit(void)92 int32_t ext_irq_deinit(void)
93 {
94 return _ext_irq_deinit();
95 }
96
97 /**
98 * \brief Register callback for the given external interrupt
99 */
ext_irq_register(const uint32_t pin,ext_irq_cb_t cb)100 int32_t ext_irq_register(const uint32_t pin, ext_irq_cb_t cb)
101 {
102 uint8_t i = 0, j = 0;
103 bool found = false;
104
105 for (; i < EXT_IRQ_AMOUNT; i++) {
106 if (ext_irqs[i].pin == pin) {
107 ext_irqs[i].cb = cb;
108 found = true;
109 break;
110 }
111 }
112
113 if (NULL == cb) {
114 if (!found) {
115 return ERR_INVALID_ARG;
116 }
117 return _ext_irq_enable(pin, false);
118 }
119
120 if (!found) {
121 for (i = 0; i < EXT_IRQ_AMOUNT; i++) {
122 if (NULL == ext_irqs[i].cb) {
123 ext_irqs[i].cb = cb;
124 ext_irqs[i].pin = pin;
125 found = true;
126 break;
127 }
128 }
129 for (; (j < EXT_IRQ_AMOUNT) && (i < EXT_IRQ_AMOUNT); j++) {
130 if ((ext_irqs[i].pin < ext_irqs[j].pin) && (ext_irqs[j].pin != 0xFFFFFFFF)) {
131 struct ext_irq tmp = ext_irqs[j];
132
133 ext_irqs[j] = ext_irqs[i];
134 ext_irqs[i] = tmp;
135 }
136 }
137 }
138
139 if (!found) {
140 return ERR_INVALID_ARG;
141 }
142
143 return _ext_irq_enable(pin, true);
144 }
145
146 /**
147 * \brief Enable external irq
148 */
ext_irq_enable(const uint32_t pin)149 int32_t ext_irq_enable(const uint32_t pin)
150 {
151 return _ext_irq_enable(pin, true);
152 }
153
154 /**
155 * \brief Disable external irq
156 */
ext_irq_disable(const uint32_t pin)157 int32_t ext_irq_disable(const uint32_t pin)
158 {
159 return _ext_irq_enable(pin, false);
160 }
161
162 /**
163 * \brief Retrieve the current driver version
164 */
ext_irq_get_version(void)165 uint32_t ext_irq_get_version(void)
166 {
167 return DRIVER_VERSION;
168 }
169
170 /**
171 * \brief Interrupt processing routine
172 *
173 * \param[in] pin The pin which triggered the interrupt
174 */
process_ext_irq(const uint32_t pin)175 static void process_ext_irq(const uint32_t pin)
176 {
177 uint8_t lower = 0, middle, upper = EXT_IRQ_AMOUNT;
178
179 while (upper >= lower) {
180 middle = (upper + lower) >> 1;
181
182 if (ext_irqs[middle].pin == pin) {
183 if (ext_irqs[middle].cb) {
184 ext_irqs[middle].cb();
185 return;
186 }
187 }
188
189 if (ext_irqs[middle].pin < pin) {
190 lower = middle + 1;
191 } else {
192 upper = middle - 1;
193 }
194 }
195 }
196