3318db2bd14ea71ed768991c07fa8eaf01291fca
[fw/altos] / src / lpc / ao_interrupt.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <string.h>
20 #include <ao_boot.h>
21
22 #ifndef IS_FLASH_LOADER
23 #error Should define IS_FLASH_LOADER
24 #define IS_FLASH_LOADER 0
25 #endif
26
27 #if !IS_FLASH_LOADER
28 #define RELOCATE_INTERRUPT      1
29 #endif
30
31 extern void main(void);
32 extern char __stack__;
33 extern char __text_start__, __text_end__;
34 extern char __data_start__, __data_end__;
35 extern char __bss_start__, __bss_end__;
36 #if RELOCATE_INTERRUPT
37 extern char __interrupt_rom__, __interrupt_start__, __interrupt_end__;
38 #endif
39
40 /* Interrupt functions */
41
42 void lpc_halt_isr(void)
43 {
44         ao_panic(AO_PANIC_CRASH);
45 }
46
47 void lpc_ignore_isr(void)
48 {
49 }
50
51 void start(void) {
52 #ifdef AO_BOOT_CHAIN
53         if (ao_boot_check_chain()) {
54 #ifdef AO_BOOT_PIN
55                 ao_boot_check_pin();
56 #endif
57         }
58 #endif
59 #if RELOCATE_INTERRUPT
60         memcpy(&__interrupt_start__, &__interrupt_rom__, &__interrupt_end__ - &__interrupt_start__);
61         lpc_scb.sysmemremap = LPC_SCB_SYSMEMREMAP_MAP_RAM << LPC_SCB_SYSMEMREMAP_MAP;
62 #endif
63         memcpy(&__data_start__, &__text_end__, &__data_end__ - &__data_start__);
64         memset(&__bss_start__, '\0', &__bss_end__ - &__bss_start__);
65         main();
66 }
67
68 #define STRINGIFY(x) #x
69
70 #define isr(name) \
71         void __attribute__ ((weak)) lpc_ ## name ## _isr(void); \
72         _Pragma(STRINGIFY(weak lpc_ ## name ## _isr = lpc_ignore_isr))
73
74 #define isr_halt(name) \
75         void __attribute__ ((weak)) lpc_ ## name ## _isr(void); \
76         _Pragma(STRINGIFY(weak lpc_ ## name ## _isr = lpc_halt_isr))
77
78 isr(nmi)
79 isr_halt(hardfault)
80 isr_halt(memmanage)
81 isr_halt(busfault)
82 isr_halt(usagefault)
83 isr(svc)
84 isr(debugmon)
85 isr(pendsv)
86 isr(systick)
87
88 isr(pin_int0)   /* IRQ0 */
89 isr(pin_int1)
90 isr(pin_int2)
91 isr(pin_int3)
92 isr(pin_int4)   /* IRQ4 */
93 isr(pin_int5)
94 isr(pin_int6)
95 isr(pin_int7)
96
97 isr(gint0)      /* IRQ8 */
98 isr(gint1)
99 isr(ssp1)
100 isr(i2c)
101
102 isr(ct16b0)     /* IRQ16 */
103 isr(ct16b1)
104 isr(ct32b0)
105 isr(ct32b1)
106 isr(ssp0)       /* IRQ20 */
107 isr(usart)
108 isr(usb_irq)
109 isr(usb_fiq)
110
111 isr(adc)        /* IRQ24 */
112 isr(wwdt)
113 isr(bod)
114 isr(flash)
115
116 isr(usb_wakeup)
117
118 #define i(addr,name)    [(addr)/4] = lpc_ ## name ## _isr
119 #define c(addr,value)   [(addr)/4] = (value)
120
121 __attribute__ ((section(".interrupt")))
122 const void *lpc_interrupt_vector[] = {
123         [0] = &__stack__,
124         [1] = start,
125         i(0x08, nmi),
126         i(0x0c, hardfault),
127         c(0x10, 0),
128         c(0x14, 0),
129         c(0x18, 0),
130         c(0x1c, 0),
131         c(0x20, 0),
132         c(0x24, 0),
133         c(0x28, 0),
134         i(0x2c, svc),
135         i(0x30, hardfault),
136         i(0x34, hardfault),
137         i(0x38, pendsv),
138         i(0x3c, systick),
139
140         i(0x40, pin_int0),      /* IRQ0 */
141         i(0x44, pin_int1),
142         i(0x48, pin_int2),
143         i(0x4c, pin_int3),
144         i(0x50, pin_int4),      /* IRQ4 */
145         i(0x54, pin_int5),
146         i(0x58, pin_int6),
147         i(0x5c, pin_int7),
148
149         i(0x60, gint0),         /* IRQ8 */
150         i(0x64, gint1),
151         i(0x68, hardfault),
152         i(0x6c, hardfault),
153         i(0x70, hardfault),     /* IRQ12 */
154         i(0x74, hardfault),
155         i(0x78, ssp1),
156         i(0x7c, i2c),
157
158         i(0x80, ct16b0),        /* IRQ16 */
159         i(0x84, ct16b1),
160         i(0x88, ct32b0),
161         i(0x8c, ct32b1),
162         i(0x90, ssp0),          /* IRQ20 */
163         i(0x94, usart),
164         i(0x98, usb_irq),
165         i(0x9c, usb_fiq),
166
167         i(0xa0, adc),           /* IRQ24 */
168         i(0xa4, wwdt),
169         i(0xa8, bod),
170         i(0xac, flash),
171
172         i(0xb0, hardfault),     /* IRQ28 */
173         i(0xb4, hardfault),
174         i(0xb8, usb_wakeup),
175         i(0xbc, hardfault),
176 };