portISR.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  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. * Changes from V2.5.2
  35. *
  36. + The critical section management functions have been changed. These no
  37. + longer modify the stack and are safe to use at all optimisation levels.
  38. + The functions are now also the same for both ARM and THUMB modes.
  39. +
  40. + Changes from V2.6.0
  41. +
  42. + Removed the 'static' from the definition of vNonPreemptiveTick() to
  43. + allow the demo to link when using the cooperative scheduler.
  44. +
  45. + Changes from V3.2.4
  46. +
  47. + The assembler statements are now included in a single asm block rather
  48. + than each line having its own asm block.
  49. */
  50. /* Scheduler includes. */
  51. #include "FreeRTOS.h"
  52. /* Constants required to handle interrupts. */
  53. #define portTIMER_MATCH_ISR_BIT ( ( uint8_t ) 0x01 )
  54. #define portCLEAR_VIC_INTERRUPT ( ( uint32_t ) 0 )
  55. /* Constants required to handle critical sections. */
  56. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  57. volatile uint32_t ulCriticalNesting = 9999UL;
  58. /*-----------------------------------------------------------*/
  59. /* ISR to handle manual context switches (from a call to taskYIELD()). */
  60. void vPortYieldProcessor( void ) __attribute__( ( interrupt( "SWI" ), naked ) );
  61. /*
  62. * The scheduler can only be started from ARM mode, hence the inclusion of this
  63. * function here.
  64. */
  65. void vPortISRStartFirstTask( void );
  66. /*-----------------------------------------------------------*/
  67. void vPortISRStartFirstTask( void )
  68. {
  69. /* Simply start the scheduler. This is included here as it can only be
  70. * called from ARM mode. */
  71. portRESTORE_CONTEXT();
  72. }
  73. /*-----------------------------------------------------------*/
  74. /*
  75. * Called by portYIELD() or taskYIELD() to manually force a context switch.
  76. *
  77. * When a context switch is performed from the task level the saved task
  78. * context is made to look as if it occurred from within the tick ISR. This
  79. * way the same restore context function can be used when restoring the context
  80. * saved from the ISR or that saved from a call to vPortYieldProcessor.
  81. */
  82. void vPortYieldProcessor( void )
  83. {
  84. /* Within an IRQ ISR the link register has an offset from the true return
  85. * address, but an SWI ISR does not. Add the offset manually so the same
  86. * ISR return code can be used in both cases. */
  87. __asm volatile ( "ADD LR, LR, #4" );
  88. /* Perform the context switch. First save the context of the current task. */
  89. portSAVE_CONTEXT();
  90. /* Find the highest priority task that is ready to run. */
  91. __asm volatile ( "bl vTaskSwitchContext" );
  92. /* Restore the context of the new task. */
  93. portRESTORE_CONTEXT();
  94. }
  95. /*-----------------------------------------------------------*/
  96. /*
  97. * The ISR used for the scheduler tick.
  98. */
  99. void vTickISR( void ) __attribute__( ( naked ) );
  100. void vTickISR( void )
  101. {
  102. /* Save the context of the interrupted task. */
  103. portSAVE_CONTEXT();
  104. /* Increment the RTOS tick count, then look for the highest priority
  105. * task that is ready to run. */
  106. __asm volatile
  107. (
  108. " bl xTaskIncrementTick \t\n" \
  109. " cmp r0, #0 \t\n" \
  110. " beq SkipContextSwitch \t\n" \
  111. " bl vTaskSwitchContext \t\n" \
  112. "SkipContextSwitch: \t\n"
  113. );
  114. /* Ready for the next interrupt. */
  115. T0_IR = portTIMER_MATCH_ISR_BIT;
  116. VICVectAddr = portCLEAR_VIC_INTERRUPT;
  117. /* Restore the context of the new task. */
  118. portRESTORE_CONTEXT();
  119. }
  120. /*-----------------------------------------------------------*/
  121. /*
  122. * The interrupt management utilities can only be called from ARM mode. When
  123. * THUMB_INTERWORK is defined the utilities are defined as functions here to
  124. * ensure a switch to ARM mode. When THUMB_INTERWORK is not defined then
  125. * the utilities are defined as macros in portmacro.h - as per other ports.
  126. */
  127. #ifdef THUMB_INTERWORK
  128. void vPortDisableInterruptsFromThumb( void ) __attribute__( ( naked ) );
  129. void vPortEnableInterruptsFromThumb( void ) __attribute__( ( naked ) );
  130. void vPortDisableInterruptsFromThumb( void )
  131. {
  132. __asm volatile (
  133. "STMDB SP!, {R0} \n\t" /* Push R0. */
  134. "MRS R0, CPSR \n\t" /* Get CPSR. */
  135. "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
  136. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  137. "LDMIA SP!, {R0} \n\t" /* Pop R0. */
  138. "BX R14" ); /* Return back to thumb. */
  139. }
  140. void vPortEnableInterruptsFromThumb( void )
  141. {
  142. __asm volatile (
  143. "STMDB SP!, {R0} \n\t" /* Push R0. */
  144. "MRS R0, CPSR \n\t" /* Get CPSR. */
  145. "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
  146. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  147. "LDMIA SP!, {R0} \n\t" /* Pop R0. */
  148. "BX R14" ); /* Return back to thumb. */
  149. }
  150. #endif /* THUMB_INTERWORK */
  151. /* The code generated by the GCC compiler uses the stack in different ways at
  152. * different optimisation levels. The interrupt flags can therefore not always
  153. * be saved to the stack. Instead the critical section nesting level is stored
  154. * in a variable, which is then saved as part of the stack context. */
  155. void vPortEnterCritical( void )
  156. {
  157. /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
  158. __asm volatile (
  159. "STMDB SP!, {R0} \n\t" /* Push R0. */
  160. "MRS R0, CPSR \n\t" /* Get CPSR. */
  161. "ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
  162. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  163. "LDMIA SP!, {R0}" ); /* Pop R0. */
  164. /* Now that interrupts are disabled, ulCriticalNesting can be accessed
  165. * directly. Increment ulCriticalNesting to keep a count of how many times
  166. * portENTER_CRITICAL() has been called. */
  167. ulCriticalNesting++;
  168. }
  169. void vPortExitCritical( void )
  170. {
  171. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  172. {
  173. /* Decrement the nesting count as we are leaving a critical section. */
  174. ulCriticalNesting--;
  175. /* If the nesting level has reached zero then interrupts should be
  176. * re-enabled. */
  177. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  178. {
  179. /* Enable interrupts as per portEXIT_CRITICAL(). */
  180. __asm volatile (
  181. "STMDB SP!, {R0} \n\t" /* Push R0. */
  182. "MRS R0, CPSR \n\t" /* Get CPSR. */
  183. "BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
  184. "MSR CPSR, R0 \n\t" /* Write back modified value. */
  185. "LDMIA SP!, {R0}" ); /* Pop R0. */
  186. }
  187. }
  188. }