context_iar.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ;/*
  2. ; * File : context_iar.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. ; * 2009-01-17 Bernard first version
  13. ; * 2009-09-27 Bernard add protect when contex switch occurs
  14. ; * 2012-01-01 aozima support context switch load/store FPU register.
  15. ; * 2013-06-18 aozima add restore MSP feature.
  16. ; */
  17. ;/**
  18. ; * @addtogroup STM32
  19. ; */
  20. ;/*@{*/
  21. SCB_VTOR EQU 0xE000ED08 ; Vector Table Offset Register
  22. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  23. NVIC_SYSPRI2 EQU 0xE000ED20 ; system priority register (2)
  24. NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV priority value (lowest)
  25. NVIC_PENDSVSET EQU 0x10000000 ; value to trigger PendSV exception
  26. SECTION .text:CODE(2)
  27. THUMB
  28. REQUIRE8
  29. PRESERVE8
  30. IMPORT rt_thread_switch_interrupt_flag
  31. IMPORT rt_interrupt_from_thread
  32. IMPORT rt_interrupt_to_thread
  33. ;/*
  34. ; * rt_base_t rt_hw_interrupt_disable();
  35. ; */
  36. EXPORT rt_hw_interrupt_disable
  37. rt_hw_interrupt_disable:
  38. MRS r0, PRIMASK
  39. CPSID I
  40. BX LR
  41. ;/*
  42. ; * void rt_hw_interrupt_enable(rt_base_t level);
  43. ; */
  44. EXPORT rt_hw_interrupt_enable
  45. rt_hw_interrupt_enable:
  46. MSR PRIMASK, r0
  47. BX LR
  48. ;/*
  49. ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  50. ; * r0 --> from
  51. ; * r1 --> to
  52. ; */
  53. EXPORT rt_hw_context_switch_interrupt
  54. EXPORT rt_hw_context_switch
  55. rt_hw_context_switch_interrupt:
  56. 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. MOV r3, #1
  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. ; r0 --> swith from thread stack
  74. ; r1 --> swith to thread stack
  75. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  76. EXPORT PendSV_Handler
  77. PendSV_Handler:
  78. ; disable interrupt to protect context switch
  79. MRS r2, PRIMASK
  80. CPSID I
  81. ; get rt_thread_switch_interrupt_flag
  82. LDR r0, =rt_thread_switch_interrupt_flag
  83. LDR r1, [r0]
  84. CBZ r1, pendsv_exit ; pendsv already handled
  85. ; clear rt_thread_switch_interrupt_flag to 0
  86. MOV r1, #0x00
  87. STR r1, [r0]
  88. LDR r0, =rt_interrupt_from_thread
  89. LDR r1, [r0]
  90. CBZ r1, swtich_to_thread ; skip register save at the first time
  91. MRS r1, psp ; get from thread stack pointer
  92. #if defined ( __ARMVFP__ )
  93. VSTMDB r1!, {d8 - d15} ; push FPU register s16~s31
  94. #endif
  95. STMFD r1!, {r4 - r11} ; push r4 - r11 register
  96. LDR r0, [r0]
  97. STR r1, [r0] ; update from thread stack pointer
  98. swtich_to_thread
  99. LDR r1, =rt_interrupt_to_thread
  100. LDR r1, [r1]
  101. LDR r1, [r1] ; load thread stack pointer
  102. LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
  103. #if defined ( __ARMVFP__ )
  104. VLDMIA r1!, {d8 - d15} ; pop FPU register s16~s31
  105. #endif
  106. MSR psp, r1 ; update stack pointer
  107. pendsv_exit
  108. ; restore interrupt
  109. MSR PRIMASK, r2
  110. ORR lr, lr, #0x04
  111. BX lr
  112. ;/*
  113. ; * void rt_hw_context_switch_to(rt_uint32 to);
  114. ; * r0 --> to
  115. ; */
  116. EXPORT rt_hw_context_switch_to
  117. rt_hw_context_switch_to:
  118. LDR r1, =rt_interrupt_to_thread
  119. STR r0, [r1]
  120. ; set from thread to 0
  121. LDR r1, =rt_interrupt_from_thread
  122. MOV r0, #0x0
  123. STR r0, [r1]
  124. ; set interrupt flag to 1
  125. LDR r1, =rt_thread_switch_interrupt_flag
  126. MOV r0, #1
  127. STR r0, [r1]
  128. ; set the PendSV exception priority
  129. LDR r0, =NVIC_SYSPRI2
  130. LDR r1, =NVIC_PENDSV_PRI
  131. LDR.W r2, [r0,#0x00] ; read
  132. ORR r1,r1,r2 ; modify
  133. STR r1, [r0] ; write-back
  134. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  135. LDR r1, =NVIC_PENDSVSET
  136. STR r1, [r0]
  137. ; restore MSP
  138. LDR r0, =SCB_VTOR
  139. LDR r0, [r0]
  140. LDR r0, [r0]
  141. NOP
  142. MSR msp, r0
  143. CPSIE I ; enable interrupts at processor level
  144. ; never reach here!
  145. ; compatible with old version
  146. EXPORT rt_hw_interrupt_thread_switch
  147. rt_hw_interrupt_thread_switch:
  148. BX lr
  149. IMPORT rt_hw_hard_fault_exception
  150. EXPORT HardFault_Handler
  151. HardFault_Handler:
  152. ; get current context
  153. MRS r0, psp ; get fault thread stack pointer
  154. PUSH {lr}
  155. BL rt_hw_hard_fault_exception
  156. POP {lr}
  157. ORR lr, lr, #0x04
  158. BX lr
  159. END