context_gcc.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * 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. * 2019-03-31 xuzhuoyi port to Cortex-M23.
  15. */
  16. .cpu cortex-m23
  17. .fpu softvfp
  18. .syntax unified
  19. .thumb
  20. .text
  21. .equ SCB_VTOR, 0xE000ED08 /* Vector Table Offset Register */
  22. .equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
  23. .equ NVIC_SHPR3, 0xE000ED20 /* system priority register (3) */
  24. .equ NVIC_PENDSV_PRI, 0xFFFF0000 /* PendSV and SysTick priority value (lowest) */
  25. .equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
  26. /*
  27. * rt_base_t rt_hw_interrupt_disable();
  28. */
  29. .global rt_hw_interrupt_disable
  30. .type rt_hw_interrupt_disable, %function
  31. rt_hw_interrupt_disable:
  32. MRS R0, PRIMASK
  33. CPSID I
  34. BX LR
  35. /*
  36. * void rt_hw_interrupt_enable(rt_base_t level);
  37. */
  38. .global rt_hw_interrupt_enable
  39. .type rt_hw_interrupt_enable, %function
  40. rt_hw_interrupt_enable:
  41. MSR PRIMASK, R0
  42. BX LR
  43. /*
  44. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  45. * R0 --> from
  46. * R1 --> to
  47. */
  48. .global rt_hw_context_switch_interrupt
  49. .type rt_hw_context_switch_interrupt, %function
  50. .global rt_hw_context_switch
  51. .type rt_hw_context_switch, %function
  52. rt_hw_context_switch_interrupt:
  53. rt_hw_context_switch:
  54. /* set rt_thread_switch_interrupt_flag to 1 */
  55. LDR R2, =rt_thread_switch_interrupt_flag
  56. LDR R3, [R2]
  57. CMP R3, #1
  58. BEQ _reswitch
  59. MOVS R3, #1
  60. STR R3, [R2]
  61. LDR R2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
  62. STR R0, [R2]
  63. _reswitch:
  64. LDR R2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
  65. STR R1, [R2]
  66. LDR R0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  67. LDR R1, =NVIC_PENDSVSET
  68. STR R1, [R0]
  69. BX LR
  70. /* R0 --> switch from thread stack
  71. * R1 --> switch to thread stack
  72. * psr, pc, LR, R12, R3, R2, R1, R0 are pushed into [from] stack
  73. */
  74. .global PendSV_Handler
  75. .type PendSV_Handler, %function
  76. PendSV_Handler:
  77. /* disable interrupt to protect context switch */
  78. MRS R2, PRIMASK
  79. CPSID I
  80. /* get rt_thread_switch_interrupt_flag */
  81. LDR R0, =rt_thread_switch_interrupt_flag
  82. LDR R1, [R0]
  83. CMP R1, #0x00
  84. BEQ pendsv_exit /* pendsv already handled */
  85. /* clear rt_thread_switch_interrupt_flag to 0 */
  86. MOVS R1, #0
  87. STR R1, [R0]
  88. LDR R0, =rt_interrupt_from_thread
  89. LDR R1, [R0]
  90. CMP R1, #0x00
  91. BEQ switch_to_thread /* skip register save at the first time */
  92. MRS R1, PSP /* get from thread stack pointer */
  93. SUBS R1, R1, #0x20 /* space for {R4 - R7} and {R8 - R11} */
  94. LDR R0, [R0]
  95. STR R1, [R0] /* update from thread stack pointer */
  96. STMIA R1!, {R4 - R7} /* push thread {R4 - R7} register to thread stack */
  97. MOV R4, R8 /* mov thread {R8 - R11} to {R4 - R7} */
  98. MOV R5, R9
  99. MOV R6, R10
  100. MOV R7, R11
  101. STMIA R1!, {R4 - R7} /* push thread {R8 - R11} high register to thread stack */
  102. switch_to_thread:
  103. LDR R1, =rt_interrupt_to_thread
  104. LDR R1, [R1]
  105. LDR R1, [R1] /* load thread stack pointer */
  106. LDMIA R1!, {R4 - R7} /* pop thread {R4 - R7} register from thread stack */
  107. PUSH {R4 - R7} /* push {R4 - R7} to MSP for copy {R8 - R11} */
  108. LDMIA R1!, {R4 - R7} /* pop thread {R8 - R11} high register from thread stack to {R4 - R7} */
  109. MOV R8, R4 /* mov {R4 - R7} to {R8 - R11} */
  110. MOV R9, R5
  111. MOV R10, R6
  112. MOV R11, R7
  113. POP {R4 - R7} /* pop {R4 - R7} from MSP */
  114. MSR PSP, R1 /* update stack pointer */
  115. pendsv_exit:
  116. /* restore interrupt */
  117. MSR PRIMASK, R2
  118. MOVS R0, #0x03
  119. RSBS R0, R0, #0x00
  120. BX R0
  121. /*
  122. * void rt_hw_context_switch_to(rt_uint32 to);
  123. * R0 --> to
  124. */
  125. .global rt_hw_context_switch_to
  126. .type rt_hw_context_switch_to, %function
  127. rt_hw_context_switch_to:
  128. LDR R1, =rt_interrupt_to_thread
  129. STR R0, [R1]
  130. /* set from thread to 0 */
  131. LDR R1, =rt_interrupt_from_thread
  132. MOVS R0, #0
  133. STR R0, [R1]
  134. /* set interrupt flag to 1 */
  135. LDR R1, =rt_thread_switch_interrupt_flag
  136. MOVS R0, #1
  137. STR R0, [R1]
  138. /* set the PendSV and SysTick exception priority */
  139. LDR R0, =NVIC_SHPR3
  140. LDR R1, =NVIC_PENDSV_PRI
  141. LDR R2, [R0,#0x00] /* read */
  142. ORRS R1, R1, R2 /* modify */
  143. STR R1, [R0] /* write-back */
  144. LDR R0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  145. LDR R1, =NVIC_PENDSVSET
  146. STR R1, [R0]
  147. NOP
  148. /* restore MSP */
  149. LDR R0, =SCB_VTOR
  150. LDR R0, [R0]
  151. LDR R0, [R0]
  152. NOP
  153. MSR MSP, R0
  154. /* enable interrupts at processor level */
  155. CPSIE I
  156. /* ensure PendSV exception taken place before subsequent operation */
  157. DSB
  158. ISB
  159. /* never reach here! */
  160. /* compatible with old version */
  161. .global rt_hw_interrupt_thread_switch
  162. .type rt_hw_interrupt_thread_switch, %function
  163. rt_hw_interrupt_thread_switch:
  164. BX LR
  165. NOP
  166. .global HardFault_Handler
  167. .type HardFault_Handler, %function
  168. HardFault_Handler:
  169. /* get current context */
  170. MRS R0, PSP /* get fault thread stack pointer */
  171. PUSH {LR}
  172. BL rt_hw_hard_fault_exception
  173. POP {PC}
  174. /*
  175. * rt_uint32_t rt_hw_interrupt_check(void);
  176. * R0 --> state
  177. */
  178. .global rt_hw_interrupt_check
  179. .type rt_hw_interrupt_check, %function
  180. rt_hw_interrupt_check:
  181. MRS R0, IPSR
  182. BX LR