context_gcc.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. */
  15. /**
  16. * @addtogroup STM32
  17. */
  18. /*@{*/
  19. .cpu cortex-m4
  20. .fpu vfpv4
  21. .syntax unified
  22. .thumb
  23. .text
  24. .equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
  25. .equ NVIC_SYSPRI2, 0xE000ED20 /* system priority register (2) */
  26. .equ NVIC_PENDSV_PRI, 0x00FF0000 /* PendSV priority value (lowest) */
  27. .equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
  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, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  69. LDR r1, =NVIC_PENDSVSET
  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, #0x00
  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. VSTMDB r1!, {d8 - d15} /* push FPU register s16~s31 */
  94. STMFD r1!, {r4 - r11} /* push r4 - r11 register */
  95. LDR r0, [r0]
  96. STR r1, [r0] /* update from thread stack pointer */
  97. swtich_to_thread:
  98. LDR r1, =rt_interrupt_to_thread
  99. LDR r1, [r1]
  100. LDR r1, [r1] /* load thread stack pointer */
  101. LDMFD r1!, {r4 - r11} /* pop r4 - r11 register */
  102. VLDMIA r1!, {d8 - d15} /* pop FPU register s16~s31 */
  103. MSR psp, r1 /* update stack pointer */
  104. pendsv_exit:
  105. /* restore interrupt */
  106. MSR PRIMASK, r2
  107. ORR lr, lr, #0x04
  108. BX lr
  109. /*
  110. * void rt_hw_context_switch_to(rt_uint32 to);
  111. * r0 --> to
  112. */
  113. .global rt_hw_context_switch_to
  114. .type rt_hw_context_switch_to, %function
  115. rt_hw_context_switch_to:
  116. LDR r1, =rt_interrupt_to_thread
  117. STR r0, [r1]
  118. /* set from thread to 0 */
  119. LDR r1, =rt_interrupt_from_thread
  120. MOV r0, #0x0
  121. STR r0, [r1]
  122. /* set interrupt flag to 1 */
  123. LDR r1, =rt_thread_switch_interrupt_flag
  124. MOV r0, #1
  125. STR r0, [r1]
  126. /* set the PendSV exception priority */
  127. LDR r0, =NVIC_SYSPRI2
  128. LDR r1, =NVIC_PENDSV_PRI
  129. LDR.W r2, [r0,#0x00] /* read */
  130. ORR r1,r1,r2 /* modify */
  131. STR r1, [r0] /* write-back */
  132. LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  133. LDR r1, =NVIC_PENDSVSET
  134. STR r1, [r0]
  135. CPSIE I /* enable interrupts at processor level */
  136. /* never reach here! */
  137. /* compatible with old version */
  138. .global rt_hw_interrupt_thread_switch
  139. .type rt_hw_interrupt_thread_switch, %function
  140. rt_hw_interrupt_thread_switch:
  141. BX lr
  142. NOP
  143. .global HardFault_Handler
  144. .type HardFault_Handler, %function
  145. HardFault_Handler:
  146. /* get current context */
  147. MRS r0, psp /* get fault thread stack pointer */
  148. PUSH {lr}
  149. BL rt_hw_hard_fault_exception
  150. POP {lr}
  151. ORR lr, lr, #0x04
  152. BX lr