portISR.c 8.0 KB

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