1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2017 Glenn Ruben Bakke
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <stdint.h>
28 
29 extern uint32_t _estack;
30 extern uint32_t _sidata;
31 extern uint32_t _sdata;
32 extern uint32_t _edata;
33 extern uint32_t _sbss;
34 extern uint32_t _ebss;
35 
36 typedef void (*func)(void);
37 
38 extern void  _start(void) __attribute__((noreturn));
39 extern void SystemInit(void);
40 
Default_Handler(void)41 void Default_Handler(void) {
42     while (1);
43 }
44 
Reset_Handler(void)45 void Reset_Handler(void) {
46     uint32_t * ram_on_addr   = (uint32_t *)0x40000524;
47     uint32_t * ram_on_b_addr = (uint32_t *)0x40000554;
48     // RAM on in on-mode
49     *ram_on_addr   = 3; // block 0 and 1
50     *ram_on_b_addr = 3; // block 2 and 3
51 #if 0
52     // RAM on in off-mode
53     ram_on_addr   = 1 << 16;
54     ram_on_b_addr = 1 << 17;
55 #endif
56 
57     uint32_t * p_src  = &_sidata;
58     uint32_t * p_dest = &_sdata;
59 
60     while (p_dest < &_edata) {
61       *p_dest++ = *p_src++;
62     }
63 
64     uint32_t * p_bss     = &_sbss;
65     uint32_t * p_bss_end = &_ebss;
66     while (p_bss < p_bss_end) {
67         *p_bss++ = 0ul;
68     }
69 
70     SystemInit();
71     _start();
72 }
73 
74 void NMI_Handler            (void) __attribute__ ((weak, alias("Default_Handler")));
75 void HardFault_Handler      (void) __attribute__ ((weak, alias("Default_Handler")));
76 void SVC_Handler            (void) __attribute__ ((weak, alias("Default_Handler")));
77 void PendSV_Handler         (void) __attribute__ ((weak, alias("Default_Handler")));
78 void SysTick_Handler        (void) __attribute__ ((weak, alias("Default_Handler")));
79 
80 void POWER_CLOCK_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
81 void RADIO_IRQHandler       (void) __attribute__ ((weak, alias("Default_Handler")));
82 void UART0_IRQHandler       (void) __attribute__ ((weak, alias("Default_Handler")));
83 void SPI0_TWI0_IRQHandler   (void) __attribute__ ((weak, alias("Default_Handler")));
84 void SPI1_TWI1_IRQHandler   (void) __attribute__ ((weak, alias("Default_Handler")));
85 void GPIOTE_IRQHandler      (void) __attribute__ ((weak, alias("Default_Handler")));
86 void ADC_IRQHandler         (void) __attribute__ ((weak, alias("Default_Handler")));
87 void TIMER0_IRQHandler      (void) __attribute__ ((weak, alias("Default_Handler")));
88 void TIMER1_IRQHandler      (void) __attribute__ ((weak, alias("Default_Handler")));
89 void TIMER2_IRQHandler      (void) __attribute__ ((weak, alias("Default_Handler")));
90 void RTC0_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
91 void TEMP_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
92 void RNG_IRQHandler         (void) __attribute__ ((weak, alias("Default_Handler")));
93 void ECB_IRQHandler         (void) __attribute__ ((weak, alias("Default_Handler")));
94 void CCM_AAR_IRQHandler     (void) __attribute__ ((weak, alias("Default_Handler")));
95 void WDT_IRQHandler         (void) __attribute__ ((weak, alias("Default_Handler")));
96 void RTC1_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
97 void QDEC_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
98 void LPCOMP_IRQHandler      (void) __attribute__ ((weak, alias("Default_Handler")));
99 void SWI0_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
100 void SWI1_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
101 void SWI2_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
102 void SWI3_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
103 void SWI4_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
104 void SWI5_IRQHandler        (void) __attribute__ ((weak, alias("Default_Handler")));
105 
106 const func __Vectors[] __attribute__ ((section(".isr_vector"),used)) = {
107     (func)&_estack,
108     Reset_Handler,
109     NMI_Handler,
110     HardFault_Handler,
111     0,
112     0,
113     0,
114     0,
115     0,
116     0,
117     0,
118     SVC_Handler,
119     0,
120     0,
121     PendSV_Handler,
122     SysTick_Handler,
123 
124     /* External Interrupts */
125     POWER_CLOCK_IRQHandler,
126     RADIO_IRQHandler,
127     UART0_IRQHandler,
128     SPI0_TWI0_IRQHandler,
129     SPI1_TWI1_IRQHandler,
130     0,
131     GPIOTE_IRQHandler,
132     ADC_IRQHandler,
133     TIMER0_IRQHandler,
134     TIMER1_IRQHandler,
135     TIMER2_IRQHandler,
136     RTC0_IRQHandler,
137     TEMP_IRQHandler,
138     RNG_IRQHandler,
139     ECB_IRQHandler,
140     CCM_AAR_IRQHandler,
141     WDT_IRQHandler,
142     RTC1_IRQHandler,
143     QDEC_IRQHandler,
144     LPCOMP_IRQHandler,
145     SWI0_IRQHandler,
146     SWI1_IRQHandler,
147     SWI2_IRQHandler,
148     SWI3_IRQHandler,
149     SWI4_IRQHandler,
150     SWI5_IRQHandler
151 };
152