context_iar.S 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. ; * 2010-01-25 Bernard first version
  9. ; * 2012-06-01 aozima set pendsv priority to 0xFF.
  10. ; * 2012-08-17 aozima fixed bug: store r8 - r11.
  11. ; * 2013-06-18 aozima add restore MSP feature.
  12. ; */
  13. ;/**
  14. ; * @addtogroup CORTEX-M0
  15. ; */
  16. ;/*@{*/
  17. SCB_VTOR EQU 0xE000ED08 ; Vector Table Offset Register
  18. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  19. NVIC_SHPR3 EQU 0xE000ED20 ; system priority register (2)
  20. NVIC_PENDSV_PRI EQU 0xFFFF0000 ; PendSV and SysTick 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_interrupt_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_interrupt
  50. EXPORT rt_hw_context_switch
  51. rt_hw_context_switch_interrupt:
  52. rt_hw_context_switch:
  53. ; set rt_thread_switch_interrupt_flag to 1
  54. LDR r2, =rt_thread_switch_interrupt_flag
  55. LDR r3, [r2]
  56. CMP r3, #1
  57. BEQ _reswitch
  58. MOVS r3, #0x1
  59. STR r3, [r2]
  60. LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread
  61. STR r0, [r2]
  62. _reswitch
  63. LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread
  64. STR r1, [r2]
  65. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  66. LDR r1, =NVIC_PENDSVSET
  67. STR r1, [r0]
  68. BX LR
  69. ; r0 --> switch from thread stack
  70. ; r1 --> switch to thread stack
  71. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  72. EXPORT PendSV_Handler
  73. PendSV_Handler:
  74. ; disable interrupt to protect context switch
  75. MRS r2, PRIMASK
  76. CPSID I
  77. ; get rt_thread_switch_interrupt_flag
  78. LDR r0, =rt_thread_switch_interrupt_flag
  79. LDR r1, [r0]
  80. CMP r1, #0x00
  81. BEQ pendsv_exit ; pendsv already handled
  82. ; clear rt_thread_switch_interrupt_flag to 0
  83. MOVS r1, #0x00
  84. STR r1, [r0]
  85. LDR r0, =rt_interrupt_from_thread
  86. LDR r1, [r0]
  87. CMP r1, #0x00
  88. BEQ switch_to_thread ; skip register save at the first time
  89. MRS r1, psp ; get from thread stack pointer
  90. SUBS r1, r1, #0x20 ; space for {r4 - r7} and {r8 - r11}
  91. LDR r0, [r0]
  92. STR r1, [r0] ; update from thread stack pointer
  93. STMIA r1!, {r4 - r7} ; push thread {r4 - r7} register to thread stack
  94. MOV r4, r8 ; mov thread {r8 - r11} to {r4 - r7}
  95. MOV r5, r9
  96. MOV r6, r10
  97. MOV r7, r11
  98. STMIA r1!, {r4 - r7} ; push thread {r8 - r11} high register to thread stack
  99. switch_to_thread
  100. LDR r1, =rt_interrupt_to_thread
  101. LDR r1, [r1]
  102. LDR r1, [r1] ; load thread stack pointer
  103. LDMIA r1!, {r4 - r7} ; pop thread {r4 - r7} register from thread stack
  104. PUSH {r4 - r7} ; push {r4 - r7} to MSP for copy {r8 - r11}
  105. LDMIA r1!, {r4 - r7} ; pop thread {r8 - r11} high register from thread stack to {r4 - r7}
  106. MOV r8, r4 ; mov {r4 - r7} to {r8 - r11}
  107. MOV r9, r5
  108. MOV r10, r6
  109. MOV r11, r7
  110. POP {r4 - r7} ; pop {r4 - r7} from MSP
  111. MSR psp, r1 ; update stack pointer
  112. pendsv_exit
  113. ; restore interrupt
  114. MSR PRIMASK, r2
  115. MOVS r0, #0x04
  116. RSBS r0, r0, #0x00
  117. BX r0
  118. ;/*
  119. ; * void rt_hw_context_switch_to(rt_uint32 to);
  120. ; * r0 --> to
  121. ; * this fucntion is used to perform the first thread switch
  122. ; */
  123. EXPORT rt_hw_context_switch_to
  124. rt_hw_context_switch_to:
  125. ; set to thread
  126. LDR r1, =rt_interrupt_to_thread
  127. STR r0, [r1]
  128. ; set from thread to 0
  129. LDR r1, =rt_interrupt_from_thread
  130. MOVS r0, #0x0
  131. STR r0, [r1]
  132. ; set interrupt flag to 1
  133. LDR r1, =rt_thread_switch_interrupt_flag
  134. MOVS r0, #1
  135. STR r0, [r1]
  136. ; set the PendSV and SysTick exception priority
  137. LDR r0, =NVIC_SHPR3
  138. LDR r1, =NVIC_PENDSV_PRI
  139. LDR r2, [r0,#0x00] ; read
  140. ORRS r1,r1,r2 ; modify
  141. STR r1, [r0] ; write-back
  142. ; trigger the PendSV exception (causes context switch)
  143. LDR r0, =NVIC_INT_CTRL
  144. LDR r1, =NVIC_PENDSVSET
  145. STR r1, [r0]
  146. NOP
  147. ; restore MSP
  148. LDR r0, =SCB_VTOR
  149. LDR r0, [r0]
  150. LDR r0, [r0]
  151. NOP
  152. MSR msp, r0
  153. ; enable interrupts at processor level
  154. CPSIE I
  155. ; ensure PendSV exception taken place before subsequent operation
  156. DSB
  157. ISB
  158. ; never reach here!
  159. ; compatible with old version
  160. EXPORT rt_hw_interrupt_thread_switch
  161. rt_hw_interrupt_thread_switch:
  162. BX lr
  163. IMPORT rt_hw_hard_fault_exception
  164. EXPORT HardFault_Handler
  165. HardFault_Handler:
  166. ; get current context
  167. MRS r0, psp ; get fault thread stack pointer
  168. PUSH {lr}
  169. BL rt_hw_hard_fault_exception
  170. POP {pc}
  171. END