-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset_handler.c
56 lines (43 loc) · 1.32 KB
/
reset_handler.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
#include <stdint.h>
#include "kl17.h"
/* Values exported by the linker */
extern uint32_t _eflash;
extern uint32_t _sdtext;
extern uint32_t _edtext;
extern uint32_t _sbss;
extern uint32_t _ebss;
extern uint32_t __main_stack_end__;
/* Pointer to the Cortex vector table (located at offset 0) */
extern uint32_t *_vectors;
/* A place for the vector table to live while in RAM */
static uint32_t ram_vectors[64] __attribute__ ((aligned (1024)));
__attribute__ ((section(".startup")))
void memcpy32(uint32_t *src, uint32_t *dest, uint32_t count) {
count /= sizeof(*src);
while (count--)
*dest++ = *src++;
}
__attribute__ ((section(".startup")))
static void init_crt(void) {
/* Relocate data and text sections to RAM */
memcpy32(&_eflash, &_sdtext, (uint32_t)&_edtext - (uint32_t)&_sdtext);
/* Clear BSS */
uint32_t *dest = &_sbss;
while (dest < &_ebss) *dest++ = 0;
/* Copy IVT to RAM */
uint32_t *src = (uint32_t *) &_vectors;
dest = &ram_vectors[0];
while (dest <= &ram_vectors[63])
*dest++ = *src++;
/* Switch to IVT now located in RAM */
SCB->VTOR = (uint32_t) &ram_vectors[0];
}
extern void __early_init(void);
__attribute__ ((noreturn))
extern void bootloader_main(void);
__attribute__ ((section(".startup"), noreturn))
void Reset_Handler(void) {
init_crt();
__early_init();
bootloader_main();
}