4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
35 * \author Francesco Sacchi <batt@develer.com>
37 * \brief System IRQ handler for Atmel AT91 ARM7 processors.
39 * In Atmel AT91 ARM7TDMI processors, there are various
40 * peripheral interrupt sources.
41 * In general, every source has its own interrupt vector, so it
42 * is possible to assign a specific handler for each interrupt
44 * However, there are a few sources called "system sources" that
45 * share a common IRQ line and vector, called "system IRQ".
46 * So a unique system IRQ dispatcher is implemented here.
47 * This module also contains an interface to manage every source
48 * independently. It is possible to assign to every system IRQ
49 * a specific IRQ handler.
51 * \see sysirq_setHandler
52 * \see sysirq_setEnable
55 #include "sysirq_at91.h"
58 #include <cfg/module.h>
59 #include <cfg/macros.h>
62 * Enable/disable the Periodic Interrupt Timer
65 INLINE void pit_setEnable(bool enable)
70 PIT_MR &= ~BV(PITIEN);
74 * Table containing all system irqs.
76 static SysIrq sysirq_tab[] =
78 /* PIT, Periodic Interval Timer (System timer)*/
81 .setEnable = pit_setEnable,
84 /* TODO: add other system sources here */
87 STATIC_ASSERT(countof(sysirq_tab) == SYSIRQ_CNT);
90 * System IRQ dispatcher.
91 * This is the entry point for all system IRQs in AT91.
92 * This function checks for interrupt enable state of
93 * various sources (system timer, etc..) and calls
94 * the corresponding handler.
96 static void sysirq_dispatcher(void) __attribute__ ((naked));
97 static void sysirq_dispatcher(void)
100 for (unsigned i = 0; i < countof(sysirq_tab); i++)
102 if (sysirq_tab[i].enabled
103 && sysirq_tab[i].handler)
104 sysirq_tab[i].handler();
110 #define SYSIRQ_PRIORITY 0 ///< default priority for system irqs.
116 * Init system IRQ handling.
117 * \note all system interrupts are disabled.
119 void sysirq_init(void)
122 IRQ_SAVE_DISABLE(flags);
124 /* Disable all system interrupts */
125 for (unsigned i = 0; i < countof(sysirq_tab); i++)
126 sysirq_tab[i].setEnable(false);
128 /* Set the vector. */
129 AIC_SVR(SYSC_ID) = sysirq_dispatcher;
130 /* Initialize to edge triggered with defined priority. */
131 AIC_SMR(SYSC_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SYSIRQ_PRIORITY;
132 /* Clear pending interrupt */
133 AIC_ICCR = BV(SYSC_ID);
134 /* Enable the system IRQ */
135 AIC_IECR = BV(SYSC_ID);
143 * Helper function used to set handler for system IRQ \a irq.
145 void sysirq_setHandler(sysirq_t irq, sysirq_handler_t handler)
147 ASSERT(irq < SYSIRQ_CNT);
148 sysirq_tab[irq].handler = handler;
152 * Helper function used to enable/disable system IRQ \a irq.
154 void sysirq_setEnable(sysirq_t irq, bool enable)
156 ASSERT(irq < SYSIRQ_CNT);
158 sysirq_tab[irq].setEnable(enable);
159 sysirq_tab[irq].enabled = enable;
163 * Helper function used to get system IRQ \a irq state.
165 bool sysirq_enabled(sysirq_t irq)
167 ASSERT(irq < SYSIRQ_CNT);
169 return sysirq_tab[irq].enabled;