context_gcc.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-01-25 Bernard first version
  9. * 2012-06-01 aozima set pendsv priority to 0xFF.
  10. * 2012-08-17 aozima fixed bug: store r8 - r11.
  11. * 2013-02-20 aozima port to gcc.
  12. * 2013-06-18 aozima add restore MSP feature.
  13. * 2013-11-04 bright fixed hardfault bug for gcc.
  14. */
  15. .cpu cortex-m0
  16. .fpu softvfp
  17. .syntax unified
  18. .thumb
  19. .text
  20. .equ SCB_VTOR, 0xE000ED08 /* Vector Table Offset Register */
  21. .equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
  22. .equ NVIC_SHPR3, 0xE000ED20 /* system priority register (3) */
  23. .equ NVIC_PENDSV_PRI, 0x00FF0000 /* PendSV priority value (lowest) */
  24. .equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
  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. MOVS 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, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  66. LDR R1, =NVIC_PENDSVSET
  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. CMP R1, #0x00
  83. BEQ pendsv_exit /* pendsv aLReady handled */
  84. /* clear rt_thread_switch_interrupt_flag to 0 */
  85. MOVS R1, #0
  86. STR R1, [R0]
  87. LDR R0, =rt_interrupt_from_thread
  88. LDR R1, [R0]
  89. CMP R1, #0x00
  90. BEQ switch_to_thread /* skip register save at the first time */
  91. MRS R1, PSP /* get from thread stack pointer */
  92. SUBS R1, R1, #0x20 /* space for {R4 - R7} and {R8 - R11} */
  93. LDR R0, [R0]
  94. STR R1, [R0] /* update from thread stack pointer */
  95. STMIA R1!, {R4 - R7} /* push thread {R4 - R7} register to thread stack */
  96. MOV R4, R8 /* mov thread {R8 - R11} to {R4 - R7} */
  97. MOV R5, R9
  98. MOV R6, R10
  99. MOV R7, R11
  100. STMIA R1!, {R4 - R7} /* push thread {R8 - R11} high register to thread stack */
  101. switch_to_thread:
  102. LDR R1, =rt_interrupt_to_thread
  103. LDR R1, [R1]
  104. LDR R1, [R1] /* load thread stack pointer */
  105. LDMIA R1!, {R4 - R7} /* pop thread {R4 - R7} register from thread stack */
  106. PUSH {R4 - R7} /* push {R4 - R7} to MSP for copy {R8 - R11} */
  107. LDMIA R1!, {R4 - R7} /* pop thread {R8 - R11} high register from thread stack to {R4 - R7} */
  108. MOV R8, R4 /* mov {R4 - R7} to {R8 - R11} */
  109. MOV R9, R5
  110. MOV R10, R6
  111. MOV R11, R7
  112. POP {R4 - R7} /* pop {R4 - R7} from MSP */
  113. MSR PSP, R1 /* update stack pointer */
  114. pendsv_exit:
  115. /* restore interrupt */
  116. MSR PRIMASK, R2
  117. MOVS R0, #0x04
  118. RSBS R0, R0, #0x00
  119. BX R0
  120. /*
  121. * void rt_hw_context_switch_to(rt_uint32 to);
  122. * R0 --> to
  123. */
  124. .global rt_hw_context_switch_to
  125. .type rt_hw_context_switch_to, %function
  126. rt_hw_context_switch_to:
  127. LDR R1, =rt_interrupt_to_thread
  128. STR R0, [R1]
  129. /* set from thread to 0 */
  130. LDR R1, =rt_interrupt_from_thread
  131. MOVS R0, #0
  132. STR R0, [R1]
  133. /* set interrupt flag to 1 */
  134. LDR R1, =rt_thread_switch_interrupt_flag
  135. MOVS R0, #1
  136. STR R0, [R1]
  137. /* set the PendSV exception priority */
  138. LDR R0, =NVIC_SHPR3
  139. LDR R1, =NVIC_PENDSV_PRI
  140. LDR R2, [R0,#0x00] /* read */
  141. ORRS R1, R1, R2 /* modify */
  142. STR R1, [R0] /* write-back */
  143. LDR R0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  144. LDR R1, =NVIC_PENDSVSET
  145. STR R1, [R0]
  146. NOP
  147. /* restore MSP */
  148. LDR R0, =SCB_VTOR
  149. LDR R0, [R0]
  150. LDR R0, [R0]
  151. NOP
  152. MSR MSP, R0
  153. /* enable interrupts at processor level */
  154. CPSIE I
  155. /* never reach here! */
  156. /* compatible with old version */
  157. .global rt_hw_interrupt_thread_switch
  158. .type rt_hw_interrupt_thread_switch, %function
  159. rt_hw_interrupt_thread_switch:
  160. BX LR
  161. NOP
  162. .global HardFault_Handler
  163. .type HardFault_Handler, %function
  164. HardFault_Handler:
  165. /* get current context */
  166. MRS R0, PSP /* get fault thread stack pointer */
  167. PUSH {LR}
  168. BL rt_hw_hard_fault_exception
  169. POP {PC}
  170. /*
  171. * rt_uint32_t rt_hw_interrupt_check(void);
  172. * R0 --> state
  173. */
  174. .global rt_hw_interrupt_check
  175. .type rt_hw_interrupt_check, %function
  176. rt_hw_interrupt_check:
  177. MRS R0, IPSR
  178. BX LR