port.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /* Scheduler includes. */
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. /*-----------------------------------------------------------
  32. * Implementation of functions defined in portable.h for the HCS12 port.
  33. *----------------------------------------------------------*/
  34. /*
  35. * Configure a timer to generate the RTOS tick at the frequency specified
  36. * within FreeRTOSConfig.h.
  37. */
  38. static void prvSetupTimerInterrupt( void );
  39. /* Interrupt service routines have to be in non-banked memory - as does the
  40. scheduler startup function. */
  41. #pragma CODE_SEG __NEAR_SEG NON_BANKED
  42. /* Manual context switch function. This is the SWI ISR. */
  43. void interrupt vPortYield( void );
  44. /* Tick context switch function. This is the timer ISR. */
  45. void interrupt vPortTickInterrupt( void );
  46. /* Simply called by xPortStartScheduler(). xPortStartScheduler() does not
  47. start the scheduler directly because the header file containing the
  48. xPortStartScheduler() prototype is part of the common kernel code, and
  49. therefore cannot use the CODE_SEG pragma. */
  50. static BaseType_t xBankedStartScheduler( void );
  51. #pragma CODE_SEG DEFAULT
  52. /* Calls to portENTER_CRITICAL() can be nested. When they are nested the
  53. critical section should not be left (i.e. interrupts should not be re-enabled)
  54. until the nesting depth reaches 0. This variable simply tracks the nesting
  55. depth. Each task maintains it's own critical nesting depth variable so
  56. uxCriticalNesting is saved and restored from the task stack during a context
  57. switch. */
  58. volatile UBaseType_t uxCriticalNesting = 0xff;
  59. /*-----------------------------------------------------------*/
  60. /*
  61. * See header file for description.
  62. */
  63. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  64. {
  65. /*
  66. Place a few bytes of known values on the bottom of the stack.
  67. This can be uncommented to provide useful stack markers when debugging.
  68. *pxTopOfStack = ( StackType_t ) 0x11;
  69. pxTopOfStack--;
  70. *pxTopOfStack = ( StackType_t ) 0x22;
  71. pxTopOfStack--;
  72. *pxTopOfStack = ( StackType_t ) 0x33;
  73. pxTopOfStack--;
  74. */
  75. /* Setup the initial stack of the task. The stack is set exactly as
  76. expected by the portRESTORE_CONTEXT() macro. In this case the stack as
  77. expected by the HCS12 RTI instruction. */
  78. /* The address of the task function is placed in the stack byte at a time. */
  79. *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 1 );
  80. pxTopOfStack--;
  81. *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 0 );
  82. pxTopOfStack--;
  83. /* Next are all the registers that form part of the task context. */
  84. /* Y register */
  85. *pxTopOfStack = ( StackType_t ) 0xff;
  86. pxTopOfStack--;
  87. *pxTopOfStack = ( StackType_t ) 0xee;
  88. pxTopOfStack--;
  89. /* X register */
  90. *pxTopOfStack = ( StackType_t ) 0xdd;
  91. pxTopOfStack--;
  92. *pxTopOfStack = ( StackType_t ) 0xcc;
  93. pxTopOfStack--;
  94. /* A register contains parameter high byte. */
  95. *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 0 );
  96. pxTopOfStack--;
  97. /* B register contains parameter low byte. */
  98. *pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 1 );
  99. pxTopOfStack--;
  100. /* CCR: Note that when the task starts interrupts will be enabled since
  101. "I" bit of CCR is cleared */
  102. *pxTopOfStack = ( StackType_t ) 0x00;
  103. pxTopOfStack--;
  104. #ifdef BANKED_MODEL
  105. /* The page of the task. */
  106. *pxTopOfStack = ( StackType_t ) ( ( int ) pxCode );
  107. pxTopOfStack--;
  108. #endif
  109. /* Finally the critical nesting depth is initialised with 0 (not within
  110. a critical section). */
  111. *pxTopOfStack = ( StackType_t ) 0x00;
  112. return pxTopOfStack;
  113. }
  114. /*-----------------------------------------------------------*/
  115. void vPortEndScheduler( void )
  116. {
  117. /* It is unlikely that the HCS12 port will get stopped. */
  118. }
  119. /*-----------------------------------------------------------*/
  120. static void prvSetupTimerInterrupt( void )
  121. {
  122. TickTimer_SetFreqHz( configTICK_RATE_HZ );
  123. TickTimer_Enable();
  124. }
  125. /*-----------------------------------------------------------*/
  126. BaseType_t xPortStartScheduler( void )
  127. {
  128. /* xPortStartScheduler() does not start the scheduler directly because
  129. the header file containing the xPortStartScheduler() prototype is part
  130. of the common kernel code, and therefore cannot use the CODE_SEG pragma.
  131. Instead it simply calls the locally defined xBankedStartScheduler() -
  132. which does use the CODE_SEG pragma. */
  133. return xBankedStartScheduler();
  134. }
  135. /*-----------------------------------------------------------*/
  136. #pragma CODE_SEG __NEAR_SEG NON_BANKED
  137. static BaseType_t xBankedStartScheduler( void )
  138. {
  139. /* Configure the timer that will generate the RTOS tick. Interrupts are
  140. disabled when this function is called. */
  141. prvSetupTimerInterrupt();
  142. /* Restore the context of the first task. */
  143. portRESTORE_CONTEXT();
  144. /* Simulate the end of an interrupt to start the scheduler off. */
  145. __asm( "rti" );
  146. /* Should not get here! */
  147. return pdFALSE;
  148. }
  149. /*-----------------------------------------------------------*/
  150. /*
  151. * Context switch functions. These are both interrupt service routines.
  152. */
  153. /*
  154. * Manual context switch forced by calling portYIELD(). This is the SWI
  155. * handler.
  156. */
  157. void interrupt vPortYield( void )
  158. {
  159. portSAVE_CONTEXT();
  160. vTaskSwitchContext();
  161. portRESTORE_CONTEXT();
  162. }
  163. /*-----------------------------------------------------------*/
  164. /*
  165. * RTOS tick interrupt service routine. If the cooperative scheduler is
  166. * being used then this simply increments the tick count. If the
  167. * preemptive scheduler is being used a context switch can occur.
  168. */
  169. void interrupt vPortTickInterrupt( void )
  170. {
  171. #if configUSE_PREEMPTION == 1
  172. {
  173. /* A context switch might happen so save the context. */
  174. portSAVE_CONTEXT();
  175. /* Increment the tick ... */
  176. if( xTaskIncrementTick() != pdFALSE )
  177. {
  178. vTaskSwitchContext();
  179. }
  180. TFLG1 = 1;
  181. /* Restore the context of a task - which may be a different task
  182. to that interrupted. */
  183. portRESTORE_CONTEXT();
  184. }
  185. #else
  186. {
  187. xTaskIncrementTick();
  188. TFLG1 = 1;
  189. }
  190. #endif
  191. }
  192. #pragma CODE_SEG DEFAULT