portASM.s 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;/*
  2. ; * FreeRTOS Kernel V10.4.3
  3. ; * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. ; *
  5. ; * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. ; * this software and associated documentation files (the "Software"), to deal in
  7. ; * the Software without restriction, including without limitation the rights to
  8. ; * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. ; * the Software, and to permit persons to whom the Software is furnished to do so,
  10. ; * subject to the following conditions:
  11. ; *
  12. ; * The above copyright notice and this permission notice shall be included in all
  13. ; * copies or substantial portions of the Software.
  14. ; *
  15. ; * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. ; * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. ; * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. ; * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. ; * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. ; * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. ; *
  22. ; * https://www.FreeRTOS.org
  23. ; * https://github.com/FreeRTOS
  24. ; *
  25. ; * 1 tab == 4 spaces!
  26. ; */
  27. INCLUDE FreeRTOSConfig.h
  28. INCLUDE portmacro.h
  29. EXTERN vTaskSwitchContext
  30. EXTERN ulPortYieldRequired
  31. EXTERN ulPortInterruptNesting
  32. EXTERN vApplicationIRQHandler
  33. PUBLIC FreeRTOS_SWI_Handler
  34. PUBLIC FreeRTOS_IRQ_Handler
  35. PUBLIC vPortRestoreTaskContext
  36. SYS_MODE EQU 0x1f
  37. SVC_MODE EQU 0x13
  38. IRQ_MODE EQU 0x12
  39. SECTION .text:CODE:ROOT(2)
  40. ARM
  41. INCLUDE portASM.h
  42. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  43. ; SVC handler is used to yield a task.
  44. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  45. FreeRTOS_SWI_Handler
  46. PRESERVE8
  47. ; Save the context of the current task and select a new task to run.
  48. portSAVE_CONTEXT
  49. LDR R0, =vTaskSwitchContext
  50. BLX R0
  51. portRESTORE_CONTEXT
  52. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  53. ; vPortRestoreTaskContext is used to start the scheduler.
  54. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  55. vPortRestoreTaskContext
  56. PRESERVE8
  57. ; Switch to system mode
  58. CPS #SYS_MODE
  59. portRESTORE_CONTEXT
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ; IRQ interrupt handler used when individual priorities cannot be masked
  62. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  63. FreeRTOS_IRQ_Handler
  64. PRESERVE8
  65. ; Return to the interrupted instruction.
  66. SUB lr, lr, #4
  67. ; Push the return address and SPSR
  68. PUSH {lr}
  69. MRS lr, SPSR
  70. PUSH {lr}
  71. ; Change to supervisor mode to allow reentry.
  72. CPS #SVC_MODE
  73. ; Push used registers.
  74. PUSH {r0-r4, r12}
  75. ; Increment nesting count. r3 holds the address of ulPortInterruptNesting
  76. ; for future use. r1 holds the original ulPortInterruptNesting value for
  77. ; future use.
  78. LDR r3, =ulPortInterruptNesting
  79. LDR r1, [r3]
  80. ADD r4, r1, #1
  81. STR r4, [r3]
  82. ; Ensure bit 2 of the stack pointer is clear. r2 holds the bit 2 value for
  83. ; future use.
  84. MOV r2, sp
  85. AND r2, r2, #4
  86. SUB sp, sp, r2
  87. PUSH {r0-r4, lr}
  88. ; Call the port part specific handler.
  89. LDR r0, =vApplicationIRQHandler
  90. BLX r0
  91. POP {r0-r4, lr}
  92. ADD sp, sp, r2
  93. CPSID i
  94. ; Write to the EOI register.
  95. ; LDR r4, =configEOI_ADDRESS
  96. ; STR r0, [r4]
  97. ; Restore the old nesting count
  98. STR r1, [r3]
  99. ; A context switch is never performed if the nesting count is not 0.
  100. CMP r1, #0
  101. BNE exit_without_switch
  102. ; Did the interrupt request a context switch? r1 holds the address of
  103. ; ulPortYieldRequired and r0 the value of ulPortYieldRequired for future
  104. ; use.
  105. LDR r1, =ulPortYieldRequired
  106. LDR r0, [r1]
  107. CMP r0, #0
  108. BNE switch_before_exit
  109. exit_without_switch
  110. ; No context switch. Restore used registers, LR_irq and SPSR before
  111. ; returning.
  112. POP {r0-r4, r12}
  113. CPS #IRQ_MODE
  114. POP {LR}
  115. MSR SPSR_cxsf, LR
  116. POP {LR}
  117. MOVS PC, LR
  118. switch_before_exit
  119. ; A context switch is to be performed. Clear the context switch pending
  120. ; flag.
  121. MOV r0, #0
  122. STR r0, [r1]
  123. ; Restore used registers, LR-irq and SPSR before saving the context
  124. ; to the task stack.
  125. POP {r0-r4, r12}
  126. CPS #IRQ_MODE
  127. POP {LR}
  128. MSR SPSR_cxsf, LR
  129. POP {LR}
  130. portSAVE_CONTEXT
  131. ; Call the function that selects the new task to execute.
  132. ; vTaskSwitchContext() if vTaskSwitchContext() uses LDRD or STRD
  133. ; instructions, or 8 byte aligned stack allocated data. LR does not need
  134. ; saving as a new LR will be loaded by portRESTORE_CONTEXT anyway.
  135. LDR r0, =vTaskSwitchContext
  136. BLX r0
  137. ; Restore the context of, and branch to, the task selected to execute next.
  138. portRESTORE_CONTEXT
  139. END