context_rvds.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ;/*
  2. ; * File : context.S
  3. ; * This file is part of RT-Thread RTOS
  4. ; * COPYRIGHT (C) 2006, 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-01-17 Bernard first version
  13. ; */
  14. ;/**
  15. ; * @addtogroup STM32
  16. ; */
  17. ;/*@{*/
  18. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  19. NVIC_SYSPRI2 EQU 0xE000ED20 ; system priority register (2)
  20. NVIC_PENDSV_PRI EQU 0x00000000 ; PendSV priority value (lowest)
  21. NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
  22. AREA |.text|, CODE, READONLY, ALIGN=2
  23. THUMB
  24. REQUIRE8
  25. PRESERVE8
  26. IMPORT rt_thread_switch_interrput_flag
  27. IMPORT rt_interrupt_from_thread
  28. IMPORT rt_interrupt_to_thread
  29. ;/*
  30. ; * rt_base_t rt_hw_interrupt_disable();
  31. ; */
  32. rt_hw_interrupt_disable PROC
  33. EXPORT rt_hw_interrupt_disable
  34. MRS r0, PRIMASK
  35. CPSID I
  36. BX LR
  37. ENDP
  38. ;/*
  39. ; * void rt_hw_interrupt_enable(rt_base_t level);
  40. ; */
  41. rt_hw_interrupt_enable PROC
  42. EXPORT rt_hw_interrupt_enable
  43. MSR PRIMASK, r0
  44. BX LR
  45. ENDP
  46. ;/*
  47. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  48. ; * r0 --> from
  49. ; * r1 --> to
  50. ; */
  51. rt_hw_context_switch PROC
  52. EXPORT rt_hw_context_switch
  53. LDR r2, =rt_interrupt_from_thread
  54. STR r0, [r2]
  55. LDR r2, =rt_interrupt_to_thread
  56. STR r1, [r2]
  57. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  58. LDR r1, =NVIC_PENDSVSET
  59. STR r1, [r0]
  60. CPSIE I ; enable interrupts at processor level
  61. BX LR
  62. ENDP
  63. ; r0 --> swith from thread stack
  64. ; r1 --> swith to thread stack
  65. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  66. rt_hw_pend_sv PROC
  67. EXPORT rt_hw_pend_sv
  68. LDR r0, =rt_interrupt_from_thread
  69. LDR r1, [r0]
  70. CBZ r1, swtich_to_thread ; skip register save at the first time
  71. MRS r1, psp ; get from thread stack pointer
  72. STMFD r1!, {r4 - r11} ; push r4 - r11 register
  73. LDR r0, [r0]
  74. STR r1, [r0] ; update from thread stack pointer
  75. swtich_to_thread
  76. LDR r1, =rt_interrupt_to_thread
  77. LDR r1, [r1]
  78. LDR r1, [r1] ; load thread stack pointer
  79. LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
  80. MSR psp, r1 ; update stack pointer
  81. ORR lr, lr, #0x04
  82. BX lr
  83. ENDP
  84. ;/*
  85. ; * void rt_hw_context_switch_to(rt_uint32 to);
  86. ; * r0 --> to
  87. ; */
  88. rt_hw_context_switch_to PROC
  89. EXPORT rt_hw_context_switch_to
  90. LDR r1, =rt_interrupt_to_thread
  91. STR r0, [r1]
  92. ; set from thread to 0
  93. LDR r1, =rt_interrupt_from_thread
  94. MOV r0, #0x0
  95. STR r0, [r1]
  96. ; set the PendSV exception priority
  97. LDR r0, =NVIC_SYSPRI2
  98. LDR r1, =NVIC_PENDSV_PRI
  99. STR r1, [r0]
  100. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  101. LDR r1, =NVIC_PENDSVSET
  102. STR r1, [r0]
  103. CPSIE I ; enable interrupts at processor level
  104. ; never reach here!
  105. ENDP
  106. ;/*
  107. ; * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to)
  108. ; * {
  109. ; * if (rt_thread_switch_interrput_flag == 1)
  110. ; * {
  111. ; * rt_interrupt_to_thread = to;
  112. ; * }
  113. ; * else
  114. ; * {
  115. ; * rt_thread_switch_interrput_flag = 1;
  116. ; * rt_interrupt_from_thread = from;
  117. ; * rt_interrupt_to_thread = to;
  118. ; * }
  119. ; * }
  120. ; */
  121. rt_hw_context_switch_interrupt PROC
  122. EXPORT rt_hw_context_switch_interrupt
  123. LDR r2, =rt_thread_switch_interrput_flag
  124. LDR r3, [r2]
  125. CMP r3, #1
  126. BEQ _reswitch
  127. MOV r3, #1 ; set rt_thread_switch_interrput_flag to 1
  128. STR r3, [r2]
  129. LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
  130. STR r0, [r2]
  131. _reswitch
  132. LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
  133. STR r1, [r2]
  134. BX lr
  135. ENDP
  136. rt_hw_interrupt_thread_switch PROC
  137. EXPORT rt_hw_interrupt_thread_switch
  138. LDR r0, =rt_thread_switch_interrput_flag
  139. LDR r1, [r0]
  140. CBZ r1, _no_switch
  141. ; clear rt_thread_switch_interrput_flag to 0
  142. MOV r1, #0x00
  143. STR r1, [r0]
  144. ; trigger context switch
  145. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  146. LDR r1, =NVIC_PENDSVSET
  147. STR r1, [r0]
  148. _no_switch
  149. BX lr
  150. ENDP
  151. END