altos: Move from newlib-nano to picolibc
[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);
118
119 __attribute__ ((section(".init")))
120 const void * const stm_interrupt_vector[] = {
121         [0] = __stack,
122         [1] = _start,
123         i(0x08, nmi),
124         i(0x0c, hardfault),
125         i(0x2c, svc),
126         i(0x30, debugmon),
127         i(0x38, pendsv),
128         i(0x3c, systick),
129         i(0x40, wwdg),          /* IRQ0 */
130         i(0x44, pvd),
131         i(0x48, rtc),
132         i(0x4c, flash),
133         i(0x50, rcc_crs),
134         i(0x54, exti0_1),
135         i(0x58, exti2_3),
136         i(0x5c, exti4_15),
137         i(0x60, tsc),
138         i(0x64, dma_ch1),
139         i(0x68, dma_ch2_3),
140         i(0x6c, dma_ch4_5_6),
141         i(0x70, adc_comp),
142         i(0x74, tim1_brk_up_trg_com),
143         i(0x78, tim1_cc),
144         i(0x7c, tim2),
145         i(0x80, tim3),
146         i(0x84, tim6_dac),
147         i(0x88, tim7),
148         i(0x8c, tim14),
149         i(0x90, tim15),
150         i(0x94, tim16),
151         i(0x98, tim17),
152         i(0x9c, i2c1),
153         i(0xa0, i2c2),
154         i(0xa4, spi1),
155         i(0xa8, spi2),
156         i(0xac, usart1),
157         i(0xb0, usart2),
158         i(0xb4, usart3_4_5_6_7_8),
159         i(0xb8, cec_can),
160         i(0xbc, usb),
161 };
162
163 void _init(void);
164
165 #if RELOCATE_INTERRUPT
166 extern char __interrupt_start[];
167 #endif
168
169 void _init(void)
170 {
171 #if AO_BOOT_CHAIN
172         if (ao_boot_check_chain()) {
173 #if AO_BOOT_PIN
174                 ao_boot_check_pin();
175 #endif
176         }
177 #endif
178         /* Turn on syscfg */
179         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
180
181 #if RELOCATE_INTERRUPT
182         /* This will erase the boot chain data */
183         memcpy(&__interrupt_start, stm_interrupt_vector, sizeof (stm_interrupt_vector));
184         stm_syscfg.cfgr1 = (stm_syscfg.cfgr1 & ~(STM_SYSCFG_CFGR1_MEM_MODE_MASK << STM_SYSCFG_CFGR1_MEM_MODE)) |
185                 (STM_SYSCFG_CFGR1_MEM_MODE_SRAM << STM_SYSCFG_CFGR1_MEM_MODE);
186 #else
187         /* Switch to Main Flash mode (DFU loader leaves us in System mode) */
188         stm_syscfg.cfgr1 = (stm_syscfg.cfgr1 & ~(STM_SYSCFG_CFGR1_MEM_MODE_MASK << STM_SYSCFG_CFGR1_MEM_MODE)) |
189                 (STM_SYSCFG_CFGR1_MEM_MODE_MAIN_FLASH << STM_SYSCFG_CFGR1_MEM_MODE);
190 #endif
191 }