context_rvds.S 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. ; * 2010-01-25 Bernard first version
  13. ; * 2012-06-01 aozima set pendsv priority to 0xFF.
  14. ; */
  15. ;/**
  16. ; * @addtogroup CORTEX-M0
  17. ; */
  18. ;/*@{*/
  19. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  20. NVIC_SHPR3 EQU 0xE000ED20 ; system priority register (2)
  21. NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV priority value (lowest)
  22. NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
  23. AREA |.text|, CODE, READONLY, ALIGN=2
  24. THUMB
  25. REQUIRE8
  26. PRESERVE8
  27. IMPORT rt_thread_switch_interrupt_flag
  28. IMPORT rt_interrupt_from_thread
  29. IMPORT rt_interrupt_to_thread
  30. ;/*
  31. ; * rt_base_t rt_hw_interrupt_disable();
  32. ; */
  33. rt_hw_interrupt_disable PROC
  34. EXPORT rt_hw_interrupt_disable
  35. MRS r0, PRIMASK
  36. CPSID I
  37. BX LR
  38. ENDP
  39. ;/*
  40. ; * void rt_hw_interrupt_enable(rt_base_t level);
  41. ; */
  42. rt_hw_interrupt_enable PROC
  43. EXPORT rt_hw_interrupt_enable
  44. MSR PRIMASK, r0
  45. BX LR
  46. ENDP
  47. ;/*
  48. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  49. ; * r0 --> from
  50. ; * r1 --> to
  51. ; */
  52. rt_hw_context_switch_interrupt
  53. EXPORT rt_hw_context_switch_interrupt
  54. rt_hw_context_switch PROC
  55. EXPORT 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. MOVS r3, #0x1
  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. ENDP
  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. PendSV_Handler PROC
  77. EXPORT PendSV_Handler
  78. ; disable interrupt to protect context switch
  79. MRS r2, PRIMASK
  80. CPSID I
  81. ; get rt_thread_switch_interrupt_flag
  82. LDR r0, =rt_thread_switch_interrupt_flag
  83. LDR r1, [r0]
  84. CMP r1, #0x00
  85. BEQ pendsv_exit ; pendsv already handled
  86. ; clear rt_thread_switch_interrupt_flag to 0
  87. MOVS r1, #0x00
  88. STR r1, [r0]
  89. LDR r0, =rt_interrupt_from_thread
  90. LDR r1, [r0]
  91. CMP r1, #0x00
  92. BEQ swtich_to_thread ; skip register save at the first time
  93. MRS r1, psp ; get from thread stack pointer
  94. SUBS r1, r1, #0x10
  95. LDR r0, [r0]
  96. STR r1, [r0] ; update from thread stack pointer
  97. STMIA r1!, {r4 - r7} ; push r4 - r7 register
  98. swtich_to_thread
  99. LDR r1, =rt_interrupt_to_thread
  100. LDR r1, [r1]
  101. LDR r1, [r1] ; load thread stack pointer
  102. LDMIA r1!, {r4 - r7} ; pop r4 - r7 register
  103. MSR psp, r1 ; update stack pointer
  104. pendsv_exit
  105. ; restore interrupt
  106. MSR PRIMASK, r2
  107. MOVS r0, #0x04
  108. RSBS r0, #0
  109. BX r0
  110. ENDP
  111. ;/*
  112. ; * void rt_hw_context_switch_to(rt_uint32 to);
  113. ; * r0 --> to
  114. ; * this fucntion is used to perform the first thread switch
  115. ; */
  116. rt_hw_context_switch_to PROC
  117. EXPORT rt_hw_context_switch_to
  118. ; set to thread
  119. LDR r1, =rt_interrupt_to_thread
  120. STR r0, [r1]
  121. ; set from thread to 0
  122. LDR r1, =rt_interrupt_from_thread
  123. MOVS r0, #0x0
  124. STR r0, [r1]
  125. ; set interrupt flag to 1
  126. LDR r1, =rt_thread_switch_interrupt_flag
  127. MOVS r0, #1
  128. STR r0, [r1]
  129. ; set the PendSV exception priority
  130. LDR r0, =NVIC_SHPR3
  131. LDR r1, =NVIC_PENDSV_PRI
  132. LDR r2, [r0,#0x00] ; read
  133. ORRS r1,r1,r2 ; modify
  134. STR r1, [r0] ; write-back
  135. ; trigger the PendSV exception (causes context switch)
  136. LDR r0, =NVIC_INT_CTRL
  137. LDR r1, =NVIC_PENDSVSET
  138. STR r1, [r0]
  139. NOP
  140. ; enable interrupts at processor level
  141. CPSIE I
  142. ; never reach here!
  143. ENDP
  144. ; compatible with old version
  145. rt_hw_interrupt_thread_switch PROC
  146. EXPORT rt_hw_interrupt_thread_switch
  147. BX lr
  148. ENDP
  149. IMPORT rt_hw_hard_fault_exception
  150. HardFault_Handler PROC
  151. EXPORT HardFault_Handler
  152. ; get current context
  153. MRS r0, psp ; get fault thread stack pointer
  154. PUSH {lr}
  155. BL rt_hw_hard_fault_exception
  156. POP {pc}
  157. ENDP
  158. END