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