embkernel
 All Classes Functions Variables Typedefs Groups Pages
RtosStartup.cpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #include "RtosInterrupt.hpp"
7 #include <stdint.h>
8 
9 extern "C" void entryPoint(void);
10 extern "C" void __libc_init_array(void);
11 extern int main(void);
12 
13 #ifndef RTOS_CFG_MAIN_STACK_SIZE
14 #define RTOS_CFG_MAIN_STACK_SIZE 128
15 #endif
16 volatile static unsigned long mainStack[RTOS_CFG_MAIN_STACK_SIZE] __attribute__ ((section(".stack")));
17 
18 __attribute__ ((section(".isr_vector"), used)) uint32_t vectorsTable[] = {
19 //CM3 default handlers
20  ((uint32_t) mainStack) + sizeof(mainStack), // 00: The initial stack pointer
21  (uint32_t) entryPoint, //
22  (uint32_t) RtosInterrupt::IRQ_NonMaskableInt, //
23  (uint32_t) RtosInterrupt::IRQ_HardFault, //
24  (uint32_t) RtosInterrupt::IRQ_MemoryManagement, //
25  (uint32_t) RtosInterrupt::IRQ_BusFault, //
26  (uint32_t) RtosInterrupt::IRQ_UsageFault, //
27  (uint32_t) 0, // 07: Reserved
28  (uint32_t) 0, // 08: Reserved
29  (uint32_t) 0, // 09: Reserved
30  (uint32_t) 0, // 10: Reserved
31  (uint32_t) RtosInterrupt::IRQ_SVCall, //
32  (uint32_t) RtosInterrupt::IRQ_DebugMonitor, //
33  (uint32_t) 0, // 13: Reserved
34  (uint32_t) RtosInterrupt::IRQ_PendSV, //
35  (uint32_t) RtosInterrupt::IRQ_SysTick, //
36 
38  (uint32_t) RtosInterrupt::IRQ_WWDG, //
39  (uint32_t) RtosInterrupt::IRQ_PVD, //
40  (uint32_t) RtosInterrupt::IRQ_TAMPER_STAMP, //
41  (uint32_t) RtosInterrupt::IRQ_RTC_WKUP, //
42  (uint32_t) RtosInterrupt::IRQ_FLASH, //
43  (uint32_t) RtosInterrupt::IRQ_RCC, //
44  (uint32_t) RtosInterrupt::IRQ_EXTI0, //
45  (uint32_t) RtosInterrupt::IRQ_EXTI1, //
46  (uint32_t) RtosInterrupt::IRQ_EXTI2_TS, //
47  (uint32_t) RtosInterrupt::IRQ_EXTI3, //
48  (uint32_t) RtosInterrupt::IRQ_EXTI4, //
49  (uint32_t) RtosInterrupt::IRQ_DMA1_Channel1, //
50  (uint32_t) RtosInterrupt::IRQ_DMA1_Channel2, //
51  (uint32_t) RtosInterrupt::IRQ_DMA1_Channel3, //
52  (uint32_t) RtosInterrupt::IRQ_DMA1_Channel4, //
53  (uint32_t) RtosInterrupt::IRQ_DMA1_Channel5, //
54  (uint32_t) RtosInterrupt::IRQ_DMA1_Channel6, //
55  (uint32_t) RtosInterrupt::IRQ_DMA1_Channel7, //
56  (uint32_t) RtosInterrupt::IRQ_ADC1_2, //
57  (uint32_t) RtosInterrupt::IRQ_USB_HP_CAN1_TX, //
58  (uint32_t) RtosInterrupt::IRQ_USB_LP_CAN1_RX0, //
59  (uint32_t) RtosInterrupt::IRQ_CAN1_RX1, //
60  (uint32_t) RtosInterrupt::IRQ_CAN1_SCE, //
61  (uint32_t) RtosInterrupt::IRQ_EXTI9_5, //
62  (uint32_t) RtosInterrupt::IRQ_TIM1_BRK_TIM15, //
63  (uint32_t) RtosInterrupt::IRQ_TIM1_UP_TIM16, //
64  (uint32_t) RtosInterrupt::IRQ_TIM1_TRG_COM_TIM17, //
65  (uint32_t) RtosInterrupt::IRQ_TIM1_CC, //
66  (uint32_t) RtosInterrupt::IRQ_TIM2, //
67  (uint32_t) RtosInterrupt::IRQ_TIM3, //
68  (uint32_t) RtosInterrupt::IRQ_TIM4, //
69  (uint32_t) RtosInterrupt::IRQ_I2C1_EV, //
70  (uint32_t) RtosInterrupt::IRQ_I2C1_ER, //
71  (uint32_t) RtosInterrupt::IRQ_I2C2_EV, //
72  (uint32_t) RtosInterrupt::IRQ_I2C2_ER, //
73  (uint32_t) RtosInterrupt::IRQ_SPI1, //
74  (uint32_t) RtosInterrupt::IRQ_SPI2, //
75  (uint32_t) RtosInterrupt::IRQ_USART1, //
76  (uint32_t) RtosInterrupt::IRQ_USART2, //
77  (uint32_t) RtosInterrupt::IRQ_USART3, //
78  (uint32_t) RtosInterrupt::IRQ_EXTI15_10, //
79  (uint32_t) RtosInterrupt::IRQ_RTC_Alarm, //
80  (uint32_t) RtosInterrupt::IRQ_USBWakeUp, //
81  (uint32_t) RtosInterrupt::IRQ_TIM8_BRK, //
82  (uint32_t) RtosInterrupt::IRQ_TIM8_UP, //
83  (uint32_t) RtosInterrupt::IRQ_TIM8_TRG_COM, //
84  (uint32_t) RtosInterrupt::IRQ_TIM8_CC, //
85  (uint32_t) RtosInterrupt::IRQ_ADC3, //
86  0, //Reserved
87  0, //Reserved
88  0, //Reserved
89  (uint32_t) RtosInterrupt::IRQ_SPI3, //
90  (uint32_t) RtosInterrupt::IRQ_UART4, //
91  (uint32_t) RtosInterrupt::IRQ_UART5, //
92  (uint32_t) RtosInterrupt::IRQ_TIM6_DAC, //
93  (uint32_t) RtosInterrupt::IRQ_TIM7, //
94  (uint32_t) RtosInterrupt::IRQ_DMA2_Channel1, //
95  (uint32_t) RtosInterrupt::IRQ_DMA2_Channel2, //
96  (uint32_t) RtosInterrupt::IRQ_DMA2_Channel3, //
97  (uint32_t) RtosInterrupt::IRQ_DMA2_Channel4, //
98  (uint32_t) RtosInterrupt::IRQ_DMA2_Channel5, //
99  (uint32_t) RtosInterrupt::IRQ_ADC4, //
100  0, //Reserved
101  0, //Reserved
102  (uint32_t) RtosInterrupt::IRQ_COMP1_2_3, //
103  (uint32_t) RtosInterrupt::IRQ_COMP4_5_6, //
104  (uint32_t) RtosInterrupt::IRQ_COMP7, //
105  0, //Reserved
106  0, //Reserved
107  0, //Reserved
108  0, //Reserved
109  0, //Reserved
110  0, //Reserved
111  0, //Reserved
112  (uint32_t) RtosInterrupt::IRQ_USB_HP, //
113  (uint32_t) RtosInterrupt::IRQ_USB_LP, //
114  (uint32_t) RtosInterrupt::IRQ_USBWakeUp_RMP, //
115  0, //Reserved
116  0, //Reserved
117  0, //Reserved
118  0, //Reserved
119  (uint32_t) RtosInterrupt::IRQ_FPU, //
120  };
121 
122 extern unsigned long _flash_data;
123 extern unsigned long _data;
124 extern unsigned long _edata;
125 extern unsigned long _bss;
126 extern unsigned long _ebss;
127 extern unsigned long _ccram_flash_data;
128 extern unsigned long _ccram_data;
129 extern unsigned long _ccram_edata;
130 
131 __attribute__ ((naked,used)) void entryPoint(void) {
132 
133  //To remove warnings
134  (void) _flash_data;
135  (void) _data;
136  (void) _edata;
137  (void) _bss;
138  (void) _ebss;
139  (void) _ccram_flash_data;
140  (void) _ccram_data;
141  (void) _ccram_edata;
142 
143  // Copy the data from flash to SRAM.
144  __asm(
145  " .thumb_func \n"
146  " ldr r0, =_flash_data \n"
147  " ldr r1, =_data \n"
148  " ldr r2, =_edata \n"
149  "loopData: \n"
150  " cmp r1, r2 \n"
151  " it eq \n"
152  " beq end \n"
153  " ldr r3, [r0], #4 \n"
154  " str r3, [r1], #4 \n"
155  " blt loopData \n"
156  "end: \n"
157  :::"r0","r1","r2","r3");
158 
159  // Zero fill the bss segment
160  __asm(
161  " .thumb_func \n"
162  " ldr r0, =_bss \n"
163  " ldr r1, =_ebss \n"
164  " mov r2, #0 \n"
165  "loopBss: \n"
166  " cmp r0, r1 \n"
167  " it lt \n"
168  " strlt r2, [r0], #4 \n"
169  " blt loopBss \n"
170  :::"r0","r1","r2");
171 
172  // Restore stack pointer
173  __asm(
174  " .thumb_func \n"
175  " ldr r13, =vectorsTable \n"
176  " ldr r13, [r13] \n"
177  :::);
178 
179  //Call the global objets constructors
180  __libc_init_array();
181 
182  // Call the application's entry point.
183  main();
184 }