2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Scheduler includes. */
34 /*-----------------------------------------------------------
35 * Implementation of functions defined in portable.h for the HCS12 port.
36 *----------------------------------------------------------*/
40 * Configure a timer to generate the RTOS tick at the frequency specified
41 * within FreeRTOSConfig.h.
43 static void prvSetupTimerInterrupt( void );
45 /* Interrupt service routines have to be in non-banked memory - as does the
46 * scheduler startup function. */
47 #pragma CODE_SEG __NEAR_SEG NON_BANKED
49 /* Manual context switch function. This is the SWI ISR. */
50 void interrupt vPortYield( void );
52 /* Tick context switch function. This is the timer ISR. */
53 void interrupt vPortTickInterrupt( void );
55 /* Simply called by xPortStartScheduler(). xPortStartScheduler() does not
56 * start the scheduler directly because the header file containing the
57 * xPortStartScheduler() prototype is part of the common kernel code, and
58 * therefore cannot use the CODE_SEG pragma. */
59 static BaseType_t xBankedStartScheduler( void );
61 #pragma CODE_SEG DEFAULT
63 /* Calls to portENTER_CRITICAL() can be nested. When they are nested the
64 * critical section should not be left (i.e. interrupts should not be re-enabled)
65 * until the nesting depth reaches 0. This variable simply tracks the nesting
66 * depth. Each task maintains it's own critical nesting depth variable so
67 * uxCriticalNesting is saved and restored from the task stack during a context
69 volatile UBaseType_t uxCriticalNesting = 0xff;
71 /*-----------------------------------------------------------*/
74 * See header file for description.
76 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
77 TaskFunction_t pxCode,
81 * Place a few bytes of known values on the bottom of the stack.
82 * This can be uncommented to provide useful stack markers when debugging.
84 * pxTopOfStack = ( StackType_t ) 0x11;
86 * pxTopOfStack = ( StackType_t ) 0x22;
88 * pxTopOfStack = ( StackType_t ) 0x33;
94 /* Setup the initial stack of the task. The stack is set exactly as
95 * expected by the portRESTORE_CONTEXT() macro. In this case the stack as
96 * expected by the HCS12 RTI instruction. */
99 /* The address of the task function is placed in the stack byte at a time. */
100 *pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pxCode ) ) + 1 );
102 *pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pxCode ) ) + 0 );
105 /* Next are all the registers that form part of the task context. */
108 *pxTopOfStack = ( StackType_t ) 0xff;
110 *pxTopOfStack = ( StackType_t ) 0xee;
114 *pxTopOfStack = ( StackType_t ) 0xdd;
116 *pxTopOfStack = ( StackType_t ) 0xcc;
119 /* A register contains parameter high byte. */
120 *pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pvParameters ) ) + 0 );
123 /* B register contains parameter low byte. */
124 *pxTopOfStack = ( StackType_t ) *( ( ( StackType_t * ) ( &pvParameters ) ) + 1 );
127 /* CCR: Note that when the task starts interrupts will be enabled since
128 * "I" bit of CCR is cleared */
129 *pxTopOfStack = ( StackType_t ) 0x00;
133 /* The page of the task. */
134 *pxTopOfStack = ( StackType_t ) ( ( int ) pxCode );
138 /* Finally the critical nesting depth is initialised with 0 (not within
139 * a critical section). */
140 *pxTopOfStack = ( StackType_t ) 0x00;
144 /*-----------------------------------------------------------*/
146 void vPortEndScheduler( void )
148 /* It is unlikely that the HCS12 port will get stopped. */
150 /*-----------------------------------------------------------*/
152 static void prvSetupTimerInterrupt( void )
154 TickTimer_SetFreqHz( configTICK_RATE_HZ );
157 /*-----------------------------------------------------------*/
159 BaseType_t xPortStartScheduler( void )
161 /* xPortStartScheduler() does not start the scheduler directly because
162 * the header file containing the xPortStartScheduler() prototype is part
163 * of the common kernel code, and therefore cannot use the CODE_SEG pragma.
164 * Instead it simply calls the locally defined xBankedStartScheduler() -
165 * which does use the CODE_SEG pragma. */
167 return xBankedStartScheduler();
169 /*-----------------------------------------------------------*/
171 #pragma CODE_SEG __NEAR_SEG NON_BANKED
173 static BaseType_t xBankedStartScheduler( void )
175 /* Configure the timer that will generate the RTOS tick. Interrupts are
176 * disabled when this function is called. */
177 prvSetupTimerInterrupt();
179 /* Restore the context of the first task. */
180 portRESTORE_CONTEXT();
182 /* Simulate the end of an interrupt to start the scheduler off. */
185 /* Should not get here! */
188 /*-----------------------------------------------------------*/
191 * Context switch functions. These are both interrupt service routines.
195 * Manual context switch forced by calling portYIELD(). This is the SWI
198 void interrupt vPortYield( void )
201 vTaskSwitchContext();
202 portRESTORE_CONTEXT();
204 /*-----------------------------------------------------------*/
207 * RTOS tick interrupt service routine. If the cooperative scheduler is
208 * being used then this simply increments the tick count. If the
209 * preemptive scheduler is being used a context switch can occur.
211 void interrupt vPortTickInterrupt( void )
213 #if configUSE_PREEMPTION == 1
215 /* A context switch might happen so save the context. */
218 /* Increment the tick ... */
219 if( xTaskIncrementTick() != pdFALSE )
221 vTaskSwitchContext();
226 /* Restore the context of a task - which may be a different task
227 * to that interrupted. */
228 portRESTORE_CONTEXT();
230 #else /* if configUSE_PREEMPTION == 1 */
232 xTaskIncrementTick();
235 #endif /* if configUSE_PREEMPTION == 1 */
238 #pragma CODE_SEG DEFAULT