start_rvds.S 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. ; */
  25. S_FRAME_SIZE EQU (18*4) ;72
  26. ;S_SPSR EQU (17*4) ;SPSR
  27. ;S_CPSR EQU (16*4) ;CPSR
  28. S_PC EQU (15*4) ;R15
  29. ;S_LR EQU (14*4) ;R14
  30. ;S_SP EQU (13*4) ;R13
  31. ;S_IP EQU (12*4) ;R12
  32. ;S_FP EQU (11*4) ;R11
  33. ;S_R10 EQU (10*4)
  34. ;S_R9 EQU (9*4)
  35. ;S_R8 EQU (8*4)
  36. ;S_R7 EQU (7*4)
  37. ;S_R6 EQU (6*4)
  38. ;S_R5 EQU (5*4)
  39. ;S_R4 EQU (4*4)
  40. ;S_R3 EQU (3*4)
  41. ;S_R2 EQU (2*4)
  42. ;S_R1 EQU (1*4)
  43. ;S_R0 EQU (0*4)
  44. MODE_USR EQU 0X10
  45. MODE_FIQ EQU 0X11
  46. MODE_IRQ EQU 0X12
  47. MODE_SVC EQU 0X13
  48. MODE_ABT EQU 0X17
  49. MODE_UND EQU 0X1B
  50. MODE_SYS EQU 0X1F
  51. MODEMASK EQU 0X1F
  52. NOINT EQU 0xC0
  53. GET rt_low_level_keil.inc
  54. ;----------------------- Stack and Heap Definitions ----------------------------
  55. AREA STACK, NOINIT, READWRITE, ALIGN=3
  56. Stack_Mem
  57. SPACE UND_STK_SIZE
  58. EXPORT UND_STACK_START
  59. UND_STACK_START
  60. ALIGN 8
  61. SPACE ABT_STK_SIZE
  62. EXPORT ABT_STACK_START
  63. ABT_STACK_START
  64. ALIGN 8
  65. SPACE FIQ_STK_SIZE
  66. EXPORT FIQ_STACK_START
  67. FIQ_STACK_START
  68. ALIGN 8
  69. SPACE IRQ_STK_SIZE
  70. EXPORT IRQ_STACK_START
  71. IRQ_STACK_START
  72. ALIGN 8
  73. SPACE SVC_STK_SIZE
  74. EXPORT SVC_STACK_START
  75. SVC_STACK_START
  76. ALIGN 8
  77. SPACE SYS_STK_SIZE
  78. EXPORT SYS_STACK_START
  79. SYS_STACK_START
  80. Stack_Top
  81. Heap_Size EQU 0x00000000
  82. AREA HEAP, NOINIT, READWRITE, ALIGN=3
  83. __heap_base
  84. Heap_Mem
  85. 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
  126. MSR CPSR_CXSF,R0
  127. LDR SP, =SVC_STACK_START
  128. ; Call low level init function,
  129. ; disable and clear all IRQs and remap internal ram to 0x00000000.
  130. LDR R0, =rt_low_level_init
  131. BLX R0
  132. ; Copy Exception Vectors to Internal RAM
  133. LDR R8, =Entry_Point ; Source
  134. LDR R9, =VECTOR_TABLE_START ; Destination
  135. CMP R8, R9
  136. BEQ Setup_Stack
  137. LDMIA R8!, {R0-R7} ; Load Vectors
  138. STMIA R9!, {R0-R7} ; Store Vectors
  139. LDMIA R8!, {R0-R7} ; Load Handler Addresses
  140. STMIA R9!, {R0-R7} ; Store Handler Addresses
  141. Setup_Stack
  142. ; Setup Stack for each mode
  143. MRS R0, CPSR
  144. BIC R0, R0, #MODEMASK
  145. ORR R1, R0, #MODE_UND:OR:NOINT
  146. MSR CPSR_cxsf, R1 ; Undef mode
  147. LDR SP, =UND_STACK_START
  148. ORR R1,R0,#MODE_ABT:OR:NOINT
  149. MSR CPSR_cxsf,R1 ; Abort mode
  150. LDR SP, =ABT_STACK_START
  151. ORR R1,R0,#MODE_IRQ:OR:NOINT
  152. MSR CPSR_cxsf,R1 ; IRQ mode
  153. LDR SP, =IRQ_STACK_START
  154. ORR R1,R0,#MODE_FIQ:OR:NOINT
  155. MSR CPSR_cxsf,R1 ; FIQ mode
  156. LDR SP, =FIQ_STACK_START
  157. ORR R1,R0,#MODE_SYS:OR:NOINT
  158. MSR CPSR_cxsf,R1 ; SYS/User mode
  159. LDR SP, =SYS_STACK_START
  160. ORR R1,R0,#MODE_SVC:OR:NOINT
  161. MSR CPSR_cxsf,R1 ; SVC mode
  162. LDR SP, =SVC_STACK_START
  163. ; Enter the C code
  164. LDR R0, =__main
  165. BLX R0
  166. ;----------------- Exception Handler -------------------------------------------
  167. IMPORT rt_hw_trap_udef
  168. IMPORT rt_hw_trap_swi
  169. IMPORT rt_hw_trap_pabt
  170. IMPORT rt_hw_trap_dabt
  171. IMPORT rt_hw_trap_resv
  172. IMPORT rt_hw_trap_irq
  173. IMPORT rt_hw_trap_fiq
  174. IMPORT rt_interrupt_enter
  175. IMPORT rt_interrupt_leave
  176. IMPORT rt_thread_switch_interrupt_flag
  177. IMPORT rt_interrupt_from_thread
  178. IMPORT rt_interrupt_to_thread
  179. Undef_Handler PROC
  180. SUB SP, SP, #S_FRAME_SIZE
  181. STMIA SP, {R0 - R12} ; Calling R0-R12
  182. ADD R8, SP, #S_PC
  183. STMDB R8, {SP, LR} ; Calling SP, LR
  184. STR LR, [R8, #0] ; Save calling PC
  185. MRS R6, SPSR
  186. STR R6, [R8, #4] ; Save CPSR
  187. STR R0, [R8, #8] ; Save SPSR
  188. MOV R0, SP
  189. BL rt_hw_trap_udef
  190. ENDP
  191. SWI_Handler PROC
  192. BL rt_hw_trap_swi
  193. ENDP
  194. PAbt_Handler PROC
  195. BL rt_hw_trap_pabt
  196. ENDP
  197. DAbt_Handler PROC
  198. SUB SP, SP, #S_FRAME_SIZE
  199. STMIA SP, {R0 - R12} ; Calling R0-R12
  200. ADD R8, SP, #S_PC
  201. STMDB R8, {SP, LR} ; Calling SP, LR
  202. STR LR, [R8, #0] ; Save calling PC
  203. MRS R6, SPSR
  204. STR R6, [R8, #4] ; Save CPSR
  205. STR R0, [R8, #8] ; Save SPSR
  206. MOV R0, SP
  207. BL rt_hw_trap_dabt
  208. ENDP
  209. Resv_Handler PROC
  210. BL rt_hw_trap_resv
  211. ENDP
  212. FIQ_Handler PROC
  213. STMFD SP!, {R0-R7,LR}
  214. BL rt_hw_trap_fiq
  215. LDMFD SP!, {R0-R7,LR}
  216. SUBS PC, LR, #4
  217. ENDP
  218. IRQ_Handler PROC
  219. STMFD SP!, {R0-R12,LR}
  220. BL rt_interrupt_enter
  221. BL rt_hw_trap_irq
  222. BL rt_interrupt_leave
  223. ; If rt_thread_switch_interrupt_flag set,
  224. ; jump to rt_hw_context_switch_interrupt_do and don't return
  225. LDR R0, =rt_thread_switch_interrupt_flag
  226. LDR R1, [R0]
  227. CMP R1, #1
  228. BEQ rt_hw_context_switch_interrupt_do
  229. LDMFD SP!, {R0-R12,LR}
  230. SUBS PC, LR, #4
  231. ENDP
  232. ;------ void rt_hw_context_switch_interrupt_do(rt_base_t flag) -----------------
  233. rt_hw_context_switch_interrupt_do PROC
  234. MOV R1, #0 ; Clear flag
  235. STR R1, [R0] ; Save to flag variable
  236. LDMFD SP!, {R0-R12,LR} ; Reload saved registers
  237. STMFD SP, {R0-R2} ; Save R0-R2
  238. SUB R1, SP, #4*3 ; Save old task's SP to R1
  239. SUB R2, LR, #4 ; Save old task's PC to R2
  240. MRS R0, SPSR ; Get CPSR of interrupt thread
  241. MSR CPSR_c, #MODE_SVC:OR:NOINT ; Switch to SVC mode and no interrupt
  242. STMFD SP!, {R2} ; Push old task's PC
  243. STMFD SP!, {R3-R12,LR} ; Push old task's LR,R12-R3
  244. LDMFD R1, {R1-R3}
  245. STMFD SP!, {R1-R3} ; Push old task's R2-R0
  246. STMFD SP!, {R0} ; Push old task's CPSR
  247. LDR R4, =rt_interrupt_from_thread
  248. LDR R5, [R4] ; R5 = stack ptr in old tasks's TCB
  249. STR SP, [R5] ; Store SP in preempted tasks's TCB
  250. LDR R6, =rt_interrupt_to_thread
  251. LDR R6, [R6] ; R6 = stack ptr in new tasks's TCB
  252. LDR SP, [R6] ; Get new task's stack pointer
  253. LDMFD SP!, {R4} ; Pop new task's SPSR
  254. MSR SPSR_cxsf, R4
  255. LDMFD SP!, {R0-R12,LR,PC}^ ; pop new task's R0-R12,LR & PC SPSR to CPSR
  256. ENDP
  257. IF :DEF:__MICROLIB
  258. EXPORT __heap_base
  259. EXPORT __heap_limit
  260. ELSE
  261. ; User Initial Stack & Heap
  262. AREA |.text|, CODE, READONLY
  263. IMPORT __use_two_region_memory
  264. EXPORT __user_initial_stackheap
  265. __user_initial_stackheap
  266. LDR R0, = Heap_Mem
  267. LDR R1, = (Stack_Mem + SYS_STK_SIZE)
  268. LDR R2, = (Heap_Mem + Heap_Size)
  269. LDR R3, = Stack_Mem
  270. BX LR
  271. ENDIF
  272. END