context_iar.S 5.7 KB

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