start_rvds.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. ;==============================================================================================
  2. ; star_rvds.s for Keil MDK 4.10
  3. ;
  4. ; SEP4020 start up code
  5. ;
  6. ; Change Logs:
  7. ; Date Author Notes
  8. ; 2010-03-17 zchong
  9. ;=============================================================================================
  10. ;
  11. PMU_PLTR EQU 0x10001000 ; PLL的稳定过渡时间
  12. PMU_PMCR EQU 0x10001004 ; 系统主时钟PLL的控制寄存器
  13. PMU_PUCR EQU 0x10001008 ; USB时钟PLL的控制寄存器
  14. PMU_PCSR EQU 0x1000100C ; 内部模块时钟源供给的控制寄存器
  15. PMU_PDSLOW EQU 0x10001010 ; SLOW状态下时钟的分频因子
  16. PMU_PMDR EQU 0x10001014 ; 芯片工作模式寄存器
  17. PMU_RCTR EQU 0x10001018 ; Reset控制寄存器
  18. PMU_CLRWAKUP EQU 0x1000101C ; WakeUp清除寄存器
  19. RTC_CTR EQU 0x1000200C ; RTC控制寄存器
  20. INTC_IER EQU 0x10000000 ; IRQ中断允许寄存器
  21. INTC_IMR EQU 0x10000008 ; IRQ中断屏蔽寄存器
  22. INTC_IFSR EQU 0x10000030 ; IRQ中断最终状态寄存器
  23. INTC_FIER EQU 0x100000C0 ; FIQ中断允许寄存器
  24. INTC_FIMR EQU 0x100000C4 ; FIQ中断屏蔽寄存器
  25. EMI_CSACONF EQU 0x11000000 ; CSA参数配置寄存器
  26. EMI_CSECONF EQU 0x11000010 ; CSE参数配置寄存器
  27. EMI_CSFCONF EQU 0x11000014 ; CSF参数配置寄存器
  28. EMI_SDCONF1 EQU 0x11000018 ; SDRAM时序配置寄存器1
  29. EMI_SDCONF2 EQU 0x1100001C ; SDRAM时序配置寄存器2, SDRAM初始化用到的配置信息
  30. EMI_REMAPCONF EQU 0x11000020 ; 片选空间及地址映射REMAP配置寄存器
  31. Mode_USR EQU 0x10
  32. Mode_FIQ EQU 0x11
  33. Mode_IRQ EQU 0x12
  34. Mode_SVC EQU 0x13
  35. Mode_ABT EQU 0x17
  36. Mode_UND EQU 0x1B
  37. Mode_SYS EQU 0x1F
  38. I_Bit EQU 0x80 ; when I bit is set, IRQ is disabled
  39. F_Bit EQU 0x40 ; when F bit is set, FIQ is disabled
  40. NOINT EQU 0xc0
  41. MASK_MODE EQU 0x0000003F
  42. MODE_SVC32 EQU 0x00000013
  43. ; Internal Memory Base Addresses
  44. FLASH_BASE EQU 0x20000000
  45. RAM_BASE EQU 0x04000000
  46. ; Stack
  47. UND_Stack_Size EQU 0x00000000
  48. SVC_Stack_Size EQU 0x00000400
  49. ABT_Stack_Size EQU 0x00000000
  50. FIQ_Stack_Size EQU 0x00000000
  51. IRQ_Stack_Size EQU 0x00000100
  52. USR_Stack_Size EQU 0x00000000
  53. ISR_Stack_Size EQU (UND_Stack_Size + SVC_Stack_Size + ABT_Stack_Size + \
  54. FIQ_Stack_Size + IRQ_Stack_Size)
  55. AREA STACK, NOINIT, READWRITE, ALIGN=3
  56. Stack_Mem SPACE USR_Stack_Size
  57. __initial_sp SPACE ISR_Stack_Size
  58. Stack_Top
  59. ; Heap
  60. Heap_Size EQU 0x00000000
  61. AREA HEAP, NOINIT, READWRITE, ALIGN=3
  62. __heap_base
  63. Heap_Mem SPACE Heap_Size
  64. __heap_limit
  65. PRESERVE8
  66. ; Area Definition and Entry Point
  67. ; Startup Code must be linked first at Address at which it expects to run.
  68. AREA RESET, CODE, READONLY
  69. ARM
  70. ; Exception Vectors
  71. ; Mapped to Address 0.
  72. ; Absolute addressing mode must be used.
  73. ; Dummy Handlers are implemented as infinite loops which can be modified.
  74. Vectors LDR PC,Reset_Addr
  75. LDR PC,Undef_Addr
  76. LDR PC,SWI_Addr
  77. LDR PC,PAbt_Addr
  78. LDR PC,DAbt_Addr
  79. NOP ; Reserved Vector
  80. LDR PC,IRQ_Addr
  81. LDR PC,FIQ_Addr
  82. Reset_Addr DCD Reset_Handler
  83. Undef_Addr DCD Undef_Handler
  84. SWI_Addr DCD SWI_Handler
  85. PAbt_Addr DCD PAbt_Handler
  86. DAbt_Addr DCD DAbt_Handler
  87. DCD 0 ; Reserved Address
  88. IRQ_Addr DCD IRQ_Handler
  89. FIQ_Addr DCD FIQ_Handler
  90. Undef_Handler B Undef_Handler
  91. SWI_Handler B SWI_Handler
  92. PAbt_Handler B PAbt_Handler
  93. DAbt_Handler B DAbt_Handler
  94. FIQ_Handler B FIQ_Handler
  95. ; Reset Handler
  96. EXPORT Reset_Handler
  97. Reset_Handler
  98. ;****************************************************************
  99. ;* 关闭看门狗
  100. ;****************************************************************
  101. LDR R0,=RTC_CTR
  102. LDR R1,=0x0
  103. STR R1,[R0]
  104. ;****************************************************************
  105. ;* 关中断
  106. ;****************************************************************
  107. MRS R0, CPSR
  108. BIC R0, R0, #MASK_MODE
  109. ORR R0, R0, #MODE_SVC32
  110. ORR R0, R0, #I_Bit
  111. ORR R0, R0, #F_Bit
  112. MSR CPSR_c, r0
  113. LDR R0,=INTC_IER
  114. LDR R1,=0x0
  115. STR R1,[R0]
  116. LDR R0,=INTC_IMR
  117. LDR R1,=0xFFFFFFFF
  118. STR R1,[R0]
  119. LDR R0,=INTC_FIER
  120. LDR R1,=0x0
  121. STR R1,[R0]
  122. LDR R0,=INTC_FIMR
  123. LDR R1,=0x0F
  124. STR R1,[R0]
  125. ;****************************************************************
  126. ;* 初始化PMU模块, 配置系统时钟
  127. ;****************************************************************
  128. LDR R4, =PMU_PCSR ; 打开所有模块时钟
  129. LDR R5, =0x0001ffff
  130. STR R5, [ R4 ]
  131. LDR R4, =PMU_PLTR ; 配置PLL稳定过度时间为保守值50us*100M.
  132. LDR R5, =0x00fa00fa
  133. STR R5, [ R4 ]
  134. LDR R4, =PMU_PMDR ; 由SLOW模式进入NORMAL模式
  135. LDR R5, =0x00000001
  136. STR R5, [ R4 ]
  137. LDR R4, =PMU_PMCR ; 配置系统时钟为72MHz 2*Fin*9=2*4*9=72MHz
  138. LDR R5, =0x00004009 ; MFCN 0->1 trigger PLL to reconfigure event when mode isn''t SLOW
  139. STR R5, [ R4 ]
  140. LDR R4, =PMU_PMCR ;
  141. LDR R5, =0x0000c009
  142. STR R5, [ R4 ]
  143. ;****************************************************************
  144. ;* 初始化EMI
  145. ;****************************************************************
  146. ; LDR R4, =EMI_CSACONF ; CSA片选时序参数配置
  147. ; LDR R5, =0x08a6a6a1
  148. ; STR R5, [ R4 ]
  149. ; LDR R4, =EMI_CSECONF ; CSE片选时序参数配置,最保守配置
  150. ; LDR R5, =0x8cfffff1
  151. ; STR R5, [ R4 ]
  152. ; LDR R4, =EMI_SDCONF1 ; SDRAM参数配置1
  153. ; LDR R5, =0x1E104177
  154. ; STR R5, [ R4 ]
  155. ; LDR R4, =EMI_SDCONF2 ; SDRAM参数配置2
  156. ; LDR R5, =0x80001860
  157. ; STR R5, [ R4 ]
  158. ; Copy Exception Vectors to Internal RAM
  159. IF :DEF:RAM_INTVEC
  160. ADR R8, Vectors ; Source
  161. LDR R9, =RAM_BASE ; Destination
  162. LDMIA R8!, {R0-R7} ; Load Vectors
  163. STMIA R9!, {R0-R7} ; Store Vectors
  164. LDMIA R8!, {R0-R7} ; Load Handler Addresses
  165. STMIA R9!, {R0-R7} ; Store Handler Addresses
  166. ENDIF
  167. ; Remap on-chip RAM to address 0
  168. IF :DEF:REMAP
  169. LDR R0, =EMI_REMAPCONF
  170. MOV R1, #0x80000000
  171. STR R1, [R0, #0] ; Remap
  172. ENDIF
  173. ; Setup Stack for each mode
  174. LDR R0, =Stack_Top
  175. ; Enter Undefined Instruction Mode and set its Stack Pointer
  176. MSR CPSR_c, #Mode_UND:OR:I_Bit:OR:F_Bit
  177. MOV SP, R0
  178. SUB R0, R0, #UND_Stack_Size
  179. ; Enter Abort Mode and set its Stack Pointer
  180. MSR CPSR_c, #Mode_ABT:OR:I_Bit:OR:F_Bit
  181. MOV SP, R0
  182. SUB R0, R0, #ABT_Stack_Size
  183. ; Enter FIQ Mode and set its Stack Pointer
  184. MSR CPSR_c, #Mode_FIQ:OR:I_Bit:OR:F_Bit
  185. MOV SP, R0
  186. SUB R0, R0, #FIQ_Stack_Size
  187. ; Enter IRQ Mode and set its Stack Pointer
  188. MSR CPSR_c, #Mode_IRQ:OR:I_Bit:OR:F_Bit
  189. MOV SP, R0
  190. SUB R0, R0, #IRQ_Stack_Size
  191. ; Enter Supervisor Mode and set its Stack Pointer
  192. MSR CPSR_c, #Mode_SVC:OR:I_Bit:OR:F_Bit
  193. MOV SP, R0
  194. SUB R0, R0, #SVC_Stack_Size
  195. ; Enter User Mode and set its Stack Pointer
  196. ; MSR CPSR_c, #Mode_USR
  197. IF :DEF:__MICROLIB
  198. EXPORT __initial_sp
  199. ELSE
  200. ; No usr mode stack here.
  201. ;MOV SP, R0
  202. ;SUB SL, SP, #USR_Stack_Size
  203. ENDIF
  204. ; Enter the C code
  205. IMPORT __main
  206. LDR R0, =__main
  207. BX R0
  208. IMPORT rt_interrupt_enter
  209. IMPORT rt_interrupt_leave
  210. IMPORT rt_thread_switch_interrput_flag
  211. IMPORT rt_interrupt_from_thread
  212. IMPORT rt_interrupt_to_thread
  213. IMPORT rt_hw_trap_irq
  214. IMPORT rt_hw_trap_abort
  215. IMPORT rt_interrupt_nest
  216. Abort_Handler PROC
  217. EXPORT Abort_Handler
  218. STMFD SP!, {R0-R12,LR}
  219. LDR R0, =rt_interrupt_nest
  220. LDR R1, [R0]
  221. CMP R1, #0
  222. DeadLoop BHI DeadLoop ; Abort happened in irq mode, halt system.
  223. BL rt_interrupt_enter
  224. BL rt_hw_trap_abort
  225. BL rt_interrupt_leave
  226. B SWITCH
  227. ENDP
  228. IRQ_Handler PROC
  229. EXPORT IRQ_Handler
  230. STMFD SP!, {R0-R12,LR}
  231. BL rt_interrupt_enter
  232. BL rt_hw_trap_irq
  233. BL rt_interrupt_leave
  234. ; if rt_thread_switch_interrput_flag set, jump to
  235. ; rt_hw_context_switch_interrupt_do and don't return
  236. SWITCH
  237. LDR R0, =rt_thread_switch_interrput_flag
  238. LDR R1, [R0]
  239. CMP R1, #1
  240. BEQ rt_hw_context_switch_interrupt_do
  241. LDMFD SP!, {R0-R12,LR}
  242. SUBS PC, LR, #4
  243. ENDP
  244. ; /*
  245. ; * void rt_hw_context_switch_interrupt_do(rt_base_t flag)
  246. ; */
  247. rt_hw_context_switch_interrupt_do PROC
  248. EXPORT rt_hw_context_switch_interrupt_do
  249. MOV r1, #0 ; clear flag
  250. STR r1, [r0]
  251. LDMFD sp!, {r0-r12,lr}; reload saved registers
  252. STMFD sp!, {r0-r3} ; save r0-r3
  253. MOV r1, sp
  254. ADD sp, sp, #16 ; restore sp
  255. SUB r2, lr, #4 ; save old task's pc to r2
  256. MRS r3, spsr ; get cpsr of interrupt thread
  257. ; switch to SVC mode and no interrupt
  258. MSR cpsr_c, #I_Bit:OR:F_Bit:OR:Mode_SVC
  259. STMFD sp!, {r2} ; push old task's pc
  260. STMFD sp!, {r4-r12,lr}; push old task's lr,r12-r4
  261. MOV r4, r1 ; Special optimised code below
  262. MOV r5, r3
  263. LDMFD r4!, {r0-r3}
  264. STMFD sp!, {r0-r3} ; push old task's r3-r0
  265. STMFD sp!, {r5} ; push old task's cpsr
  266. MRS r4, spsr
  267. STMFD sp!, {r4} ; push old task's spsr
  268. LDR r4, =rt_interrupt_from_thread
  269. LDR r5, [r4]
  270. STR sp, [r5] ; store sp in preempted tasks's TCB
  271. LDR r6, =rt_interrupt_to_thread
  272. LDR r6, [r6]
  273. LDR sp, [r6] ; get new task's stack pointer
  274. LDMFD sp!, {r4} ; pop new task's spsr
  275. MSR spsr_cxsf, r4
  276. LDMFD sp!, {r4} ; pop new task's psr
  277. MSR cpsr_cxsf, r4
  278. LDMFD sp!, {r0-r12,lr,pc} ; pop new task's r0-r12,lr & pc
  279. ENDP
  280. IF :DEF:__MICROLIB
  281. EXPORT __heap_base
  282. EXPORT __heap_limit
  283. ELSE
  284. ; User Initial Stack & Heap
  285. AREA |.text|, CODE, READONLY
  286. IMPORT __use_two_region_memory
  287. EXPORT __user_initial_stackheap
  288. __user_initial_stackheap
  289. LDR R0, = Heap_Mem
  290. LDR R1, = (Stack_Mem + IRQ_Stack_Size)
  291. LDR R2, = (Heap_Mem + Heap_Size)
  292. LDR R3, = Stack_Mem
  293. BX LR
  294. ENDIF
  295. END