Debian Codegraph @

ports/qemu/mcu/arm/startup.c

raw
1 /*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2018-2021 Damien P. George
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 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "py/runtime.h"
32
33 #include "shared/runtime/semihosting_arm.h"
34 #include "uart.h"
35
36 extern uint32_t _estack, _sidata, _sdata, _edata, _sbss, _ebss;
37
38 __attribute__((naked)) void Reset_Handler(void) {
39 // set stack pointer
40 __asm volatile ("ldr r0, =_estack");
41 __asm volatile ("mov sp, r0");
42 // copy .data section from flash to RAM
43 for (uint32_t *src = &_sidata, *dest = &_sdata; dest < &_edata;) {
44 *dest++ = *src++;
45 }
46 // zero out .bss section
47 for (uint32_t *dest = &_sbss; dest < &_ebss;) {
48 *dest++ = 0;
49 }
50 // jump to board initialisation
51 void _start(void);
52 _start();
53 }
54
55 MP_NORETURN void Default_Handler(void) {
56 for (;;) {
57 }
58 }
59
60 #if defined(__ARM_ARCH_ISA_ARM)
61
62 // ARM architecture with standard ARM ISA.
63
64 __attribute__((naked, section(".isr_vector"))) void isr_vector(void) {
65 __asm volatile (
66 "b Reset_Handler\n"
67 "b Default_Handler\n"
68 "b Default_Handler\n"
69 "b Default_Handler\n"
70 "b Default_Handler\n"
71 "nop\n"
72 "b Default_Handler\n"
73 "b Default_Handler\n"
74 );
75 }
76
77 #elif defined(__ARM_ARCH_ISA_THUMB)
78
79 extern void NMI_Handler(void);
80 extern void HardFault_Handler(void);
81 extern void MemManage_Handler(void);
82 extern void BusFault_Handler(void);
83 extern void UsageFault_Handler(void);
84 extern void SVC_Handler(void);
85 extern void DebugMon_Handler(void);
86 extern void PendSV_Handler(void);
87 extern void SysTick_Handler(void);
88
89 // ARM architecture with Thumb-only ISA.
90
91 const uint32_t isr_vector[] __attribute__((section(".isr_vector"))) = {
92 (uint32_t)&_estack,
93 (uint32_t)&Reset_Handler,
94 (uint32_t)&NMI_Handler,
95 (uint32_t)&HardFault_Handler,
96 (uint32_t)&MemManage_Handler,
97 (uint32_t)&BusFault_Handler,
98 (uint32_t)&UsageFault_Handler,
99 0,
100 0,
101 0,
102 0,
103 (uint32_t)&SVC_Handler,
104 (uint32_t)&DebugMon_Handler,
105 0,
106 (uint32_t)&PendSV_Handler,
107 (uint32_t)&SysTick_Handler,
108 };
109
110 #endif
111
112 void _start(void) {
113 mp_semihosting_init();
114
115 // Enable the UART
116 uart_init();
117
118 // Now that we have a basic system up and running we can call main
119 extern int main();
120 main(0, 0);
121
122 // Finished
123 exit(0);
124 }
125
126 void exit(int status) {
127 // Force qemu to exit using ARM Semihosting
128 mp_semihosting_exit(status);
129 for (;;) {
130 }
131 }
132
133 #ifndef NDEBUG
134 void __assert_func(const char *file, int line, const char *func, const char *expr) {
135 (void)func;
136 printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
137 exit(1);
138 }
139 #endif