]> git.gag.com Git - fw/altos/blob - src/stmf0/ao_interrupt.c
altos/stmf0: Remove some incorrect defines in stm32f0.h
[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 #ifndef RELOCATE_INTERRUPT
30 #if !IS_FLASH_LOADER
31 #define RELOCATE_INTERRUPT      1
32 #endif
33 #endif
34
35 /* Interrupt functions */
36
37 void stm_halt_isr(void)
38 {
39         ao_panic(AO_PANIC_CRASH);
40 }
41
42 void stm_ignore_isr(void)
43 {
44 }
45
46 uint32_t
47 stm_flash_size(void) {
48         uint16_t        dev_id = stm_dev_id();
49         uint16_t        kbytes = 0;
50
51         switch (dev_id) {
52         case 0x445:
53                 kbytes = stm_flash_size_04x.f_size;
54                 break;
55         }
56         return (uint32_t) kbytes * 1024;
57 }
58
59 #define STRINGIFY(x) #x
60
61 #define isr(name) \
62         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
63         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_ignore_isr))
64
65 #define isr_halt(name) \
66         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
67         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_halt_isr))
68
69 isr(nmi)
70 isr_halt(hardfault)
71 isr_halt(memmanage)
72 isr_halt(busfault)
73 isr_halt(usagefault)
74 isr(svc)
75 isr(debugmon)
76 isr(pendsv)
77 isr(systick)
78 isr(wwdg)
79 isr(pvd)
80 isr(rtc)
81 isr(flash)
82 isr(rcc_crs)
83 isr(exti0_1)
84 isr(exti2_3)
85 isr(exti4_15)
86 isr(tsc)
87 isr(dma_ch1)
88 isr(dma_ch2_3)
89 isr(dma_ch4_5_6)
90 isr(adc_comp)
91 isr(tim1_brk_up_trg_com)
92 isr(tim1_cc)
93 isr(tim2)
94 isr(tim3)
95 isr(tim6_dac)
96 isr(tim7)
97 isr(tim14)
98 isr(tim15)
99 isr(tim16)
100 isr(tim17)
101 isr(i2c1)
102 isr(i2c2)
103 isr(spi1)
104 isr(spi2)
105 isr(usart1)
106 isr(usart2)
107 isr(usart3_4_5_6_7_8)
108 isr(cec_can)
109 isr(usb)
110
111 #undef isr
112 #undef isr_halt
113
114 #define i(addr,name)    [(addr)/4] = stm_ ## name ## _isr
115
116 extern char __stack[];
117 void _start(void) __attribute__((__noreturn__));
118 void main(void) __attribute__((__noreturn__));
119
120 __attribute__ ((section(".init")))
121 const void * const __interrupt_vector[0x30] = {
122         [0] = __stack,
123         [1] = _start,
124         i(0x08, nmi),
125         i(0x0c, hardfault),
126         i(0x2c, svc),
127         i(0x30, debugmon),
128         i(0x38, pendsv),
129         i(0x3c, systick),
130         i(0x40, wwdg),          /* IRQ0 */
131         i(0x44, pvd),
132         i(0x48, rtc),
133         i(0x4c, flash),
134         i(0x50, rcc_crs),
135         i(0x54, exti0_1),
136         i(0x58, exti2_3),
137         i(0x5c, exti4_15),
138         i(0x60, tsc),
139         i(0x64, dma_ch1),
140         i(0x68, dma_ch2_3),
141         i(0x6c, dma_ch4_5_6),
142         i(0x70, adc_comp),
143         i(0x74, tim1_brk_up_trg_com),
144         i(0x78, tim1_cc),
145         i(0x7c, tim2),
146         i(0x80, tim3),
147         i(0x84, tim6_dac),
148         i(0x88, tim7),
149         i(0x8c, tim14),
150         i(0x90, tim15),
151         i(0x94, tim16),
152         i(0x98, tim17),
153         i(0x9c, i2c1),
154         i(0xa0, i2c2),
155         i(0xa4, spi1),
156         i(0xa8, spi2),
157         i(0xac, usart1),
158         i(0xb0, usart2),
159         i(0xb4, usart3_4_5_6_7_8),
160         i(0xb8, cec_can),
161         i(0xbc, usb),
162 };
163
164 /*
165  * Previous versions of this code had a 256 byte interupt vector. Add
166  * some padding to make sure the other low ROM variables land at the
167  * same address
168  */
169
170 __attribute__ ((section(".init.0")))
171 const void *const __interrupt_pad[0x10];
172
173 void *__interrupt_ram[sizeof(__interrupt_vector)/sizeof(__interrupt_vector[0])] __attribute__((section(".preserve.1")));
174
175 extern char __data_source[];
176 extern char __data_start[];
177 extern char __data_size[];
178 extern char __bss_start[];
179 extern char __bss_size[];
180
181 void _start(void)
182 {
183         memcpy(__data_start, __data_source, (uintptr_t) __data_size);
184         memset(__bss_start, '\0', (uintptr_t) __bss_size);
185
186 #if AO_BOOT_CHAIN
187         if (ao_boot_check_chain()) {
188 #if AO_BOOT_PIN
189                 if (ao_boot_check_pin())
190 #endif
191                 {
192                         ao_boot_chain(AO_BOOT_APPLICATION_BASE);
193                 }
194         }
195 #endif
196         /* Turn on syscfg */
197         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
198
199 #if RELOCATE_INTERRUPT
200         memcpy(__interrupt_ram, __interrupt_vector, sizeof(__interrupt_ram));
201         stm_syscfg.cfgr1 = (stm_syscfg.cfgr1 & ~(STM_SYSCFG_CFGR1_MEM_MODE_MASK << STM_SYSCFG_CFGR1_MEM_MODE)) |
202                 (STM_SYSCFG_CFGR1_MEM_MODE_SRAM << STM_SYSCFG_CFGR1_MEM_MODE);
203 #else
204         /* Switch to Main Flash mode (DFU loader leaves us in System mode) */
205         stm_syscfg.cfgr1 = (stm_syscfg.cfgr1 & ~(STM_SYSCFG_CFGR1_MEM_MODE_MASK << STM_SYSCFG_CFGR1_MEM_MODE)) |
206                 (STM_SYSCFG_CFGR1_MEM_MODE_MAIN_FLASH << STM_SYSCFG_CFGR1_MEM_MODE);
207 #endif
208         main();
209 }