context_gcc.S 6.2 KB

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