irq_cm0.s 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-M0 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. MOV R0,LR
  41. LSRS R0,R0,#3 ; Determine return stack from EXC_RETURN bit 2
  42. BCC SVC_MSP ; Branch if return stack is MSP
  43. MRS R0,PSP ; Get PSP
  44. SVC_Number
  45. LDR R1,[R0,#24] ; Load saved PC from stack
  46. SUBS R1,R1,#2 ; Point to SVC instruction
  47. LDRB R1,[R1] ; Load SVC number
  48. CMP R1,#0
  49. BNE SVC_User ; Branch if not SVC 0
  50. PUSH {R0,LR} ; Save SP and EXC_RETURN
  51. LDMIA R0,{R0-R3} ; Load function parameters from stack
  52. BLX R7 ; Call service function
  53. POP {R2,R3} ; Restore SP and EXC_RETURN
  54. STMIA R2!,{R0-R1} ; Store function return values
  55. MOV LR,R3 ; Set EXC_RETURN
  56. SVC_Context
  57. LDR R3,=osRtxInfo+I_T_RUN_OFS; Load address of osRtxInfo.run
  58. LDMIA R3!,{R1,R2} ; Load osRtxInfo.thread.run: curr & next
  59. CMP R1,R2 ; Check if thread switch is required
  60. BEQ SVC_Exit ; Branch when threads are the same
  61. CMP R1,#0
  62. BEQ SVC_ContextSwitch ; Branch if running thread is deleted
  63. SVC_ContextSave
  64. MRS R0,PSP ; Get PSP
  65. SUBS R0,R0,#32 ; Calculate SP
  66. STR R0,[R1,#TCB_SP_OFS] ; Store SP
  67. STMIA R0!,{R4-R7} ; Save R4..R7
  68. MOV R4,R8
  69. MOV R5,R9
  70. MOV R6,R10
  71. MOV R7,R11
  72. STMIA R0!,{R4-R7} ; Save R8..R11
  73. SVC_ContextSwitch
  74. SUBS R3,R3,#8 ; Adjust address
  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. LDR R0,[R2,#TCB_SP_OFS] ; Load SP
  84. ADDS R0,R0,#16 ; Adjust address
  85. LDMIA R0!,{R4-R7} ; Restore R8..R11
  86. MOV R8,R4
  87. MOV R9,R5
  88. MOV R10,R6
  89. MOV R11,R7
  90. MSR PSP,R0 ; Set PSP
  91. SUBS R0,R0,#32 ; Adjust address
  92. LDMIA R0!,{R4-R7} ; Restore R4..R7
  93. MOVS R0,#~0xFFFFFFFD
  94. MVNS R0,R0 ; Set EXC_RETURN value
  95. BX R0 ; Exit from handler
  96. SVC_MSP
  97. MRS R0,MSP ; Get MSP
  98. B SVC_Number
  99. SVC_Exit
  100. BX LR ; Exit from handler
  101. SVC_User
  102. LDR R2,=osRtxUserSVC ; Load address of SVC table
  103. LDR R3,[R2] ; Load SVC maximum number
  104. CMP R1,R3 ; Check SVC number range
  105. BHI SVC_Exit ; Branch if out of range
  106. PUSH {R0,LR} ; Save SP and EXC_RETURN
  107. LSLS R1,R1,#2
  108. LDR R3,[R2,R1] ; Load address of SVC function
  109. MOV R12,R3
  110. LDMIA R0,{R0-R3} ; Load function parameters from stack
  111. BLX R12 ; Call service function
  112. POP {R2,R3} ; Restore SP and EXC_RETURN
  113. STR R0,[R2] ; Store function return value
  114. MOV LR,R3 ; Set EXC_RETURN
  115. BX LR ; Return from handler
  116. ALIGN
  117. ENDP
  118. PendSV_Handler PROC
  119. EXPORT PendSV_Handler
  120. IMPORT osRtxPendSV_Handler
  121. PUSH {R0,LR} ; Save EXC_RETURN
  122. BL osRtxPendSV_Handler ; Call osRtxPendSV_Handler
  123. POP {R0,R1} ; Restore EXC_RETURN
  124. MOV LR,R1 ; Set EXC_RETURN
  125. B SVC_Context
  126. ALIGN
  127. ENDP
  128. SysTick_Handler PROC
  129. EXPORT SysTick_Handler
  130. IMPORT osRtxTick_Handler
  131. PUSH {R0,LR} ; Save EXC_RETURN
  132. BL osRtxTick_Handler ; Call osRtxTick_Handler
  133. POP {R0,R1} ; Restore EXC_RETURN
  134. MOV LR,R1 ; Set EXC_RETURN
  135. B SVC_Context
  136. ALIGN
  137. ENDP
  138. END