context_rvds.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ;/*
  2. ; * File : context_rvds.S
  3. ; * This file is part of RT-Thread RTOS
  4. ; * COPYRIGHT (C) 2006 - 2013, 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. ; * 2013-06-18 aozima add restore MSP feature.
  14. ; * 2013-07-09 aozima enhancement hard fault exception handler.
  15. ; */
  16. ;/**
  17. ; * @addtogroup CORTEX-M3
  18. ; */
  19. ;/*@{*/
  20. SCB_VTOR EQU 0xE000ED08 ; Vector Table Offset Register
  21. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  22. NVIC_SYSPRI2 EQU 0xE000ED20 ; system priority register (2)
  23. NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV priority value (lowest)
  24. NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
  25. AREA |.text|, CODE, READONLY, ALIGN=2
  26. THUMB
  27. REQUIRE8
  28. PRESERVE8
  29. IMPORT rt_thread_switch_interrupt_flag
  30. IMPORT rt_interrupt_from_thread
  31. IMPORT rt_interrupt_to_thread
  32. ;/*
  33. ; * rt_base_t rt_hw_interrupt_disable();
  34. ; */
  35. rt_hw_interrupt_disable PROC
  36. EXPORT rt_hw_interrupt_disable
  37. MRS r0, PRIMASK
  38. CPSID I
  39. BX LR
  40. ENDP
  41. ;/*
  42. ; * void rt_hw_interrupt_enable(rt_base_t level);
  43. ; */
  44. rt_hw_interrupt_enable PROC
  45. EXPORT rt_hw_interrupt_enable
  46. MSR PRIMASK, r0
  47. BX LR
  48. ENDP
  49. ;/*
  50. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  51. ; * r0 --> from
  52. ; * r1 --> to
  53. ; */
  54. rt_hw_context_switch_interrupt
  55. EXPORT rt_hw_context_switch_interrupt
  56. rt_hw_context_switch PROC
  57. EXPORT rt_hw_context_switch
  58. ; set rt_thread_switch_interrupt_flag to 1
  59. LDR r2, =rt_thread_switch_interrupt_flag
  60. LDR r3, [r2]
  61. CMP r3, #1
  62. BEQ _reswitch
  63. MOV r3, #1
  64. STR r3, [r2]
  65. LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
  66. STR r0, [r2]
  67. _reswitch
  68. LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
  69. STR r1, [r2]
  70. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  71. LDR r1, =NVIC_PENDSVSET
  72. STR r1, [r0]
  73. BX LR
  74. ENDP
  75. ; r0 --> switch from thread stack
  76. ; r1 --> switch to thread stack
  77. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  78. PendSV_Handler PROC
  79. EXPORT PendSV_Handler
  80. ; disable interrupt to protect context switch
  81. MRS r2, PRIMASK
  82. CPSID I
  83. ; get rt_thread_switch_interrupt_flag
  84. LDR r0, =rt_thread_switch_interrupt_flag
  85. LDR r1, [r0]
  86. CBZ r1, pendsv_exit ; pendsv already handled
  87. ; clear rt_thread_switch_interrupt_flag to 0
  88. MOV r1, #0x00
  89. STR r1, [r0]
  90. LDR r0, =rt_interrupt_from_thread
  91. LDR r1, [r0]
  92. CBZ r1, switch_to_thread ; skip register save at the first time
  93. MRS r1, psp ; get from thread stack pointer
  94. STMFD r1!, {r4 - r11} ; push r4 - r11 register
  95. LDR r0, [r0]
  96. STR r1, [r0] ; update from thread stack pointer
  97. switch_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. MSR psp, r1 ; update stack pointer
  103. pendsv_exit
  104. ; restore interrupt
  105. MSR PRIMASK, r2
  106. ORR lr, lr, #0x04
  107. BX lr
  108. ENDP
  109. ;/*
  110. ; * void rt_hw_context_switch_to(rt_uint32 to);
  111. ; * r0 --> to
  112. ; * this fucntion is used to perform the first thread switch
  113. ; */
  114. rt_hw_context_switch_to PROC
  115. EXPORT rt_hw_context_switch_to
  116. ; set to thread
  117. LDR r1, =rt_interrupt_to_thread
  118. STR r0, [r1]
  119. ; set from thread to 0
  120. LDR r1, =rt_interrupt_from_thread
  121. MOV r0, #0x0
  122. STR r0, [r1]
  123. ; set interrupt flag to 1
  124. LDR r1, =rt_thread_switch_interrupt_flag
  125. MOV r0, #1
  126. STR r0, [r1]
  127. ; set the PendSV exception priority
  128. LDR r0, =NVIC_SYSPRI2
  129. LDR r1, =NVIC_PENDSV_PRI
  130. LDR.W r2, [r0,#0x00] ; read
  131. ORR r1,r1,r2 ; modify
  132. STR r1, [r0] ; write-back
  133. ; trigger the PendSV exception (causes context switch)
  134. LDR r0, =NVIC_INT_CTRL
  135. LDR r1, =NVIC_PENDSVSET
  136. STR r1, [r0]
  137. ; restore MSP
  138. LDR r0, =SCB_VTOR
  139. LDR r0, [r0]
  140. LDR r0, [r0]
  141. MSR msp, r0
  142. ; enable interrupts at processor level
  143. CPSIE I
  144. ; never reach here!
  145. ENDP
  146. ; compatible with old version
  147. rt_hw_interrupt_thread_switch PROC
  148. EXPORT rt_hw_interrupt_thread_switch
  149. BX lr
  150. NOP
  151. ENDP
  152. IMPORT rt_hw_hard_fault_exception
  153. EXPORT HardFault_Handler
  154. HardFault_Handler PROC
  155. ; get current context
  156. TST lr, #0x04 ; if(!EXC_RETURN[2])
  157. MRSNE r0, msp ; get fault context from handler.
  158. MRSEQ r0, psp ; get fault context from thread.
  159. STMFD r0!, {r4 - r11} ; push r4 - r11 register
  160. STMFD r0!, {lr} ; push exec_return register
  161. MSRNE msp, r0 ; update stack pointer to MSP.
  162. MSREQ psp, r0 ; update stack pointer to PSP.
  163. PUSH {lr}
  164. BL rt_hw_hard_fault_exception
  165. POP {lr}
  166. ORR lr, lr, #0x04
  167. BX lr
  168. ENDP
  169. END