YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
reset_rp2040.S
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// Low-level reset code for the RP2040 Launchpad
15//
16// This code will
17// - Check wether the image is a COPY_TO_xxx image, and relocates
18// the image if necessary
19// - Sets the VTOR register
20// - Jumps to the C++-part of the reset routine (Reset_Handler)
21//
22// The following boot scenarios for the RP2040 Launchpad are supported:
23//
24// 1.) Normal flash image
25// OpenOcd upload: Works, target is reset and enters reset_handler
26// via bootrom and boot2.
27// OpenOcd debug: Works, targer is reset and enters reset_handler
28// via bootrom and boot2.
29// UF2 upload: Works
30//
31// 2.) Image linked with COPY_TO_RAM
32// OpenOcd upload: Works, target is reset and enters reset_handler
33// via bootrom and boot2.
34// OpenOcd debug: Works, targer is reset and enters reset_handler
35// via bootrom and boot2.
36// UF2 upload: Works
37//
38// 3.) Images linked with LOAD_INTO_RAM
39// OpenOcd upload: Works, target is not reset and resumes at 0x20000001.
40// OpenOcd debug: Needs special debugger setup, reset is not allowed.
41// UF2 upload: Works
42
43#define VTOR_REG 0xe000ed08
44
45 .syntax unified
46 .thumb
47 .section .reset,"ax"
48 .global reset_handler
49 .global _elf_entry_point
50
51 // This is a fake vector table to start the
52 // reset_handler, in case this code is put right
53 // at the beginning of the flash (0x10000000).
54 .word __StackTop
55 .word reset_handler
56
57 .thumb_func
58reset_handler:
59
60 // Check if we have to relocate the image
61 ldr r0, =__image_load__
62 ldr r1, =__image_start__
63 cmp r0, r1
64 beq no_copy
65
66copy_loop:
67 // Copy the complete image (.text, .rodata, .data)
68 // to the runtime memory area in RAM
69 ldr r2, =__image_end__
70 ldmia r0!, {r3}
71 stmia r1!, {r3}
72 cmp r1, r2
73 blo copy_loop
74
75no_copy:
76 // Setup stack pointer, because the C-part
77 // of the reset handler will use it!!
78 ldr r0, =__StackTop
79 msr msp, r0
80
81 // Set the ISR vector offset register (VTOR)
82 ldr r0, =__vector_start__
83 ldr r1, =VTOR_REG
84 str r0, [r1]
85
86 // Finally jump to the C++-based part of
87 // the reset handler, which will initialize
88 // the system and jump to main(). Use a long
89 // jump here in case the jump target is in RAM
90 ldr r0, =Reset_Handler
91 blx r0
92
93endless:
94 // We should never get here, but just in case
95 // run into an endless loop
96 b endless
97
98
99 // If the image is entered via the _elf_entry_point,
100 // take a loop through the bootrom, to make sure the
101 // flash and other related things are initialized
102 .thumb_func
103_elf_entry_point:
104 // Set the ISR vector offset register (VTOR)
105 movs r0, #0
106 ldr r1, =VTOR_REG
107 str r0, [r1]
108
109 // r1=initial SP, r2=reset handler of bootrom
110 ldmia r0!, {r1, r2}
111 msr msp, r1
112 blx r2
113
114 // We should never get here, but just in case
115 // run into an endless loop
116 b endless
117
118 .end