context_gcc.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * File : context_gcc.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 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. */
  14. /**
  15. * @addtogroup STM32
  16. */
  17. /*@{*/
  18. .cpu cortex-m3
  19. .fpu softvfp
  20. .syntax unified
  21. .thumb
  22. .text
  23. .equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
  24. .equ NVIC_SYSPRI2, 0xE000ED20 /* system priority register (2) */
  25. .equ NVIC_PENDSV_PRI, 0x00FF0000 /* PendSV priority value (lowest) */
  26. .equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
  27. /*
  28. * rt_base_t rt_hw_interrupt_disable();
  29. */
  30. .global rt_hw_interrupt_disable
  31. rt_hw_interrupt_disable:
  32. MRS r0, PRIMASK
  33. CPSID I
  34. BX LR
  35. /*
  36. * void rt_hw_interrupt_enable(rt_base_t level);
  37. */
  38. .global rt_hw_interrupt_enable
  39. rt_hw_interrupt_enable:
  40. MSR PRIMASK, r0
  41. BX LR
  42. /*
  43. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  44. * r0 --> from
  45. * r1 --> to
  46. */
  47. .global rt_hw_context_switch_interrupt
  48. .global rt_hw_context_switch
  49. rt_hw_context_switch_interrupt:
  50. rt_hw_context_switch:
  51. /* set rt_thread_switch_interrput_flag to 1 */
  52. LDR r2, =rt_thread_switch_interrput_flag
  53. LDR r3, [r2]
  54. CMP r3, #1
  55. BEQ _reswitch
  56. MOV r3, #1
  57. STR r3, [r2]
  58. LDR r2, =rt_interrupt_from_thread /* set rt_interrupt_from_thread */
  59. STR r0, [r2]
  60. _reswitch:
  61. LDR r2, =rt_interrupt_to_thread /* set rt_interrupt_to_thread */
  62. STR r1, [r2]
  63. LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  64. LDR r1, =NVIC_PENDSVSET
  65. STR r1, [r0]
  66. BX LR
  67. /* r0 --> swith from thread stack
  68. * r1 --> swith to thread stack
  69. * psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  70. */
  71. .global rt_hw_pend_sv
  72. rt_hw_pend_sv:
  73. /* disable interrupt to protect context switch */
  74. MRS r2, PRIMASK
  75. CPSID I
  76. /* clear rt_thread_switch_interrput_flag to 0 */
  77. LDR r0, =rt_thread_switch_interrput_flag
  78. MOV r1, #0x00
  79. STR r1, [r0]
  80. LDR r0, =rt_interrupt_from_thread
  81. LDR r1, [r0]
  82. CBZ r1, swtich_to_thread /* skip register save at the first time */
  83. MRS r1, psp /* get from thread stack pointer */
  84. STMFD r1!, {r4 - r11} /* push r4 - r11 register */
  85. LDR r0, [r0]
  86. STR r1, [r0] /* update from thread stack pointer */
  87. swtich_to_thread:
  88. LDR r1, =rt_interrupt_to_thread
  89. LDR r1, [r1]
  90. LDR r1, [r1] /* load thread stack pointer */
  91. LDMFD r1!, {r4 - r11} /* pop r4 - r11 register */
  92. MSR psp, r1 /* update stack pointer */
  93. /* restore interrupt */
  94. MSR PRIMASK, r2
  95. ORR lr, lr, #0x04
  96. BX lr
  97. /*
  98. * void rt_hw_context_switch_to(rt_uint32 to);
  99. * r0 --> to
  100. */
  101. .global rt_hw_context_switch_to
  102. rt_hw_context_switch_to:
  103. LDR r1, =rt_interrupt_to_thread
  104. STR r0, [r1]
  105. /* set from thread to 0 */
  106. LDR r1, =rt_interrupt_from_thread
  107. MOV r0, #0x0
  108. STR r0, [r1]
  109. /* set the PendSV exception priority */
  110. LDR r0, =NVIC_SYSPRI2
  111. LDR r1, =NVIC_PENDSV_PRI
  112. STR r1, [r0]
  113. LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  114. LDR r1, =NVIC_PENDSVSET
  115. STR r1, [r0]
  116. CPSIE I /* enable interrupts at processor level */
  117. /* never reach here! */
  118. /* compatible with old version */
  119. .global rt_hw_interrupt_thread_switch
  120. rt_hw_interrupt_thread_switch:
  121. BX lr
  122. NOP