-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32_startup.c
278 lines (252 loc) · 13.3 KB
/
stm32_startup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Author: Gabriel Rodgers
// Date: 6/30/2024
// this is the startup file for a simple program running on an
// STM32L476RGT6 MCU
//headers and macros
#include <stdint.h>
#define SRAM_START 0x20000000U
#define SRAM_SIZE (96U*1024U) //96 KB, SRAM1 = 96 KB, SRAM2 = 32 KB (should be 128 KB)
#define SRAM_END (SRAM_START + SRAM_SIZE)
#define STACK_START SRAM_END
#define GPIOA_ODR (*((volatile uint32_t *) (0x48000014)))
//use extern uint32_t to access symbols from linker script!
extern uint32_t _etext;
extern uint32_t _sdata;
extern uint32_t _edata;
extern uint32_t _la_data;
extern uint32_t _estack;
extern uint32_t _sbss;
extern uint32_t _ebss;
//main() prototype
int main(void);
//stdlib initialization function prototype
void __libc_init_array(void);
//isr declarations; weak allows later mods of NMI_handler function in .c source file
void Reset_Handler(void);
void NMI_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void HardFault_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void MemManage_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void BusFault_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void UsageFault_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void SVC_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void DebugMon_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void PendSV_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void SysTick_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
void WWDG_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void PVD_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TAMP_STAMP_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void RTC_WKUP_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void FLASH_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void RCC_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void EXTI0_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void EXTI1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void EXTI2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void EXTI3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void EXTI4_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream0_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream4_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream5_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream6_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA1_Stream7_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void ADC1_2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void CAN1_TX_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void CAN1_RX0_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void CAN1_RX1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void CAN1_SCE_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void EXTI9_5_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM1_BRK_TIM15_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM1_UP_TIM16_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM1_TRG_COM_TIM17_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM1_CC_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM4_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void I2C1_EV_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void I2C1_ER_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void I2C2_EV_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void I2C2_ER_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void SPI1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void SPI2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void USART1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void USART2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void USART3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void EXTI15_10_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void RTC_Alarm_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DFSDM1_FLT3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM8_BRK_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM8_UP_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM8_TRG_COM_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM8_CC_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void ADC3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void FMC_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void SDMMC1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM5_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void SPI3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void UART4_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void UART5_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM6_DACUNDER_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TIM7_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA2_Stream0_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA2_Stream1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA2_Stream2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA2_Stream3_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA2_Stream4_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DFSDM1_FLT0_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DFSDM1_FLT1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DFSDM1_FLT2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void COMP_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void LPTIM1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void LPTIM2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void OTG_FS_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA2_Stream5_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void DMA2_Stream6_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void LPUART1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void QUADSPI_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void I2C3_EV_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void SAI1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void SAI2_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void SWPMI1_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void TSC_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void LCD_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void RNG_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
void FPU_IRQHandler (void) __attribute__ ((weak, alias("Default_Handler")));
//vector table: starts at 0x0000 0000, ends at 0x0000 01A8
//0x0000 0000 - 0x0000 003C: system level exception handlers
//0x0000 0040 - 0x0000 01A8: interrupts (91 total)
//initialize the vector table; the __attribute__ stuff places the vector table
//in a new section called .isr_vector
uint32_t vectors[] __attribute__((section(".isr_vector"))) = {
STACK_START,
(uint32_t) Reset_Handler, //must typecast this
(uint32_t) NMI_Handler, //address of default_handler is stored here
(uint32_t) HardFault_Handler,
(uint32_t) MemManage_Handler,
(uint32_t) BusFault_Handler,
(uint32_t) UsageFault_Handler,
0,
0,
0,
0,
(uint32_t) SVC_Handler,
(uint32_t) DebugMon_Handler,
0,
(uint32_t) PendSV_Handler,
(uint32_t) SysTick_Handler,
(uint32_t) WWDG_IRQHandler,
(uint32_t) PVD_IRQHandler,
(uint32_t) TAMP_STAMP_IRQHandler,
(uint32_t) RTC_WKUP_IRQHandler,
(uint32_t) FLASH_IRQHandler,
(uint32_t) RCC_IRQHandler,
(uint32_t) EXTI0_IRQHandler,
(uint32_t) EXTI1_IRQHandler,
(uint32_t) EXTI2_IRQHandler,
(uint32_t) EXTI3_IRQHandler,
(uint32_t) EXTI4_IRQHandler,
(uint32_t) DMA1_Stream1_IRQHandler,
(uint32_t) DMA1_Stream2_IRQHandler,
(uint32_t) DMA1_Stream3_IRQHandler,
(uint32_t) DMA1_Stream4_IRQHandler,
(uint32_t) DMA1_Stream5_IRQHandler,
(uint32_t) DMA1_Stream6_IRQHandler,
(uint32_t) DMA1_Stream7_IRQHandler,
(uint32_t) ADC1_2_IRQHandler,
(uint32_t) CAN1_TX_IRQHandler,
(uint32_t) CAN1_RX0_IRQHandler,
(uint32_t) CAN1_RX1_IRQHandler,
(uint32_t) CAN1_SCE_IRQHandler,
(uint32_t) EXTI9_5_IRQHandler,
(uint32_t) TIM1_BRK_TIM15_IRQHandler,
(uint32_t) TIM1_UP_TIM16_IRQHandler,
(uint32_t) TIM1_TRG_COM_TIM17_IRQHandler,
(uint32_t) TIM1_CC_IRQHandler,
(uint32_t) TIM2_IRQHandler,
(uint32_t) TIM3_IRQHandler,
(uint32_t) TIM4_IRQHandler,
(uint32_t) I2C1_EV_IRQHandler,
(uint32_t) I2C1_ER_IRQHandler,
(uint32_t) I2C2_EV_IRQHandler,
(uint32_t) I2C2_ER_IRQHandler,
(uint32_t) SPI1_IRQHandler,
(uint32_t) SPI2_IRQHandler,
(uint32_t) USART1_IRQHandler,
(uint32_t) USART2_IRQHandler,
(uint32_t) USART3_IRQHandler,
(uint32_t) EXTI15_10_IRQHandler,
(uint32_t) RTC_Alarm_IRQHandler,
(uint32_t) DFSDM1_FLT3_IRQHandler,
(uint32_t) TIM8_BRK_IRQHandler,
(uint32_t) TIM8_UP_IRQHandler,
(uint32_t) TIM8_TRG_COM_IRQHandler,
(uint32_t) TIM8_CC_IRQHandler,
(uint32_t) ADC3_IRQHandler,
(uint32_t) FMC_IRQHandler,
(uint32_t) SDMMC1_IRQHandler,
(uint32_t) TIM5_IRQHandler,
(uint32_t) SPI3_IRQHandler,
(uint32_t) UART4_IRQHandler,
(uint32_t) UART5_IRQHandler,
(uint32_t) TIM6_DACUNDER_IRQHandler,
(uint32_t) TIM7_IRQHandler,
(uint32_t) DMA2_Stream0_IRQHandler,
(uint32_t) DMA2_Stream1_IRQHandler,
(uint32_t) DMA2_Stream2_IRQHandler,
(uint32_t) DMA2_Stream3_IRQHandler,
(uint32_t) DMA2_Stream4_IRQHandler,
(uint32_t) DFSDM1_FLT0_IRQHandler,
(uint32_t) DFSDM1_FLT1_IRQHandler,
(uint32_t) DFSDM1_FLT2_IRQHandler,
(uint32_t) COMP_IRQHandler,
(uint32_t) LPTIM1_IRQHandler,
(uint32_t) LPTIM2_IRQHandler,
(uint32_t) OTG_FS_IRQHandler,
(uint32_t) DMA2_Stream5_IRQHandler,
(uint32_t) DMA2_Stream6_IRQHandler,
(uint32_t) LPUART1_IRQHandler,
(uint32_t) QUADSPI_IRQHandler,
(uint32_t) I2C3_EV_IRQHandler,
(uint32_t) I2C3_EV_IRQHandler,
(uint32_t) SAI1_IRQHandler,
(uint32_t) SAI2_IRQHandler,
(uint32_t) SWPMI1_IRQHandler,
(uint32_t) TSC_IRQHandler,
(uint32_t) LCD_IRQHandler,
0,
(uint32_t) RNG_IRQHandler,
(uint32_t) FPU_IRQHandler,
}; //can also put __attribute__ stuff at the end of a definition/declaration
//isr definitions -- don't have to define all these exceptions, only the used
//ones; we can also create a default handler:
//dummy function
void Default_Handler(void){
while(1);
}
//this initializes the .data and .bss section
void Reset_Handler(void){
//initialize the stack pointer using inline asm
__asm__ ("ldr sp, =_estack");
//copy .data section to SRAM
//first find the size of the .data section, must typecast addresses corresponding to the symbols
uint32_t size = (uint32_t)&_edata - (uint32_t)&_sdata;
//pDst is a pointer to the destination of the data section (in SRAM)
uint8_t *pDst = (uint8_t*)&_sdata;
//pSrc is a pointer to the source of the data section (in FLASH)
uint8_t *pSrc = (uint8_t*)&_la_data;
for(uint32_t i=0; i < size; i++){
*pDst++ = *pSrc++;
}
//initialize the .bss section to zero in SRAM; first find the size of the .bss section
size = (uint32_t)&_ebss - (uint32_t)&_sbss;
//reassign pDst to the address of the start of the .bss section
pDst = (uint8_t*)&_sbss;
for(uint32_t i=0; i < size; i++){
*pDst++ = 0;
}
//call initialization function of stdlib
__libc_init_array();
//call main()
main();
}