portISR.c 7.8 KB

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