start_rvds.S 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ;/*
  2. ; * File : start_rvds.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-08-14 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. S_FRAME_SIZE EQU (18*4) ;72
  27. ;S_SPSR EQU (17*4) ;SPSR
  28. ;S_CPSR EQU (16*4) ;CPSR
  29. S_PC EQU (15*4) ;R15
  30. ;S_LR EQU (14*4) ;R14
  31. ;S_SP EQU (13*4) ;R13
  32. ;S_IP EQU (12*4) ;R12
  33. ;S_FP EQU (11*4) ;R11
  34. ;S_R10 EQU (10*4)
  35. ;S_R9 EQU (9*4)
  36. ;S_R8 EQU (8*4)
  37. ;S_R7 EQU (7*4)
  38. ;S_R6 EQU (6*4)
  39. ;S_R5 EQU (5*4)
  40. ;S_R4 EQU (4*4)
  41. ;S_R3 EQU (3*4)
  42. ;S_R2 EQU (2*4)
  43. ;S_R1 EQU (1*4)
  44. ;S_R0 EQU (0*4)
  45. MODE_USR EQU 0X10
  46. MODE_FIQ EQU 0X11
  47. MODE_IRQ EQU 0X12
  48. MODE_SVC EQU 0X13
  49. MODE_ABT EQU 0X17
  50. MODE_UND EQU 0X1B
  51. MODE_SYS EQU 0X1F
  52. MODEMASK EQU 0X1F
  53. NOINT EQU 0xC0
  54. GET rt_low_level_keil.inc
  55. ;----------------------- Stack and Heap Definitions ----------------------------
  56. AREA STACK, NOINIT, READWRITE, ALIGN=2
  57. Stack_Mem
  58. SPACE UND_STK_SIZE
  59. EXPORT UND_STACK_START
  60. UND_STACK_START
  61. ALIGN 4
  62. SPACE ABT_STK_SIZE
  63. EXPORT ABT_STACK_START
  64. ABT_STACK_START
  65. ALIGN 4
  66. SPACE FIQ_STK_SIZE
  67. EXPORT FIQ_STACK_START
  68. FIQ_STACK_START
  69. ALIGN 4
  70. SPACE IRQ_STK_SIZE
  71. EXPORT IRQ_STACK_START
  72. IRQ_STACK_START
  73. ALIGN 4
  74. SPACE SYS_STK_SIZE
  75. EXPORT SYS_STACK_START
  76. SYS_STACK_START
  77. ALIGN 4
  78. SPACE SVC_STK_SIZE
  79. EXPORT SVC_STACK_START
  80. SVC_STACK_START
  81. Stack_Top
  82. PRESERVE8
  83. ;--------------Jump vector table------------------------------------------------
  84. EXPORT Entry_Point
  85. AREA RESET, CODE, READONLY
  86. ARM
  87. Entry_Point
  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. DCD Reset_Handler
  98. vector_undef
  99. DCD Undef_Handler
  100. vector_swi
  101. DCD SWI_Handler
  102. vector_pabt
  103. DCD PAbt_Handler
  104. vector_dabt
  105. DCD DAbt_Handler
  106. vector_resv
  107. DCD Resv_Handler
  108. vector_irq
  109. DCD IRQ_Handler
  110. vector_fiq
  111. DCD FIQ_Handler
  112. ;----------------- Reset Handler -----------------------------------------------
  113. IMPORT rt_low_level_init
  114. IMPORT __main
  115. EXPORT Reset_Handler
  116. Reset_Handler
  117. ; set the cpu to SVC32 mode
  118. MRS R0,CPSR
  119. BIC R0,R0,#MODEMASK
  120. ORR R0,R0,#MODE_SVC:OR:NOINT
  121. MSR CPSR_cxsf,R0
  122. ; Set CO-Processor
  123. ; little-end,disbale I/D Cache MMU, vector table is 0x00000000
  124. MRC p15, 0, R0, c1, c0, 0 ; Read CP15
  125. LDR R1, =0x00003085 ; set clear bits
  126. BIC R0, R0, R1
  127. MCR p15, 0, R0, c1, c0, 0 ; Write CP15
  128. ; Call low level init function,
  129. ; disable and clear all IRQs, Init MMU, Init interrupt controller, etc.
  130. LDR SP, =SVC_STACK_START
  131. LDR R0, =rt_low_level_init
  132. BLX R0
  133. Setup_Stack
  134. ; Setup Stack for each mode
  135. MRS R0, CPSR
  136. BIC R0, R0, #MODEMASK
  137. ORR R1, R0, #MODE_UND:OR:NOINT
  138. MSR CPSR_cxsf, R1 ; Undef mode
  139. LDR SP, =UND_STACK_START
  140. ORR R1,R0,#MODE_ABT:OR:NOINT
  141. MSR CPSR_cxsf,R1 ; Abort mode
  142. LDR SP, =ABT_STACK_START
  143. ORR R1,R0,#MODE_IRQ:OR:NOINT
  144. MSR CPSR_cxsf,R1 ; IRQ mode
  145. LDR SP, =IRQ_STACK_START
  146. ORR R1,R0,#MODE_FIQ:OR:NOINT
  147. MSR CPSR_cxsf,R1 ; FIQ mode
  148. LDR SP, =FIQ_STACK_START
  149. ORR R1,R0,#MODE_SYS:OR:NOINT
  150. MSR CPSR_cxsf,R1 ; SYS/User mode
  151. LDR SP, =SYS_STACK_START
  152. ORR R1,R0,#MODE_SVC:OR:NOINT
  153. MSR CPSR_cxsf,R1 ; SVC mode
  154. LDR SP, =SVC_STACK_START
  155. ; Enter the C code
  156. LDR R0, =__main
  157. BLX R0
  158. ;----------------- Exception Handler -------------------------------------------
  159. IMPORT rt_hw_trap_udef
  160. IMPORT rt_hw_trap_swi
  161. IMPORT rt_hw_trap_pabt
  162. IMPORT rt_hw_trap_dabt
  163. IMPORT rt_hw_trap_resv
  164. IMPORT rt_hw_trap_irq
  165. IMPORT rt_hw_trap_fiq
  166. IMPORT rt_interrupt_enter
  167. IMPORT rt_interrupt_leave
  168. IMPORT rt_thread_switch_interrupt_flag
  169. IMPORT rt_interrupt_from_thread
  170. IMPORT rt_interrupt_to_thread
  171. Undef_Handler PROC
  172. SUB SP, SP, #S_FRAME_SIZE
  173. STMIA SP, {R0 - R12} ; Calling R0-R12
  174. ADD R8, SP, #S_PC
  175. STMDB R8, {SP, LR} ; Calling SP, LR
  176. STR LR, [R8, #0] ; Save calling PC
  177. MRS R6, SPSR
  178. STR R6, [R8, #4] ; Save CPSR
  179. STR R0, [R8, #8] ; Save SPSR
  180. MOV R0, SP
  181. BL rt_hw_trap_udef
  182. ENDP
  183. SWI_Handler PROC
  184. BL rt_hw_trap_swi
  185. ENDP
  186. PAbt_Handler PROC
  187. BL rt_hw_trap_pabt
  188. ENDP
  189. DAbt_Handler PROC
  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. ENDP
  201. Resv_Handler PROC
  202. BL rt_hw_trap_resv
  203. ENDP
  204. FIQ_Handler PROC
  205. STMFD SP!, {R0-R7,LR}
  206. BL rt_hw_trap_fiq
  207. LDMFD SP!, {R0-R7,LR}
  208. SUBS PC, LR, #4
  209. ENDP
  210. IRQ_Handler PROC
  211. STMFD SP!, {R0-R12,LR}
  212. BL rt_interrupt_enter
  213. BL rt_hw_trap_irq
  214. BL rt_interrupt_leave
  215. ; If rt_thread_switch_interrupt_flag set,
  216. ; jump to rt_hw_context_switch_interrupt_do and don't return
  217. LDR R0, =rt_thread_switch_interrupt_flag
  218. LDR R1, [R0]
  219. CMP R1, #1
  220. BEQ rt_hw_context_switch_interrupt_do
  221. LDMFD SP!, {R0-R12,LR}
  222. SUBS PC, LR, #4
  223. ENDP
  224. ;------ void rt_hw_context_switch_interrupt_do(rt_base_t flag) -----------------
  225. rt_hw_context_switch_interrupt_do PROC
  226. MOV R1, #0 ; Clear flag
  227. STR R1, [R0] ; Save to flag variable
  228. LDMFD SP!, {R0-R12,LR} ; Reload saved registers
  229. STMFD SP, {R0-R2} ; Save R0-R2
  230. SUB R1, SP, #4*3 ; Save old task's SP to R1
  231. SUB R2, LR, #4 ; Save old task's PC to R2
  232. MRS R0, SPSR ; Get CPSR of interrupt thread
  233. MSR CPSR_c, #MODE_SVC:OR:NOINT ; Switch to SVC mode and no interrupt
  234. STMFD SP!, {R2} ; Push old task's PC
  235. STMFD SP!, {R3-R12,LR} ; Push old task's LR,R12-R3
  236. LDMFD R1, {R1-R3}
  237. STMFD SP!, {R1-R3} ; Push old task's R2-R0
  238. STMFD SP!, {R0} ; Push old task's CPSR
  239. LDR R4, =rt_interrupt_from_thread
  240. LDR R5, [R4] ; R5 = stack ptr in old tasks's TCB
  241. STR SP, [R5] ; Store SP in preempted tasks's TCB
  242. LDR R6, =rt_interrupt_to_thread
  243. LDR R6, [R6] ; R6 = stack ptr in new tasks's TCB
  244. LDR SP, [R6] ; Get new task's stack pointer
  245. LDMFD SP!, {R4} ; Pop new task's SPSR
  246. MSR SPSR_cxsf, R4
  247. LDMFD SP!, {R0-R12,LR,PC}^ ; pop new task's R0-R12,LR & PC SPSR to CPSR
  248. ENDP
  249. END