context_ccs.asm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. STMFD sp!, {lr} ; push pc (lr should be pushed in place of PC)
  48. STMFD sp!, {r0-r12, lr} ; push lr & register file
  49. MRS r4, cpsr
  50. TST lr, #0x01
  51. BEQ _ARM_MODE
  52. ORR r4, r4, #0x20 ; it's thumb code
  53. _ARM_MODE
  54. STMFD sp!, {r4} ; push cpsr
  55. STR sp, [r0] ; store sp in preempted tasks TCB
  56. LDR sp, [r1] ; get new task stack pointer
  57. LDMFD sp!, {r4} ; pop new task cpsr to spsr
  58. MSR spsr_cxsf, r4
  59. BIC r4, r4, #0x20 ; must be ARM mode
  60. MSR cpsr_cxsf, r4
  61. LDMFD sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
  62. ;/*
  63. ; * void rt_hw_context_switch_to(rt_uint32 to);
  64. ; * r0 --> to
  65. ; */
  66. .def rt_hw_context_switch_to
  67. rt_hw_context_switch_to
  68. LDR sp, [r0] ; get new task stack pointer
  69. LDMFD sp!, {r4} ; pop new task cpsr to spsr
  70. MSR spsr_cxsf, r4
  71. LDMFD sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
  72. ;/*
  73. ; * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  74. ; */
  75. .def rt_hw_context_switch_interrupt
  76. rt_hw_context_switch_interrupt
  77. LDR r2, pintflag
  78. LDR r3, [r2]
  79. CMP r3, #1
  80. BEQ _reswitch
  81. MOV r3, #1 ; set rt_thread_switch_interrupt_flag to 1
  82. STR r3, [r2]
  83. LDR r2, pfromthread ; set rt_interrupt_from_thread
  84. STR r0, [r2]
  85. _reswitch
  86. LDR r2, ptothread ; set rt_interrupt_to_thread
  87. STR r1, [r2]
  88. BX lr
  89. .def IRQ_Handler
  90. IRQ_Handler
  91. STMFD sp!, {r0-r12,lr}
  92. BL rt_interrupt_enter
  93. BL rt_hw_trap_irq
  94. BL rt_interrupt_leave
  95. ; if rt_thread_switch_interrupt_flag set, jump to
  96. ; rt_hw_context_switch_interrupt_do and don't return
  97. LDR r0, pintflag
  98. LDR r1, [r0]
  99. CMP r1, #1
  100. BEQ rt_hw_context_switch_interrupt_do
  101. LDMFD sp!, {r0-r12,lr}
  102. SUBS pc, lr, #4
  103. ; /*
  104. ; * void rt_hw_context_switch_interrupt_do(rt_base_t flag)
  105. ; */
  106. .def rt_hw_context_switch_interrupt_do
  107. rt_hw_context_switch_interrupt_do
  108. MOV r1, #0 ; clear flag
  109. STR r1, [r0]
  110. LDMFD sp!, {r0-r12,lr} ; reload saved registers
  111. STMFD sp, {r0-r3} ; save r0-r3. We will restore r0-r3 in the SVC
  112. ; mode so there is no need to update SP.
  113. SUB r1, sp, #16 ; save the right SP value in r1, so we could restore r0-r3.
  114. SUB r2, lr, #4 ; save old task's pc to r2
  115. MRS r3, spsr ; get cpsr of interrupt thread
  116. ; switch to SVC mode and no interrupt
  117. CPSID IF, #0x13
  118. STMFD sp!, {r2} ; push old task's pc
  119. STMFD sp!, {r4-r12,lr} ; push old task's lr,r12-r4
  120. MOV r4, r1 ; move original irq sp to r4
  121. MOV r5, r3 ; move spsr to r5 FIXME: use `MRS r5 spsr` here?
  122. LDMFD r4!, {r0-r3} ; restore r0-r3 of the interrupted thread
  123. STMFD sp!, {r0-r3} ; push old task's r3-r0
  124. ; FIXME: or move the `MRS r5 spsr` here
  125. STMFD sp!, {r5} ; push old task's cpsr
  126. LDR r4, pfromthread
  127. LDR r5, [r4]
  128. STR sp, [r5] ; store sp in preempted tasks's TCB
  129. LDR r6, ptothread
  130. LDR r6, [r6]
  131. LDR sp, [r6] ; get new task's stack pointer
  132. LDMFD sp!, {r4} ; pop new task's cpsr to spsr
  133. MSR spsr_cxsf, r4
  134. LDMFD sp!, {r0-r12,lr,pc}^ ; pop new task's r0-r12,lr & pc, copy spsr to cpsr
  135. pintflag .word rt_thread_switch_interrupt_flag
  136. pfromthread .word rt_interrupt_from_thread
  137. ptothread .word rt_interrupt_to_thread