Switch from GPLv2 to GPLv2+
[fw/altos] / src / stmf0 / 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 "stm32f0.h"
21 #include <string.h>
22 #include <ao_boot.h>
23
24 #ifndef IS_FLASH_LOADER
25 #error Should define IS_FLASH_LOADER
26 #define IS_FLASH_LOADER 0
27 #endif
28
29 #if !IS_FLASH_LOADER
30 #define RELOCATE_INTERRUPT      1
31 #endif
32
33 extern void main(void);
34 extern char __stack__;
35 extern char __text_start__, __text_end__;
36 extern char __data_start__, __data_end__;
37 extern char __bss_start__, __bss_end__;
38 #if RELOCATE_INTERRUPT
39 extern char __interrupt_rom__, __interrupt_start__, __interrupt_end__;
40 #endif
41
42 /* Interrupt functions */
43
44 void stm_halt_isr(void)
45 {
46         ao_panic(AO_PANIC_CRASH);
47 }
48
49 void stm_ignore_isr(void)
50 {
51 }
52
53 const void *stm_interrupt_vector[];
54
55 uint32_t
56 stm_flash_size(void) {
57         uint16_t        dev_id = stm_dev_id();
58         uint16_t        kbytes = 0;
59
60         switch (dev_id) {
61         case 0x445:
62                 kbytes = stm_flash_size_04x.f_size;
63                 break;
64         }
65         return (uint32_t) kbytes * 1024;
66 }
67
68 void start(void)
69 {
70 #ifdef AO_BOOT_CHAIN
71         if (ao_boot_check_chain()) {
72 #ifdef AO_BOOT_PIN
73                 ao_boot_check_pin();
74 #endif
75         }
76 #endif
77 #if RELOCATE_INTERRUPT
78         /* Turn on syscfg */
79         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
80
81         memcpy(&__interrupt_start__, &__interrupt_rom__, &__interrupt_end__ - &__interrupt_start__);
82         stm_syscfg.cfgr1 = (stm_syscfg.cfgr1 & ~(STM_SYSCFG_CFGR1_MEM_MODE_MASK << STM_SYSCFG_CFGR1_MEM_MODE)) |
83                 (STM_SYSCFG_CFGR1_MEM_MODE_SRAM << STM_SYSCFG_CFGR1_MEM_MODE);
84 #endif
85         memcpy(&__data_start__, &__text_end__, &__data_end__ - &__data_start__);
86         memset(&__bss_start__, '\0', &__bss_end__ - &__bss_start__);
87         main();
88 }
89
90 #define STRINGIFY(x) #x
91
92 #define isr(name) \
93         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
94         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_ignore_isr))
95
96 #define isr_halt(name) \
97         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
98         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_halt_isr))
99
100 isr(nmi)
101 isr_halt(hardfault)
102 isr_halt(memmanage)
103 isr_halt(busfault)
104 isr_halt(usagefault)
105 isr(svc)
106 isr(debugmon)
107 isr(pendsv)
108 isr(systick)
109 isr(wwdg)
110 isr(pvd)
111 isr(rtc)
112 isr(flash)
113 isr(rcc_crs)
114 isr(exti0_1)
115 isr(exti2_3)
116 isr(exti4_15)
117 isr(tsc)
118 isr(dma_ch1)
119 isr(dma_ch2_3)
120 isr(dma_ch4_5_6)
121 isr(adc_comp)
122 isr(tim1_brk_up_trg_com)
123 isr(tim1_cc)
124 isr(tim2)
125 isr(tim3)
126 isr(tim6_dac)
127 isr(tim7)
128 isr(tim14)
129 isr(tim15)
130 isr(tim16)
131 isr(tim17)
132 isr(i2c1)
133 isr(i2c2)
134 isr(spi1)
135 isr(spi2)
136 isr(usart1)
137 isr(usart2)
138 isr(usart3_4_5_6_7_8)
139 isr(cec_can)
140 isr(usb)
141
142 #define i(addr,name)    [(addr)/4] = stm_ ## name ## _isr
143
144 __attribute__ ((section(".interrupt")))
145 const void *stm_interrupt_vector[] = {
146         [0] = &__stack__,
147         [1] = start,
148         i(0x08, nmi),
149         i(0x0c, hardfault),
150         i(0x2c, svc),
151         i(0x30, debugmon),
152         i(0x38, pendsv),
153         i(0x3c, systick),
154         i(0x40, wwdg),          /* IRQ0 */
155         i(0x44, pvd),
156         i(0x48, rtc),
157         i(0x4c, flash),
158         i(0x50, rcc_crs),
159         i(0x54, exti0_1),
160         i(0x58, exti2_3),
161         i(0x5c, exti4_15),
162         i(0x60, tsc),
163         i(0x64, dma_ch1),
164         i(0x68, dma_ch2_3),
165         i(0x6c, dma_ch4_5_6),
166         i(0x70, adc_comp),
167         i(0x74, tim1_brk_up_trg_com),
168         i(0x78, tim1_cc),
169         i(0x7c, tim2),
170         i(0x80, tim3),
171         i(0x84, tim6_dac),
172         i(0x88, tim7),
173         i(0x8c, tim14),
174         i(0x90, tim15),
175         i(0x94, tim16),
176         i(0x98, tim17),
177         i(0x9c, i2c1),
178         i(0xa0, i2c2),
179         i(0xa4, spi1),
180         i(0xa8, spi2),
181         i(0xac, usart1),
182         i(0xb0, usart2),
183         i(0xb4, usart3_4_5_6_7_8),
184         i(0xb8, cec_can),
185         i(0xbc, usb),
186 };