context_iar.S 5.7 KB

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