irq_cm3.s 5.5 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-M3 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. PRESERVE8
  28. THUMB
  29. AREA |.constdata|, DATA, READONLY
  30. EXPORT irqRtxLib
  31. irqRtxLib DCB 0 ; Non weak library reference
  32. AREA |.text|, CODE, READONLY
  33. SVC_Handler PROC
  34. EXPORT SVC_Handler
  35. IMPORT osRtxUserSVC
  36. IMPORT osRtxInfo
  37. IF :DEF:MPU_LOAD
  38. IMPORT osRtxMpuLoad
  39. ENDIF
  40. TST LR,#0x04 ; Determine return stack from EXC_RETURN bit 2
  41. ITE EQ
  42. MRSEQ R0,MSP ; Get MSP if return stack is MSP
  43. MRSNE R0,PSP ; Get PSP if return stack is PSP
  44. LDR R1,[R0,#24] ; Load saved PC from stack
  45. LDRB R1,[R1,#-2] ; Load SVC number
  46. CBNZ R1,SVC_User ; Branch if not SVC 0
  47. PUSH {R0,LR} ; Save SP and EXC_RETURN
  48. LDM R0,{R0-R3,R12} ; Load function parameters and address from stack
  49. BLX R12 ; Call service function
  50. POP {R12,LR} ; Restore SP and EXC_RETURN
  51. STM R12,{R0-R1} ; Store function return values
  52. SVC_Context
  53. LDR R3,=osRtxInfo+I_T_RUN_OFS; Load address of osRtxInfo.run
  54. LDM R3,{R1,R2} ; Load osRtxInfo.thread.run: curr & next
  55. CMP R1,R2 ; Check if thread switch is required
  56. IT EQ
  57. BXEQ LR ; Exit when threads are the same
  58. CBZ R1,SVC_ContextSwitch ; Branch if running thread is deleted
  59. SVC_ContextSave
  60. STMDB R12!,{R4-R11} ; Save R4..R11
  61. STR R12,[R1,#TCB_SP_OFS] ; Store SP
  62. SVC_ContextSwitch
  63. STR R2,[R3] ; osRtxInfo.thread.run: curr = next
  64. IF :DEF:MPU_LOAD
  65. PUSH {R2,R3} ; Save registers
  66. MOV R0,R2 ; osRtxMpuLoad parameter
  67. BL osRtxMpuLoad ; Load MPU for next thread
  68. POP {R2,R3} ; Restore registers
  69. ENDIF
  70. SVC_ContextRestore
  71. LDR R0,[R2,#TCB_SP_OFS] ; Load SP
  72. LDMIA R0!,{R4-R11} ; Restore R4..R11
  73. MSR PSP,R0 ; Set PSP
  74. MVN LR,#~0xFFFFFFFD ; Set EXC_RETURN value
  75. SVC_Exit
  76. BX LR ; Exit from handler
  77. SVC_User
  78. LDR R2,=osRtxUserSVC ; Load address of SVC table
  79. LDR R3,[R2] ; Load SVC maximum number
  80. CMP R1,R3 ; Check SVC number range
  81. BHI SVC_Exit ; Branch if out of range
  82. PUSH {R0,LR} ; Save SP and EXC_RETURN
  83. LDR R12,[R2,R1,LSL #2] ; Load address of SVC function
  84. LDM R0,{R0-R3} ; Load function parameters from stack
  85. BLX R12 ; Call service function
  86. POP {R12,LR} ; Restore SP and EXC_RETURN
  87. STR R0,[R12] ; Store function return value
  88. BX LR ; Return from handler
  89. ALIGN
  90. ENDP
  91. PendSV_Handler PROC
  92. EXPORT PendSV_Handler
  93. IMPORT osRtxPendSV_Handler
  94. PUSH {R0,LR} ; Save EXC_RETURN
  95. BL osRtxPendSV_Handler ; Call osRtxPendSV_Handler
  96. POP {R0,LR} ; Restore EXC_RETURN
  97. MRS R12,PSP
  98. B SVC_Context
  99. ALIGN
  100. ENDP
  101. SysTick_Handler PROC
  102. EXPORT SysTick_Handler
  103. IMPORT osRtxTick_Handler
  104. PUSH {R0,LR} ; Save EXC_RETURN
  105. BL osRtxTick_Handler ; Call osRtxTick_Handler
  106. POP {R0,LR} ; Restore EXC_RETURN
  107. MRS R12,PSP
  108. B SVC_Context
  109. ALIGN
  110. ENDP
  111. END