c62870b745ffebfb616f9e0498d61e7c919ff81c
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include "stm32l.h"
21 #include <string.h>
22 #include <ao_boot.h>
23
24 extern void main(void);
25
26 /* Interrupt functions */
27
28 void stm_halt_isr(void)
29 {
30         ao_panic(AO_PANIC_CRASH);
31 }
32
33 void stm_ignore_isr(void)
34 {
35 }
36
37 void *const __interrupt_vector[];
38
39 uint32_t
40 stm_flash_size(void) {
41         uint16_t        dev_id = stm_dev_id();
42         uint16_t        kbytes = 0;
43
44         switch (dev_id) {
45         case 0x416:     /* cat 1 */
46                 kbytes = stm_flash_size_medium.f_size;
47                 break;
48         case 0x429:     /* cat 2 */
49                 kbytes = stm_flash_size_medium.f_size & 0xff;
50                 break;
51         case 0x427:     /* cat 3 */
52                 kbytes = stm_flash_size_large.f_size;
53                 break;
54         case 0x436:     /* cat 4 */
55                 switch (stm_flash_size_large.f_size) {
56                 case 0:
57                         kbytes = 256;
58                         break;
59                 case 1:
60                         kbytes = 384;
61                         break;
62                 }
63                 break;
64         case 0x437:     /* cat 5 */
65                 kbytes = stm_flash_size_large.f_size;
66                 break;
67         }
68         return (uint32_t) kbytes * 1024;
69 }
70
71 #define STRINGIFY(x) #x
72
73 #define isr(name) \
74         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
75         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_ignore_isr))
76
77 #define isr_halt(name) \
78         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
79         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_halt_isr))
80
81 isr(nmi)
82 isr_halt(hardfault)
83 isr_halt(memmanage)
84 isr_halt(busfault)
85 isr_halt(usagefault)
86 isr(svc)
87 isr(debugmon)
88 isr(pendsv)
89 isr(systick)
90 isr(wwdg)
91 isr(pvd)
92 isr(tamper_stamp)
93 isr(rtc_wkup)
94 isr(flash)
95 isr(rcc)
96 isr(exti0)
97 isr(exti1)
98 isr(exti2)
99 isr(exti3)
100 isr(exti4)
101 isr(dma1_channel1)
102 isr(dma1_channel2)
103 isr(dma1_channel3)
104 isr(dma1_channel4)
105 isr(dma1_channel5)
106 isr(dma1_channel6)
107 isr(dma1_channel7)
108 isr(adc1)
109 isr(usb_hp)
110 isr(usb_lp)
111 isr(dac)
112 isr(comp)
113 isr(exti9_5)
114 isr(lcd)
115 isr(tim9)
116 isr(tim10)
117 isr(tim11)
118 isr(tim2)
119 isr(tim3)
120 isr(tim4)
121 isr(i2c1_ev)
122 isr(i2c1_er)
123 isr(i2c2_ev)
124 isr(i2c2_er)
125 isr(spi1)
126 isr(spi2)
127 isr(usart1)
128 isr(usart2)
129 isr(usart3)
130 isr(exti15_10)
131 isr(rtc_alarm)
132 isr(usb_fs_wkup)
133 isr(tim6)
134 isr(tim7)
135
136 #define i(addr,name)    [(addr)/4] = stm_ ## name ## _isr
137
138 extern char __stack[];
139 void _start(void) __attribute__((__noreturn__));
140 void main(void) __attribute__((__noreturn__));
141
142 /* This must be exactly 256 bytes long so that the configuration data
143  * gets loaded at the right place
144  */
145
146 __attribute__ ((section(".init")))
147 void * const __interrupt_vector[64] = {
148         [0] = &__stack,
149         [1] = _start,
150         i(0x08, nmi),
151         i(0x0c, hardfault),
152         i(0x10, memmanage),
153         i(0x14, busfault),
154         i(0x18, usagefault),
155         i(0x2c, svc),
156         i(0x30, debugmon),
157         i(0x38, pendsv),
158         i(0x3c, systick),
159         i(0x40, wwdg),
160         i(0x44, pvd),
161         i(0x48, tamper_stamp),
162         i(0x4c, rtc_wkup),
163         i(0x50, flash),
164         i(0x54, rcc),
165         i(0x58, exti0),
166         i(0x5c, exti1),
167         i(0x60, exti2),
168         i(0x64, exti3),
169         i(0x68, exti4),
170         i(0x6c, dma1_channel1),
171         i(0x70, dma1_channel2),
172         i(0x74, dma1_channel3),
173         i(0x78, dma1_channel4),
174         i(0x7c, dma1_channel5),
175         i(0x80, dma1_channel6),
176         i(0x84, dma1_channel7),
177         i(0x88, adc1),
178         i(0x8c, usb_hp),
179         i(0x90, usb_lp),
180         i(0x94, dac),
181         i(0x98, comp),
182         i(0x9c, exti9_5),
183         i(0xa0, lcd),
184         i(0xa4, tim9),
185         i(0xa8, tim10),
186         i(0xac, tim11),
187         i(0xb0, tim2),
188         i(0xb4, tim3),
189         i(0xb8, tim4),
190         i(0xbc, i2c1_ev),
191         i(0xc0, i2c1_er),
192         i(0xc4, i2c2_ev),
193         i(0xc8, i2c2_er),
194         i(0xcc, spi1),
195         i(0xd0, spi2),
196         i(0xd4, usart1),
197         i(0xd8, usart2),
198         i(0xdc, usart3),
199         i(0xe0, exti15_10),
200         i(0xe4, rtc_alarm),
201         i(0xe8, usb_fs_wkup),
202         i(0xec, tim6),
203         i(0xf0, tim7),
204 };
205
206 extern char __data_source[];
207 extern char __data_start[];
208 extern char __data_size[];
209 extern char __bss_start[];
210 extern char __bss_size[];
211
212 void _start(void) {
213         memcpy(__data_start, __data_source, (uintptr_t) __data_size);
214         memset(__bss_start, '\0', (uintptr_t) __bss_size);
215
216 #ifdef AO_BOOT_CHAIN
217         if (ao_boot_check_chain()) {
218 #ifdef AO_BOOT_PIN
219                 if (ao_boot_check_pin())
220 #endif
221                 {
222                         ao_boot_chain(AO_BOOT_APPLICATION_BASE);
223                 }
224         }
225 #endif
226         /* Set interrupt vector table offset */
227         stm_nvic.vto = (uint32_t) &__interrupt_vector;
228         main();
229 }