]> git.gag.com Git - fw/altos/blob - src/stm/ao_interrupt.c
altos/stm: Support SPI modes other than 0
[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; 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 "stm32l.h"
21 #include <string.h>
22 #include <ao_boot.h>
23
24 extern void main(void);
25 extern char __stack__;
26 extern char __text_start__, __text_end__;
27 extern char _start__, _end__;
28 extern char __bss_start__, __bss_end__;
29
30 /* Interrupt functions */
31
32 void stm_halt_isr(void)
33 {
34         ao_panic(AO_PANIC_CRASH);
35 }
36
37 void stm_ignore_isr(void)
38 {
39 }
40
41 const void *stm_interrupt_vector[];
42
43 uint32_t
44 stm_flash_size(void) {
45         uint16_t        dev_id = stm_dev_id();
46         uint16_t        kbytes = 0;
47
48         switch (dev_id) {
49         case 0x416:     /* cat 1 */
50                 kbytes = stm_flash_size_medium.f_size;
51                 break;
52         case 0x429:     /* cat 2 */
53                 kbytes = stm_flash_size_medium.f_size & 0xff;
54                 break;
55         case 0x427:     /* cat 3 */
56                 kbytes = stm_flash_size_large.f_size;
57                 break;
58         case 0x436:     /* cat 4 */
59                 switch (stm_flash_size_large.f_size) {
60                 case 0:
61                         kbytes = 256;
62                         break;
63                 case 1:
64                         kbytes = 384;
65                         break;
66                 }
67                 break;
68         case 0x437:     /* cat 5 */
69                 kbytes = stm_flash_size_large.f_size;
70                 break;
71         }
72         return (uint32_t) kbytes * 1024;
73 }
74
75 void start(void)
76 {
77 #ifdef AO_BOOT_CHAIN
78         if (ao_boot_check_chain()) {
79 #ifdef AO_BOOT_PIN
80                 ao_boot_check_pin();
81 #endif
82         }
83 #endif
84         /* Set interrupt vector table offset */
85         stm_nvic.vto = (uint32_t) &stm_interrupt_vector;
86         memcpy(&_start__, &__text_end__, &_end__ - &_start__);
87         memset(&__bss_start__, '\0', &__bss_end__ - &__bss_start__);
88         main();
89 }
90
91 #define STRINGIFY(x) #x
92
93 #define isr(name) \
94         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
95         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_ignore_isr))
96
97 #define isr_halt(name) \
98         void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
99         _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_halt_isr))
100
101 isr(nmi)
102 isr_halt(hardfault)
103 isr_halt(memmanage)
104 isr_halt(busfault)
105 isr_halt(usagefault)
106 isr(svc)
107 isr(debugmon)
108 isr(pendsv)
109 isr(systick)
110 isr(wwdg)
111 isr(pvd)
112 isr(tamper_stamp)
113 isr(rtc_wkup)
114 isr(flash)
115 isr(rcc)
116 isr(exti0)
117 isr(exti1)
118 isr(exti2)
119 isr(exti3)
120 isr(exti4)
121 isr(dma1_channel1)
122 isr(dma1_channel2)
123 isr(dma1_channel3)
124 isr(dma1_channel4)
125 isr(dma1_channel5)
126 isr(dma1_channel6)
127 isr(dma1_channel7)
128 isr(adc1)
129 isr(usb_hp)
130 isr(usb_lp)
131 isr(dac)
132 isr(comp)
133 isr(exti9_5)
134 isr(lcd)
135 isr(tim9)
136 isr(tim10)
137 isr(tim11)
138 isr(tim2)
139 isr(tim3)
140 isr(tim4)
141 isr(i2c1_ev)
142 isr(i2c1_er)
143 isr(i2c2_ev)
144 isr(i2c2_er)
145 isr(spi1)
146 isr(spi2)
147 isr(usart1)
148 isr(usart2)
149 isr(usart3)
150 isr(exti15_10)
151 isr(rtc_alarm)
152 isr(usb_fs_wkup)
153 isr(tim6)
154 isr(tim7)
155
156 #define i(addr,name)    [(addr)/4] = stm_ ## name ## _isr
157
158 __attribute__ ((section(".interrupt")))
159 const void *stm_interrupt_vector[] = {
160         [0] = &__stack__,
161         [1] = start,
162         i(0x08, nmi),
163         i(0x0c, hardfault),
164         i(0x10, memmanage),
165         i(0x14, busfault),
166         i(0x18, usagefault),
167         i(0x2c, svc),
168         i(0x30, debugmon),
169         i(0x38, pendsv),
170         i(0x3c, systick),
171         i(0x40, wwdg),
172         i(0x44, pvd),
173         i(0x48, tamper_stamp),
174         i(0x4c, rtc_wkup),
175         i(0x50, flash),
176         i(0x54, rcc),
177         i(0x58, exti0),
178         i(0x5c, exti1),
179         i(0x60, exti2),
180         i(0x64, exti3),
181         i(0x68, exti4),
182         i(0x6c, dma1_channel1),
183         i(0x70, dma1_channel2),
184         i(0x74, dma1_channel3),
185         i(0x78, dma1_channel4),
186         i(0x7c, dma1_channel5),
187         i(0x80, dma1_channel6),
188         i(0x84, dma1_channel7),
189         i(0x88, adc1),
190         i(0x8c, usb_hp),
191         i(0x90, usb_lp),
192         i(0x94, dac),
193         i(0x98, comp),
194         i(0x9c, exti9_5),
195         i(0xa0, lcd),
196         i(0xa4, tim9),
197         i(0xa8, tim10),
198         i(0xac, tim11),
199         i(0xb0, tim2),
200         i(0xb4, tim3),
201         i(0xb8, tim4),
202         i(0xbc, i2c1_ev),
203         i(0xc0, i2c1_er),
204         i(0xc4, i2c2_ev),
205         i(0xc8, i2c2_er),
206         i(0xcc, spi1),
207         i(0xd0, spi2),
208         i(0xd4, usart1),
209         i(0xd8, usart2),
210         i(0xdc, usart3),
211         i(0xe0, exti15_10),
212         i(0xe4, rtc_alarm),
213         i(0xe8, usb_fs_wkup),
214         i(0xec, tim6),
215         i(0xf0, tim7),
216 };