port.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /* GCC/HCS12 port by Jefferson L Smith, 2005 */
  29. /* Scheduler includes. */
  30. #include "FreeRTOS.h"
  31. #include "task.h"
  32. /* Port includes */
  33. #include <sys/ports_def.h>
  34. /*-----------------------------------------------------------
  35. * Implementation of functions defined in portable.h for the HCS12 port.
  36. *----------------------------------------------------------*/
  37. /*
  38. * Configure a timer to generate the RTOS tick at the frequency specified
  39. * within FreeRTOSConfig.h.
  40. */
  41. static void prvSetupTimerInterrupt( void );
  42. /* NOTE: Interrupt service routines must be in non-banked memory - as does the
  43. * scheduler startup function. */
  44. #define ATTR_NEAR __attribute__( ( near ) )
  45. /* Manual context switch function. This is the SWI ISR. */
  46. /* __attribute__((interrupt)) */
  47. void ATTR_NEAR vPortYield( void );
  48. /* Tick context switch function. This is the timer ISR. */
  49. /* __attribute__((interrupt)) */
  50. void ATTR_NEAR vPortTickInterrupt( void );
  51. /* Function in non-banked memory which actually switches to first task. */
  52. BaseType_t ATTR_NEAR xStartSchedulerNear( void );
  53. /* Calls to portENTER_CRITICAL() can be nested. When they are nested the
  54. * critical section should not be left (i.e. interrupts should not be re-enabled)
  55. * until the nesting depth reaches 0. This variable simply tracks the nesting
  56. * depth. Each task maintains it's own critical nesting depth variable so
  57. * uxCriticalNesting is saved and restored from the task stack during a context
  58. * switch. */
  59. volatile UBaseType_t uxCriticalNesting = 0x80; /* un-initialized */
  60. /*-----------------------------------------------------------*/
  61. /*
  62. * See header file for description.
  63. */
  64. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  65. TaskFunction_t pxCode,
  66. void * pvParameters )
  67. {
  68. /* Setup the initial stack of the task. The stack is set exactly as
  69. * expected by the portRESTORE_CONTEXT() macro. In this case the stack as
  70. * expected by the HCS12 RTI instruction. */
  71. /* The address of the task function is placed in the stack byte at a time. */
  72. *pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pxCode ) ) + 1 );
  73. *--pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pxCode ) ) + 0 );
  74. /* Next are all the registers that form part of the task context. */
  75. /* Y register */
  76. *--pxTopOfStack = ( StackType_t ) 0xff;
  77. *--pxTopOfStack = ( StackType_t ) 0xee;
  78. /* X register */
  79. *--pxTopOfStack = ( StackType_t ) 0xdd;
  80. *--pxTopOfStack = ( StackType_t ) 0xcc;
  81. /* A register contains parameter high byte. */
  82. *--pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pvParameters ) ) + 0 );
  83. /* B register contains parameter low byte. */
  84. *--pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pvParameters ) ) + 1 );
  85. /* CCR: Note that when the task starts interrupts will be enabled since
  86. * "I" bit of CCR is cleared */
  87. *--pxTopOfStack = ( StackType_t ) 0x80; /* keeps Stop disabled (MCU default) */
  88. /* tmp softregs used by GCC. Values right now don't matter. */
  89. __asm( "\n\
  90. movw _.frame, 2,-%0 \n\
  91. movw _.tmp, 2,-%0 \n\
  92. movw _.z, 2,-%0 \n\
  93. movw _.xy, 2,-%0 \n\
  94. ;movw _.d2, 2,-%0 \n\
  95. ;movw _.d1, 2,-%0 \n\
  96. " : "=A" ( pxTopOfStack ) : "0" ( pxTopOfStack ) );
  97. #ifdef BANKED_MODEL
  98. /* The page of the task. */
  99. *--pxTopOfStack = 0x30; /* can only directly start in PPAGE 0x30 */
  100. #endif
  101. /* The critical nesting depth is initialised with 0 (meaning not in
  102. * a critical section). */
  103. *--pxTopOfStack = ( StackType_t ) 0x00;
  104. return pxTopOfStack;
  105. }
  106. /*-----------------------------------------------------------*/
  107. void vPortEndScheduler( void )
  108. {
  109. /* It is unlikely that the HCS12 port will get stopped. */
  110. }
  111. /*-----------------------------------------------------------*/
  112. static void prvSetupTimerInterrupt( void )
  113. {
  114. /* Enable hardware RTI timer */
  115. /* Ignores configTICK_RATE_HZ */
  116. RTICTL = 0x50; /* 16 MHz xtal: 976.56 Hz, 1024mS */
  117. CRGINT |= 0x80; /* RTIE */
  118. }
  119. /*-----------------------------------------------------------*/
  120. BaseType_t xPortStartScheduler( void )
  121. {
  122. /* xPortStartScheduler() does not start the scheduler directly because
  123. * the header file containing the xPortStartScheduler() prototype is part
  124. * of the common kernel code, and therefore cannot use the CODE_SEG pragma.
  125. * Instead it simply calls the locally defined xNearStartScheduler() -
  126. * which does use the CODE_SEG pragma. */
  127. int16_t register d;
  128. __asm( "jmp xStartSchedulerNear ; will never return" : "=d" ( d ) );
  129. return d;
  130. }
  131. /*-----------------------------------------------------------*/
  132. BaseType_t xStartSchedulerNear( void )
  133. {
  134. /* Configure the timer that will generate the RTOS tick. Interrupts are
  135. * disabled when this function is called. */
  136. prvSetupTimerInterrupt();
  137. /* Restore the context of the first task. */
  138. portRESTORE_CONTEXT();
  139. portISR_TAIL();
  140. /* Should not get here! */
  141. return pdFALSE;
  142. }
  143. /*-----------------------------------------------------------*/
  144. /*
  145. * Context switch functions. These are interrupt service routines.
  146. */
  147. /*
  148. * Manual context switch forced by calling portYIELD(). This is the SWI
  149. * handler.
  150. */
  151. void vPortYield( void )
  152. {
  153. portISR_HEAD();
  154. /* NOTE: This is the trap routine (swi) although not defined as a trap.
  155. * It will fill the stack the same way as an ISR in order to mix preemtion
  156. * and cooperative yield. */
  157. portSAVE_CONTEXT();
  158. vTaskSwitchContext();
  159. portRESTORE_CONTEXT();
  160. portISR_TAIL();
  161. }
  162. /*-----------------------------------------------------------*/
  163. /*
  164. * RTOS tick interrupt service routine. If the cooperative scheduler is
  165. * being used then this simply increments the tick count. If the
  166. * preemptive scheduler is being used a context switch can occur.
  167. */
  168. void vPortTickInterrupt( void )
  169. {
  170. portISR_HEAD();
  171. /* Clear tick timer flag */
  172. CRGFLG = 0x80;
  173. #if configUSE_PREEMPTION == 1
  174. {
  175. /* A context switch might happen so save the context. */
  176. portSAVE_CONTEXT();
  177. /* Increment the tick ... */
  178. if( xTaskIncrementTick() != pdFALSE )
  179. {
  180. /* A context switch is necessary. */
  181. vTaskSwitchContext();
  182. }
  183. /* Restore the context of a task - which may be a different task
  184. * to that interrupted. */
  185. portRESTORE_CONTEXT();
  186. }
  187. #else /* if configUSE_PREEMPTION == 1 */
  188. {
  189. xTaskIncrementTick();
  190. }
  191. #endif /* if configUSE_PREEMPTION == 1 */
  192. portISR_TAIL();
  193. }