969e6a0f7b6a23a7433785f114c76765005509bb
[fw/altos] / src / stm / ao_interrupt.c
1 /*
2  * Copyright © 2012 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 "stm32l.h"
20 #include <string.h>
21 #include <ao_boot.h>
22
23 extern void main(void);
24 extern char __stack__;
25 extern char __text_start__, __text_end__;
26 extern char __data_start__, __data_end__;
27 extern char __bss_start__, __bss_end__;
28
29 /* Interrupt functions */
30
31 void stm_halt_isr(void)
32 {
33         ao_panic(AO_PANIC_CRASH);
34 }
35
36 void stm_ignore_isr(void)
37 {
38 }
39
40 const void *stm_interrupt_vector[];
41
42 void start(void)
43 {
44 #ifdef AO_BOOT_CHAIN
45         if (ao_boot_check_chain()) {
46 #ifdef AO_BOOT_PIN
47                 ao_boot_check_pin();
48 #endif
49         }
50 #endif
51         /* Set interrupt vector table offset */
52         stm_nvic.vto = (uint32_t) &stm_interrupt_vector;
53         memcpy(&__data_start__, &__text_end__, &__data_end__ - &__data_start__);
54         memset(&__bss_start__, '\0', &__bss_end__ - &__bss_start__);
55         main();
56 }
57
58 #define STRINGIFY(x) #x
59
60 #define isr(name) \
61         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
62         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_ignore_isr))
63
64 #define isr_halt(name) \
65         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
66         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_halt_isr))
67
68 isr(nmi)
69 isr_halt(hardfault)
70 isr_halt(memmanage)
71 isr_halt(busfault)
72 isr_halt(usagefault)
73 isr(svc)
74 isr(debugmon)
75 isr(pendsv)
76 isr(systick)
77 isr(wwdg)
78 isr(pvd)
79 isr(tamper_stamp)
80 isr(rtc_wkup)
81 isr(flash)
82 isr(rcc)
83 isr(exti0)
84 isr(exti1)
85 isr(exti2)
86 isr(exti3)
87 isr(exti4)
88 isr(dma1_channel1)
89 isr(dma1_channel2)
90 isr(dma1_channel3)
91 isr(dma1_channel4)
92 isr(dma1_channel5)
93 isr(dma1_channel6)
94 isr(dma1_channel7)
95 isr(adc1)
96 isr(usb_hp)
97 isr(usb_lp)
98 isr(dac)
99 isr(comp)
100 isr(exti9_5)
101 isr(lcd)
102 isr(tim9)
103 isr(tim10)
104 isr(tim11)
105 isr(tim2)
106 isr(tim3)
107 isr(tim4)
108 isr(i2c1_ev)
109 isr(i2c1_er)
110 isr(i2c2_ev)
111 isr(i2c2_er)
112 isr(spi1)
113 isr(spi2)
114 isr(usart1)
115 isr(usart2)
116 isr(usart3)
117 isr(exti15_10)
118 isr(rtc_alarm)
119 isr(usb_fs_wkup)
120 isr(tim6)
121 isr(tim7)
122
123 #define i(addr,name)    [(addr)/4] = stm_ ## name ## _isr
124
125 __attribute__ ((section(".interrupt")))
126 const void *stm_interrupt_vector[] = {
127         [0] = &__stack__,
128         [1] = start,
129         i(0x08, nmi),
130         i(0x0c, hardfault),
131         i(0x10, memmanage),
132         i(0x14, busfault),
133         i(0x18, usagefault),
134         i(0x2c, svc),
135         i(0x30, debugmon),
136         i(0x38, pendsv),
137         i(0x3c, systick),
138         i(0x40, wwdg),
139         i(0x44, pvd),
140         i(0x48, tamper_stamp),
141         i(0x4c, rtc_wkup),
142         i(0x50, flash),
143         i(0x54, rcc),
144         i(0x58, exti0),
145         i(0x5c, exti1),
146         i(0x60, exti2),
147         i(0x64, exti3),
148         i(0x68, exti4),
149         i(0x6c, dma1_channel1),
150         i(0x70, dma1_channel2),
151         i(0x74, dma1_channel3),
152         i(0x78, dma1_channel4),
153         i(0x7c, dma1_channel5),
154         i(0x80, dma1_channel6),
155         i(0x84, dma1_channel7),
156         i(0x88, adc1),
157         i(0x8c, usb_hp),
158         i(0x90, usb_lp),
159         i(0x94, dac),
160         i(0x98, comp),
161         i(0x9c, exti9_5),
162         i(0xa0, lcd),
163         i(0xa4, tim9),
164         i(0xa8, tim10),
165         i(0xac, tim11),
166         i(0xb0, tim2),
167         i(0xb4, tim3),
168         i(0xb8, tim4),
169         i(0xbc, i2c1_ev),
170         i(0xc0, i2c1_er),
171         i(0xc4, i2c2_ev),
172         i(0xc8, i2c2_er),
173         i(0xcc, spi1),
174         i(0xd0, spi2),
175         i(0xd4, usart1),
176         i(0xd8, usart2),
177         i(0xdc, usart3),
178         i(0xe0, exti15_10),
179         i(0xe4, rtc_alarm),
180         i(0xe8, usb_fs_wkup),
181         i(0xec, tim6),
182         i(0xf0, tim7),
183 };