context.s 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. PUSH ST1
  58. SETC INTM,DBGM
  59. MOV AL, *--SP
  60. LRETR
  61. .endasmfunc
  62. ;
  63. ; void rt_hw_interrupt_enable(rt_base_t level);
  64. ;
  65. .asmfunc
  66. _rt_hw_interrupt_enable:
  67. MOV *SP++, AL
  68. POP ST1
  69. LRETR
  70. .endasmfunc
  71. ;
  72. ; void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  73. ; r0 --> from
  74. ; r4 --> to
  75. .asmfunc
  76. _rt_hw_context_switch_interrupt:
  77. MOVL XAR0, #0
  78. MOV AR0, AL
  79. MOVL XAR4, *-SP[4]
  80. ; set rt_thread_switch_interrupt_flag to 1
  81. MOVL XAR5, #_rt_thread_switch_interrupt_flag
  82. MOVL XAR6, *XAR5
  83. MOVL ACC, XAR6
  84. CMPB AL, #1
  85. B _reswitch, EQ
  86. MOVL XAR6, #1
  87. MOVL *XAR5, XAR6
  88. MOVL XAR5, #_rt_interrupt_from_thread ; set rt_interrupt_from_thread
  89. MOVL *XAR5, XAR0
  90. _reswitch:
  91. MOVL XAR5, #_rt_interrupt_to_thread ; set rt_interrupt_to_thread
  92. MOVL *XAR5, XAR4
  93. LRETR
  94. .endasmfunc
  95. ;
  96. ; void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  97. ; r0 --> from
  98. ; r4 --> to
  99. .asmfunc
  100. _rt_hw_context_switch:
  101. MOVL XAR0, #0
  102. MOV AR0, AL
  103. MOVL XAR4, *-SP[4]
  104. ; set rt_thread_switch_interrupt_flag to 1
  105. MOVL XAR5, #_rt_thread_switch_interrupt_flag
  106. MOVL XAR6, *XAR5
  107. MOVL ACC, XAR6
  108. CMPB AL, #1
  109. B _reswitch2, EQ
  110. MOVL XAR6, #1
  111. MOVL *XAR5, XAR6
  112. MOVL XAR5, #_rt_interrupt_from_thread ; set rt_interrupt_from_thread
  113. MOVL *XAR5, XAR0
  114. _reswitch2:
  115. MOVL XAR5, #_rt_interrupt_to_thread ; set rt_interrupt_to_thread
  116. MOVL *XAR5, XAR4
  117. TRAP #16
  118. LRETR
  119. .endasmfunc
  120. .asmfunc
  121. _RTOSINT_Handler:
  122. ; disable interrupt to protect context switch
  123. DINT
  124. ; get rt_thread_switch_interrupt_flag
  125. MOV AR0, #_rt_thread_switch_interrupt_flag
  126. MOV AL, *AR0
  127. MOV AR1, AL
  128. CMP AR1, #0
  129. B rtosint_exit, EQ ; pendsv already handled
  130. ; clear rt_thread_switch_interrupt_flag to 0
  131. MOV AR1, #0x00
  132. MOV *AR0, AR1
  133. MOV AR0, #_rt_interrupt_from_thread
  134. MOV AL, *AR0
  135. MOV AR1, AL
  136. CMP AR1, #0
  137. B switch_to_thread, EQ ; skip register save at the first time
  138. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  139. ; TST lr, #0x10 ; if(!EXC_RETURN[4])
  140. ; VSTMDBEQ r1!, {d8 - d15} ; push FPU register s16~s31
  141. ;#endif
  142. RT_CTX_SAVE ; push r4 - r11 register
  143. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  144. ; MOV r4, #0x00 ; flag = 0
  145. ; TST lr, #0x10 ; if(!EXC_RETURN[4])
  146. ; MOVEQ r4, #0x01 ; flag = 1
  147. ; STMFD r1!, {r4} ; push flag
  148. ;#endif
  149. MOV AL, *AR0
  150. MOV AR0, AL
  151. MOVZ AR1, @SP ; get from thread stack pointer
  152. MOV *AR0, AR1 ; update from thread stack pointer
  153. switch_to_thread:
  154. MOV AR1, #_rt_interrupt_to_thread
  155. MOV AL, *AR1
  156. MOV AR1, AL
  157. MOV AL, *AR1
  158. MOV AR1, AL ; load thread stack pointer
  159. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  160. ; LDMFD r1!, {r3} ; pop flag
  161. ;#endif
  162. MOV @SP, AR1
  163. RT_CTX_RESTORE ; pop r4 - r11 register
  164. rtosint_exit:
  165. ; restore interrupt
  166. EINT
  167. IRET
  168. .endasmfunc
  169. .asmfunc
  170. _rt_hw_get_st0:
  171. PUSH ST0
  172. POP AL
  173. LRETR
  174. .endasmfunc
  175. .asmfunc
  176. _rt_hw_get_st1:
  177. PUSH ST1
  178. POP AL
  179. LRETR
  180. .endasmfunc
  181. ;
  182. ; * void rt_hw_context_switch_to(rt_uint32 to);
  183. ; * r0 --> to
  184. .asmfunc
  185. _rt_hw_context_switch_to:
  186. MOV AR1, #_rt_interrupt_to_thread
  187. MOV *AR1, AL
  188. ;#if defined (__VFP_FP__) && !defined(__SOFTFP__)
  189. ; CLEAR CONTROL.FPCA
  190. ; MRS r2, CONTROL ; read
  191. ; BIC r2, #0x04 ; modify
  192. ; MSR CONTROL, r2 ; write-back
  193. ;#endif
  194. ; set from thread to 0
  195. MOV AR1, #_rt_interrupt_from_thread
  196. MOV AR0, #0x0
  197. MOV *AR1, AR0
  198. ; set interrupt flag to 1
  199. MOV AR1, #_rt_thread_switch_interrupt_flag
  200. MOV AR0, #1
  201. MOV *AR1, AR0
  202. TRAP #16
  203. ; never reach here!
  204. .endasmfunc
  205. ; compatible with old version
  206. .asmfunc
  207. _rt_hw_interrupt_thread_switch:
  208. LRETR
  209. NOP
  210. .endasmfunc
  211. .end