• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 HPMicro
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <stdint.h>
7 #include "hpm_common.h"
8 #include "hpm_soc.h"
9 #include "hpm_l1c_drv.h"
10 #include "hpm_interrupt.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 extern void system_init(void);
16 extern void __libc_init_array(void);
17 extern void __libc_fini_array(void);
18 #ifdef __cplusplus
19 }
20 #endif
21 
_clean_up(void)22 __attribute__((weak)) void _clean_up(void)
23 {
24     /* clean up plic, it will help while debuging */
25     disable_irq_from_intc();
26     intc_m_set_threshold(0);
27     for (uint32_t irq = 0; irq < 128; irq++) {
28         intc_m_complete_irq(irq);
29     }
30     /* clear any bits left in plic enable regster */
31     for (uint32_t i = 0; i < 4; i++) {
32         *(volatile uint32_t *)(HPM_PLIC_BASE + HPM_PLIC_ENABLE_OFFSET + (i << 2)) = 0;
33     }
34 }
35 
c_startup(void)36 __attribute__((weak)) void c_startup(void)
37 {
38     uint32_t i, size;
39 #if defined(FLASH_XIP) || defined(FLASH_UF2)
40     extern uint8_t __vector_ram_start__[], __vector_ram_end__[], __vector_load_addr__[];
41     size = __vector_ram_end__ - __vector_ram_start__;
42     for (i = 0; i < size; i++) {
43         *(__vector_ram_start__ + i) = *(__vector_load_addr__ + i);
44     }
45 #endif
46 
47     extern uint8_t __etext[];
48     extern uint8_t __bss_start__[], __bss_end__[];
49     extern uint8_t __data_start__[], __data_end__[];
50     extern uint8_t __noncacheable_bss_start__[], __noncacheable_bss_end__[];
51     extern uint8_t __ramfunc_start__[], __ramfunc_end__[];
52     extern uint8_t __noncacheable_init_start__[], __noncacheable_init_end__[];
53 
54     /* bss section */
55     size = __bss_end__ - __bss_start__;
56     for (i = 0; i < size; i++) {
57         *(__bss_start__ + i) = 0;
58     }
59 
60     /* noncacheable bss section */
61     size = __noncacheable_bss_end__ - __noncacheable_bss_start__;
62     for (i = 0; i < size; i++) {
63         *(__noncacheable_bss_start__ + i) = 0;
64     }
65 
66     /* data section LMA: etext */
67     size = __data_end__ - __data_start__;
68     for (i = 0; i < size; i++) {
69         *(__data_start__ + i) = *(__etext + i);
70     }
71 
72     /* ramfunc section LMA: etext + data length */
73     size = __ramfunc_end__ - __ramfunc_start__;
74     for (i = 0; i < size; i++) {
75         *(__ramfunc_start__ + i) = *(__etext + (__data_end__ - __data_start__) + i);
76     }
77 
78     /* noncacheable init section LMA: etext + data length + ramfunc legnth */
79     size = __noncacheable_init_end__ - __noncacheable_init_start__;
80     for (i = 0; i < size; i++) {
81         *(__noncacheable_init_start__ + i) = *(__etext + (__data_end__ - __data_start__) + (__ramfunc_end__ - __ramfunc_start__) + i);
82     }
83 }
84 
main(void)85 __attribute__((weak)) int main(void)
86 {
87     while (1) {
88     }
89 }
90 
reset_handler(void)91 __attribute__((weak)) void reset_handler(void)
92 {
93     l1c_dc_disable();
94     l1c_dc_invalidate_all();
95 #if !defined(__SEGGER_RTL_VERSION) || defined(__GNU_LINKER)
96     /*
97      * Initialize LMA/VMA sections.
98      * Relocation for any sections that need to be copied from LMA to VMA.
99      */
100     c_startup();
101 #endif
102 
103     /* Call platform specific hardware initialization */
104     system_init();
105 
106 #if !defined(__SEGGER_RTL_VERSION) || defined(__GNU_LINKER)
107     /* Do global constructors */
108     //__libc_init_array();
109 #endif
110 
111     /* Entry function */
112     main();
113 }
114 
115 /*
116  * When compiling C++ code with static objects, the compiler inserts
117  * a call to __cxa_atexit() with __dso_handle as one of the arguments.
118  * The dummy versions of these symbols should be provided.
119  */
__cxa_atexit(void (* arg1)(void *),void * arg2,void * arg3)120 void __cxa_atexit(void (*arg1)(void *), void *arg2, void *arg3)
121 {
122 }
123 
124 #ifndef __SEGGER_RTL_VERSION
125 void *__dso_handle = (void *) &__dso_handle;
126 #endif
127 
_init(void)128 __attribute__((weak)) void _init(void)
129 {
130 }
131