context_iar.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. SECTION .text:CODE(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. EXPORT rt_hw_interrupt_disable
  33. rt_hw_interrupt_disable:
  34. MRS r0, PRIMASK
  35. CPSID I
  36. BX LR
  37. ;/*
  38. ; * void rt_hw_interrupt_enable(rt_base_t level);
  39. ; */
  40. EXPORT rt_hw_interrupt_enable
  41. rt_hw_interrupt_enable:
  42. MSR PRIMASK, r0
  43. BX LR
  44. ;/*
  45. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  46. ; * r0 --> from
  47. ; * r1 --> to
  48. ; */
  49. EXPORT rt_hw_context_switch
  50. rt_hw_context_switch:
  51. LDR r2, =rt_interrupt_from_thread
  52. STR r0, [r2]
  53. LDR r2, =rt_interrupt_to_thread
  54. STR r1, [r2]
  55. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  56. LDR r1, =NVIC_PENDSVSET
  57. STR r1, [r0]
  58. CPSIE I ; enable interrupts at processor level
  59. BX LR
  60. ; r0 --> swith from thread stack
  61. ; r1 --> swith to thread stack
  62. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  63. EXPORT rt_hw_pend_sv
  64. rt_hw_pend_sv:
  65. LDR r0, =rt_interrupt_from_thread
  66. LDR r1, [r0]
  67. CBZ r1, swtich_to_thread ; skip register save at the first time
  68. MRS r1, psp ; get from thread stack pointer
  69. STMFD r1!, {r4 - r11} ; push r4 - r11 register
  70. LDR r0, [r0]
  71. STR r1, [r0] ; update from thread stack pointer
  72. swtich_to_thread
  73. LDR r1, =rt_interrupt_to_thread
  74. LDR r1, [r1]
  75. LDR r1, [r1] ; load thread stack pointer
  76. LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
  77. MSR psp, r1 ; update stack pointer
  78. ORR lr, lr, #0x04
  79. BX lr
  80. ;/*
  81. ; * void rt_hw_context_switch_to(rt_uint32 to);
  82. ; * r0 --> to
  83. ; */
  84. EXPORT rt_hw_context_switch_to
  85. rt_hw_context_switch_to:
  86. LDR r1, =rt_interrupt_to_thread
  87. STR r0, [r1]
  88. ; set from thread to 0
  89. LDR r1, =rt_interrupt_from_thread
  90. MOV r0, #0x0
  91. STR r0, [r1]
  92. ; set the PendSV exception priority
  93. LDR r0, =NVIC_SYSPRI2
  94. LDR r1, =NVIC_PENDSV_PRI
  95. STR r1, [r0]
  96. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  97. LDR r1, =NVIC_PENDSVSET
  98. STR r1, [r0]
  99. CPSIE I ; enable interrupts at processor level
  100. ; never reach here!
  101. ;/*
  102. ; * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to)
  103. ; * {
  104. ; * if (rt_thread_switch_interrput_flag == 1)
  105. ; * {
  106. ; * rt_interrupt_to_thread = to;
  107. ; * }
  108. ; * else
  109. ; * {
  110. ; * rt_thread_switch_interrput_flag = 1;
  111. ; * rt_interrupt_from_thread = from;
  112. ; * rt_interrupt_to_thread = to;
  113. ; * }
  114. ; * }
  115. ; */
  116. EXPORT rt_hw_context_switch_interrupt
  117. rt_hw_context_switch_interrupt:
  118. LDR r2, =rt_thread_switch_interrput_flag
  119. LDR r3, [r2]
  120. CMP r3, #1
  121. BEQ _reswitch
  122. MOV r3, #1 ; set rt_thread_switch_interrput_flag to 1
  123. STR r3, [r2]
  124. LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
  125. STR r0, [r2]
  126. _reswitch:
  127. LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
  128. STR r1, [r2]
  129. BX lr
  130. EXPORT rt_hw_interrupt_thread_switch
  131. rt_hw_interrupt_thread_switch:
  132. LDR r0, =rt_thread_switch_interrput_flag
  133. LDR r1, [r0]
  134. CBZ r1, _no_switch
  135. ; clear rt_thread_switch_interrput_flag to 0
  136. MOV r1, #0x00
  137. STR r1, [r0]
  138. ; trigger context switch
  139. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  140. LDR r1, =NVIC_PENDSVSET
  141. STR r1, [r0]
  142. _no_switch:
  143. BX lr
  144. END