| 1 |
;- ---------------------------------------------------------------------------- |
| 2 |
;- ATMEL Microcontroller Software Support - ROUSSET - |
| 3 |
;- ---------------------------------------------------------------------------- |
| 4 |
;- DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 5 |
;- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 6 |
;- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 7 |
;- DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 8 |
;- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 9 |
;- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 10 |
;- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 11 |
;- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 12 |
;- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 13 |
;- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 14 |
;- ---------------------------------------------------------------------------- |
| 15 |
;- File source : Cstartup.s79 |
| 16 |
;- Object : Generic CStartup |
| 17 |
;- 1.0 01/Sep/05 FBr : Creation |
| 18 |
;- 1.1 09/Sep/05 JPP : Change Interrupt management |
| 19 |
;------------------------------------------------------------------------------ |
| 20 |
|
| 21 |
;------------------------------------------------------------------------------ |
| 22 |
; Include your AT91 Library files |
| 23 |
;------------------------------------------------------------------------------ |
| 24 |
#include "AT91SAM7X256_inc.h" |
| 25 |
;------------------------------------------------------------------------------ |
| 26 |
|
| 27 |
;------------------------------------------------------------------------------ |
| 28 |
; ?RESET |
| 29 |
; Reset Vector. |
| 30 |
; Normally, segment INTVEC is linked at address 0. |
| 31 |
; For debugging purposes, INTVEC may be placed at other addresses. |
| 32 |
; A debugger that honors the entry point will start the |
| 33 |
; program in a normal way even if INTVEC is not at address 0. |
| 34 |
;------------------------------------------------------------------------------ |
| 35 |
|
| 36 |
PROGRAM ?RESET ;- Begins a program module |
| 37 |
RSEG INTRAMEND_REMAP ;- Begins a relocatable segment |
| 38 |
RSEG ICODE:CODE (2) ;- Begins a relocatable segment : corresponding address is 32-bit aligned |
| 39 |
CODE32 ;- Always ARM mode after reset |
| 40 |
ORG 0 ;- Sets the location counter: corresponds to the RESET vector address |
| 41 |
|
| 42 |
;------------------------------------------------------------------------------ |
| 43 |
;- Exception vectors |
| 44 |
;------------------------------------------------------------------------------ |
| 45 |
;- These vectors can be read at address 0 or at RAM address |
| 46 |
;- They ABSOLUTELY requires to be in relative addresssing mode in order to |
| 47 |
;- guarantee a valid jump. For the moment, all are just looping. |
| 48 |
;- If an exception occurs before remap, this would result in an infinite loop. |
| 49 |
;- To ensure if a exeption occurs before start application to infinite loop. |
| 50 |
;------------------------------------------------------------------------------ |
| 51 |
|
| 52 |
reset |
| 53 |
B InitReset ; 0x00 Reset handler |
| 54 |
undefvec: |
| 55 |
B undefvec ; 0x04 Undefined Instruction |
| 56 |
swivec: |
| 57 |
B swivec ; 0x08 Software Interrupt |
| 58 |
pabtvec: |
| 59 |
B pabtvec ; 0x0C Prefetch Abort |
| 60 |
dabtvec: |
| 61 |
B dabtvec ; 0x10 Data Abort |
| 62 |
rsvdvec: |
| 63 |
B rsvdvec ; 0x14 reserved |
| 64 |
irqvec: |
| 65 |
B IRQ_Handler_Entry ; 0x18 IRQ |
| 66 |
|
| 67 |
fiqvec: ; 0x1c FIQ |
| 68 |
;------------------------------------------------------------------------------ |
| 69 |
;- Function : FIQ_Handler_Entry |
| 70 |
;- Treatments : FIQ Controller Interrupt Handler. |
| 71 |
;- Called Functions : AIC_FVR[interrupt] |
| 72 |
;------------------------------------------------------------------------------ |
| 73 |
|
| 74 |
FIQ_Handler_Entry: |
| 75 |
|
| 76 |
;- Switch in SVC/User Mode to allow User Stack access for C code |
| 77 |
; because the FIQ is not yet acknowledged |
| 78 |
|
| 79 |
;- Save and r0 in FIQ_Register |
| 80 |
mov r9,r0 |
| 81 |
ldr r0 , [r8, #AIC_FVR] |
| 82 |
msr CPSR_c,#I_BIT | F_BIT | ARM_MODE_SVC |
| 83 |
;- Save scratch/used registers and LR in User Stack |
| 84 |
stmfd sp!, { r1-r3, r12, lr} |
| 85 |
|
| 86 |
;- Branch to the routine pointed by the AIC_FVR |
| 87 |
mov r14, pc |
| 88 |
bx r0 |
| 89 |
|
| 90 |
;- Restore scratch/used registers and LR from User Stack |
| 91 |
ldmia sp!, { r1-r3, r12, lr} |
| 92 |
|
| 93 |
;- Leave Interrupts disabled and switch back in FIQ mode |
| 94 |
msr CPSR_c, #I_BIT | F_BIT | ARM_MODE_FIQ |
| 95 |
|
| 96 |
;- Restore the R0 ARM_MODE_SVC register |
| 97 |
mov r0,r9 |
| 98 |
|
| 99 |
;- Restore the Program Counter using the LR_fiq directly in the PC |
| 100 |
subs pc,lr,#4 |
| 101 |
|
| 102 |
;------------------------------------------------------------------------------ |
| 103 |
;- Manage exception: The exception must be ensure in ARM mode |
| 104 |
;------------------------------------------------------------------------------ |
| 105 |
;------------------------------------------------------------------------------ |
| 106 |
;- Function : IRQ_Handler_Entry |
| 107 |
;- Treatments : IRQ Controller Interrupt Handler. |
| 108 |
;- Called Functions : AIC_IVR[interrupt] |
| 109 |
;------------------------------------------------------------------------------ |
| 110 |
IRQ_Handler_Entry: |
| 111 |
|
| 112 |
;------------------------- |
| 113 |
;- Manage Exception Entry |
| 114 |
;------------------------- |
| 115 |
;- Adjust and save LR_irq in IRQ stack |
| 116 |
sub lr, lr, #4 |
| 117 |
stmfd sp!, {lr} |
| 118 |
|
| 119 |
;- Save r0 and SPSR (need to be saved for nested interrupt) |
| 120 |
mrs r14, SPSR |
| 121 |
stmfd sp!, {r0,r14} |
| 122 |
|
| 123 |
;- Write in the IVR to support Protect Mode |
| 124 |
;- No effect in Normal Mode |
| 125 |
;- De-assert the NIRQ and clear the source in Protect Mode |
| 126 |
ldr r14, =AT91C_BASE_AIC |
| 127 |
ldr r0 , [r14, #AIC_IVR] |
| 128 |
str r14, [r14, #AIC_IVR] |
| 129 |
|
| 130 |
;- Enable Interrupt and Switch in Supervisor Mode |
| 131 |
msr CPSR_c, #ARM_MODE_SVC |
| 132 |
|
| 133 |
;- Save scratch/used registers and LR in User Stack |
| 134 |
stmfd sp!, { r1-r3, r12, r14} |
| 135 |
|
| 136 |
;---------------------------------------------- |
| 137 |
;- Branch to the routine pointed by the AIC_IVR |
| 138 |
;---------------------------------------------- |
| 139 |
mov r14, pc |
| 140 |
bx r0 |
| 141 |
|
| 142 |
;---------------------------------------------- |
| 143 |
;- Manage Exception Exit |
| 144 |
;---------------------------------------------- |
| 145 |
;- Restore scratch/used registers and LR from User Stack |
| 146 |
ldmia sp!, { r1-r3, r12, r14} |
| 147 |
|
| 148 |
;- Disable Interrupt and switch back in IRQ mode |
| 149 |
msr CPSR_c, #I_BIT | ARM_MODE_IRQ |
| 150 |
|
| 151 |
;- Mark the End of Interrupt on the AIC |
| 152 |
ldr r14, =AT91C_BASE_AIC |
| 153 |
str r14, [r14, #AIC_EOICR] |
| 154 |
|
| 155 |
;- Restore SPSR_irq and r0 from IRQ stack |
| 156 |
ldmia sp!, {r0,r14} |
| 157 |
msr SPSR_cxsf, r14 |
| 158 |
|
| 159 |
;- Restore adjusted LR_irq from IRQ stack directly in the PC |
| 160 |
ldmia sp!, {pc}^ |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
InitReset: |
| 165 |
|
| 166 |
;------------------------------------------------------------------------------ |
| 167 |
;- Low level Init is performed in a C function: AT91F_LowLevelInit |
| 168 |
;- Init Stack Pointer to a valid memory area before calling AT91F_LowLevelInit |
| 169 |
;------------------------------------------------------------------------------ |
| 170 |
|
| 171 |
;- Retrieve end of RAM address |
| 172 |
__iramend EQU SFB(INTRAMEND_REMAP) ;- Segment begin |
| 173 |
|
| 174 |
EXTERN AT91F_LowLevelInit |
| 175 |
ldr r13,=__iramend ;- Temporary stack in internal RAM for Low Level Init execution |
| 176 |
ldr r0,=AT91F_LowLevelInit |
| 177 |
mov lr, pc |
| 178 |
bx r0 ;- Branch on C function (with interworking) |
| 179 |
|
| 180 |
;------------------------------------------------------------------------------ |
| 181 |
;- Top of Stack Definition |
| 182 |
;------------------------------------------------------------------------------ |
| 183 |
;- Interrupt and Supervisor Stack are located at the top of internal memory in |
| 184 |
;- order to speed the exception handling context saving and restoring. |
| 185 |
;- ARM_MODE_SVC (Application, C) Stack is located at the top of the external memory. |
| 186 |
;------------------------------------------------------------------------------ |
| 187 |
|
| 188 |
IRQ_STACK_SIZE EQU (3*8*4) ; 3 words to be saved per interrupt priority level |
| 189 |
ARM_MODE_FIQ EQU 0x11 |
| 190 |
ARM_MODE_IRQ EQU 0x12 |
| 191 |
ARM_MODE_SVC EQU 0x13 |
| 192 |
I_BIT EQU 0x80 |
| 193 |
F_BIT EQU 0x40 |
| 194 |
|
| 195 |
;------------------------------------------------------------------------------ |
| 196 |
;- Setup the stack for each mode |
| 197 |
;------------------------------------------------------------------------------ |
| 198 |
ldr r0, =__iramend |
| 199 |
|
| 200 |
;- Set up Fast Interrupt Mode and set FIQ Mode Stack |
| 201 |
msr CPSR_c, #ARM_MODE_FIQ | I_BIT | F_BIT |
| 202 |
;- Init the FIQ register |
| 203 |
ldr r8, =AT91C_BASE_AIC |
| 204 |
|
| 205 |
;- Set up Interrupt Mode and set IRQ Mode Stack |
| 206 |
msr CPSR_c, #ARM_MODE_IRQ | I_BIT | F_BIT |
| 207 |
mov r13, r0 ; Init stack IRQ |
| 208 |
sub r0, r0, #IRQ_STACK_SIZE |
| 209 |
|
| 210 |
;- Enable interrupt & Set up Supervisor Mode and set Supervisor Mode Stack |
| 211 |
msr CPSR_c, #ARM_MODE_SVC |
| 212 |
mov r13, r0 |
| 213 |
|
| 214 |
;------------------------------------------------------------------------------ |
| 215 |
; Initialize segments. |
| 216 |
;------------------------------------------------------------------------------ |
| 217 |
; __segment_init is assumed to use |
| 218 |
; instruction set and to be reachable by BL from the ICODE segment |
| 219 |
; (it is safest to link them in segment ICODE). |
| 220 |
;------------------------------------------------------------------------------ |
| 221 |
EXTERN __segment_init |
| 222 |
ldr r0,=__segment_init |
| 223 |
mov lr, pc |
| 224 |
bx r0 |
| 225 |
|
| 226 |
;------------------------------------------------------------------------------ |
| 227 |
;- Branch on C code Main function (with interworking) |
| 228 |
;------------------------------------------------------------------------------ |
| 229 |
EXTERN main |
| 230 |
PUBLIC __main |
| 231 |
?jump_to_main: |
| 232 |
ldr lr,=?call_exit |
| 233 |
ldr r0,=main |
| 234 |
__main: |
| 235 |
bx r0 |
| 236 |
|
| 237 |
;------------------------------------------------------------------------------ |
| 238 |
;- Loop for ever |
| 239 |
;------------------------------------------------------------------------------ |
| 240 |
;- End of application. Normally, never occur. |
| 241 |
;- Could jump on Software Reset ( B 0x0 ). |
| 242 |
;------------------------------------------------------------------------------ |
| 243 |
?call_exit: |
| 244 |
End |
| 245 |
b End |
| 246 |
|
| 247 |
;------------------------------------------------------------------------------ |
| 248 |
;- Exception Vectors |
| 249 |
;------------------------------------------------------------------------------ |
| 250 |
PUBLIC AT91F_Default_FIQ_handler |
| 251 |
PUBLIC AT91F_Default_IRQ_handler |
| 252 |
PUBLIC AT91F_Spurious_handler |
| 253 |
|
| 254 |
CODE32 ; Always ARM mode after exeption |
| 255 |
|
| 256 |
AT91F_Default_FIQ_handler |
| 257 |
b AT91F_Default_FIQ_handler |
| 258 |
|
| 259 |
AT91F_Default_IRQ_handler |
| 260 |
b AT91F_Default_IRQ_handler |
| 261 |
|
| 262 |
AT91F_Spurious_handler |
| 263 |
b AT91F_Spurious_handler |
| 264 |
|
| 265 |
ENDMOD ;- Terminates the assembly of the current module |
| 266 |
END ;- Terminates the assembly of the last module in a file |