Add STM self-flashing loader
[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
22 extern void main(void);
23 extern char __stack__;
24 extern char __text_start__, __text_end__;
25 extern char __data_start__, __data_end__;
26 extern char __bss_start__, __bss_end__;
27
28 /* Interrupt functions */
29
30 void stm_halt_isr(void)
31 {
32         ao_panic(AO_PANIC_CRASH);
33 }
34
35 void stm_ignore_isr(void)
36 {
37 }
38
39 const void *stm_interrupt_vector[];
40
41 #define BOOT_FETCH(o)   (*((uint32_t *) (AO_BOOT_APPLICATION_BASE + (o))))
42
43 #ifdef AO_BOOT_APPLICATION_PIN
44 #include <ao_exti.h>
45 #endif
46
47 void start(void) {
48 #ifdef AO_BOOT_APPLICATION_PIN
49         uint16_t v;
50
51         /* Enable power interface clock */
52         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_PWREN);
53         
54         /* Enable the input pin */
55         ao_enable_input(&AO_BOOT_APPLICATION_GPIO, AO_BOOT_APPLICATION_PIN,
56                         AO_BOOT_APPLICATION_MODE);
57
58         /* Read the value */
59         v = stm_gpio_get(&AO_BOOT_APPLICATION_GPIO, AO_BOOT_APPLICATION_PIN);
60
61         /* Reset the chip to turn off the port and the power interface clock */
62         ao_gpio_set_mode(&AO_BOOT_APPLICATION_GPIO, AO_BOOT_APPLICATION_PIN, 0);
63         ao_disable_port(&AO_BOOT_APPLICATION_GPIO);
64         stm_rcc.apb1enr &= ~(1 << STM_RCC_APB1ENR_PWREN);
65         if (v == AO_BOOT_APPLICATION_VALUE)
66         {
67                 uint32_t        sp;
68                 uint32_t        pc;
69
70                 sp = BOOT_FETCH(0);
71                 pc = BOOT_FETCH(4);
72                 asm ("mov sp, %0" : : "r" (sp));
73                 asm ("mov lr, %0" : : "r" (pc));
74                 asm ("bx lr");
75         }
76 #endif
77
78         /* Set interrupt vector table offset */
79         stm_nvic.vto = (uint32_t) &stm_interrupt_vector;
80         memcpy(&__data_start__, &__text_end__, &__data_end__ - &__data_start__);
81         memset(&__bss_start__, '\0', &__bss_end__ - &__bss_start__);
82         main();
83 }
84
85 #define STRINGIFY(x) #x
86
87 #define isr(name) \
88         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
89         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_ignore_isr))
90
91 #define isr_halt(name) \
92         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
93         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_halt_isr))
94
95 isr(nmi)
96 isr_halt(hardfault)
97 isr_halt(memmanage)
98 isr_halt(busfault)
99 isr_halt(usagefault)
100 isr(svc)
101 isr(debugmon)
102 isr(pendsv)
103 isr(systick)
104 isr(wwdg)
105 isr(pvd)
106 isr(tamper_stamp)
107 isr(rtc_wkup)
108 isr(flash)
109 isr(rcc)
110 isr(exti0)
111 isr(exti1)
112 isr(exti2)
113 isr(exti3)
114 isr(exti4)
115 isr(dma1_channel1)
116 isr(dma1_channel2)
117 isr(dma1_channel3)
118 isr(dma1_channel4)
119 isr(dma1_channel5)
120 isr(dma1_channel6)
121 isr(dma1_channel7)
122 isr(adc1)
123 isr(usb_hp)
124 isr(usb_lp)
125 isr(dac)
126 isr(comp)
127 isr(exti9_5)
128 isr(lcd)
129 isr(tim9)
130 isr(tim10)
131 isr(tim11)
132 isr(tim2)
133 isr(tim3)
134 isr(tim4)
135 isr(i2c1_ev)
136 isr(i2c1_er)
137 isr(i2c2_ev)
138 isr(i2c2_er)
139 isr(spi1)
140 isr(spi2)
141 isr(usart1)
142 isr(usart2)
143 isr(usart3)
144 isr(exti15_10)
145 isr(rtc_alarm)
146 isr(usb_fs_wkup)
147 isr(tim6)
148 isr(tim7)
149
150 #define i(addr,name)    [(addr)/4] = stm_ ## name ## _isr
151
152 __attribute__ ((section(".interrupt")))
153 const void *stm_interrupt_vector[] = {
154         [0] = &__stack__,
155         [1] = start,
156         i(0x08, nmi),
157         i(0x0c, hardfault),
158         i(0x10, memmanage),
159         i(0x14, busfault),
160         i(0x18, usagefault),
161         i(0x2c, svc),
162         i(0x30, debugmon),
163         i(0x38, pendsv),
164         i(0x3c, systick),
165         i(0x40, wwdg),
166         i(0x44, pvd),
167         i(0x48, tamper_stamp),
168         i(0x4c, rtc_wkup),
169         i(0x50, flash),
170         i(0x54, rcc),
171         i(0x58, exti0),
172         i(0x5c, exti1),
173         i(0x60, exti2),
174         i(0x64, exti3),
175         i(0x68, exti4),
176         i(0x6c, dma1_channel1),
177         i(0x70, dma1_channel2),
178         i(0x74, dma1_channel3),
179         i(0x78, dma1_channel4),
180         i(0x7c, dma1_channel5),
181         i(0x80, dma1_channel6),
182         i(0x84, dma1_channel7),
183         i(0x88, adc1),
184         i(0x8c, usb_hp),
185         i(0x90, usb_lp),
186         i(0x94, dac),
187         i(0x98, comp),
188         i(0x9c, exti9_5),
189         i(0xa0, lcd),
190         i(0xa4, tim9),
191         i(0xa8, tim10),
192         i(0xac, tim11),
193         i(0xb0, tim2),
194         i(0xb4, tim3),
195         i(0xb8, tim4),
196         i(0xbc, i2c1_ev),
197         i(0xc0, i2c1_er),
198         i(0xc4, i2c2_ev),
199         i(0xc8, i2c2_er),
200         i(0xcc, spi1),
201         i(0xd0, spi2),
202         i(0xd4, usart1),
203         i(0xd8, usart2),
204         i(0xdc, usart3),
205         i(0xe0, exti15_10),
206         i(0xe4, rtc_alarm),
207         i(0xe8, usb_fs_wkup),
208         i(0xec, tim6),
209         i(0xf0, tim7),
210 };