context_gcc.S 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. * 2009-10-11 Bernard first version
  9. * 2012-01-01 aozima support context switch load/store FPU register.
  10. * 2013-06-18 aozima add restore MSP feature.
  11. * 2013-06-23 aozima support lazy stack optimized.
  12. * 2018-07-24 aozima enhancement hard fault exception handler.
  13. */
  14. /**
  15. * @addtogroup cortex-m85
  16. */
  17. /*@{*/
  18. .cpu cortex-m7
  19. .syntax unified
  20. .thumb
  21. .text
  22. .equ SCB_VTOR, 0xE000ED08 /* Vector Table Offset Register */
  23. .equ NVIC_INT_CTRL, 0xE000ED04 /* interrupt control state register */
  24. .equ NVIC_SYSPRI2, 0xE000ED20 /* system priority register (2) */
  25. .equ NVIC_PENDSV_PRI, 0xFFFF0000 /* PendSV and SysTick priority value (lowest) */
  26. .equ NVIC_PENDSVSET, 0x10000000 /* value to trigger PendSV exception */
  27. /*
  28. * rt_base_t rt_hw_interrupt_disable();
  29. */
  30. .global rt_hw_interrupt_disable
  31. .type rt_hw_interrupt_disable, %function
  32. rt_hw_interrupt_disable:
  33. MRS r0, PRIMASK
  34. CPSID I
  35. BX LR
  36. /*
  37. * void rt_hw_interrupt_enable(rt_base_t level);
  38. */
  39. .global rt_hw_interrupt_enable
  40. .type rt_hw_interrupt_enable, %function
  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. .global rt_hw_context_switch_interrupt
  50. .type rt_hw_context_switch_interrupt, %function
  51. .global rt_hw_context_switch
  52. .type rt_hw_context_switch, %function
  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. MOV r3, #1
  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 --> switch from thread stack
  72. * r1 --> switch to thread stack
  73. * psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack
  74. */
  75. .global PendSV_Handler
  76. .type PendSV_Handler, %function
  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, switch_to_thread /* skip register save at the first time */
  91. MRS r1, psp /* get from thread stack pointer */
  92. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  93. TST lr, #0x10 /* if(!EXC_RETURN[4]) */
  94. IT EQ
  95. VSTMDBEQ r1!, {d8 - d15} /* push FPU register s16~s31 */
  96. #endif
  97. STMFD r1!, {r4 - r11} /* push r4 - r11 register */
  98. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  99. MOV r4, #0x00 /* flag = 0 */
  100. TST lr, #0x10 /* if(!EXC_RETURN[4]) */
  101. IT EQ
  102. MOVEQ r4, #0x01 /* flag = 1 */
  103. STMFD r1!, {r4} /* push flag */
  104. #endif
  105. LDR r0, [r0]
  106. STR r1, [r0] /* update from thread stack pointer */
  107. switch_to_thread:
  108. /* set PSPLIM register */
  109. PUSH {LR}
  110. bl TaskSwitch_StackCheck
  111. POP {LR}
  112. LDR r1, =rt_interrupt_to_thread
  113. LDR r1, [r1]
  114. LDR r1, [r1] /* load thread stack pointer */
  115. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  116. LDMFD r1!, {r3} /* pop flag */
  117. #endif
  118. LDMFD r1!, {r4 - r11} /* pop r4 - r11 register */
  119. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  120. CMP r3, #0 /* if(flag_r3 != 0) */
  121. IT NE
  122. VLDMIANE r1!, {d8 - d15} /* pop FPU register s16~s31 */
  123. #endif
  124. MSR psp, r1 /* update stack pointer */
  125. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  126. ORR lr, lr, #0x10 /* lr |= (1 << 4), clean FPCA. */
  127. CMP r3, #0 /* if(flag_r3 != 0) */
  128. IT NE
  129. BICNE lr, lr, #0x10 /* lr &= ~(1 << 4), set FPCA. */
  130. #endif
  131. pendsv_exit:
  132. /* restore interrupt */
  133. MSR PRIMASK, r2
  134. ORR lr, lr, #0x04
  135. BX lr
  136. /*
  137. * void rt_hw_context_switch_to(rt_uint32 to);
  138. * r0 --> to
  139. */
  140. .global rt_hw_context_switch_to
  141. .type rt_hw_context_switch_to, %function
  142. rt_hw_context_switch_to:
  143. LDR r1, =rt_interrupt_to_thread
  144. STR r0, [r1]
  145. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  146. /* CLEAR CONTROL.FPCA */
  147. MRS r2, CONTROL /* read */
  148. BIC r2, #0x04 /* modify */
  149. MSR CONTROL, r2 /* write-back */
  150. #endif
  151. /* set from thread to 0 */
  152. LDR r1, =rt_interrupt_from_thread
  153. MOV r0, #0x0
  154. STR r0, [r1]
  155. /* set interrupt flag to 1 */
  156. LDR r1, =rt_thread_switch_interrupt_flag
  157. MOV r0, #1
  158. STR r0, [r1]
  159. /* set the PendSV and SysTick exception priority */
  160. LDR r0, =NVIC_SYSPRI2
  161. LDR r1, =NVIC_PENDSV_PRI
  162. LDR.W r2, [r0,#0x00] /* read */
  163. ORR r1,r1,r2 /* modify */
  164. STR r1, [r0] /* write-back */
  165. LDR r0, =NVIC_INT_CTRL /* trigger the PendSV exception (causes context switch) */
  166. LDR r1, =NVIC_PENDSVSET
  167. STR r1, [r0]
  168. /* restore MSP */
  169. LDR r0, =SCB_VTOR
  170. LDR r0, [r0]
  171. LDR r0, [r0]
  172. NOP
  173. MSR msp, r0
  174. /* enable interrupts at processor level */
  175. CPSIE F
  176. CPSIE I
  177. /* ensure PendSV exception taken place before subsequent operation */
  178. DSB
  179. ISB
  180. /* never reach here! */
  181. /* compatible with old version */
  182. .global rt_hw_interrupt_thread_switch
  183. .type rt_hw_interrupt_thread_switch, %function
  184. rt_hw_interrupt_thread_switch:
  185. BX lr
  186. NOP
  187. .global HardFault_Handler
  188. .type HardFault_Handler, %function
  189. HardFault_Handler:
  190. /* get current context */
  191. MRS r0, msp /* get fault context from handler. */
  192. TST lr, #0x04 /* if(!EXC_RETURN[2]) */
  193. BEQ _get_sp_done
  194. MRS r0, psp /* get fault context from thread. */
  195. _get_sp_done:
  196. STMFD r0!, {r4 - r11} /* push r4 - r11 register */
  197. #if defined (__VFP_FP__) && !defined(__SOFTFP__)
  198. STMFD r0!, {lr} /* push dummy for flag */
  199. #endif
  200. STMFD r0!, {lr} /* push exec_return register */
  201. TST lr, #0x04 /* if(!EXC_RETURN[2]) */
  202. BEQ _update_msp
  203. MSR psp, r0 /* update stack pointer to PSP. */
  204. B _update_done
  205. _update_msp:
  206. MSR msp, r0 /* update stack pointer to MSP. */
  207. _update_done:
  208. PUSH {LR}
  209. BL rt_hw_hard_fault_exception
  210. POP {LR}
  211. ORR lr, lr, #0x04
  212. BX lr