context_rvds.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ;/*
  2. ; * File : context_rvds.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-01-17 Bernard first version
  13. ; */
  14. ;/**
  15. ; * @addtogroup CORTEX-M3
  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 0x00FF0000 ; 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_interrupt_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_interrupt
  52. EXPORT rt_hw_context_switch_interrupt
  53. rt_hw_context_switch PROC
  54. EXPORT rt_hw_context_switch
  55. ; set rt_thread_switch_interrupt_flag to 1
  56. LDR r2, =rt_thread_switch_interrupt_flag
  57. LDR r3, [r2]
  58. CMP r3, #1
  59. BEQ _reswitch
  60. MOV r3, #1
  61. STR r3, [r2]
  62. LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
  63. STR r0, [r2]
  64. _reswitch
  65. LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
  66. STR r1, [r2]
  67. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  68. LDR r1, =NVIC_PENDSVSET
  69. STR r1, [r0]
  70. BX LR
  71. ENDP
  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. PendSV_Handler PROC
  76. EXPORT PendSV_Handler
  77. ; disable interrupt to protect context switch
  78. MRS r2, PRIMASK
  79. CPSID I
  80. ; get rt_thread_switch_interrupt_flag
  81. LDR r0, =rt_thread_switch_interrupt_flag
  82. LDR r1, [r0]
  83. CBZ r1, pendsv_exit ; pendsv already handled
  84. ; clear rt_thread_switch_interrupt_flag to 0
  85. MOV r1, #0x00
  86. STR r1, [r0]
  87. LDR r0, =rt_interrupt_from_thread
  88. LDR r1, [r0]
  89. CBZ r1, swtich_to_thread ; skip register save at the first time
  90. MRS r1, psp ; get from thread stack pointer
  91. STMFD r1!, {r4 - r11} ; push r4 - r11 register
  92. LDR r0, [r0]
  93. STR r1, [r0] ; update from thread stack pointer
  94. swtich_to_thread
  95. LDR r1, =rt_interrupt_to_thread
  96. LDR r1, [r1]
  97. LDR r1, [r1] ; load thread stack pointer
  98. LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
  99. MSR psp, r1 ; update stack pointer
  100. pendsv_exit
  101. ; restore interrupt
  102. MSR PRIMASK, r2
  103. ORR lr, lr, #0x04
  104. BX lr
  105. ENDP
  106. ;/*
  107. ; * void rt_hw_context_switch_to(rt_uint32 to);
  108. ; * r0 --> to
  109. ; * this fucntion is used to perform the first thread switch
  110. ; */
  111. rt_hw_context_switch_to PROC
  112. EXPORT rt_hw_context_switch_to
  113. ; set to thread
  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, #0x0
  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, =NVIC_SYSPRI2
  126. LDR r1, =NVIC_PENDSV_PRI
  127. LDR.W r2, [r0,#0x00] ; read
  128. ORR r1,r1,r2 ; modify
  129. STR r1, [r0] ; write-back
  130. ; trigger the PendSV exception (causes context switch)
  131. LDR r0, =NVIC_INT_CTRL
  132. LDR r1, =NVIC_PENDSVSET
  133. STR r1, [r0]
  134. ; enable interrupts at processor level
  135. CPSIE I
  136. ; never reach here!
  137. ENDP
  138. ; compatible with old version
  139. rt_hw_interrupt_thread_switch PROC
  140. EXPORT rt_hw_interrupt_thread_switch
  141. BX lr
  142. NOP
  143. ENDP
  144. IMPORT rt_hw_hard_fault_exception
  145. EXPORT HardFault_Handler
  146. HardFault_Handler PROC
  147. ; get current context
  148. MRS r0, psp ; get fault thread stack pointer
  149. PUSH {lr}
  150. BL rt_hw_hard_fault_exception
  151. POP {lr}
  152. ORR lr, lr, #0x04
  153. BX lr
  154. ENDP
  155. END