portISR.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*-----------------------------------------------------------
  29. * Components that can be compiled to either ARM or THUMB mode are
  30. * contained in port.c The ISR routines, which can only be compiled
  31. * to ARM mode, are contained in this file.
  32. *----------------------------------------------------------*/
  33. /*
  34. */
  35. /* Scheduler includes. */
  36. #include "FreeRTOS.h"
  37. #include "task.h"
  38. /* Constants required to handle critical sections. */
  39. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  40. volatile uint32_t ulCriticalNesting = 9999UL;
  41. /*-----------------------------------------------------------*/
  42. /*
  43. * The scheduler can only be started from ARM mode, hence the inclusion of this
  44. * function here.
  45. */
  46. void vPortISRStartFirstTask( void );
  47. /*-----------------------------------------------------------*/
  48. void vPortISRStartFirstTask( void )
  49. {
  50. /* Simply start the scheduler. This is included here as it can only be
  51. called from ARM mode. */
  52. asm volatile ( \
  53. "LDR R0, =pxCurrentTCB \n\t" \
  54. "LDR R0, [R0] \n\t" \
  55. "LDR LR, [R0] \n\t" \
  56. \
  57. /* The critical nesting depth is the first item on the stack. */ \
  58. /* Load it into the ulCriticalNesting variable. */ \
  59. "LDR R0, =ulCriticalNesting \n\t" \
  60. "LDMFD LR!, {R1} \n\t" \
  61. "STR R1, [R0] \n\t" \
  62. \
  63. /* Get the SPSR from the stack. */ \
  64. "LDMFD LR!, {R0} \n\t" \
  65. "MSR SPSR, R0 \n\t" \
  66. \
  67. /* Restore all system mode registers for the task. */ \
  68. "LDMFD LR, {R0-R14}^ \n\t" \
  69. "NOP \n\t" \
  70. \
  71. /* Restore the return address. */ \
  72. "LDR LR, [LR, #+60] \n\t" \
  73. \
  74. /* And return - correcting the offset in the LR to obtain the */ \
  75. /* correct address. */ \
  76. "SUBS PC, LR, #4 \n\t" \
  77. );
  78. }
  79. /*-----------------------------------------------------------*/
  80. void vPortTickISR( void )
  81. {
  82. /* Increment the RTOS tick count, then look for the highest priority
  83. task that is ready to run. */
  84. if( xTaskIncrementTick() != pdFALSE )
  85. {
  86. vTaskSwitchContext();
  87. }
  88. /* Ready for the next interrupt. */
  89. TB_ClearITPendingBit( TB_IT_Update );
  90. }
  91. /*-----------------------------------------------------------*/
  92. /*
  93. * The interrupt management utilities can only be called from ARM mode. When
  94. * THUMB_INTERWORK is defined the utilities are defined as functions here to
  95. * ensure a switch to ARM mode. When THUMB_INTERWORK is not defined then
  96. * the utilities are defined as macros in portmacro.h - as per other ports.
  97. */
  98. #ifdef THUMB_INTERWORK
  99. void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));
  100. void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));
  101. void vPortDisableInterruptsFromThumb( void )
  102. {
  103. asm volatile (
  104. "STMDB SP!, {R0} \n\t" /* Push R0. */
  105. "MRS R0, CPSR \n\t" /* Get CPSR. */
  106. "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
  107. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  108. "LDMIA SP!, {R0} \n\t" /* Pop R0. */
  109. "BX R14" ); /* Return back to thumb. */
  110. }
  111. void vPortEnableInterruptsFromThumb( void )
  112. {
  113. asm volatile (
  114. "STMDB SP!, {R0} \n\t" /* Push R0. */
  115. "MRS R0, CPSR \n\t" /* Get CPSR. */
  116. "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
  117. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  118. "LDMIA SP!, {R0} \n\t" /* Pop R0. */
  119. "BX R14" ); /* Return back to thumb. */
  120. }
  121. #endif /* THUMB_INTERWORK */
  122. /*-----------------------------------------------------------*/
  123. void vPortEnterCritical( void )
  124. {
  125. /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
  126. asm volatile (
  127. "STMDB SP!, {R0} \n\t" /* Push R0. */
  128. "MRS R0, CPSR \n\t" /* Get CPSR. */
  129. "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
  130. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  131. "LDMIA SP!, {R0}" ); /* Pop R0. */
  132. /* Now interrupts are disabled ulCriticalNesting can be accessed
  133. directly. Increment ulCriticalNesting to keep a count of how many times
  134. portENTER_CRITICAL() has been called. */
  135. ulCriticalNesting++;
  136. }
  137. /*-----------------------------------------------------------*/
  138. void vPortExitCritical( void )
  139. {
  140. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  141. {
  142. /* Decrement the nesting count as we are leaving a critical section. */
  143. ulCriticalNesting--;
  144. /* If the nesting level has reached zero then interrupts should be
  145. re-enabled. */
  146. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  147. {
  148. /* Enable interrupts as per portEXIT_CRITICAL(). */
  149. asm volatile (
  150. "STMDB SP!, {R0} \n\t" /* Push R0. */
  151. "MRS R0, CPSR \n\t" /* Get CPSR. */
  152. "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
  153. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  154. "LDMIA SP!, {R0}" ); /* Pop R0. */
  155. }
  156. }
  157. }