2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
24 #ifndef IS_FLASH_LOADER
25 #error Should define IS_FLASH_LOADER
26 #define IS_FLASH_LOADER 0
29 #ifndef RELOCATE_INTERRUPT
31 #define RELOCATE_INTERRUPT 1
35 /* Interrupt functions */
37 void stm_halt_isr(void)
39 ao_panic(AO_PANIC_CRASH);
42 void stm_ignore_isr(void)
47 stm_flash_size(void) {
48 uint16_t dev_id = stm_dev_id();
53 kbytes = stm_flash_size_04x.f_size;
56 return (uint32_t) kbytes * 1024;
59 #define STRINGIFY(x) #x
62 void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
63 _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_ignore_isr))
65 #define isr_halt(name) \
66 void __attribute__ ((weak)) stm_ ## name ## _isr(void); \
67 _Pragma(STRINGIFY(weak stm_ ## name ## _isr = stm_halt_isr))
91 isr(tim1_brk_up_trg_com)
107 isr(usart3_4_5_6_7_8)
114 #define i(addr,name) [(addr)/4] = stm_ ## name ## _isr
116 extern char __stack[];
117 void _start(void) __attribute__((__noreturn__));
118 void main(void) __attribute__((__noreturn__));
120 __attribute__ ((section(".init")))
121 const void * const __interrupt_vector[0x30] = {
130 i(0x40, wwdg), /* IRQ0 */
141 i(0x6c, dma_ch4_5_6),
143 i(0x74, tim1_brk_up_trg_com),
159 i(0xb4, usart3_4_5_6_7_8),
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
170 __attribute__ ((section(".init.0")))
171 const void *const __interrupt_pad[0x10];
173 void *__interrupt_ram[sizeof(__interrupt_vector)/sizeof(__interrupt_vector[0])] __attribute__((section(".preserve.1")));
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[];
183 memcpy(__data_start, __data_source, (uintptr_t) __data_size);
184 memset(__bss_start, '\0', (uintptr_t) __bss_size);
187 if (ao_boot_check_chain()) {
189 if (ao_boot_check_pin())
192 ao_boot_chain(AO_BOOT_APPLICATION_BASE);
197 stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
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);
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);