context_gcc.S 6.0 KB

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