update credits
[librepilot.git] / flight / pios / stm32f10x / pios_exti.c
blobf1a3693d85902120614eb4cfda57e2a9b9abf2c7
1 /**
2 ******************************************************************************
4 * @addtogroup PIOS PIOS Core hardware abstraction layer
5 * @{
6 * @addtogroup PIOS_EXTI External Interrupt Handlers
7 * @brief External interrupt handler functions
8 * @{
10 * @file pios_exti.c
11 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
12 * @brief External Interrupt Handlers
13 * @see The GNU Public License (GPL) Version 3
15 *****************************************************************************/
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "pios.h"
34 #ifdef PIOS_INCLUDE_EXTI
36 /* Map EXTI line to full config */
37 #define EXTI_MAX_LINES 16
39 static pios_exti_vector_t pios_exti_vector[EXTI_MAX_LINES];
41 static uint8_t PIOS_EXTI_line_to_index(uint32_t line)
43 switch (line) {
44 case EXTI_Line0: return 0;
46 case EXTI_Line1: return 1;
48 case EXTI_Line2: return 2;
50 case EXTI_Line3: return 3;
52 case EXTI_Line4: return 4;
54 case EXTI_Line5: return 5;
56 case EXTI_Line6: return 6;
58 case EXTI_Line7: return 7;
60 case EXTI_Line8: return 8;
62 case EXTI_Line9: return 9;
64 case EXTI_Line10: return 10;
66 case EXTI_Line11: return 11;
68 case EXTI_Line12: return 12;
70 case EXTI_Line13: return 13;
72 case EXTI_Line14: return 14;
74 case EXTI_Line15: return 15;
77 PIOS_Assert(0);
78 return 0xFF;
81 uint8_t PIOS_EXTI_gpio_port_to_exti_source_port(GPIO_TypeDef *gpio_port)
83 switch ((uint32_t)gpio_port) {
84 case (uint32_t)GPIOA: return GPIO_PortSourceGPIOA;
86 case (uint32_t)GPIOB: return GPIO_PortSourceGPIOB;
88 case (uint32_t)GPIOC: return GPIO_PortSourceGPIOC;
90 case (uint32_t)GPIOD: return GPIO_PortSourceGPIOD;
92 case (uint32_t)GPIOE: return GPIO_PortSourceGPIOE;
94 case (uint32_t)GPIOF: return GPIO_PortSourceGPIOF;
96 case (uint32_t)GPIOG: return GPIO_PortSourceGPIOG;
99 PIOS_Assert(0);
100 return 0xFF;
103 uint8_t PIOS_EXTI_gpio_pin_to_exti_source_pin(uint32_t gpio_pin)
105 switch ((uint32_t)gpio_pin) {
106 case GPIO_Pin_0: return GPIO_PinSource0;
108 case GPIO_Pin_1: return GPIO_PinSource1;
110 case GPIO_Pin_2: return GPIO_PinSource2;
112 case GPIO_Pin_3: return GPIO_PinSource3;
114 case GPIO_Pin_4: return GPIO_PinSource4;
116 case GPIO_Pin_5: return GPIO_PinSource5;
118 case GPIO_Pin_6: return GPIO_PinSource6;
120 case GPIO_Pin_7: return GPIO_PinSource7;
122 case GPIO_Pin_8: return GPIO_PinSource8;
124 case GPIO_Pin_9: return GPIO_PinSource9;
126 case GPIO_Pin_10: return GPIO_PinSource10;
128 case GPIO_Pin_11: return GPIO_PinSource11;
130 case GPIO_Pin_12: return GPIO_PinSource12;
132 case GPIO_Pin_13: return GPIO_PinSource13;
134 case GPIO_Pin_14: return GPIO_PinSource14;
136 case GPIO_Pin_15: return GPIO_PinSource15;
139 PIOS_Assert(0);
140 return 0xFF;
143 int32_t PIOS_EXTI_Init(const struct pios_exti_cfg *cfg)
145 PIOS_Assert(cfg);
147 /* Connect this config to the requested vector */
148 uint8_t line_index = PIOS_EXTI_line_to_index(cfg->line);
150 if (pios_exti_vector[line_index]) {
151 /* Someone else already has this mapped */
152 return -1;
155 /* Bind the vector to the exti line */
156 pios_exti_vector[line_index] = cfg->vector;
158 /* Initialize the GPIO pin */
159 GPIO_Init(cfg->pin.gpio, &cfg->pin.init);
161 /* Set up the EXTI interrupt source */
162 uint8_t exti_source_port = PIOS_EXTI_gpio_port_to_exti_source_port(cfg->pin.gpio);
163 uint8_t exti_source_pin = PIOS_EXTI_gpio_pin_to_exti_source_pin(cfg->pin.init.GPIO_Pin);
164 GPIO_EXTILineConfig(exti_source_port, exti_source_pin);
165 EXTI_Init(&cfg->exti.init);
167 /* Enable the interrupt channel */
168 NVIC_Init(&cfg->irq.init);
170 return 0;
173 int32_t PIOS_EXTI_DeInit(const struct pios_exti_cfg *cfg)
175 uint8_t line_index = PIOS_EXTI_line_to_index(cfg->line);
177 if (pios_exti_vector[line_index] == cfg->vector) {
178 EXTI_InitTypeDef disable = cfg->exti.init;
179 disable.EXTI_LineCmd = DISABLE;
181 EXTI_Init(&disable);
182 pios_exti_vector[line_index] = 0;
184 return 0;
187 return -1;
190 static bool PIOS_EXTI_generic_irq_handler(uint8_t line_index)
192 if (pios_exti_vector[line_index]) {
193 return pios_exti_vector[line_index]();
196 /* Unconfigured interrupt just fired! */
197 return false;
200 #ifdef PIOS_INCLUDE_FREERTOS
201 #define PIOS_EXTI_HANDLE_LINE(line, woken) \
202 if (EXTI_GetITStatus(EXTI_Line##line) != RESET) { \
203 EXTI_ClearITPendingBit(EXTI_Line##line); \
204 woken = PIOS_EXTI_generic_irq_handler(line) ? pdTRUE : woken; \
206 #else
207 #define PIOS_EXTI_HANDLE_LINE(line, woken) \
208 if (EXTI_GetITStatus(EXTI_Line##line) != RESET) { \
209 EXTI_ClearITPendingBit(EXTI_Line##line); \
210 PIOS_EXTI_generic_irq_handler(line); \
212 #endif
214 /* Bind Interrupt Handlers */
216 static void PIOS_EXTI_0_irq_handler(void)
218 #ifdef PIOS_INCLUDE_FREERTOS
219 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
220 #else
221 bool xHigherPriorityTaskWoken; // dummy variable
222 #endif
223 PIOS_EXTI_HANDLE_LINE(0, xHigherPriorityTaskWoken);
224 #ifdef PIOS_INCLUDE_FREERTOS
225 portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
226 #endif
228 void EXTI0_IRQHandler(void) __attribute__((alias("PIOS_EXTI_0_irq_handler")));
230 static void PIOS_EXTI_1_irq_handler(void)
232 #ifdef PIOS_INCLUDE_FREERTOS
233 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
234 #else
235 bool xHigherPriorityTaskWoken; // dummy variable
236 #endif
237 PIOS_EXTI_HANDLE_LINE(1, xHigherPriorityTaskWoken);
238 #ifdef PIOS_INCLUDE_FREERTOS
239 portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
240 #endif
242 void EXTI1_IRQHandler(void) __attribute__((alias("PIOS_EXTI_1_irq_handler")));
244 static void PIOS_EXTI_2_irq_handler(void)
246 #ifdef PIOS_INCLUDE_FREERTOS
247 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
248 #else
249 bool xHigherPriorityTaskWoken; // dummy variable
250 #endif
251 PIOS_EXTI_HANDLE_LINE(2, xHigherPriorityTaskWoken);
252 #ifdef PIOS_INCLUDE_FREERTOS
253 portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
254 #endif
256 void EXTI2_IRQHandler(void) __attribute__((alias("PIOS_EXTI_2_irq_handler")));
258 static void PIOS_EXTI_3_irq_handler(void)
260 #ifdef PIOS_INCLUDE_FREERTOS
261 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
262 #else
263 bool xHigherPriorityTaskWoken; // dummy variable
264 #endif
265 PIOS_EXTI_HANDLE_LINE(3, xHigherPriorityTaskWoken);
266 #ifdef PIOS_INCLUDE_FREERTOS
267 portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
268 #endif
270 void EXTI3_IRQHandler(void) __attribute__((alias("PIOS_EXTI_3_irq_handler")));
272 static void PIOS_EXTI_4_irq_handler(void)
274 #ifdef PIOS_INCLUDE_FREERTOS
275 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
276 #else
277 bool xHigherPriorityTaskWoken; // dummy variable
278 #endif
279 PIOS_EXTI_HANDLE_LINE(4, xHigherPriorityTaskWoken);
280 #ifdef PIOS_INCLUDE_FREERTOS
281 portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
282 #endif
284 void EXTI4_IRQHandler(void) __attribute__((alias("PIOS_EXTI_4_irq_handler")));
286 static void PIOS_EXTI_9_5_irq_handler(void)
288 #ifdef PIOS_INCLUDE_FREERTOS
289 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
290 #else
291 bool xHigherPriorityTaskWoken; // dummy variable
292 #endif
293 PIOS_EXTI_HANDLE_LINE(5, xHigherPriorityTaskWoken);
294 PIOS_EXTI_HANDLE_LINE(6, xHigherPriorityTaskWoken);
295 PIOS_EXTI_HANDLE_LINE(7, xHigherPriorityTaskWoken);
296 PIOS_EXTI_HANDLE_LINE(8, xHigherPriorityTaskWoken);
297 PIOS_EXTI_HANDLE_LINE(9, xHigherPriorityTaskWoken);
298 #ifdef PIOS_INCLUDE_FREERTOS
299 portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
300 #endif
302 void EXTI9_5_IRQHandler(void) __attribute__((alias("PIOS_EXTI_9_5_irq_handler")));
304 static void PIOS_EXTI_15_10_irq_handler(void)
306 #ifdef PIOS_INCLUDE_FREERTOS
307 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
308 #else
309 bool xHigherPriorityTaskWoken; // dummy variable
310 #endif
311 PIOS_EXTI_HANDLE_LINE(10, xHigherPriorityTaskWoken);
312 PIOS_EXTI_HANDLE_LINE(11, xHigherPriorityTaskWoken);
313 PIOS_EXTI_HANDLE_LINE(12, xHigherPriorityTaskWoken);
314 PIOS_EXTI_HANDLE_LINE(13, xHigherPriorityTaskWoken);
315 PIOS_EXTI_HANDLE_LINE(14, xHigherPriorityTaskWoken);
316 PIOS_EXTI_HANDLE_LINE(15, xHigherPriorityTaskWoken);
317 #ifdef PIOS_INCLUDE_FREERTOS
318 portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
319 #endif
321 void EXTI15_10_IRQHandler(void) __attribute__((alias("PIOS_EXTI_15_10_irq_handler")));
323 #endif /* PIOS_INCLUDE_EXTI */
326 * @}
327 * @}