context_gcc.S 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. */
  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, 0xFFFF0000 /* PendSV and SysTick priority value (lowest) */
  24. .equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
  25. #include "../rtconfig.h"
  26. #ifdef RT_USING_SMP
  27. .equ SIO_CPUID, 0xd0000000 /* CPUID */
  28. #define rt_hw_interrupt_disable rt_hw_local_irq_disable
  29. #define rt_hw_interrupt_enable rt_hw_local_irq_enable
  30. #endif
  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. #ifndef RT_USING_SMP
  60. /* set rt_thread_switch_interrupt_flag to 1 */
  61. LDR R2, =rt_thread_switch_interrupt_flag
  62. LDR R3, [R2]
  63. CMP R3, #1
  64. BEQ _reswitch
  65. MOVS R3, #1
  66. STR R3, [R2]
  67. LDR R2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
  68. STR R0, [R2]
  69. _reswitch:
  70. LDR R2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
  71. STR R1, [R2]
  72. #else
  73. /* context_switch_to smp */
  74. PUSH {LR}
  75. BL __rt_cpu_switch
  76. POP {R2}
  77. #endif
  78. LDR R0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  79. LDR R1, =NVIC_PENDSVSET
  80. STR R1, [R0]
  81. #ifndef RT_USING_SMP
  82. BX LR
  83. #else
  84. BX R2
  85. #endif
  86. /* R0 --> switch from thread stack
  87. * R1 --> switch to thread stack
  88. * psr, pc, LR, R12, R3, R2, R1, R0 are pushed into [from] stack
  89. */
  90. .global PendSV_Handler
  91. .type PendSV_Handler, %function
  92. PendSV_Handler:
  93. /* disable interrupt to protect context switch */
  94. MRS R2, PRIMASK
  95. CPSID I
  96. #ifndef RT_USING_SMP
  97. /* get rt_thread_switch_interrupt_flag */
  98. LDR R0, =rt_thread_switch_interrupt_flag
  99. #else
  100. LDR R0, =SIO_CPUID
  101. LDR R1, [R0]
  102. LDR R3, =rt_thread_switch_array
  103. CMP R1, #0
  104. BEQ cpu0_info
  105. ADDS R3, #12
  106. cpu0_info:
  107. MOV R0, R3
  108. #endif
  109. LDR R1, [R0]
  110. CMP R1, #0x00
  111. BEQ pendsv_exit /* pendsv already handled */
  112. /* clear rt_thread_switch_interrupt_flag to 0 */
  113. MOVS R1, #0
  114. STR R1, [R0]
  115. #ifndef RT_USING_SMP
  116. LDR R0, =rt_interrupt_from_thread
  117. #else
  118. ADDS R0, #4
  119. #endif
  120. LDR R1, [R0]
  121. CMP R1, #0x00
  122. BEQ switch_to_thread /* skip register save at the first time */
  123. MRS R1, PSP /* get from thread stack pointer */
  124. SUBS R1, R1, #0x20 /* space for {R4 - R7} and {R8 - R11} */
  125. LDR R0, [R0]
  126. STR R1, [R0] /* update from thread stack pointer */
  127. STMIA R1!, {R4 - R7} /* push thread {R4 - R7} register to thread stack */
  128. MOV R4, R8 /* mov thread {R8 - R11} to {R4 - R7} */
  129. MOV R5, R9
  130. MOV R6, R10
  131. MOV R7, R11
  132. STMIA R1!, {R4 - R7} /* push thread {R8 - R11} high register to thread stack */
  133. switch_to_thread:
  134. #ifndef RT_USING_SMP
  135. LDR R1, =rt_interrupt_to_thread
  136. #else
  137. MOV R1, R3
  138. ADDS R1, #8
  139. #endif
  140. LDR R1, [R1]
  141. LDR R1, [R1] /* load thread stack pointer */
  142. LDMIA R1!, {R4 - R7} /* pop thread {R4 - R7} register from thread stack */
  143. PUSH {R4 - R7} /* push {R4 - R7} to MSP for copy {R8 - R11} */
  144. LDMIA R1!, {R4 - R7} /* pop thread {R8 - R11} high register from thread stack to {R4 - R7} */
  145. MOV R8, R4 /* mov {R4 - R7} to {R8 - R11} */
  146. MOV R9, R5
  147. MOV R10, R6
  148. MOV R11, R7
  149. POP {R4 - R7} /* pop {R4 - R7} from MSP */
  150. MSR PSP, R1 /* update stack pointer */
  151. pendsv_exit:
  152. /* restore interrupt */
  153. MSR PRIMASK, R2
  154. MOVS R0, #0x03
  155. RSBS R0, R0, #0x00
  156. BX R0
  157. /*
  158. * void rt_hw_context_switch_to(rt_uint32 to);
  159. * R0 --> to
  160. */
  161. .global rt_hw_context_switch_to
  162. .type rt_hw_context_switch_to, %function
  163. rt_hw_context_switch_to:
  164. #ifndef RT_USING_SMP
  165. LDR R1, =rt_interrupt_to_thread
  166. STR R0, [R1]
  167. /* set from thread to 0 */
  168. LDR R1, =rt_interrupt_from_thread
  169. MOVS R0, #0
  170. STR R0, [R1]
  171. /* set interrupt flag to 1 */
  172. LDR R1, =rt_thread_switch_interrupt_flag
  173. MOVS R0, #1
  174. STR R0, [R1]
  175. #else
  176. /* context_switch_to smp */
  177. MOV R2,R1
  178. MOV R1,R0
  179. MOVS R0,#0
  180. BL __rt_cpu_switch
  181. #endif
  182. /* set the PendSV and SysTick exception priority */
  183. LDR R0, =NVIC_SHPR3
  184. LDR R1, =NVIC_PENDSV_PRI
  185. LDR R2, [R0,#0x00] /* read */
  186. ORRS R1, R1, R2 /* modify */
  187. STR R1, [R0] /* write-back */
  188. LDR R0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  189. LDR R1, =NVIC_PENDSVSET
  190. STR R1, [R0]
  191. NOP
  192. /* restore MSP */
  193. LDR R0, =SCB_VTOR
  194. LDR R0, [R0]
  195. LDR R0, [R0]
  196. NOP
  197. MSR MSP, R0
  198. /* enable interrupts at processor level */
  199. CPSIE I
  200. /* ensure PendSV exception taken place before subsequent operation */
  201. DSB
  202. ISB
  203. /* never reach here! */
  204. /* compatible with old version */
  205. .global rt_hw_interrupt_thread_switch
  206. .type rt_hw_interrupt_thread_switch, %function
  207. rt_hw_interrupt_thread_switch:
  208. BX LR
  209. NOP
  210. .global HardFault_Handler
  211. .type HardFault_Handler, %function
  212. HardFault_Handler:
  213. /* get current context */
  214. MRS R0, PSP /* get fault thread stack pointer */
  215. PUSH {LR}
  216. BL rt_hw_hard_fault_exception
  217. POP {PC}
  218. /*
  219. * rt_uint32_t rt_hw_interrupt_check(void);
  220. * R0 --> state
  221. */
  222. .global rt_hw_interrupt_check
  223. .type rt_hw_interrupt_check, %function
  224. rt_hw_interrupt_check:
  225. MRS R0, IPSR
  226. BX LR