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