context.s 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. ;
  2. ; Copyright (c) 2006-2018, RT-Thread Development Team
  3. ;
  4. ; SPDX-License-Identifier: Apache-2.0
  5. ;
  6. ; Change Logs:
  7. ; Date Author Notes
  8. ; 2018-09-01 xuzhuoyi the first version.
  9. ;
  10. .ref _rt_interrupt_to_thread
  11. .ref _rt_interrupt_from_thread
  12. .ref _rt_thread_switch_interrupt_flag
  13. .def _RTOSINT_Handler
  14. .def _rt_hw_get_st0
  15. .def _rt_hw_get_st1
  16. .def _rt_hw_context_switch_interrupt
  17. .def _rt_hw_context_switch
  18. .def _rt_hw_context_switch_to
  19. .def _rt_hw_interrupt_thread_switch
  20. .def _rt_hw_interrupt_disable
  21. .def _rt_hw_interrupt_enable
  22. RT_CTX_SAVE .macro
  23. PUSH AR1H:AR0H
  24. PUSH XAR2
  25. PUSH XAR3
  26. PUSH XAR4
  27. PUSH XAR5
  28. PUSH XAR6
  29. PUSH XAR7
  30. PUSH XT
  31. PUSH RPC
  32. .endm
  33. RT_CTX_RESTORE .macro
  34. POP RPC
  35. POP XT
  36. POP XAR7
  37. POP XAR6
  38. POP XAR5
  39. POP XAR4
  40. POP XAR3
  41. POP XAR2
  42. MOVZ AR0 , @SP
  43. SUBB XAR0, #6
  44. MOVL ACC , *XAR0
  45. AND ACC, #0xFFFF << 16
  46. MOV AL, IER
  47. MOVL *XAR0, ACC
  48. POP AR1H:AR0H
  49. .endm
  50. .text
  51. .newblock
  52. ;
  53. ; rt_base_t rt_hw_interrupt_disable();
  54. ;
  55. .asmfunc
  56. _rt_hw_interrupt_disable:
  57. DINT
  58. LRETR
  59. .endasmfunc
  60. ;
  61. ; void rt_hw_interrupt_enable(rt_base_t level);
  62. ;
  63. .asmfunc
  64. _rt_hw_interrupt_enable:
  65. EINT
  66. LRETR
  67. .endasmfunc
  68. ;
  69. ; void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  70. ; r0 --> from
  71. ; r4 --> to
  72. .asmfunc
  73. _rt_hw_context_switch_interrupt:
  74. MOVL XAR0, #0
  75. MOV AR0, AL
  76. MOVL XAR4, *-SP[4]
  77. ; set rt_thread_switch_interrupt_flag to 1
  78. MOVL XAR5, #_rt_thread_switch_interrupt_flag
  79. MOVL XAR6, *XAR5
  80. MOVL ACC, XAR6
  81. CMPB AL, #1
  82. B _reswitch, EQ
  83. MOVL XAR6, #1
  84. MOVL *XAR5, XAR6
  85. MOVL XAR5, #_rt_interrupt_from_thread ; set rt_interrupt_from_thread
  86. MOVL *XAR5, XAR0
  87. _reswitch:
  88. MOVL XAR5, #_rt_interrupt_to_thread ; set rt_interrupt_to_thread
  89. MOVL *XAR5, XAR4
  90. LRETR
  91. .endasmfunc
  92. ;
  93. ; void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  94. ; r0 --> from
  95. ; r4 --> to
  96. .asmfunc
  97. _rt_hw_context_switch:
  98. MOVL XAR0, #0
  99. MOV AR0, AL
  100. MOVL XAR4, *-SP[4]
  101. ; set rt_thread_switch_interrupt_flag to 1
  102. MOVL XAR5, #_rt_thread_switch_interrupt_flag
  103. MOVL XAR6, *XAR5
  104. MOVL ACC, XAR6
  105. CMPB AL, #1
  106. B _reswitch2, EQ
  107. MOVL XAR6, #1
  108. MOVL *XAR5, XAR6
  109. MOVL XAR5, #_rt_interrupt_from_thread ; set rt_interrupt_from_thread
  110. MOVL *XAR5, XAR0
  111. _reswitch2:
  112. MOVL XAR5, #_rt_interrupt_to_thread ; set rt_interrupt_to_thread
  113. MOVL *XAR5, XAR4
  114. TRAP #16
  115. LRETR
  116. .endasmfunc
  117. .asmfunc
  118. _RTOSINT_Handler:
  119. ; disable interrupt to protect context switch
  120. DINT
  121. ; get rt_thread_switch_interrupt_flag
  122. MOV AR0, #_rt_thread_switch_interrupt_flag
  123. MOV AL, *AR0
  124. MOV AR1, AL
  125. CMP AR1, #0
  126. B rtosint_exit, EQ ; pendsv already handled
  127. ; clear rt_thread_switch_interrupt_flag to 0
  128. MOV AR1, #0x00
  129. MOV *AR0, AR1
  130. MOV AR0, #_rt_interrupt_from_thread
  131. MOV AL, *AR0
  132. MOV AR1, AL
  133. CMP AR1, #0
  134. B switch_to_thread, EQ ; skip register save at the first time
  135. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  136. ; TST lr, #0x10 ; if(!EXC_RETURN[4])
  137. ; VSTMDBEQ r1!, {d8 - d15} ; push FPU register s16~s31
  138. ;#endif
  139. RT_CTX_SAVE ; push r4 - r11 register
  140. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  141. ; MOV r4, #0x00 ; flag = 0
  142. ; TST lr, #0x10 ; if(!EXC_RETURN[4])
  143. ; MOVEQ r4, #0x01 ; flag = 1
  144. ; STMFD r1!, {r4} ; push flag
  145. ;#endif
  146. MOV AL, *AR0
  147. MOV AR0, AL
  148. MOVZ AR1, @SP ; get from thread stack pointer
  149. MOV *AR0, AR1 ; update from thread stack pointer
  150. switch_to_thread:
  151. MOV AR1, #_rt_interrupt_to_thread
  152. MOV AL, *AR1
  153. MOV AR1, AL
  154. MOV AL, *AR1
  155. MOV AR1, AL ; load thread stack pointer
  156. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  157. ; LDMFD r1!, {r3} ; pop flag
  158. ;#endif
  159. MOV @SP, AR1
  160. RT_CTX_RESTORE ; pop r4 - r11 register
  161. rtosint_exit:
  162. ; restore interrupt
  163. EINT
  164. IRET
  165. .endasmfunc
  166. .asmfunc
  167. _rt_hw_get_st0:
  168. PUSH ST0
  169. POP AL
  170. LRETR
  171. .endasmfunc
  172. .asmfunc
  173. _rt_hw_get_st1:
  174. PUSH ST1
  175. POP AL
  176. LRETR
  177. .endasmfunc
  178. ;
  179. ; * void rt_hw_context_switch_to(rt_uint32 to);
  180. ; * r0 --> to
  181. .asmfunc
  182. _rt_hw_context_switch_to:
  183. MOV AR1, #_rt_interrupt_to_thread
  184. MOV *AR1, AL
  185. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  186. ; CLEAR CONTROL.FPCA
  187. ; MRS r2, CONTROL ; read
  188. ; BIC r2, #0x04 ; modify
  189. ; MSR CONTROL, r2 ; write-back
  190. ;#endif
  191. ; set from thread to 0
  192. MOV AR1, #_rt_interrupt_from_thread
  193. MOV AR0, #0x0
  194. MOV *AR1, AR0
  195. ; set interrupt flag to 1
  196. MOV AR1, #_rt_thread_switch_interrupt_flag
  197. MOV AR0, #1
  198. MOV *AR1, AR0
  199. TRAP #16
  200. ; never reach here!
  201. .endasmfunc
  202. ; compatible with old version
  203. .asmfunc
  204. _rt_hw_interrupt_thread_switch:
  205. LRETR
  206. NOP
  207. .endasmfunc
  208. .end