context_gcc.S 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * File : context_gcc.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2013, 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-10-11 Bernard First version
  13. * 2010-12-29 onelife Modify for EFM32
  14. * 2011-06-17 onelife Merge all of the assembly source code into context_gcc.S
  15. * 2011-07-12 onelife Add interrupt context check function
  16. * 2013-06-18 aozima add restore MSP feature.
  17. * 2013-07-09 aozima enhancement hard fault exception handler.
  18. */
  19. .cpu cortex-m3
  20. .fpu softvfp
  21. .syntax unified
  22. .thumb
  23. .text
  24. .equ SCB_VTOR, 0xE000ED08 /* Vector Table Offset Register */
  25. .equ ICSR, 0xE000ED04 /* interrupt control state register */
  26. .equ PENDSVSET_BIT, 0x10000000 /* value to trigger PendSV exception */
  27. .equ SHPR3, 0xE000ED20 /* system priority register (3) */
  28. .equ PENDSV_PRI_LOWEST, 0x00FF0000 /* PendSV priority value (lowest) */
  29. /*
  30. * rt_base_t rt_hw_interrupt_disable();
  31. */
  32. .global rt_hw_interrupt_disable
  33. .type rt_hw_interrupt_disable, %function
  34. rt_hw_interrupt_disable:
  35. MRS R0, PRIMASK
  36. CPSID I
  37. BX LR
  38. /*
  39. * void rt_hw_interrupt_enable(rt_base_t level);
  40. */
  41. .global rt_hw_interrupt_enable
  42. .type rt_hw_interrupt_enable, %function
  43. rt_hw_interrupt_enable:
  44. MSR PRIMASK, R0
  45. BX LR
  46. /*
  47. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  48. * R0 --> from
  49. * R1 --> to
  50. */
  51. .global rt_hw_context_switch_interrupt
  52. .type rt_hw_context_switch_interrupt, %function
  53. .global rt_hw_context_switch
  54. .type rt_hw_context_switch, %function
  55. rt_hw_context_switch_interrupt:
  56. rt_hw_context_switch:
  57. /* set rt_thread_switch_interrupt_flag to 1 */
  58. LDR R2, =rt_thread_switch_interrupt_flag
  59. LDR R3, [R2]
  60. CMP R3, #1
  61. BEQ _reswitch
  62. MOV R3, #1
  63. STR R3, [R2]
  64. LDR R2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
  65. STR R0, [R2]
  66. _reswitch:
  67. LDR R2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
  68. STR R1, [R2]
  69. LDR R0, =ICSR /* trigger the PendSV exception (causes context switch) */
  70. LDR R1, =PENDSVSET_BIT
  71. STR R1, [R0]
  72. BX LR
  73. /* R0 --> switch from thread stack
  74. * R1 --> switch to thread stack
  75. * psr, pc, LR, R12, R3, R2, R1, R0 are pushed into [from] stack
  76. */
  77. .global PendSV_Handler
  78. .type PendSV_Handler, %function
  79. PendSV_Handler:
  80. /* disable interrupt to protect context switch */
  81. MRS R2, PRIMASK
  82. CPSID I
  83. /* get rt_thread_switch_interrupt_flag */
  84. LDR R0, =rt_thread_switch_interrupt_flag
  85. LDR R1, [R0]
  86. CBZ R1, pendsv_exit /* pendsv aLReady handled */
  87. /* clear rt_thread_switch_interrupt_flag to 0 */
  88. MOV R1, #0
  89. STR R1, [R0]
  90. LDR R0, =rt_interrupt_from_thread
  91. LDR R1, [R0]
  92. CBZ R1, switch_to_thread /* skip register save at the first time */
  93. MRS R1, PSP /* get from thread stack pointer */
  94. STMFD R1!, {R4 - R11} /* push R4 - R11 register */
  95. LDR R0, [R0]
  96. STR R1, [R0] /* update from thread stack pointer */
  97. switch_to_thread:
  98. LDR R1, =rt_interrupt_to_thread
  99. LDR R1, [R1]
  100. LDR R1, [R1] /* load thread stack pointer */
  101. LDMFD R1!, {R4 - R11} /* pop R4 - R11 register */
  102. MSR PSP, R1 /* update stack pointer */
  103. pendsv_exit:
  104. /* restore interrupt */
  105. MSR PRIMASK, R2
  106. ORR LR, LR, #0x04
  107. BX LR
  108. /*
  109. * void rt_hw_context_switch_to(rt_uint32 to);
  110. * R0 --> to
  111. */
  112. .global rt_hw_context_switch_to
  113. .type rt_hw_context_switch_to, %function
  114. rt_hw_context_switch_to:
  115. LDR R1, =rt_interrupt_to_thread
  116. STR R0, [R1]
  117. /* set from thread to 0 */
  118. LDR R1, =rt_interrupt_from_thread
  119. MOV R0, #0
  120. STR R0, [R1]
  121. /* set interrupt flag to 1 */
  122. LDR R1, =rt_thread_switch_interrupt_flag
  123. MOV R0, #1
  124. STR R0, [R1]
  125. /* set the PendSV exception priority */
  126. LDR R0, =SHPR3
  127. LDR R1, =PENDSV_PRI_LOWEST
  128. LDR.W R2, [R0,#0] /* read */
  129. ORR R1, R1, R2 /* modify */
  130. STR R1, [R0] /* write-back */
  131. LDR R0, =ICSR /* trigger the PendSV exception (causes context switch) */
  132. LDR R1, =PENDSVSET_BIT
  133. STR R1, [R0]
  134. /* restore MSP */
  135. LDR r0, =SCB_VTOR
  136. LDR r0, [r0]
  137. LDR r0, [r0]
  138. NOP
  139. MSR msp, r0
  140. CPSIE I /* enable interrupts at processor level */
  141. /* never reach here! */
  142. /* compatible with old version */
  143. .global rt_hw_interrupt_thread_switch
  144. .type rt_hw_interrupt_thread_switch, %function
  145. rt_hw_interrupt_thread_switch:
  146. BX LR
  147. NOP
  148. .global HardFault_Handler
  149. .type HardFault_Handler, %function
  150. HardFault_Handler:
  151. /* get current context */
  152. MRS r0, msp /* get fault context from handler. */
  153. TST lr, #0x04 /* if(!EXC_RETURN[2]) */
  154. BEQ _get_sp_done
  155. MRS r0, psp /* get fault context from thread. */
  156. _get_sp_done:
  157. STMFD r0!, {r4 - r11} /* push r4 - r11 register */
  158. STMFD r0!, {lr} /* push exec_return register */
  159. TST lr, #0x04 /* if(!EXC_RETURN[2])
  160. BEQ _update_msp
  161. MSR psp, r0 /* update stack pointer to PSP. */
  162. B _update_done
  163. _update_msp:
  164. MSR msp, r0 /* update stack pointer to MSP. */
  165. _update_done:
  166. PUSH {LR}
  167. BL rt_hw_hard_fault_exception
  168. POP {LR}
  169. ORR LR, LR, #0x04
  170. BX LR
  171. /*
  172. * rt_uint32_t rt_hw_interrupt_check(void);
  173. * R0 --> state
  174. */
  175. .global rt_hw_interrupt_check
  176. .type rt_hw_interrupt_check, %function
  177. rt_hw_interrupt_check:
  178. MRS R0, IPSR
  179. BX LR