irq_cm4f.s 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. NAME irq_cm4f.s
  26. I_T_RUN_OFS EQU 20 ; osRtxInfo.thread.run offset
  27. TCB_SP_OFS EQU 56 ; TCB.SP offset
  28. TCB_SF_OFS EQU 34 ; TCB.stack_frame offset
  29. PRESERVE8
  30. SECTION .rodata:DATA:NOROOT(2)
  31. EXPORT irqRtxLib
  32. irqRtxLib DCB 0 ; Non weak library reference
  33. THUMB
  34. SECTION .text:CODE:NOROOT(2)
  35. SVC_Handler
  36. EXPORT SVC_Handler
  37. IMPORT osRtxUserSVC
  38. IMPORT osRtxInfo
  39. TST LR,#0x04 ; Determine return stack from EXC_RETURN bit 2
  40. ITE EQ
  41. MRSEQ R0,MSP ; Get MSP if return stack is MSP
  42. MRSNE R0,PSP ; Get PSP if return stack is PSP
  43. LDR R1,[R0,#24] ; Load saved PC from stack
  44. LDRB R1,[R1,#-2] ; Load SVC number
  45. CBNZ R1,SVC_User ; Branch if not SVC 0
  46. PUSH {R0,LR} ; Save SP and EXC_RETURN
  47. LDM R0,{R0-R3,R12} ; Load function parameters and address from stack
  48. BLX R12 ; Call service function
  49. POP {R12,LR} ; Restore SP and EXC_RETURN
  50. STM R12,{R0-R1} ; Store function return values
  51. SVC_Context
  52. LDR R3,=osRtxInfo+I_T_RUN_OFS; Load address of osRtxInfo.run
  53. LDM R3,{R1,R2} ; Load osRtxInfo.thread.run: curr & next
  54. CMP R1,R2 ; Check if thread switch is required
  55. IT EQ
  56. BXEQ LR ; Exit when threads are the same
  57. CBNZ R1,SVC_ContextSave ; Branch if running thread is not deleted
  58. TST LR,#0x10 ; Check if extended stack frame
  59. BNE SVC_ContextSwitch
  60. LDR R1,=0xE000EF34 ; FPCCR Address
  61. LDR R0,[R1] ; Load FPCCR
  62. BIC R0,R0,#1 ; Clear LSPACT (Lazy state)
  63. STR R0,[R1] ; Store FPCCR
  64. B SVC_ContextSwitch
  65. SVC_ContextSave
  66. STMDB R12!,{R4-R11} ; Save R4..R11
  67. TST LR,#0x10 ; Check if extended stack frame
  68. IT EQ
  69. VSTMDBEQ R12!,{S16-S31} ; Save VFP S16.S31
  70. STR R12,[R1,#TCB_SP_OFS] ; Store SP
  71. STRB LR, [R1,#TCB_SF_OFS] ; Store stack frame information
  72. SVC_ContextSwitch
  73. STR R2,[R3] ; osRtxInfo.thread.run: curr = next
  74. SVC_ContextRestore
  75. LDRB R1,[R2,#TCB_SF_OFS] ; Load stack frame information
  76. LDR R0,[R2,#TCB_SP_OFS] ; Load SP
  77. ORR LR,R1,#0xFFFFFF00 ; Set EXC_RETURN
  78. TST LR,#0x10 ; Check if extended stack frame
  79. IT EQ
  80. VLDMIAEQ R0!,{S16-S31} ; Restore VFP S16..S31
  81. LDMIA R0!,{R4-R11} ; Restore R4..R11
  82. MSR PSP,R0 ; Set PSP
  83. SVC_Exit
  84. BX LR ; Exit from handler
  85. SVC_User
  86. LDR R2,=osRtxUserSVC ; Load address of SVC table
  87. LDR R3,[R2] ; Load SVC maximum number
  88. CMP R1,R3 ; Check SVC number range
  89. BHI SVC_Exit ; Branch if out of range
  90. PUSH {R0,LR} ; Save SP and EXC_RETURN
  91. LDR R12,[R2,R1,LSL #2] ; Load address of SVC function
  92. LDM R0,{R0-R3} ; Load function parameters from stack
  93. BLX R12 ; Call service function
  94. POP {R12,LR} ; Restore SP and EXC_RETURN
  95. STR R0,[R12] ; Store function return value
  96. BX LR ; Return from handler
  97. PendSV_Handler
  98. EXPORT PendSV_Handler
  99. IMPORT osRtxPendSV_Handler
  100. PUSH {R0,LR} ; Save EXC_RETURN
  101. BL osRtxPendSV_Handler ; Call osRtxPendSV_Handler
  102. POP {R0,LR} ; Restore EXC_RETURN
  103. MRS R12,PSP
  104. B SVC_Context
  105. SysTick_Handler
  106. EXPORT SysTick_Handler
  107. IMPORT osRtxTick_Handler
  108. PUSH {R0,LR} ; Save EXC_RETURN
  109. BL osRtxTick_Handler ; Call osRtxTick_Handler
  110. POP {R0,LR} ; Restore EXC_RETURN
  111. MRS R12,PSP
  112. B SVC_Context
  113. END