context_ccs.asm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. rt_hw_interrupt_disable
  30. MRS r0, cpsr
  31. CPSID IF
  32. BX lr
  33. ;/*
  34. ; * void rt_hw_interrupt_enable(rt_base_t level);
  35. ; */
  36. .def rt_hw_interrupt_enable
  37. rt_hw_interrupt_enable
  38. MSR cpsr_c, r0
  39. BX lr
  40. ;/*
  41. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  42. ; * r0 --> from
  43. ; * r1 --> to
  44. ; */
  45. .def rt_hw_context_switch
  46. rt_hw_context_switch
  47. STMDB sp!, {lr} ; push pc (lr should be pushed in place of PC)
  48. STMDB sp!, {r0-r12, lr} ; push lr & register file
  49. MRS r4, cpsr
  50. TST lr, #0x01
  51. ORRNE r4, r4, #0x20 ; it's thumb code
  52. STMDB sp!, {r4} ; push cpsr
  53. .if (__TI_VFP_SUPPORT__)
  54. VMRS r4, fpexc
  55. TST r4, #0x40000000
  56. BEQ __no_vfp_frame1
  57. VSTMDB sp!, {d0-d15}
  58. VMRS r5, fpscr
  59. ; TODO: add support for Common VFPv3.
  60. ; Save registers like FPINST, FPINST2
  61. STMDB sp!, {r5}
  62. __no_vfp_frame1
  63. STMDB sp!, {r4}
  64. .endif
  65. STR sp, [r0] ; store sp in preempted tasks TCB
  66. LDR sp, [r1] ; get new task stack pointer
  67. .if (__TI_VFP_SUPPORT__)
  68. LDMIA sp!, {r0} ; get fpexc
  69. VMSR fpexc, r0 ; restore fpexc
  70. TST r0, #0x40000000
  71. BEQ __no_vfp_frame2
  72. LDMIA sp!, {r1} ; get fpscr
  73. VMSR fpscr, r1
  74. VLDMIA sp!, {d0-d15}
  75. __no_vfp_frame2
  76. .endif
  77. LDMIA sp!, {r4} ; pop new task cpsr to spsr
  78. MSR spsr_cxsf, r4
  79. LDMIA sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
  80. ;/*
  81. ; * void rt_hw_context_switch_to(rt_uint32 to);
  82. ; * r0 --> to
  83. ; */
  84. .def rt_hw_context_switch_to
  85. rt_hw_context_switch_to
  86. LDR sp, [r0] ; get new task stack pointer
  87. .if (__TI_VFP_SUPPORT__)
  88. LDMIA sp!, {r0} ; get fpexc
  89. VMSR fpexc, r0
  90. TST r0, #0x40000000
  91. BEQ __no_vfp_frame_to
  92. LDMIA sp!, {r1} ; get fpscr
  93. VMSR fpscr, r1
  94. VLDMIA sp!, {d0-d15}
  95. __no_vfp_frame_to
  96. .endif
  97. LDMIA sp!, {r4} ; pop new task cpsr to spsr
  98. MSR spsr_cxsf, r4
  99. LDMIA sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
  100. ;/*
  101. ; * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  102. ; */
  103. .def rt_hw_context_switch_interrupt
  104. rt_hw_context_switch_interrupt
  105. LDR r2, pintflag
  106. LDR r3, [r2]
  107. CMP r3, #1
  108. BEQ _reswitch
  109. MOV r3, #1 ; set rt_thread_switch_interrupt_flag to 1
  110. STR r3, [r2]
  111. LDR r2, pfromthread ; set rt_interrupt_from_thread
  112. STR r0, [r2]
  113. _reswitch
  114. LDR r2, ptothread ; set rt_interrupt_to_thread
  115. STR r1, [r2]
  116. BX lr
  117. .def IRQ_Handler
  118. IRQ_Handler
  119. STMDB sp!, {r0-r12,lr}
  120. .if (__TI_VFP_SUPPORT__)
  121. VMRS r0, fpexc
  122. TST r0, #0x40000000
  123. BEQ __no_vfp_frame_str_irq
  124. VSTMDB sp!, {d0-d15}
  125. VMRS r1, fpscr
  126. ; TODO: add support for Common VFPv3.
  127. ; Save registers like FPINST, FPINST2
  128. STMDB sp!, {r1}
  129. __no_vfp_frame_str_irq
  130. STMDB sp!, {r0}
  131. .endif
  132. BL rt_interrupt_enter
  133. BL rt_hw_trap_irq
  134. BL rt_interrupt_leave
  135. ; if rt_thread_switch_interrupt_flag set, jump to
  136. ; rt_hw_context_switch_interrupt_do and don't return
  137. LDR r0, pintflag
  138. LDR r1, [r0]
  139. CMP r1, #1
  140. BEQ rt_hw_context_switch_interrupt_do
  141. .if (__TI_VFP_SUPPORT__)
  142. LDMIA sp!, {r0} ; get fpexc
  143. VMSR fpexc, r0
  144. TST r0, #0x40000000
  145. BEQ __no_vfp_frame_ldr_irq
  146. LDMIA sp!, {r1} ; get fpscr
  147. VMSR fpscr, r1
  148. VLDMIA sp!, {d0-d15}
  149. __no_vfp_frame_ldr_irq
  150. .endif
  151. LDMIA sp!, {r0-r12,lr}
  152. SUBS pc, lr, #4
  153. ; /*
  154. ; * void rt_hw_context_switch_interrupt_do(rt_base_t flag)
  155. ; */
  156. .def rt_hw_context_switch_interrupt_do
  157. rt_hw_context_switch_interrupt_do
  158. MOV r1, #0 ; clear flag
  159. STR r1, [r0]
  160. .if (__TI_VFP_SUPPORT__)
  161. LDMIA sp!, {r0} ; get fpexc
  162. VMSR fpexc, r0
  163. TST r0, #0x40000000
  164. BEQ __no_vfp_frame_do1
  165. LDMIA sp!, {r1} ; get fpscr
  166. VMSR fpscr, r1
  167. VLDMIA sp!, {d0-d15}
  168. __no_vfp_frame_do1
  169. .endif
  170. LDMIA sp!, {r0-r12,lr} ; reload saved registers
  171. STMDB sp, {r0-r3} ; save r0-r3. We will restore r0-r3 in the SVC
  172. ; mode so there is no need to update SP.
  173. SUB r1, sp, #16 ; save the right SP value in r1, so we could restore r0-r3.
  174. SUB r2, lr, #4 ; save old task's pc to r2
  175. MRS r3, spsr ; get cpsr of interrupt thread
  176. ; switch to SVC mode and no interrupt
  177. CPSID IF, #0x13
  178. STMDB sp!, {r2} ; push old task's pc
  179. STMDB sp!, {r4-r12,lr} ; push old task's lr,r12-r4
  180. LDMIA r1!, {r4-r7} ; restore r0-r3 of the interrupted thread
  181. STMDB sp!, {r4-r7} ; push old task's r3-r0. We don't need to push/pop them to
  182. ; r0-r3 because we just want to transfer the data and don't
  183. ; use them here.
  184. STMDB sp!, {r3} ; push old task's cpsr
  185. .if (__TI_VFP_SUPPORT__)
  186. VMRS r0, fpexc
  187. TST r0, #0x40000000
  188. BEQ __no_vfp_frame_do2
  189. VSTMDB sp!, {d0-d15}
  190. VMRS r1, fpscr
  191. ; TODO: add support for Common VFPv3.
  192. ; Save registers like FPINST, FPINST2
  193. STMDB sp!, {r1}
  194. __no_vfp_frame_do2
  195. STMDB sp!, {r0}
  196. .endif
  197. LDR r4, pfromthread
  198. LDR r5, [r4]
  199. STR sp, [r5] ; store sp in preempted tasks's TCB
  200. LDR r6, ptothread
  201. LDR r6, [r6]
  202. LDR sp, [r6] ; get new task's stack pointer
  203. .if (__TI_VFP_SUPPORT__)
  204. LDMIA sp!, {r0} ; get fpexc
  205. VMSR fpexc, r0
  206. TST r0, #0x40000000
  207. BEQ __no_vfp_frame_do3
  208. LDMIA sp!, {r1} ; get fpscr
  209. VMSR fpscr, r1
  210. VLDMIA sp!, {d0-d15}
  211. __no_vfp_frame_do3
  212. .endif
  213. LDMIA sp!, {r4} ; pop new task's cpsr to spsr
  214. MSR spsr_cxsf, r4
  215. LDMIA sp!, {r0-r12,lr,pc}^ ; pop new task's r0-r12,lr & pc, copy spsr to cpsr
  216. pintflag .word rt_thread_switch_interrupt_flag
  217. pfromthread .word rt_interrupt_from_thread
  218. ptothread .word rt_interrupt_to_thread