context_gcc.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. */
  18. .cpu cortex-m3
  19. .fpu softvfp
  20. .syntax unified
  21. .thumb
  22. .text
  23. .equ SCB_VTOR, 0xE000ED04 /* Vector Table Offset Register */
  24. .equ ICSR, 0xE000ED04 /* interrupt control state register */
  25. .equ PENDSVSET_BIT, 0x10000000 /* value to trigger PendSV exception */
  26. .equ SHPR3, 0xE000ED20 /* system priority register (3) */
  27. .equ PENDSV_PRI_LOWEST, 0x00FF0000 /* PendSV priority value (lowest) */
  28. /*
  29. * rt_base_t rt_hw_interrupt_disable();
  30. */
  31. .global rt_hw_interrupt_disable
  32. .type rt_hw_interrupt_disable, %function
  33. rt_hw_interrupt_disable:
  34. MRS R0, PRIMASK
  35. CPSID I
  36. BX LR
  37. /*
  38. * void rt_hw_interrupt_enable(rt_base_t level);
  39. */
  40. .global rt_hw_interrupt_enable
  41. .type rt_hw_interrupt_enable, %function
  42. rt_hw_interrupt_enable:
  43. MSR PRIMASK, R0
  44. BX LR
  45. /*
  46. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  47. * R0 --> from
  48. * R1 --> to
  49. */
  50. .global rt_hw_context_switch_interrupt
  51. .type rt_hw_context_switch_interrupt, %function
  52. .global rt_hw_context_switch
  53. .type rt_hw_context_switch, %function
  54. rt_hw_context_switch_interrupt:
  55. rt_hw_context_switch:
  56. /* set rt_thread_switch_interrupt_flag to 1 */
  57. LDR R2, =rt_thread_switch_interrupt_flag
  58. LDR R3, [R2]
  59. CMP R3, #1
  60. BEQ _reswitch
  61. MOV R3, #1
  62. STR R3, [R2]
  63. LDR R2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
  64. STR R0, [R2]
  65. _reswitch:
  66. LDR R2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
  67. STR R1, [R2]
  68. LDR R0, =ICSR /* trigger the PendSV exception (causes context switch) */
  69. LDR R1, =PENDSVSET_BIT
  70. STR R1, [R0]
  71. BX LR
  72. /* R0 --> swith from thread stack
  73. * R1 --> swith to thread stack
  74. * psr, pc, LR, R12, R3, R2, R1, R0 are pushed into [from] stack
  75. */
  76. .global PendSV_Handler
  77. .type PendSV_Handler, %function
  78. PendSV_Handler:
  79. /* disable interrupt to protect context switch */
  80. MRS R2, PRIMASK
  81. CPSID I
  82. /* get rt_thread_switch_interrupt_flag */
  83. LDR R0, =rt_thread_switch_interrupt_flag
  84. LDR R1, [R0]
  85. CBZ R1, pendsv_exit /* pendsv aLReady handled */
  86. /* clear rt_thread_switch_interrupt_flag to 0 */
  87. MOV R1, #0
  88. STR R1, [R0]
  89. LDR R0, =rt_interrupt_from_thread
  90. LDR R1, [R0]
  91. CBZ R1, swtich_to_thread /* skip register save at the first time */
  92. MRS R1, PSP /* get from thread stack pointer */
  93. STMFD R1!, {R4 - R11} /* push R4 - R11 register */
  94. LDR R0, [R0]
  95. STR R1, [R0] /* update from thread stack pointer */
  96. swtich_to_thread:
  97. LDR R1, =rt_interrupt_to_thread
  98. LDR R1, [R1]
  99. LDR R1, [R1] /* load thread stack pointer */
  100. LDMFD R1!, {R4 - R11} /* pop R4 - R11 register */
  101. MSR PSP, R1 /* update stack pointer */
  102. pendsv_exit:
  103. /* restore interrupt */
  104. MSR PRIMASK, R2
  105. ORR LR, LR, #0x04
  106. BX LR
  107. /*
  108. * void rt_hw_context_switch_to(rt_uint32 to);
  109. * R0 --> to
  110. */
  111. .global rt_hw_context_switch_to
  112. .type rt_hw_context_switch_to, %function
  113. rt_hw_context_switch_to:
  114. LDR R1, =rt_interrupt_to_thread
  115. STR R0, [R1]
  116. /* set from thread to 0 */
  117. LDR R1, =rt_interrupt_from_thread
  118. MOV R0, #0
  119. STR R0, [R1]
  120. /* set interrupt flag to 1 */
  121. LDR R1, =rt_thread_switch_interrupt_flag
  122. MOV R0, #1
  123. STR R0, [R1]
  124. /* set the PendSV exception priority */
  125. LDR R0, =SHPR3
  126. LDR R1, =PENDSV_PRI_LOWEST
  127. LDR.W R2, [R0,#0] /* read */
  128. ORR R1, R1, R2 /* modify */
  129. STR R1, [R0] /* write-back */
  130. LDR R0, =ICSR /* trigger the PendSV exception (causes context switch) */
  131. LDR R1, =PENDSVSET_BIT
  132. STR R1, [R0]
  133. /* restore MSP */
  134. LDR r0, =SCB_VTOR
  135. LDR r0, [r0]
  136. LDR r0, [r0]
  137. NOP
  138. MSR msp, r0
  139. CPSIE I /* enable interrupts at processor level */
  140. /* never reach here! */
  141. /* compatible with old version */
  142. .global rt_hw_interrupt_thread_switch
  143. .type rt_hw_interrupt_thread_switch, %function
  144. rt_hw_interrupt_thread_switch:
  145. BX LR
  146. NOP
  147. .global HardFault_Handler
  148. .type HardFault_Handler, %function
  149. HardFault_Handler:
  150. /* get current context */
  151. MRS R0, PSP /* get fault thread stack pointer */
  152. PUSH {LR}
  153. BL rt_hw_hard_fault_exception
  154. POP {LR}
  155. ORR LR, LR, #0x04
  156. BX LR
  157. /*
  158. * rt_uint32_t rt_hw_interrupt_check(void);
  159. * R0 --> state
  160. */
  161. .global rt_hw_interrupt_check
  162. .type rt_hw_interrupt_check, %function
  163. rt_hw_interrupt_check:
  164. MRS R0, IPSR
  165. BX LR