context_rvds.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;/*
  2. ; * File : context.S
  3. ; * This file is part of RT-Thread RTOS
  4. ; * COPYRIGHT (C) 2006, 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. ; */
  14. ;/**
  15. ; * @addtogroup STM32
  16. ; */
  17. ;/*@{*/
  18. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  19. NVIC_SYSPRI2 EQU 0xE000ED20 ; system priority register (2)
  20. NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV priority value (lowest)
  21. NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
  22. AREA |.text|, CODE, READONLY, ALIGN=2
  23. THUMB
  24. REQUIRE8
  25. PRESERVE8
  26. IMPORT rt_thread_switch_interrput_flag
  27. IMPORT rt_interrupt_from_thread
  28. IMPORT rt_interrupt_to_thread
  29. ;/*
  30. ; * rt_base_t rt_hw_interrupt_disable();
  31. ; */
  32. rt_hw_interrupt_disable PROC
  33. EXPORT rt_hw_interrupt_disable
  34. MRS r0, PRIMASK
  35. CPSID I
  36. BX LR
  37. ENDP
  38. ;/*
  39. ; * void rt_hw_interrupt_enable(rt_base_t level);
  40. ; */
  41. rt_hw_interrupt_enable PROC
  42. EXPORT rt_hw_interrupt_enable
  43. MSR PRIMASK, r0
  44. BX LR
  45. ENDP
  46. ;/*
  47. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  48. ; * r0 --> from
  49. ; * r1 --> to
  50. ; */
  51. rt_hw_context_switch_interrupt
  52. EXPORT rt_hw_context_switch_interrupt
  53. rt_hw_context_switch PROC
  54. EXPORT rt_hw_context_switch
  55. ; set rt_thread_switch_interrput_flag to 1
  56. LDR r2, =rt_thread_switch_interrput_flag
  57. LDR r3, [r2]
  58. CMP r3, #1
  59. BEQ _reswitch
  60. MOV r3, #1
  61. STR r3, [r2]
  62. LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
  63. STR r0, [r2]
  64. _reswitch
  65. LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
  66. STR r1, [r2]
  67. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  68. LDR r1, =NVIC_PENDSVSET
  69. STR r1, [r0]
  70. BX LR
  71. ENDP
  72. ; r0 --> swith from thread stack
  73. ; r1 --> swith to thread stack
  74. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  75. rt_hw_pend_sv PROC
  76. EXPORT rt_hw_pend_sv
  77. ; disable interrupt to protect context switch
  78. MRS r2, PRIMASK
  79. CPSID I
  80. ; clear rt_thread_switch_interrput_flag to 0
  81. LDR r0, =rt_thread_switch_interrput_flag
  82. MOV r1, #0x00
  83. STR r1, [r0]
  84. LDR r0, =rt_interrupt_from_thread
  85. LDR r1, [r0]
  86. CBZ r1, swtich_to_thread ; skip register save at the first time
  87. MRS r1, psp ; get from thread stack pointer
  88. STMFD r1!, {r4 - r11} ; push r4 - r11 register
  89. LDR r0, [r0]
  90. STR r1, [r0] ; update from thread stack pointer
  91. swtich_to_thread
  92. LDR r1, =rt_interrupt_to_thread
  93. LDR r1, [r1]
  94. LDR r1, [r1] ; load thread stack pointer
  95. LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
  96. MSR psp, r1 ; update stack pointer
  97. ; restore interrupt
  98. MSR PRIMASK, r2
  99. ORR lr, lr, #0x04
  100. BX lr
  101. ENDP
  102. ;/*
  103. ; * void rt_hw_context_switch_to(rt_uint32 to);
  104. ; * r0 --> to
  105. ; */
  106. rt_hw_context_switch_to PROC
  107. EXPORT rt_hw_context_switch_to
  108. LDR r1, =rt_interrupt_to_thread
  109. STR r0, [r1]
  110. ; set from thread to 0
  111. LDR r1, =rt_interrupt_from_thread
  112. MOV r0, #0x0
  113. STR r0, [r1]
  114. ; set the PendSV exception priority
  115. LDR r0, =NVIC_SYSPRI2
  116. LDR r1, =NVIC_PENDSV_PRI
  117. STR r1, [r0]
  118. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  119. LDR r1, =NVIC_PENDSVSET
  120. STR r1, [r0]
  121. CPSIE I ; enable interrupts at processor level
  122. ; never reach here!
  123. ENDP
  124. ; compatible with old version
  125. rt_hw_interrupt_thread_switch PROC
  126. EXPORT rt_hw_interrupt_thread_switch
  127. BX lr
  128. NOP
  129. ENDP
  130. END