context_gcc.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /******************************************************************//**
  2. * @file context_gcc.S
  3. * @brief Context switch functions
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author Bernard, onelife
  6. * @version 0.4 beta
  7. **********************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file LICENSE in this
  10. * distribution or at http://www.rt-thread.org/license/LICENSE
  11. **********************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2009-10-11 Bernard first version
  15. * 2010-12-29 onelife Modify for EFM32
  16. * 2011-06-17 onelife Merge all of the assembly source code into context_gcc.S
  17. *********************************************************************/
  18. /******************************************************************//**
  19. * @addtogroup cortex-m3
  20. * @{
  21. *********************************************************************/
  22. .cpu cortex-m3
  23. .fpu softvfp
  24. .syntax unified
  25. .thumb
  26. .text
  27. .equ ICSR, 0xE000ED04 /* interrupt control state register */
  28. .equ PENDSVSET_BIT, 0x10000000 /* value to trigger PendSV exception */
  29. .equ SHPR3, 0xE000ED20 /* system priority register (3) */
  30. .equ PENDSV_PRI_LOWEST, 0x00FF0000 /* PendSV priority value (lowest) */
  31. /*
  32. * rt_base_t rt_hw_interrupt_disable();
  33. */
  34. .global rt_hw_interrupt_disable
  35. .type rt_hw_interrupt_disable, %function
  36. rt_hw_interrupt_disable:
  37. MRS R0, PRIMASK
  38. CPSID I
  39. BX LR
  40. /*
  41. * void rt_hw_interrupt_enable(rt_base_t level);
  42. */
  43. .global rt_hw_interrupt_enable
  44. .type rt_hw_interrupt_enable, %function
  45. rt_hw_interrupt_enable:
  46. MSR PRIMASK, R0
  47. BX LR
  48. /*
  49. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  50. * R0 --> from
  51. * R1 --> to
  52. */
  53. .global rt_hw_context_switch_interrupt
  54. .type rt_hw_context_switch_interrupt, %function
  55. .global rt_hw_context_switch
  56. .type rt_hw_context_switch, %function
  57. rt_hw_context_switch_interrupt:
  58. rt_hw_context_switch:
  59. /* set rt_thread_switch_interrput_flag to 1 */
  60. LDR R2, =rt_thread_switch_interrput_flag
  61. LDR R3, [R2]
  62. CMP R3, #1
  63. BEQ _reswitch
  64. MOV R3, #1
  65. STR R3, [R2]
  66. LDR R2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
  67. STR R0, [R2]
  68. _reswitch:
  69. LDR R2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
  70. STR R1, [R2]
  71. LDR R0, =ICSR /* trigger the PendSV exception (causes context switch) */
  72. LDR R1, =PENDSVSET_BIT
  73. STR R1, [R0]
  74. BX LR
  75. /* R0 --> swith from thread stack
  76. * R1 --> swith to thread stack
  77. * psr, pc, LR, R12, R3, R2, R1, R0 are pushed into [from] stack
  78. */
  79. .global PendSV_Handler
  80. .type PendSV_Handler, %function
  81. PendSV_Handler:
  82. /* disable interrupt to protect context switch */
  83. MRS R2, PRIMASK
  84. CPSID I
  85. /* get rt_thread_switch_interrupt_flag */
  86. LDR R0, =rt_thread_switch_interrput_flag
  87. LDR R1, [R0]
  88. CBZ R1, pendsv_exit /* pendsv aLReady handled */
  89. /* clear rt_thread_switch_interrput_flag to 0 */
  90. MOV R1, #0
  91. STR R1, [R0]
  92. LDR R0, =rt_interrupt_from_thread
  93. LDR R1, [R0]
  94. CBZ R1, swtich_to_thread /* skip register save at the first time */
  95. MRS R1, PSP /* get from thread stack pointer */
  96. STMFD R1!, {R4 - R11} /* push R4 - R11 register */
  97. LDR R0, [R0]
  98. STR R1, [R0] /* update from thread stack pointer */
  99. swtich_to_thread:
  100. LDR R1, =rt_interrupt_to_thread
  101. LDR R1, [R1]
  102. LDR R1, [R1] /* load thread stack pointer */
  103. LDMFD R1!, {R4 - R11} /* pop R4 - R11 register */
  104. MSR PSP, R1 /* update stack pointer */
  105. pendsv_exit:
  106. /* restore interrupt */
  107. MSR PRIMASK, R2
  108. ORR LR, LR, #0x04
  109. BX LR
  110. /*
  111. * void rt_hw_context_switch_to(rt_uint32 to);
  112. * R0 --> to
  113. */
  114. .global rt_hw_context_switch_to
  115. .type rt_hw_context_switch_to, %function
  116. rt_hw_context_switch_to:
  117. LDR R1, =rt_interrupt_to_thread
  118. STR R0, [R1]
  119. /* set from thread to 0 */
  120. LDR R1, =rt_interrupt_from_thread
  121. MOV R0, #0
  122. STR R0, [R1]
  123. /* set interrupt flag to 1 */
  124. LDR R1, =rt_thread_switch_interrput_flag
  125. MOV R0, #1
  126. STR R0, [R1]
  127. /* set the PendSV exception priority */
  128. LDR R0, =SHPR3
  129. LDR R1, =PENDSV_PRI_LOWEST
  130. LDR.W R2, [R0,#0] /* read */
  131. ORR R1, R1, R2 /* modify */
  132. STR R1, [R0] /* write-back */
  133. LDR R0, =ICSR /* trigger the PendSV exception (causes context switch) */
  134. LDR R1, =PENDSVSET_BIT
  135. STR R1, [R0]
  136. CPSIE I /* enable interrupts at processor level */
  137. /* never reach here! */
  138. /* compatible with old version */
  139. .global rt_hw_interrupt_thread_switch
  140. .type rt_hw_interrupt_thread_switch, %function
  141. rt_hw_interrupt_thread_switch:
  142. BX LR
  143. NOP
  144. .global HardFault_Handler
  145. .type HardFault_Handler, %function
  146. HardFault_Handler:
  147. /* get current context */
  148. MRS r0, psp /* get fault thread stack pointer */
  149. PUSH {lr}
  150. BL rt_hw_hard_fault_exception
  151. POP {lr}
  152. ORR lr, lr, #0x04
  153. BX lr
  154. /******************************************************************//**
  155. * @}
  156. *********************************************************************/