start_rvds.S 9.4 KB

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