context_rvds.S 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. ; * 2012-08-17 aozima fixed bug: store r8 - r11.
  15. ; */
  16. ;/**
  17. ; * @addtogroup CORTEX-M0
  18. ; */
  19. ;/*@{*/
  20. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  21. NVIC_SHPR3 EQU 0xE000ED20 ; system priority register (2)
  22. NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV priority value (lowest)
  23. NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
  24. AREA |.text|, CODE, READONLY, ALIGN=2
  25. THUMB
  26. REQUIRE8
  27. PRESERVE8
  28. IMPORT rt_thread_switch_interrupt_flag
  29. IMPORT rt_interrupt_from_thread
  30. IMPORT rt_interrupt_to_thread
  31. ;/*
  32. ; * rt_base_t rt_hw_interrupt_disable();
  33. ; */
  34. rt_hw_interrupt_disable PROC
  35. EXPORT rt_hw_interrupt_disable
  36. MRS r0, PRIMASK
  37. CPSID I
  38. BX LR
  39. ENDP
  40. ;/*
  41. ; * void rt_hw_interrupt_enable(rt_base_t level);
  42. ; */
  43. rt_hw_interrupt_enable PROC
  44. EXPORT rt_hw_interrupt_enable
  45. MSR PRIMASK, r0
  46. BX LR
  47. ENDP
  48. ;/*
  49. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  50. ; * r0 --> from
  51. ; * r1 --> to
  52. ; */
  53. rt_hw_context_switch_interrupt
  54. EXPORT rt_hw_context_switch_interrupt
  55. rt_hw_context_switch PROC
  56. EXPORT rt_hw_context_switch
  57. ; set rt_thread_switch_interrupt_flag to 1
  58. LDR r2, =rt_thread_switch_interrupt_flag
  59. LDR r3, [r2]
  60. CMP r3, #1
  61. BEQ _reswitch
  62. MOVS r3, #0x01
  63. STR r3, [r2]
  64. LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
  65. STR r0, [r2]
  66. _reswitch
  67. LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
  68. STR r1, [r2]
  69. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  70. LDR r1, =NVIC_PENDSVSET
  71. STR r1, [r0]
  72. BX LR
  73. ENDP
  74. ; r0 --> swith from thread stack
  75. ; r1 --> swith to thread stack
  76. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  77. PendSV_Handler PROC
  78. EXPORT PendSV_Handler
  79. ; disable interrupt to protect context switch
  80. MRS r2, PRIMASK
  81. CPSID I
  82. ; get rt_thread_switch_interrupt_flag
  83. LDR r0, =rt_thread_switch_interrupt_flag
  84. LDR r1, [r0]
  85. CMP r1, #0x00
  86. BEQ pendsv_exit ; pendsv already handled
  87. ; clear rt_thread_switch_interrupt_flag to 0
  88. MOVS r1, #0x00
  89. STR r1, [r0]
  90. LDR r0, =rt_interrupt_from_thread
  91. LDR r1, [r0]
  92. CMP r1, #0x00
  93. BEQ swtich_to_thread ; skip register save at the first time
  94. MRS r1, psp ; get from thread stack pointer
  95. SUBS r1, r1, #0x20 ; space for {r4 - r7} and {r8 - r11}
  96. LDR r0, [r0]
  97. STR r1, [r0] ; update from thread stack pointer
  98. STMIA r1!, {r4 - r7} ; push thread {r4 - r7} register to thread stack
  99. MOV r4, r8 ; mov thread {r8 - r11} to {r4 - r7}
  100. MOV r5, r9
  101. MOV r6, r10
  102. MOV r7, r11
  103. STMIA r1!, {r4 - r7} ; push thread {r8 - r11} high register to thread stack
  104. swtich_to_thread
  105. LDR r1, =rt_interrupt_to_thread
  106. LDR r1, [r1]
  107. LDR r1, [r1] ; load thread stack pointer
  108. LDMIA r1!, {r4 - r7} ; pop thread {r4 - r7} register from thread stack
  109. PUSH {r4 - r7} ; push {r4 - r7} to MSP for copy {r8 - r11}
  110. LDMIA r1!, {r4 - r7} ; pop thread {r8 - r11} high register from thread stack to {r4 - r7}
  111. MOV r8, r4 ; mov {r4 - r7} to {r8 - r11}
  112. MOV r9, r5
  113. MOV r10, r6
  114. MOV r11, r7
  115. POP {r4 - r7} ; pop {r4 - r7} from MSP
  116. MSR psp, r1 ; update stack pointer
  117. pendsv_exit
  118. ; restore interrupt
  119. MSR PRIMASK, r2
  120. MOVS r0, #0x04
  121. RSBS r0, r0, #0x00
  122. BX r0
  123. ENDP
  124. ;/*
  125. ; * void rt_hw_context_switch_to(rt_uint32 to);
  126. ; * r0 --> to
  127. ; * this fucntion is used to perform the first thread switch
  128. ; */
  129. rt_hw_context_switch_to PROC
  130. EXPORT rt_hw_context_switch_to
  131. ; set to thread
  132. LDR r1, =rt_interrupt_to_thread
  133. STR r0, [r1]
  134. ; set from thread to 0
  135. LDR r1, =rt_interrupt_from_thread
  136. MOVS r0, #0x0
  137. STR r0, [r1]
  138. ; set interrupt flag to 1
  139. LDR r1, =rt_thread_switch_interrupt_flag
  140. MOVS r0, #1
  141. STR r0, [r1]
  142. ; set the PendSV exception priority
  143. LDR r0, =NVIC_SHPR3
  144. LDR r1, =NVIC_PENDSV_PRI
  145. LDR r2, [r0,#0x00] ; read
  146. ORRS r1,r1,r2 ; modify
  147. STR r1, [r0] ; write-back
  148. ; trigger the PendSV exception (causes context switch)
  149. LDR r0, =NVIC_INT_CTRL
  150. LDR r1, =NVIC_PENDSVSET
  151. STR r1, [r0]
  152. NOP
  153. ; enable interrupts at processor level
  154. CPSIE I
  155. ; never reach here!
  156. ENDP
  157. ; compatible with old version
  158. rt_hw_interrupt_thread_switch PROC
  159. EXPORT rt_hw_interrupt_thread_switch
  160. BX lr
  161. ENDP
  162. IMPORT rt_hw_hard_fault_exception
  163. HardFault_Handler PROC
  164. EXPORT HardFault_Handler
  165. ; get current context
  166. MRS r0, psp ; get fault thread stack pointer
  167. PUSH {lr}
  168. BL rt_hw_hard_fault_exception
  169. POP {pc}
  170. ENDP
  171. END