portISR.c 9.2 KB

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