irq_cm4f.s 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;/*
  2. ; * Copyright (c) 2013-2018 Arm Limited. All rights reserved.
  3. ; *
  4. ; * SPDX-License-Identifier: Apache-2.0
  5. ; *
  6. ; * Licensed under the Apache License, Version 2.0 (the License); you may
  7. ; * not use this file except in compliance with the License.
  8. ; * You may obtain a copy of the License at
  9. ; *
  10. ; * www.apache.org/licenses/LICENSE-2.0
  11. ; *
  12. ; * Unless required by applicable law or agreed to in writing, software
  13. ; * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. ; * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. ; * See the License for the specific language governing permissions and
  16. ; * limitations under the License.
  17. ; *
  18. ; * -----------------------------------------------------------------------------
  19. ; *
  20. ; * Project: CMSIS-RTOS RTX
  21. ; * Title: Cortex-M4F Exception handlers
  22. ; *
  23. ; * -----------------------------------------------------------------------------
  24. ; */
  25. I_T_RUN_OFS EQU 20 ; osRtxInfo.thread.run offset
  26. TCB_SP_OFS EQU 56 ; TCB.SP offset
  27. TCB_SF_OFS EQU 34 ; TCB.stack_frame offset
  28. PRESERVE8
  29. THUMB
  30. AREA |.constdata|, DATA, READONLY
  31. EXPORT irqRtxLib
  32. irqRtxLib DCB 0 ; Non weak library reference
  33. AREA |.text|, CODE, READONLY
  34. SVC_Handler PROC
  35. EXPORT SVC_Handler
  36. IMPORT osRtxUserSVC
  37. IMPORT osRtxInfo
  38. IF :DEF:MPU_LOAD
  39. IMPORT osRtxMpuLoad
  40. ENDIF
  41. TST LR,#0x04 ; Determine return stack from EXC_RETURN bit 2
  42. ITE EQ
  43. MRSEQ R0,MSP ; Get MSP if return stack is MSP
  44. MRSNE R0,PSP ; Get PSP if return stack is PSP
  45. LDR R1,[R0,#24] ; Load saved PC from stack
  46. LDRB R1,[R1,#-2] ; Load SVC number
  47. CBNZ R1,SVC_User ; Branch if not SVC 0
  48. PUSH {R0,LR} ; Save SP and EXC_RETURN
  49. LDM R0,{R0-R3,R12} ; Load function parameters and address from stack
  50. BLX R12 ; Call service function
  51. POP {R12,LR} ; Restore SP and EXC_RETURN
  52. STM R12,{R0-R1} ; Store function return values
  53. SVC_Context
  54. LDR R3,=osRtxInfo+I_T_RUN_OFS; Load address of osRtxInfo.run
  55. LDM R3,{R1,R2} ; Load osRtxInfo.thread.run: curr & next
  56. CMP R1,R2 ; Check if thread switch is required
  57. IT EQ
  58. BXEQ LR ; Exit when threads are the same
  59. CBNZ R1,SVC_ContextSave ; Branch if running thread is not deleted
  60. TST LR,#0x10 ; Check if extended stack frame
  61. BNE SVC_ContextSwitch
  62. LDR R1,=0xE000EF34 ; FPCCR Address
  63. LDR R0,[R1] ; Load FPCCR
  64. BIC R0,R0,#1 ; Clear LSPACT (Lazy state)
  65. STR R0,[R1] ; Store FPCCR
  66. B SVC_ContextSwitch
  67. SVC_ContextSave
  68. STMDB R12!,{R4-R11} ; Save R4..R11
  69. TST LR,#0x10 ; Check if extended stack frame
  70. IT EQ
  71. VSTMDBEQ R12!,{S16-S31} ; Save VFP S16.S31
  72. STR R12,[R1,#TCB_SP_OFS] ; Store SP
  73. STRB LR, [R1,#TCB_SF_OFS] ; Store stack frame information
  74. SVC_ContextSwitch
  75. STR R2,[R3] ; osRtxInfo.thread.run: curr = next
  76. IF :DEF:MPU_LOAD
  77. PUSH {R2,R3} ; Save registers
  78. MOV R0,R2 ; osRtxMpuLoad parameter
  79. BL osRtxMpuLoad ; Load MPU for next thread
  80. POP {R2,R3} ; Restore registers
  81. ENDIF
  82. SVC_ContextRestore
  83. LDRB R1,[R2,#TCB_SF_OFS] ; Load stack frame information
  84. LDR R0,[R2,#TCB_SP_OFS] ; Load SP
  85. ORR LR,R1,#0xFFFFFF00 ; Set EXC_RETURN
  86. TST LR,#0x10 ; Check if extended stack frame
  87. IT EQ
  88. VLDMIAEQ R0!,{S16-S31} ; Restore VFP S16..S31
  89. LDMIA R0!,{R4-R11} ; Restore R4..R11
  90. MSR PSP,R0 ; Set PSP
  91. SVC_Exit
  92. BX LR ; Exit from handler
  93. SVC_User
  94. LDR R2,=osRtxUserSVC ; Load address of SVC table
  95. LDR R3,[R2] ; Load SVC maximum number
  96. CMP R1,R3 ; Check SVC number range
  97. BHI SVC_Exit ; Branch if out of range
  98. PUSH {R0,LR} ; Save SP and EXC_RETURN
  99. LDR R12,[R2,R1,LSL #2] ; Load address of SVC function
  100. LDM R0,{R0-R3} ; Load function parameters from stack
  101. BLX R12 ; Call service function
  102. POP {R12,LR} ; Restore SP and EXC_RETURN
  103. STR R0,[R12] ; Store function return value
  104. BX LR ; Return from handler
  105. ALIGN
  106. ENDP
  107. PendSV_Handler PROC
  108. EXPORT PendSV_Handler
  109. IMPORT osRtxPendSV_Handler
  110. PUSH {R0,LR} ; Save EXC_RETURN
  111. BL osRtxPendSV_Handler ; Call osRtxPendSV_Handler
  112. POP {R0,LR} ; Restore EXC_RETURN
  113. MRS R12,PSP
  114. B SVC_Context
  115. ALIGN
  116. ENDP
  117. SysTick_Handler PROC
  118. EXPORT SysTick_Handler
  119. IMPORT osRtxTick_Handler
  120. PUSH {R0,LR} ; Save EXC_RETURN
  121. BL osRtxTick_Handler ; Call osRtxTick_Handler
  122. POP {R0,LR} ; Restore EXC_RETURN
  123. MRS R12,PSP
  124. B SVC_Context
  125. ALIGN
  126. ENDP
  127. END