context_iar.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. ; */
  15. ;/**
  16. ; * @addtogroup STM32
  17. ; */
  18. ;/*@{*/
  19. NVIC_INT_CTRL EQU 0xE000ED04 ; interrupt control state register
  20. NVIC_SYSPRI2 EQU 0xE000ED20 ; system priority register (2)
  21. NVIC_PENDSV_PRI EQU 0x00FF0000 ; PendSV 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_interrput_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_interrput_flag to 1
  55. LDR r2, =rt_thread_switch_interrput_flag
  56. LDR r3, [r2]
  57. CMP r3, #1
  58. BEQ _reswitch
  59. MOV r3, #1
  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 --> swith from thread stack
  71. ; r1 --> swith to thread stack
  72. ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  73. EXPORT rt_hw_pend_sv
  74. rt_hw_pend_sv:
  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_interrput_flag
  80. LDR r1, [r0]
  81. CBZ r1, pendsv_exit ; pendsv already handled
  82. ; clear rt_thread_switch_interrput_flag to 0
  83. MOV r1, #0x00
  84. STR r1, [r0]
  85. LDR r0, =rt_interrupt_from_thread
  86. LDR r1, [r0]
  87. CBZ r1, swtich_to_thread ; skip register save at the first time
  88. MRS r1, psp ; get from thread stack pointer
  89. STMFD r1!, {r4 - r11} ; push r4 - r11 register
  90. LDR r0, [r0]
  91. STR r1, [r0] ; update from thread stack pointer
  92. swtich_to_thread
  93. LDR r1, =rt_interrupt_to_thread
  94. LDR r1, [r1]
  95. LDR r1, [r1] ; load thread stack pointer
  96. LDMFD r1!, {r4 - r11} ; pop r4 - r11 register
  97. MSR psp, r1 ; update stack pointer
  98. pendsv_exit
  99. ; restore interrupt
  100. MSR PRIMASK, r2
  101. ORR lr, lr, #0x04
  102. BX lr
  103. ;/*
  104. ; * void rt_hw_context_switch_to(rt_uint32 to);
  105. ; * r0 --> to
  106. ; */
  107. EXPORT rt_hw_context_switch_to
  108. rt_hw_context_switch_to:
  109. LDR r1, =rt_interrupt_to_thread
  110. STR r0, [r1]
  111. ; set from thread to 0
  112. LDR r1, =rt_interrupt_from_thread
  113. MOV r0, #0x0
  114. STR r0, [r1]
  115. ; set interrupt flag to 1
  116. LDR r1, =rt_thread_switch_interrput_flag
  117. MOV r0, #1
  118. STR r0, [r1]
  119. ; set the PendSV exception priority
  120. LDR r0, =NVIC_SYSPRI2
  121. LDR r1, =NVIC_PENDSV_PRI
  122. STR r1, [r0]
  123. LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch)
  124. LDR r1, =NVIC_PENDSVSET
  125. STR r1, [r0]
  126. CPSIE I ; enable interrupts at processor level
  127. ; never reach here!
  128. ; compatible with old version
  129. EXPORT rt_hw_interrupt_thread_switch
  130. rt_hw_interrupt_thread_switch:
  131. BX lr
  132. END