context_gcc.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * File : context_gcc.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, 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. * 2009-10-11 Bernard first version
  13. * 2012-01-01 aozima support context switch load/store FPU register.
  14. * 2013-06-18 aozima add restore MSP feature.
  15. */
  16. /**
  17. * @addtogroup STM32
  18. */
  19. /*@{*/
  20. .cpu cortex-m4
  21. .syntax unified
  22. .thumb
  23. .text
  24. .equ SCB_VTOR, 0xE000ED04 /* Vector Table Offset Register */
  25. .equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
  26. .equ NVIC_SYSPRI2, 0xE000ED20 /* system priority register (2) */
  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. MOV 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 --> swith from thread stack
  74. * r1 --> swith 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. CBZ r1, pendsv_exit /* pendsv already handled */
  87. /* clear rt_thread_switch_interrupt_flag to 0 */
  88. MOV r1, #0x00
  89. STR r1, [r0]
  90. LDR r0, =rt_interrupt_from_thread
  91. LDR r1, [r0]
  92. CBZ r1, swtich_to_thread /* skip register save at the first time */
  93. MRS r1, psp /* get from thread stack pointer */
  94. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  95. VSTMDB r1!, {d8 - d15} /* push FPU register s16~s31 */
  96. #endif
  97. STMFD r1!, {r4 - r11} /* push r4 - r11 register */
  98. LDR r0, [r0]
  99. STR r1, [r0] /* update from thread stack pointer */
  100. swtich_to_thread:
  101. LDR r1, =rt_interrupt_to_thread
  102. LDR r1, [r1]
  103. LDR r1, [r1] /* load thread stack pointer */
  104. LDMFD r1!, {r4 - r11} /* pop r4 - r11 register */
  105. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  106. VLDMIA r1!, {d8 - d15} /* pop FPU register s16~s31 */
  107. #endif
  108. MSR psp, r1 /* update stack pointer */
  109. pendsv_exit:
  110. /* restore interrupt */
  111. MSR PRIMASK, r2
  112. ORR lr, lr, #0x04
  113. BX lr
  114. /*
  115. * void rt_hw_context_switch_to(rt_uint32 to);
  116. * r0 --> to
  117. */
  118. .global rt_hw_context_switch_to
  119. .type rt_hw_context_switch_to, %function
  120. rt_hw_context_switch_to:
  121. LDR r1, =rt_interrupt_to_thread
  122. STR r0, [r1]
  123. /* set from thread to 0 */
  124. LDR r1, =rt_interrupt_from_thread
  125. MOV r0, #0x0
  126. STR r0, [r1]
  127. /* set interrupt flag to 1 */
  128. LDR r1, =rt_thread_switch_interrupt_flag
  129. MOV r0, #1
  130. STR r0, [r1]
  131. /* set the PendSV exception priority */
  132. LDR r0, =NVIC_SYSPRI2
  133. LDR r1, =NVIC_PENDSV_PRI
  134. LDR.W r2, [r0,#0x00] /* read */
  135. ORR r1,r1,r2 /* modify */
  136. STR r1, [r0] /* write-back */
  137. LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  138. LDR r1, =NVIC_PENDSVSET
  139. STR r1, [r0]
  140. /* restore MSP */
  141. LDR r0, =SCB_VTOR
  142. LDR r0, [r0]
  143. LDR r0, [r0]
  144. NOP
  145. MSR msp, r0
  146. CPSIE I /* enable interrupts at processor level */
  147. /* never reach here! */
  148. /* compatible with old version */
  149. .global rt_hw_interrupt_thread_switch
  150. .type rt_hw_interrupt_thread_switch, %function
  151. rt_hw_interrupt_thread_switch:
  152. BX lr
  153. NOP
  154. .global HardFault_Handler
  155. .type HardFault_Handler, %function
  156. HardFault_Handler:
  157. /* get current context */
  158. MRS r0, psp /* get fault thread stack pointer */
  159. PUSH {lr}
  160. BL rt_hw_hard_fault_exception
  161. POP {lr}
  162. ORR lr, lr, #0x04
  163. BX lr