context_ccs.asm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ;/*
  2. ; * File : context_ccs.asm
  3. ; * This file is part of RT-Thread RTOS
  4. ; * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. ; *
  6. ; * The license and distribution terms for this file may be
  7. ; * found in the file LICENSE in this distribution or at
  8. ; * http://www.rt-thread.org/license/LICENSE
  9. ; *
  10. ; * Change Logs:
  11. ; * Date Author Notes
  12. ; * 2009-01-20 Bernard first version
  13. ; * 2011-07-22 Bernard added thumb mode porting
  14. ; * 2013-05-24 Grissiom port to CCS
  15. ; * 2013-05-26 Grissiom optimize for ARMv7
  16. ; */
  17. .text
  18. .arm
  19. .ref rt_thread_switch_interrupt_flag
  20. .ref rt_interrupt_from_thread
  21. .ref rt_interrupt_to_thread
  22. .ref rt_interrupt_enter
  23. .ref rt_interrupt_leave
  24. .ref rt_hw_trap_irq
  25. ;/*
  26. ; * rt_base_t rt_hw_interrupt_disable();
  27. ; */
  28. .def rt_hw_interrupt_disable
  29. .asmfunc
  30. rt_hw_interrupt_disable
  31. MRS r0, cpsr
  32. CPSID IF
  33. BX lr
  34. .endasmfunc
  35. ;/*
  36. ; * void rt_hw_interrupt_enable(rt_base_t level);
  37. ; */
  38. .def rt_hw_interrupt_enable
  39. .asmfunc
  40. rt_hw_interrupt_enable
  41. MSR cpsr_c, r0
  42. BX lr
  43. .endasmfunc
  44. ;/*
  45. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  46. ; * r0 --> from
  47. ; * r1 --> to
  48. ; */
  49. .def rt_hw_context_switch
  50. .asmfunc
  51. rt_hw_context_switch
  52. STMDB sp!, {lr} ; push pc (lr should be pushed in place of PC)
  53. STMDB sp!, {r0-r12, lr} ; push lr & register file
  54. MRS r4, cpsr
  55. TST lr, #0x01
  56. ORRNE r4, r4, #0x20 ; it's thumb code
  57. STMDB sp!, {r4} ; push cpsr
  58. .if (__TI_VFP_SUPPORT__)
  59. VMRS r4, fpexc
  60. TST r4, #0x40000000
  61. BEQ __no_vfp_frame1
  62. VSTMDB sp!, {d0-d15}
  63. VMRS r5, fpscr
  64. ; TODO: add support for Common VFPv3.
  65. ; Save registers like FPINST, FPINST2
  66. STMDB sp!, {r5}
  67. __no_vfp_frame1
  68. STMDB sp!, {r4}
  69. .endif
  70. STR sp, [r0] ; store sp in preempted tasks TCB
  71. LDR sp, [r1] ; get new task stack pointer
  72. .if (__TI_VFP_SUPPORT__)
  73. LDMIA sp!, {r0} ; get fpexc
  74. VMSR fpexc, r0 ; restore fpexc
  75. TST r0, #0x40000000
  76. BEQ __no_vfp_frame2
  77. LDMIA sp!, {r1} ; get fpscr
  78. VMSR fpscr, r1
  79. VLDMIA sp!, {d0-d15}
  80. __no_vfp_frame2
  81. .endif
  82. LDMIA sp!, {r4} ; pop new task cpsr to spsr
  83. MSR spsr_cxsf, r4
  84. LDMIA sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
  85. .endasmfunc
  86. ;/*
  87. ; * void rt_hw_context_switch_to(rt_uint32 to);
  88. ; * r0 --> to
  89. ; */
  90. .def rt_hw_context_switch_to
  91. .asmfunc
  92. rt_hw_context_switch_to
  93. LDR sp, [r0] ; get new task stack pointer
  94. .if (__TI_VFP_SUPPORT__)
  95. LDMIA sp!, {r0} ; get fpexc
  96. VMSR fpexc, r0
  97. TST r0, #0x40000000
  98. BEQ __no_vfp_frame_to
  99. LDMIA sp!, {r1} ; get fpscr
  100. VMSR fpscr, r1
  101. VLDMIA sp!, {d0-d15}
  102. __no_vfp_frame_to
  103. .endif
  104. LDMIA sp!, {r4} ; pop new task cpsr to spsr
  105. MSR spsr_cxsf, r4
  106. LDMIA sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
  107. .endasmfunc
  108. ;/*
  109. ; * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  110. ; */
  111. .def rt_hw_context_switch_interrupt
  112. .asmfunc
  113. rt_hw_context_switch_interrupt
  114. LDR r2, pintflag
  115. LDR r3, [r2]
  116. CMP r3, #1
  117. BEQ _reswitch
  118. MOV r3, #1 ; set rt_thread_switch_interrupt_flag to 1
  119. STR r3, [r2]
  120. LDR r2, pfromthread ; set rt_interrupt_from_thread
  121. STR r0, [r2]
  122. _reswitch
  123. LDR r2, ptothread ; set rt_interrupt_to_thread
  124. STR r1, [r2]
  125. BX lr
  126. .endasmfunc
  127. .def IRQ_Handler
  128. IRQ_Handler
  129. STMDB sp!, {r0-r12,lr}
  130. .if (__TI_VFP_SUPPORT__)
  131. VMRS r0, fpexc
  132. TST r0, #0x40000000
  133. BEQ __no_vfp_frame_str_irq
  134. VSTMDB sp!, {d0-d15}
  135. VMRS r1, fpscr
  136. ; TODO: add support for Common VFPv3.
  137. ; Save registers like FPINST, FPINST2
  138. STMDB sp!, {r1}
  139. __no_vfp_frame_str_irq
  140. STMDB sp!, {r0}
  141. .endif
  142. BL rt_interrupt_enter
  143. BL rt_hw_trap_irq
  144. BL rt_interrupt_leave
  145. ; if rt_thread_switch_interrupt_flag set, jump to
  146. ; rt_hw_context_switch_interrupt_do and don't return
  147. LDR r0, pintflag
  148. LDR r1, [r0]
  149. CMP r1, #1
  150. BEQ rt_hw_context_switch_interrupt_do
  151. .if (__TI_VFP_SUPPORT__)
  152. LDMIA sp!, {r0} ; get fpexc
  153. VMSR fpexc, r0
  154. TST r0, #0x40000000
  155. BEQ __no_vfp_frame_ldr_irq
  156. LDMIA sp!, {r1} ; get fpscr
  157. VMSR fpscr, r1
  158. VLDMIA sp!, {d0-d15}
  159. __no_vfp_frame_ldr_irq
  160. .endif
  161. LDMIA sp!, {r0-r12,lr}
  162. SUBS pc, lr, #4
  163. ; /*
  164. ; * void rt_hw_context_switch_interrupt_do(rt_base_t flag)
  165. ; */
  166. .def rt_hw_context_switch_interrupt_do
  167. rt_hw_context_switch_interrupt_do
  168. MOV r1, #0 ; clear flag
  169. STR r1, [r0]
  170. .if (__TI_VFP_SUPPORT__)
  171. LDMIA sp!, {r0} ; get fpexc
  172. VMSR fpexc, r0
  173. TST r0, #0x40000000
  174. BEQ __no_vfp_frame_do1
  175. LDMIA sp!, {r1} ; get fpscr
  176. VMSR fpscr, r1
  177. VLDMIA sp!, {d0-d15}
  178. __no_vfp_frame_do1
  179. .endif
  180. LDMIA sp!, {r0-r12,lr} ; reload saved registers
  181. STMDB sp, {r0-r3} ; save r0-r3. We will restore r0-r3 in the SVC
  182. ; mode so there is no need to update SP.
  183. SUB r1, sp, #16 ; save the right SP value in r1, so we could restore r0-r3.
  184. SUB r2, lr, #4 ; save old task's pc to r2
  185. MRS r3, spsr ; get cpsr of interrupt thread
  186. ; switch to SVC mode and no interrupt
  187. CPSID IF, #0x13
  188. STMDB sp!, {r2} ; push old task's pc
  189. STMDB sp!, {r4-r12,lr} ; push old task's lr,r12-r4
  190. LDMIA r1!, {r4-r7} ; restore r0-r3 of the interrupted thread
  191. STMDB sp!, {r4-r7} ; push old task's r3-r0. We don't need to push/pop them to
  192. ; r0-r3 because we just want to transfer the data and don't
  193. ; use them here.
  194. STMDB sp!, {r3} ; push old task's cpsr
  195. .if (__TI_VFP_SUPPORT__)
  196. VMRS r0, fpexc
  197. TST r0, #0x40000000
  198. BEQ __no_vfp_frame_do2
  199. VSTMDB sp!, {d0-d15}
  200. VMRS r1, fpscr
  201. ; TODO: add support for Common VFPv3.
  202. ; Save registers like FPINST, FPINST2
  203. STMDB sp!, {r1}
  204. __no_vfp_frame_do2
  205. STMDB sp!, {r0}
  206. .endif
  207. LDR r4, pfromthread
  208. LDR r5, [r4]
  209. STR sp, [r5] ; store sp in preempted tasks's TCB
  210. LDR r6, ptothread
  211. LDR r6, [r6]
  212. LDR sp, [r6] ; get new task's stack pointer
  213. .if (__TI_VFP_SUPPORT__)
  214. LDMIA sp!, {r0} ; get fpexc
  215. VMSR fpexc, r0
  216. TST r0, #0x40000000
  217. BEQ __no_vfp_frame_do3
  218. LDMIA sp!, {r1} ; get fpscr
  219. VMSR fpscr, r1
  220. VLDMIA sp!, {d0-d15}
  221. __no_vfp_frame_do3
  222. .endif
  223. LDMIA sp!, {r4} ; pop new task's cpsr to spsr
  224. MSR spsr_cxsf, r4
  225. LDMIA sp!, {r0-r12,lr,pc}^ ; pop new task's r0-r12,lr & pc, copy spsr to cpsr
  226. pintflag .word rt_thread_switch_interrupt_flag
  227. pfromthread .word rt_interrupt_from_thread
  228. ptothread .word rt_interrupt_to_thread