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