start_rvds.S 12 KB

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