start_iar.S 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ;/*
  2. ; * File : start.S
  3. ; * This file is part of RT-Thread RTOS
  4. ; * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. ; *
  6. ; * This program is free software; you can redistribute it and/or modify
  7. ; * it under the terms of the GNU General Public License as published by
  8. ; * the Free Software Foundation; either version 2 of the License, or
  9. ; * (at your option) any later version.
  10. ; *
  11. ; * This program is distributed in the hope that it will be useful,
  12. ; * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ; * GNU General Public License for more details.
  15. ; *
  16. ; * You should have received a copy of the GNU General Public License along
  17. ; * with this program; if not, write to the Free Software Foundation, Inc.,
  18. ; * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. ; *
  20. ; * Change Logs:
  21. ; * Date Author Notes
  22. ; * 2011-01-13 weety first version
  23. ; * 2015-04-15 ArdaFu Split from AT91SAM9260 BSP
  24. ; * 2015-04-21 ArdaFu Remove remap code. Using mmu to map vector table
  25. ; */
  26. #define S_FRAME_SIZE (18*4) ;72
  27. ;#define S_SPSR (17*4) ;SPSR
  28. ;#define S_CPSR (16*4) ;CPSR
  29. #define S_PC (15*4) ;R15
  30. ;#define S_LR (14*4) ;R14
  31. ;#define S_SP (13*4) ;R13
  32. ;#define S_IP (12*4) ;R12
  33. ;#define S_FP (11*4) ;R11
  34. ;#define S_R10 (10*4)
  35. ;#define S_R9 (9*4)
  36. ;#define S_R8 (8*4)
  37. ;#define S_R7 (7*4)
  38. ;#define S_R6 (6*4)
  39. ;#define S_R5 (5*4)
  40. ;#define S_R4 (4*4)
  41. ;#define S_R3 (3*4)
  42. ;#define S_R2 (2*4)
  43. ;#define S_R1 (1*4)
  44. ;#define S_R0 (0*4)
  45. #define MODE_SYS 0x1F
  46. #define MODE_FIQ 0x11
  47. #define MODE_IRQ 0x12
  48. #define MODE_SVC 0x13
  49. #define MODE_ABT 0x17
  50. #define MODE_UND 0x1B
  51. #define MODEMASK 0x1F
  52. #define NOINT 0xC0
  53. #include "rt_low_level_iar.inc"
  54. ;----------------------- Stack and Heap Definitions ----------------------------
  55. MODULE ?cstartup
  56. SECTION .noinit:DATA:NOROOT(3)
  57. DATA
  58. DS8 UND_STK_SIZE
  59. PUBLIC UND_STACK_START
  60. UND_STACK_START:
  61. ALIGNRAM 2
  62. DS8 ABT_STK_SIZE
  63. PUBLIC ABT_STACK_START
  64. ABT_STACK_START:
  65. ALIGNRAM 2
  66. DS8 FIQ_STK_SIZE
  67. PUBLIC FIQ_STACK_START
  68. FIQ_STACK_START:
  69. ALIGNRAM 2
  70. DS8 IRQ_STK_SIZE
  71. PUBLIC IRQ_STACK_START
  72. IRQ_STACK_START:
  73. ALIGNRAM 2
  74. DS8 SYS_STK_SIZE
  75. PUBLIC SYS_STACK_START
  76. SYS_STACK_START:
  77. ALIGNRAM 2
  78. DS8 SVC_STK_SIZE
  79. PUBLIC SVC_STACK_START
  80. SVC_STACK_START:
  81. ;--------------Jump vector table------------------------------------------------
  82. SECTION .intvec:CODE:ROOT(2)
  83. ARM
  84. PUBLIC Entry_Point
  85. Entry_Point:
  86. __iar_init$$done: ; The interrupt vector is not needed
  87. ; until after copy initialization is done
  88. LDR PC, vector_reset
  89. LDR PC, vector_undef
  90. LDR PC, vector_swi
  91. LDR PC, vector_pabt
  92. LDR PC, vector_dabt
  93. LDR PC, vector_resv
  94. LDR PC, vector_irq
  95. LDR PC, vector_fiq
  96. vector_reset:
  97. DC32 Reset_Handler
  98. vector_undef:
  99. DC32 Undef_Handler
  100. vector_swi:
  101. DC32 SWI_Handler
  102. vector_pabt:
  103. DC32 PAbt_Handler
  104. vector_dabt:
  105. DC32 DAbt_Handler
  106. vector_resv:
  107. DC32 Resv_Handler
  108. vector_irq:
  109. DC32 IRQ_Handler
  110. vector_fiq:
  111. DC32 FIQ_Handler
  112. ;----------------- Reset Handler -----------------------------------------------
  113. EXTERN rt_low_level_init
  114. EXTERN ?main
  115. PUBLIC __iar_program_start
  116. __iar_program_start:
  117. Reset_Handler:
  118. ; Set the cpu to SVC32 mode
  119. MRS R0, CPSR
  120. BIC R0, R0, #MODEMASK
  121. ORR R0, R0, #MODE_SVC|NOINT
  122. MSR CPSR_cxsf, R0
  123. ; Set CO-Processor
  124. ; little-end,disbale I/D Cache MMU, vector table is 0x00000000
  125. MRC P15, 0, R0, C1, C0, 0 ; Read CP15
  126. LDR R1, =0x00003085 ; set clear bits
  127. BIC R0, R0, R1
  128. MCR P15, 0, R0, C1, C0, 0 ; Write CP15
  129. ; Call low level init function,
  130. ; disable and clear all IRQs, Init MMU, Init interrupt controller, etc.
  131. LDR SP, =SVC_STACK_START
  132. LDR R0, =rt_low_level_init
  133. BLX R0
  134. Setup_Stack:
  135. ; Setup Stack for each mode
  136. MRS R0, CPSR
  137. BIC R0, R0, #MODEMASK
  138. ORR R1, R0, #MODE_UND|NOINT
  139. MSR CPSR_cxsf, R1 ; Undef mode
  140. LDR SP, =UND_STACK_START
  141. ORR R1,R0,#MODE_ABT|NOINT
  142. MSR CPSR_cxsf,R1 ; Abort mode
  143. LDR SP, =ABT_STACK_START
  144. ORR R1,R0,#MODE_IRQ|NOINT
  145. MSR CPSR_cxsf,R1 ; IRQ mode
  146. LDR SP, =IRQ_STACK_START
  147. ORR R1,R0,#MODE_FIQ|NOINT
  148. MSR CPSR_cxsf,R1 ; FIQ mode
  149. LDR SP, =FIQ_STACK_START
  150. ORR R1,R0,#MODE_SYS|NOINT
  151. MSR CPSR_cxsf,R1 ; SYS/User mode
  152. LDR SP, =SYS_STACK_START
  153. ORR R1,R0,#MODE_SVC|NOINT
  154. MSR CPSR_cxsf,R1 ; SVC mode
  155. LDR SP, =SVC_STACK_START
  156. ; Enter the C code
  157. LDR R0, =?main
  158. BLX R0
  159. ;----------------- Exception Handler -------------------------------------------
  160. IMPORT rt_hw_trap_udef
  161. IMPORT rt_hw_trap_swi
  162. IMPORT rt_hw_trap_pabt
  163. IMPORT rt_hw_trap_dabt
  164. IMPORT rt_hw_trap_resv
  165. IMPORT rt_hw_trap_irq
  166. IMPORT rt_hw_trap_fiq
  167. IMPORT rt_interrupt_enter
  168. IMPORT rt_interrupt_leave
  169. IMPORT rt_thread_switch_interrupt_flag
  170. IMPORT rt_interrupt_from_thread
  171. IMPORT rt_interrupt_to_thread
  172. SECTION .text:CODE:ROOT(2)
  173. ARM
  174. Undef_Handler:
  175. SUB SP, SP, #S_FRAME_SIZE
  176. STMIA SP, {R0 - R12} ; Calling R0-R12
  177. ADD R8, SP, #S_PC
  178. STMDB R8, {SP, LR} ; Calling SP, LR
  179. STR LR, [R8, #0] ; Save calling PC
  180. MRS R6, SPSR
  181. STR R6, [R8, #4] ; Save CPSR
  182. STR R0, [R8, #8] ; Save SPSR
  183. MOV R0, SP
  184. BL rt_hw_trap_udef
  185. SWI_Handler:
  186. BL rt_hw_trap_swi
  187. PAbt_Handler:
  188. BL rt_hw_trap_pabt
  189. DAbt_Handler:
  190. SUB SP, SP, #S_FRAME_SIZE
  191. STMIA SP, {R0 - R12} ; Calling R0-R12
  192. ADD R8, SP, #S_PC
  193. STMDB R8, {SP, LR} ; Calling SP, LR
  194. STR LR, [R8, #0] ; Save calling PC
  195. MRS R6, SPSR
  196. STR R6, [R8, #4] ; Save CPSR
  197. STR R0, [R8, #8] ; Save SPSR
  198. MOV R0, SP
  199. BL rt_hw_trap_dabt
  200. Resv_Handler:
  201. BL rt_hw_trap_resv
  202. IRQ_Handler:
  203. STMFD SP!, {R0-R12,LR}
  204. BL rt_interrupt_enter
  205. BL rt_hw_trap_irq
  206. BL rt_interrupt_leave
  207. ; If rt_thread_switch_interrupt_flag set,
  208. ; jump to rt_hw_context_switch_interrupt_do and don't return
  209. LDR R0, =rt_thread_switch_interrupt_flag
  210. LDR R1, [R0]
  211. CMP R1, #1
  212. BEQ rt_hw_context_switch_interrupt_do
  213. LDMFD SP!, {R0-R12,LR}
  214. SUBS PC, LR, #4
  215. FIQ_Handler:
  216. STMFD SP!, {R0-R7,LR}
  217. BL rt_hw_trap_fiq
  218. LDMFD SP!, {R0-R7,LR}
  219. SUBS PC, LR, #4
  220. ;------ void rt_hw_context_switch_interrupt_do(rt_base_t flag) -----------------
  221. rt_hw_context_switch_interrupt_do:
  222. MOV R1, #0 ; Clear flag
  223. STR R1, [R0] ; Save to flag variable
  224. LDMFD SP!, {R0-R12,LR} ; Reload saved registers
  225. STMFD SP, {R0-R2} ; Save R0-R2
  226. SUB R1, SP, #4*3 ; Save old task's SP to R1
  227. SUB R2, LR, #4 ; Save old task's PC to R2
  228. MRS R0, SPSR ; Get CPSR of interrupt thread
  229. MSR CPSR_c, #MODE_SVC|NOINT ; Switch to SVC mode and no interrupt
  230. STMFD SP!, {R2} ; Push old task's PC
  231. STMFD SP!, {R3-R12,LR} ; Push old task's LR,R12-R3
  232. LDMFD R1, {R1-R3}
  233. STMFD SP!, {R1-R3} ; Push old task's R2-R0
  234. STMFD SP!, {R0} ; Push old task's CPSR
  235. LDR R4, =rt_interrupt_from_thread
  236. LDR R5, [R4] ; R5 = stack ptr in old tasks's TCB
  237. STR SP, [R5] ; Store SP in preempted tasks's TCB
  238. LDR R6, =rt_interrupt_to_thread
  239. LDR R6, [R6] ; R6 = stack ptr in new tasks's TCB
  240. LDR SP, [R6] ; Get new task's stack pointer
  241. LDMFD SP!, {R4} ; Pop new task's SPSR
  242. MSR SPSR_cxsf, R4
  243. LDMFD SP!, {R0-R12,LR,PC}^ ; pop new task's R0-R12,LR & PC SPSR to CPSR
  244. END